c++ - 另一个进程中的断点处理

标签 c++ debugging winapi assembly

遵循 Read eax register 中给出的建议,我使用 winapi 编写了一个简单的调试器。 我的目标是每次在另一个线程中执行汇编指令后读取 eax 寄存器。 它正在运行,我设法在另一个进程中放置了一个硬件断点。

当断点到达被调试线程内部时,问题出现了,我可以按预期读取 eax 寄存器,但我仍然找不到任何方法来恢复线程的执行。

我的代码:

 int _tmain(int argc, _TCHAR* argv[])
 {
 // Finding Window
HWND window = FindWindow(0, _T("Test"));
if( window == 0 )
{
    printf("Process not found!\n");
    return 0;
}

DWORD_PTR pID = 0;
GetWindowThreadProcessId(window, &pID);


// Get Handle
//HANDLE _handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pID);

DWORD_PTR eax = 0;
DWORD_PTR address = 0xC31E1B; // address of the instruction after the call for hardware breakpoint

DebugActiveProcess(pID); // PID of target process

// Avoid killing app on exit
DebugSetProcessKillOnExit(false);

// get thread ID of the main thread in process
DWORD_PTR dwThreadID = GetProcessThreadID(pID);

// gain access to the thread
HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, dwThreadID);

SetDebugPrivilege(true);

//ctx.Dr6=0;           //clear debug status register (only bits 0-3 of dr6 are cleared by processor)

CONTEXT ctx = {0};
ctx.ContextFlags = CONTEXT_DEBUG_REGISTERS | CONTEXT_INTEGER;
ctx.Dr0 = address;
ctx.Dr7 = 0x00000001;


// hThread with enough permissions
SetThreadContext(hThread, &ctx); 

DEBUG_EVENT dbgEvent;
while (true)
{
    if (WaitForDebugEvent(&dbgEvent, INFINITE) == 0)
        break;

    if (dbgEvent.dwDebugEventCode == EXCEPTION_DEBUG_EVENT &&
        dbgEvent.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_SINGLE_STEP) // EXCEPTION_BREAKPOINT
    {
        if (dbgEvent.u.Exception.ExceptionRecord.ExceptionAddress == (LPVOID)address)
        {
            GetThreadContext(hThread, &ctx);
            eax = ctx.Eax; // eax get
            std::cout<<eax<<"\n";
            // Resume execution
            ctx.Eip = address + 0x3;
            SetThreadContext(hThread, &ctx);
        }

    }

    ContinueDebugEvent(dbgEvent.dwProcessId, dbgEvent.dwThreadId, DBG_CONTINUE);
}
return 0;
}

感谢帮助!!

最佳答案

取自:Hardware breakpoints EXCEPTION_SINGLE_STEP all the time

整个逻辑应该如下:

  1. 添加断点。
  2. 触发它后,您删除断点并在 EFLAGS (0x100) 中设置单步标志
  3. 下次调用循环时,重新启用硬件断点。

所以你的 while 循环应该是这样的:

while (true)
{
    if (WaitForDebugEvent(&dbgEvent, INFINITE) == 0)
        break;

    if (dbgEvent.dwDebugEventCode == EXCEPTION_DEBUG_EVENT &&
        dbgEvent.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_SINGLE_STEP) // EXCEPTION_BREAKPOINT
    {
        CONTEXT newCtx = {0};
        newCtx.ContextFlags = CONTEXT_ALL;
        GetThreadContext(hThread, &newCtx);
        if (dbgEvent.u.Exception.ExceptionRecord.ExceptionAddress == (LPVOID)address)
        {
            newCtx.Dr0 = newCtx.Dr6 = newCtx.Dr7 = 0;
            newCtx.EFlags |= (1 << 8);
        }else{
            newCtx.Dr0 = address;
            newCtx.Dr7 = 0x00000001;
            newCtx.EFlags &= ~(1 << 8);
        }
        SetThreadContext(hThread, &newCtx);
    }
    ContinueDebugEvent(dbgEvent.dwProcessId, dbgEvent.dwThreadId, DBG_CONTINUE);
}

关于c++ - 另一个进程中的断点处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16534397/

相关文章:

C++、多重集、时间复杂度

c++ - 有人能给我一个支持代理的非托管 C++ HTML 客户端的例子吗?

android - 创建安卓数据库表

c++ - 调试段错误

windows - 如果窗口跨越多个显示器,我无法绘制它

winapi - 带有 FLAG_DELETE_ON_CLOSE 的 Win32 API CreateDirectory。可能的?

c++ - 已初始化静态变量的警告 C26495

c++ - C++中的二维数组加法

JavaScript 木马剖析

c - 在 C 中加载或保存位图句柄 win32