
Python을 이용한 바이너리 패치
하나 이상의 간단한 Python 스크립트를 사용하여 ELF 바이너리를 패치합니다.
사용법:
patch <binary> <patchdir|file> [patchdir|file...]
하나 이상의 Python 패치 파일을 포함하며, 바이너리에서 알파벳 순서로 실행됩니다.
주소를 NOP 처리하고, 어셈블리 함수를 주입하며, 엔트리 포인트를 후킹합니다:
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)
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*는 다음 중 하나입니다:
raw='data'
hex='0bfe'
asm='nop'
jmp=0xaddr
c='void func() { int a; a = 1; }' (inject에서만 지원되며, patch에서는 지원되지 않음)
일부 스크립트는 ida/ 경로에 있습니다. 다음과 같이 실행합니다:
/Applications/IDA\ Pro\ 6.8/idaq.app/Contents/MacOS/idaq64 -A -Sida/allfuncs.py a.out
이렇게 실행하면 allfuncs.py가 a.out.funcs 파일을 생성하며, 이 파일은 하드닝 스크립트에서 사용됩니다.
현재는 다소 CGC 및 x86에 특화되어 있지만, 향후 범용 사용을 위해 포팅될 예정입니다.
./deps.sh를 실행하여 자동으로 설치합니다.