使用 PyDbg 进行线程切换

逆向工程 视窗 调试器 线
2021-06-23 05:08:43

我在 pydbg 中从调试一个线程切换到另一个线程时遇到问题。我对多线程没有太多经验,所以我希望我只是遗漏了一些明显的东西。

基本上,我想挂起所有线程,然后在一个线程中开始单步执行。就我而言,有两个线程。

首先,我暂停所有线程。然后,我在线程 2 恢复时 EIP 所在的位置设置断点。(此位置通过使用 IDA 确认)。然后,我像在任何其他上下文中一样启用单步执行,并恢复线程 2。

但是,pydbg 似乎没有捕获断点异常!线程 2 似乎恢复,即使它必须命中该地址,也没有迹象表明 pydbg 正在捕获断点异常。我在 pydbg 的内部断点处理程序中包含了一个“打印“HIT BREAKPOINT”,并且在恢复线程 2 后似乎永远不会被调用。

我不太确定下一步要去哪里,所以任何建议都值得赞赏!

    dbg.suspend_all_threads()
    print dbg.enumerate_threads()[0]
    oldcontext = dbg.get_thread_context(thread_id=dbg.enumerate_threads()[0])
    if (dbg.disasm(oldcontext.Eip) == "ret"):
        print disasm_at(dbg,oldcontext.Eip)
        print "Thread EIP at a ret"
        addrstr = int("0x"+(dbg.read(oldcontext.Esp + 4,4))[::-1].encode("hex"),16)
        print hex(addrstr)
        dbg.bp_set(0x7C90D21A,handler=Thread_Start_bp_Handler)
        print dbg.read(0x7C90D21A,1).encode("hex")
    dbg.bp_set(oldcontext.Eip + dbg.instruction.length,handler=Thread_Start_bp_Handler)
    dbg.set_thread_context(oldcontext,thread_id=dbg.enumerate_threads()[0])
    dbg.context = oldcontext
    dbg.resume_thread(dbg.enumerate_threads()[0])
    dbg.single_step(enable=True)
    return DBG_CONTINUE

对“神奇数字”感到抱歉,但据我所知,它们是正确的。

1个回答

你的一个问题是,您尝试通过单步线程2,你仅指线程1中的代码:

dbg.enumerate_threads()[0] # <--- Return handle to the first thread.

此外,您发布的代码并不能反映您脚本的完整结构,这使得很难判断您是否有其他错误。您还尝试在反汇编指令的子分支中设置断点,这在逻辑上对我来说没有多大意义。让我试着解释我所知道的,并有条理地列出来。这样你就可以回顾你的代码,重新思考并纠正它。

让我们从使用 pydbg 调试应用程序的基本框架开始:

  1. 创建调试器实例
  2. 附上流程
  3. 设置断点
  4. 运行
  5. 断点被击中 - 处理它。

这是它的样子:

from pydbg import *
from pydbg.defines import *

# This is maximum number of instructions we will log
MAX_INSTRUCTIONS = 20

# Address of the breakpoint
func_address = "0x7C90D21A"

# Create debugger instance
dbg = pydbg()

# PID to attach to
pid = int(raw_input("Enter PID: "))

# Attach to the process with debugger instance created earlier.
# Attaching the debugger will pause the process.
dbg.attach(pid)

# Let's set the breakpoint and handler as thread_step_setter,
# which we will define a little later...
dbg.bp_set(func_address, handler=thread_step_setter)

# Let's set our "personalized" handler for Single Step Exception
# It will get triggered if execution of a thread goes into single step mode.
dbg.set_callback(EXCEPTION_SINGLE_STEP, single_step_handler)

# Setup is done. Let's run it...
dbg.run() 

现在有了基本结构,让我们为断点和单步定义我们的个性化处理程序。下面的代码片段定义了我们的“自定义”处理程序。将会发生的是,当断点命中时,我们将遍历线程并将它们设置为单步模式。它将依次触发单步异常,我们将处理和反汇编 MAX_INSTRUCTIONS 条指令:

def thread_step_setter(dbg):
    dbg.suspend_all_threads()
    for thread_id in dbg.enumerate_threads():
        print "Single step for thread: 0x%08x" % thread_id
        h_thread = dbg.open_thread(thread_id)
        dbg.single_step(True, h_thread)
        dbg.close_handle(h_thread)

    # Resume execution, which will pass control to step handler
    dbg.resume_all_threads()

    return DBG_CONTINUE

def single_step_handler(dbg):
    global total_instructions
    if instructions == MAX_INSTRUCTION:
        dbg.single_step(False)
        return DBG_CONTINUE
    else:
        # Disassemble the instruction
        current_instruction = dbg.disasm(dbg.context,Eip)
        print "#%d\t0x%08x : %s" % (total_instructions, dbg.context.Eip, current_instruction)
        total_instructions += 1
        dbg.single_step(True)

    return DBG_CONTINUE

披露者:我不保证如果复制和粘贴上面的代码会起作用。我打出来的,还没有测试。但是,如果获得了基本的理解,那么小的语法错误就可以很容易地修复。如果我有的话,我提前道歉。我目前没有办法或时间来测试它。

我真的希望它能帮到你。