ايران ويج

نسخه‌ی کامل: استفاده از DEBUG_EVENT در پایتون
شما در حال مشاهده‌ی نسخه‌ی متنی این صفحه می‌باشید. مشاهده‌ی نسخه‌ی کامل با قالب بندی مناسب.
سورس کد من به این شکل است :
فایل my_debugger:

کد:
from ctypes import *
from my_debugger_define import *


kernel32 = windll.kernel32
class debugger():
    def __init__(self):
        pass
        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)):
            self.h_Process = self.open_Process(process_information.dwProcessId)
            print "[*] We have successfully launched the process!"
            print "[*] PID: %d" % process_information.dwProcessId
        else:
            print "[*] Error: 0x%08x." % kernel32.GetLastError()

    def open_Process(self, pid):
        PROCESS_ALL_ACCESS = (0x000F0000L | 0x00100000L | 0xFFF)
        h_Process = kernel32.OpenProcess(PROCESS_ALL_ACCESS, False, pid)
        return h_Process

    def attach(self, pid):
        self.h_Process = self.open_Process(pid)
            # We attempt to attach to the Process
            # if this fails we exit the call
        if kernel32.DebugActiveProcess(pid):
            self.debugger_active = True
            self.pid = int(pid)
            self.run()
        else:
            print "[*] Unable to attach to the Process."

    def run(self):
    # Now we have to poll the debuggee for
    # debugging events
        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):
    # We aren't going to build any event handlers
    # just yet. Let's just resume the Process for now.
            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 "[*] Finished debugging. Exiting..."
            return True
        else:
            print "There was an error"
            return False
[*]

فایل my_debugger_define :

کد:
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),
    ]


[*]

من تو ویندوز xp sp3 تو pycharm این کد رو اجرا میکنم اما این اررور رو برمیگردونه :

کد:
  File "C:\GrayHatPy\my_debbuger.py", line 58, in get_debug_event
    debug_event = DEBUG_EVENT()
NameError: global name 'DEBUG_EVENT' is not defined


[*]

مشکل از کجاست ؟؟؟
در ضمن این کد مربوط به کتاب Grayaht for paython هست...
مثل اینکه کدهای توی کتاب یکم باگ دارن

مثلاهاش از اینجا دانلود کن و اجرا کن ببین درست میشه؟

my_debugger_defines.py

my_debugger.py

و اینکه خوندم نوشته بود باید ویندوزت ۳۲ بیت باشه!
مال شما که ۶۴ بیت نیست؟!


--------------

کدهاش هم اینه که توی سایت خود کنتابه
اینا هم تست کن
https://www.nostarch.com/download/ghpython_src.zip
اینا هم آپدیت های کتاب هستن

ببن کدت واسه کدون صفحست آپیدتش رو پیدا کن

https://www.nostarch.com/ghpython.htm#updates