프로세스 attach는 디버깅을 할 준비를 한다는 것이다. Windows에서는 디버깅 할 프로세스의 pid를 DebugActiveProcess 함수의 인자로 넘기기만 하면 디버그 이벤트가 발생하여 디버깅을 할 수 있게 된다.
디버그가 가능한 상태로 만든 후에는 WaitForDebugEvent 함수를 써서 디버그 이벤트를 처리할 수 있다. 이벤트가 끝나면 ContinueDebugEvent로 프로세스 실행이 계속되게 한다.
CreateToolhelp32Snapshot을 이용하면 쓰레드들의 snapshot을 얻을 수 있는데, 쓰레드들을 관리하는 THREADENTRY32라는 구조체에서 th32OwnerProcessID 멤버와 프로세스의 ID와 비교하는 방식으로 디버깅 대상 프로세스의 쓰레드들을 얻을 수 있다.
이렇게 얻어온 쓰레드 리스트들을 GetThreadContext 함수로 넘겨서 각 쓰레드들의 Context를 볼 수 있다. 상기한 내용은 아래와 같은 코드로 구현할 수 있다.
def open_process(self,pid):
h_process = kernel32.OpenProcess(PROCESS_ALL_ACCESS,False,pid)
return h_process
def attach(self,pid):
self.h_process = self.open_process(pid)
if kernel32.DebugActiveProcess(pid):
self.debugger_active = True
self.pid = int(pid)
else:
print "[*] Unable to attach to the process."
print "[*] GetLastError : %d"%kernel32.GetLastError()
def run(self):
while self.debugger_active == True:
self.get_debug_event()
def get_debug_event(self):
debug_event = DEBUG_EVENT()
continue_status = DBG_CONTINUE
if kernel32.WaitForDebugEvent(byref(debug_event),INFINITE):
raw_input("Press a key to continue...")
self.debugger_active = False
kernel32.ContinueDebugEvent(debug_event.dwProcessId, debug_event.dwThreadId, continue_status)
def detach(self):
if kernel32.DebugActiveProcessStop(self.pid):
print "[*] Fininshed debugging. Exiting..."
return True
else:
print "There was an error"
return False
'Study > Grayhat python' 카테고리의 다른 글
브레이크 포인트 종류 (0) | 2015.10.16 |
---|