Thursday, April 28, 2016

Modify binary file

It's been a while not writing anything because the my career changing to verify IC.
However, there is the little python script for modifying binary file byte by byte. The following code opens a file and seek to offset 0xAFF7 at the beginning. It writes 0xCC, 0xE2, 0x54, 0x7a into file.

import struct
magic = (0xCCE2, 0x547A)
with open("binary.bin", "r+b") as f:
    f.seek(0xAFF7)
    for m in magic:
        f.write(struct.pack('h', m))
    f.close()