Shuzheng Shuzheng - 4 months ago 19
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

. The value
is an integer.

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

f = open(program, "wb"), 0) # absolute

The code works perfectly when reading the byte at
, but writing to this position results in
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.