API Hooking 예제(notepad 저장 내용 변경하기)
Aplication/Python2015. 8. 27. 10:55
pydbg : ctypes를 이용해서 Win32 API를 호출하는 부분은 모두 pydbg 모듈 내부에서 처리해 준다.
pydbg Download : http://rotiples.tistory.com/33
hook_container DownLoad : http://pedram.openrce.org/PaiMei/heap_trace/hook_container.py
import hook_container import sys from pydbg import * from pydbg.defines import * ''' BOOL WINAPI WriteFile( _In_ HANDLE hFile, _In_ LPCVOID lpBuffer, _In_ DWORD nNumberOfBytesToWrite, _Out_opt_ LPDWORD lpNumberOfBytesWritten, _Inout_opt_ LPOVERLAPPED lpOverlapped ); ''' dbg = pydbg() isProcess = False orgPattern = "sun" #찾을 문자 repPattern = "rotiple" #변경할 문자 processName = "notepad.exe" #후킹 목표 ProcessName def replaceString(dbg, args): #콜백 함수 선언 buffer = dbg.read_process_memory(args[1], args[2]) #메모리 값 읽기 #지정된 주소에서 지정된 길이 만큼 메모리주소를 읽어서 반환(Kernel32.ReadProcessMemory) if orgPattern in buffer: #메모리 값에서 패턴 검사 print "[APIHooking] Before : %s" % buffer buffer = buffer.replace(orgPattern, repPattern) #값 변경 replace = dbg.write_process_memory(args[1], buffer) #메모리 값 쓰기 #변경된 값을 메모리에 저장(Kernel32.WriteProcessMemory) print "[APIHooking] After : %s" % dbg.read_process_memory(args[1], args[2]) return DBG_CONTINUE for(pid, name) in dbg.enumerate_processes(): # 프로세스 ID 리스트 얻기 #Kernel32.CreateToolhelp32Snapshot if name.lower() == processName : isProcess = True hooks = hook_container.hook_container() dbg.attach(pid) #프로세스 핸들 구하기 #kernl32.OpenProcess,Kernel32.DebugActiveProcess print "Saves a process handle in self.h_process of pid[%d]" % pid hookAddress = dbg.func_resolve_debuggee("kernel32.dll", "WriteFile") #중단점을 설치할 함수의 주소 구하기 if hookAddress: hooks.add(dbg, hookAddress, 5, replaceString, None) #중단점 설정 print "sets a breakpoint at the designated address : 0x%08x" % hookAddress break else: print "[Error] : couldn't resolve hook address" sys.exit(-1) if isProcess: print "waiting for occuring debugger event" dbg.run() #디버그 시작 else: print "[Error] : There in no process [%s]" % processName sys.exit(-1)
'Aplication > Python' 카테고리의 다른 글
파이썬 소스코드 검색 하는곳 (0) | 2015.08.27 |
---|---|
python:pyHook 을 이용한 Keyboard Hooking 예제 (0) | 2015.08.25 |
KeyBoard Hooking (키보드 후킹)(python2.7) (0) | 2015.08.24 |
윈도우용 Python 모듈 (pip 설치 불가 포함) (0) | 2015.08.24 |
py2exe, psycopg2 설치하기 (0) | 2015.08.21 |