
patching binaire depuis Python
Patche un binaire ELF en utilisant un ou plusieurs scripts Python simples.
Utilisation :
patch <binaire> <répertoire_patch|fichier> [répertoire_patch|fichier...]
Contient un ou plusieurs fichiers de patch Python, qui seront exécutés par ordre alphabétique sur un binaire.
Noper une adresse, injecter une fonction assembleur et hooker le point d'entrée :
def simple_patch(pt):
# nop out a jump at the entry point
pt.patch(pt.entry, hex='90' * 5)
# inject assembly into the binary and return the address
addr = pt.inject(asm='mov eax, 1; ret')
# hook the entry point to make it call addr (ret will run the original entry point)
pt.hook(pt.entry, addr)
Remplacer une fonction C :
def replace_free(pt):
# pretend free() is at this address:
old_free = 0x804fc4
# inject a function to replace free()
new_free = pt.inject(c=r'''
void free_stub(void *addr) {
printf("stubbed free(%p)\n", addr);
}
''')
# patch the beginning of free() with a jump to our new function
pt.patch(old_free, jmp=new_free)
addr = search(data)
hook(addr, new_addr)
patch(addr, *compile arg*)
addr = inject(*compile arg*)
*compile arg* est l'un des éléments suivants :
raw='data'
hex='0bfe'
asm='nop'
jmp=0xaddr
c='void func() { int a; a = 1; }' (uniquement supporté sur inject, pas sur patch)
Certains scripts se trouvent dans le chemin ida/. Exécutez-les comme ceci :
/Applications/IDA\ Pro\ 6.8/idaq.app/Contents/MacOS/idaq64 -A -Sida/allfuncs.py a.out
Lorsqu'il est invoqué ainsi, allfuncs.py génère a.out.funcs qui est utilisé par les scripts de durcissement.
Ceux-ci sont pour l'instant spécifiques à CGC et x86, mais seront portés pour une utilisation générale à l'avenir.
./deps.sh pour les installer automatiquement.