Shuzheng Shuzheng - 8 months ago 30
Linux Question

Overwriting a single byte in a binary file makes it unrecognizable by objdump?

I want to overwrite a single byte in a binary file at a specific

offset
. The value
cval
is an integer.

To do this, I've written the following code:

f = open(program, "wb")
f.seek(offset, 0) # absolute
#print(f.read(1))
f.write(bytes([cval]))
f.close()


The code works perfectly when reading the byte at
offset
, but writing to this position results in
objdump
cannot recognize the ELF binary.

mfo@mfo-Ubuntu:~/llvm-ir-obfuscation/checker$ objdump -dF fac_c
objdump: fac_c: File format not recognized


I don't see what I do wrong?

Answer Source

You opened your file with w mode, which truncates the file when opening it. So your final file will only consist of the one byte you wrote.

Just open it in update mode:

f = open(program, "r+b")

and it should be fine.