如何在稍后在 pykd 中加载的 dll 中设置 bp?

逆向工程 视窗 风袋 pykd
2021-06-25 22:57:10

我正在使用 pykd 调试一个应用程序,该应用程序仅在满足某些条件时才加载 dll。如何在 pykd 中尚未加载的 dll 中设置断点,以便我的处理程序获得回调?目前我的代码看起来像这样

class ExceptionHandler(pykd.eventHandler):
    def __init__(self):
        pykd.eventHandler.__init__(self)

    def onException(self, exceptionInfo):
        return pykd.eventResult.NoChange

    def onBreakpoint(self, id):
        return pykd.eventResult.NoChange

    def onThreadStart(self):
        return pykd.eventResult.NoChange

    def onThreadStop(self):
        return pykd.eventResult.NoChange

    def onLoadModule(self, base, name):
        print "onLoadModule " + name
        # sys.stdout.flush()
        # if name == "test_module":
        #     # test_module = pykd.module("test_module")
        #     # test_module.reload()
        #     # pykd.setBp(test_module.offset('test_function'), breakCount)
        #     # print pykd.dbgCommand("bl")
        #     print pykd.dbgCommand('bp test_module!test_function "r;gc"')
        #     # print pykd.dbgCommand("bl")
        #     # print "Breakpoint Set %x" % (test_module.offset('test_function'))
        #     print "Breakpoint Set"
        return pykd.eventResult.NoChange

    def onUnloadModule(self, base, name):
        return pykd.eventResult.NoChange

pykd.initialize()
pykd.handler = ExceptionHandler()
pykd.startProcess("testmydelayedload.exe %s\\%s" % (os.getcwd(), sys.argv[1].strip()))
alloc_module = pykd.module("ntdll")
alloc_module.reload()
b0 = pykd.setBp(alloc_module.offset('RtlAllocateHeap')+0xe6, breakCount)
b1 = pykd.setBp(alloc_module.offset('RtlFreeHeap'), breakCount)
pykd.loadExt("C:\\Program Files\\Windows Kits\\10\\Debuggers\\x86\\winext\\ext.dll")
pykd.go()
pykd.killAllProcesses()

我尝试使用手动设置断点,pykd.dbgCommand但在这种情况下不会触发回调。onLoadModule除了pykd.eventResult.NoChange在设置 bp 时,我尝试更改 to 的返回值我错过了什么?

1个回答

我刚刚想通了,存储 bp 的变量应该是全局的,否则它将无法工作。它应该在类的上下文之外活着。

def onLoadModule(self, base, name):
        global test_function, test_module
        print "onLoadModule " + name
        if name == "jscript":
            test_module = pykd.module("jscript")
            test_module.reload()
            test_function = pykd.setBp(test_module.offset('test_function'), breakCount)
            print "Breakpoint Set %x" % (test_module.offset('test_function'))
        return pykd.eventResult.NoChange