BT is a static binary rewriting engine that can
- insert new instructions at arbitrary location
- modify existing instructions
- create new sections with precise control
- add entries to existing sections (e.g.,
.rel.text
) - ...
This example demonstrates how to insert an "nop" instruction between every two instructions.
- save the following code to nop.py ``` import sys from bintran import Elf32
def add_nop(elf):
elf.insert(*[(i.address, '\x90') for i in elf.disasm()])
return elf
if __name__ == '__main__':
with open(sys.argv[1], 'rb') as obj:
elf = Elf32(obj.read())
elf = add_nop(elf)
with open(sys.argv[1], 'wb') as obj:
obj.write(str(elf))
```
- prepare an object file test.o (maybe [helloworld] (http://en.wikipedia.org/wiki/List_of_Hello_world_program_examples#C)?)
- remember the original layout:
objdump -d test.o
- try to insert "nop":
python nop.py test.o
- see the new layout:
objdump -d test.o
- check if such modification breaks the program by linking and running
test.py
contains a few other examples such as replacing CALL
instruction
to semantically same instructions: PUSH
+ JMP
. Take a look.