Python修改exe之类的游戏文件中的数值

发布于:2024-05-13 ⋅ 阅读:(211) ⋅ 点赞:(0)

场景

某些游戏数值(攻击力、射程、速度…)被写在exe之类的文件里
要先查找游戏数值,然后修改

查找

首先,要查找数值,大数重复较少,建议从大数找起

F = '游戏原件.exe'

def find_your_sister0(a: bytes):
    """查找字符串"""
    length = len(a)
    with open(F, 'rb') as f:
        b = f.read()
    for i in range(len(b) - length + 1):
        if b[i: i + length] == a:
            print(i, b[i - 30: i + length + 30])

def find_your_sister1(a: int):
    """查找0~255的数"""
    with open(F, 'rb') as f:
        b = f.read()  # <class 'bytes'>
    for i in range(len(b)):
        if b[i] == a:
            print(i)

def find_your_sister2(a: int):
    """查找0~65535的数"""
    ab = a.to_bytes(2, byteorder='little')
    print(a, '转字节数组', ab)
    with open(F, 'rb') as f:
        b = f.read()
    for i in range(len(b) - 1):
        if b[i: i + 2] == ab:
            print(i)

if __name__ == '__main__':
    # find_your_sister0(b'fighter')
    # find_your_sister1(11)
    find_your_sister2(13536)

修改

F1 = '游戏原件.exe'
F2 = '游戏魔改文件.exe'

def cp():
    with open(F1, 'rb') as f:
        b = f.read()
    with open(F2, 'wb') as f:
        f.write(b)

def replace1(new_data: int, offset: int):
    """修改1个byte"""
    with open(F2, 'rb') as f:
        b = f.read()
    with open(F2, 'wb') as f:
        f.write(b[:offset])
        f.write(new_data.to_bytes(1, byteorder='little'))
        f.write(b[offset + 1:])
    print(offset, '偏移量的位置', b[offset], '修改为', new_data)

def replace2(new_data: int, offset: int):
    """修改2个bytes"""
    nb = new_data.to_bytes(2, byteorder='little')  # 例如:256 --> b'\x00\x01'
    with open(F2, 'rb') as f:
        b = f.read()
    with open(F2, 'wb') as f:
        f.write(b[:offset])
        f.write(nb)
        f.write(b[offset + 2:])
    print(offset, '和', offset + 1, '偏移量的位置', b[offset: offset + 2], '修改为', nb)

if __name__ == '__main__':
    cp()
    replace2(1600, 27935)  # 离子炮HP
    replace1(76, 439952)  # 离子炮A
    replace1(29, 440055)  # 风暴A
    replace2(630, 376339)  # 大喷火HP
    replace1(23, 393697)  # 小喷火V
    replace1(20, 393726)  # 中喷火V
    replace1(19, 393759)  # 大喷火V
    replace1(24, 393977)  # 阿基里斯V
    replace1(9, 393290)  # 重炮兵V
    replace1(13, 393490)  # 重防空V
    replace2(1100, 439604)  # 阿基里斯A

补充

字节

字节(Byte)是计算机信息技术用于计量存储容量的一种计量单位,储存的数值范围为0~255

WinHex是一款十六进制编辑器,编辑界面如下

to_bytes

函数说明

Return an array of bytes representing an integer.

  length
    Length of bytes object to use.  An OverflowError is raised if the
    integer is not representable with the given number of bytes.
  byteorder
    The byte order used to represent the integer.  If byteorder is 'big',
    the most significant byte is at the beginning of the byte array.  If
    byteorder is 'little', the most significant byte is at the end of the
    byte array.  To request the native byte order of the host system, use
    `sys.byteorder' as the byte order value.
  signed
    Determines whether two's complement is used to represent the integer.
    If signed is False and a negative integer is given, an OverflowError
    is raised.

函数示例(256–>100–>0100–>01,00–>00,01

>>> a = 256

>>> hex(a)
'0x100'

>>> a.to_bytes(2, byteorder='little')
b'\x00\x01'