例子代碼: python
調試器須要具有兩種基本能力:打開執行程序做爲自身子進程運行、附加一個現有進程的能力 函數
第一種使用CreateProcessA函數,重要的參數是:lpApplicationName(可執行文件所在路徑)、lpCommandLine(命令行參數)、dwCreationFlags(可調式設置)、lpStartupInfo(結構體STARINFO)和lpProcessInformation(結構體PROCESS_INFORMATION),其他參數能夠設置爲NULL。 spa
my_debugger_defines.py 命令行
#__author__ = 'zt' #coding=utf-8 from ctypes import * #爲ctype變量建立符合匈牙利命名風格的匿名,這樣能夠使代碼更接近Win32的風格 WORD = c_ushort DWORD = c_ulong LPBYTE = POINTER(c_ubyte) LPTSTR = POINTER(c_char) HANDLE = c_void_p #常值定義 DEBUG_RPOCESS = 0x00000001 CREATE_NEW_CONSOLE = 0x00000010 #定義函數CreateProcessA()所需的結構體 class STARTUPINFO(Structure): _fields_ = [ ("cb", DWORD), ("lpReserved", LPTSTR), ("lpDesktop", LPTSTR), ("lpTitle", LPTSTR), ("dwX", DWORD), ("dwY", DWORD), ("dwXSize", DWORD), ("dwYSize", DWORD), ("dwXCountChars", DWORD), ("dwYCountChars", DWORD), ("dwFillAttribute", DWORD), ("dwFlags", DWORD), ("wShowWindow", WORD), ("cbReserved2", WORD), ("lpReserved2", LPBYTE), ("hStdInput", HANDLE), ("hStdOutput", HANDLE), ("hStdError", HANDLE), ] class PROCESS_INFORMATION(Structure): _fields_ = [ ("hProcess", HANDLE), ("hThread", HANDLE), ("dwProcessId", DWORD), ("dwThreadId", DWORD), ]my_debugger.py
#__author__ = 'zt' #coding=utf-8 from ctypes import * from my_debugger_defines import * kernel32 = windll.kernel32 class debugger(): def __init__(self): pass def load(self,path_to_exe): #參數dwCreationFlags中的標誌位控制着進程的建立方式。你若但願新建立的進程獨佔一個新的控制檯窗口,而不是與父進程 #共用同一個控制檯,你能夠加上標誌位CREATE_NEW_CONSOLE creation_flags = DEBUG_PROCESS creation_flags = DEBUG_RPOCESS #實例化以前定義的結構體 startupinfo = STARTUPINFO() process_information = PROCESS_INFORMATION() #在如下兩個成員變量的共同做用下,新建進程將在一個單獨的窗體中被顯示,你能夠經過改變結構體STARTUPINFO中的各成員 #變量的值來控制debugee進程的行爲。 startupinfo.dwFlags = 0x1 startupinfo.wShowWindow = 0x0 #設置結構體STARTUPINFO中的成員變量 #cb的值,用以表示結構體自己的大小 startupinfo.cb = sizeof(startupinfo) if kernel32.CreateProcessA( path_to_exe, None, None, None, None, creation_flags, None, None, byref(startupinfo), byref(process_information) ): print "[*] We have successfully lauched the process!" print "[*] PID: %d" % process_information.dwProcessId else: print "[*] Error: 0x%08x." % kernel32.GetLastError()my_test.py
__author__ = 'zt' import my_debugger debugger = my_debugger.debugger() debugger.load("C:\\WINDOWS\\system32\\calc.exe")