##Pythonに噛まれました
Windows APIを利用し,デバッガを作成しています.
言語はPython(2.7)です.
現在,手動で電卓を開き,プロセスIDを取得し,
電卓のプロセスを停止するプログラムを作成しています.
プログラムのコンパイルは無事に通るのですが,
アタッチに失敗します.
なぜなのかと解決策を質問したいです.
また以前,
PROCESS_ALL_ACCESSが定義されていないと
コンパイルが通らなかったことがあります.
定義を行って修正しましたが
問題がないかもお願いいたします.
###実行例
実行例(PID:1620):
Enter the PID of the process to attach to:1620
[*] Unable to attach to the process.
There was an error
##my_debugger_defines
from ctypes import * WORD = c_ushort DWORD = c_ulong LPBYTE = POINTER(c_ubyte) LPTSTR = POINTER(c_char) HANDLE = c_void_p DEBUG_PROCESS = 0x00000001 CREATE_NEW_CONSOLE = 0x00000010 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_debuggera
from ctypes import * from my_debugger_defines import * kernel32 = windll.kernel32 #PROCESS_ALL_ACCESS = 2035711 PROCESS_ALL_ACCESS = (0x000F0000L | 0x00100000L | 0xFFF) class debugger(): def __init__(self): self.h_process = None self.pid = None self.debugger_active = False def load(self,path_to_exe): creation_flags = DEBUG_PROCESS startupinfo = STARTUPINFO() process_information = PROCESS_INFORMATION() startupinfo.dwFlags = 0x1 startupinfo.wShowWindow = 0x0 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 self.h_process = self.open_process(process_information.dwProcessId) else: print "Error: 0x%08x." % kernel32.GetLastError() 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." 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 datach(self): if kernel32.DebugActiveProcessStop(self.pid): print "[*] Finished debugging. Exiting..." return True else: print "There was an error" return False
##実行ファイル
import my_debuggera debugger = my_debuggera.debugger() pid = raw_input("Enter the PID of the process to attach to:") debugger.attach(int(pid)) debugger.run() debugger.datach()
###再定義
PROCESS_ALL_ACCESS
###補足情報
Python2.7
Windows7

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/15 18:50