Love My Love

Binary I/O operation

2019.07.26

I coped with character type coversion to let it looks like c type, and search bytes from *.dll to modify it. so, there is sth I learned and written down here.

# Binary I/O operation

Define!

def hex_read(filepath: str) -> bytearray:

    if os.path.isfile(filepath):
        with open(filepath, 'rb') as f:
            data = bytearray(f.read())
    return data

Usage:

dll_data = hex_read('D:\\test\\test.dll')

Find bytes and replace it:

index_start = dll_data.find(b'\x85\x69\xf0\x7f')
index_end = index_start + 100
if index_start != -1:
    data_old = dll_data[index_start:index_end]
    data_new = replacebytes  # replacebytes is the content you want to replace
    dll_data_new = dll_data.replace(data_old, data_new)
with open('D:\\test\\test.dll', 'wb') as f:
        f.write(dll_data_new)
发表评论