from my_debugger_defines import *
from ctypes import *
import sys

 

# whether or not using the debugger, Debugger 상태에 대한 체크를 위한 Bool 변수
debugger_active = False

 

# Allocate the variable which is DEBUG_EVENT Structure, DEBUG_EVENT 구조체 할당
debug_event=DEBUG_EVENT()
continue_status=DBG_CONTINUE

 

# To use the APIs of kernel32 Library, kernel32.dll 사용하고 싶으면 이걸 해야지!
kernel32 = windll.kernel32

 

# the pid value inputted by user convert to Integer, pid 값을 인자 값으로 받아서 처리
pid = int(sys.argv[1])

 

# Process Open and Result, Debug 하려면 OpenProcess를 PROCESS_ALL_ACCESS로 하라는 ..
test = kernel32.OpenProcess(PROCESS_ALL_ACCESS,False,pid)
print '[*] OpenProcess() Result : %d' % test

 

# Process Attach, 디버거에 입력 받은 프로세스(pid)를 붙인다. debugger_active 변수를 True로
if kernel32.DebugActiveProcess(pid):
 print '[*] DebugActiveProcess Start !'
 debugger_active = True
 
# If Process Attach is successful, then
while debugger_active == True:


 # Wait for receiving specific Debug Event From Debugee, it is like WaitForSingleObject()
 if kernel32.WaitForDebugEvent(byref(debug_event),100):
  print ' [**] dwDebugEventCode : %d' % debug_event.dwDebugEventCode
  print ' [**] dwProcessId : %d' % debug_event.dwProcessId
  print ' [**] dwThreadId : %d' % debug_event.dwThreadId
  
  # To Release the Infinite Loop status
  # When specific keys pushed, debugger_active status changes False from True
  raw_input('Press a key to continue...')
        debugger_active=False
  
  # Resume the Debugee's running
        kernel32.ContinueDebugEvent(\
                debug_event.dwProcessId,\
                debug_event.dwThreadId,\
                continue_status)
 
if kernel32.DebugActiveProcessStop(pid):
 print '[*] DebugActiveProcess Stop !'

 

my_debugger_yong.py

 

my_debugger_yong.py

 

 

 

 

1. debugger 제작에 필요한 사전 define 변수 값들

 

my_debugger_defines.py

 

 

2. 실제 debugging Class 및 Class의 함수들이 들어 있는 소스

 

my_debugger.py

 

 

3. my_debugger.py에 정의된 class 를 선언하고, class의 함수를 이용

 

my_debugger_test.py


            
 출처 : http://ha.ckwith.me/entry/실행중인-프로세스를-분석할수-있는-디버거를-만들어보자

         (공유 내용에 대한 문제 있을 시, 삭제하도록 하겠습니다.)

 

 

우선 파이썬으로 Debugging 툴을 구성하기 위해서는

1) 해당 프로세스에 대한 접근 (OpenProcess) 이 가능해야 하며

2) Attach를 하기 위한 작업 (DebugActiveProcess, WaitForDebugEvent) 이 가능해야 한다.

3) Attache를 해제하기 위한 Detach 작업 (ContineuDebugEvent, DebugActiveProcessStop) 이 이뤄져야 한다.

 

 

# python 2.7.8 기준

 

1. 파이썬 설치 이후 Paimei 설치 (Paimei가 뭔지 잘모르나 일단 설치)

 

PaiMei-1.1.win32.exe

 

2. 아래 pydasm.pyd를 다운로드 후에 "C:\Python27\Lib\site-packages\pydbg\" 에 복사 후 덮어 씌움

 

pydasm.pyd

 

3. 아래 __init__.py 파일을 다운로두 후에 "C:\Python27\Lib\ctypes\"에 복사 후 덮어 씌움

 

__init__.py

 

4. python 에서 import pydbg 하여 에러 메시지 없으면 정상 설치

http://cdpython.tistory.com/35

 

http://ha.ckwith.me/entry/열혈강의-파이썬-간단한-명령어-해석기-설계

+ Recent posts