gpt4 book ai didi

c# - DebugActiveProcess 崩溃

转载 作者:可可西里 更新时间:2023-11-01 11:46:08 25 4
gpt4 key购买 nike

我正在尝试调试处于挂起状态的子进程。问题是,当我尝试这样做时,应用程序不会启动(它启动但立即退出)。这有助于防止逆向工程,但我做不到。

static void DebuggingTest(PROCESS_INFORMATION pi, int imageBase, int sizeOfHeaders)
{
int oldProtection = 0;
const uint STATUS_GUARD_PAGE_VIOLATION = 0x80000001;
const int DBG_CONTINUE = 0x00010002;
DEBUG_EVENT evt = new DEBUG_EVENT();
if (!DebugActiveProcess(Convert.ToInt32(pi.dwProcessId)))
throw new Win32Exception();
else
MessageBox.Show(pi.dwProcessId + " process is being debugged.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

while (true)
{
if (!WaitForDebugEvent(out evt, -1))
throw new Win32Exception();
else
MessageBox.Show("WaitForDebugEvent executed.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

switch (evt.dwDebugEventCode)
{
case DebugEventType.CREATE_PROCESS_DEBUG_EVENT:
//if (!VirtualProtectEx(pi.hProcess, imageBase, sizeOfHeaders, 320, ref oldProtection))
// throw new Exception();
ResumeThread(pi.hThread);
MessageBox.Show("CREATE_PROCESS_DEBUG_EVENT executed.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case DebugEventType.EXCEPTION_DEBUG_EVENT:
if (evt.Exception.ExceptionRecord.ExceptionCode == STATUS_GUARD_PAGE_VIOLATION)
{
if (!VirtualProtectEx(pi.hProcess, imageBase, sizeOfHeaders, 320, ref oldProtection))
throw new Exception();
}
MessageBox.Show("EXCEPTION_DEBUG_EVENT executed.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case DebugEventType.EXIT_PROCESS_DEBUG_EVENT:
if (!DebugActiveProcessStop(Convert.ToInt32(pi.dwProcessId)))
throw new Win32Exception();
if (!TerminateProcess(pi.hProcess, 0))
throw new Win32Exception();
MessageBox.Show("EXIT_PROCESS_DEBUG_EVENT executed.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case DebugEventType.LOAD_DLL_DEBUG_EVENT:
if (CloseHandle(evt.LoadDll.hFile))
MessageBox.Show("CloseHandle executed.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}

if (ContinueDebugEvent(evt.dwProcessId, evt.dwThreadId, DBG_CONTINUE))
MessageBox.Show("Last ContinueDebugEvent executed.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

编辑:VirtualProtectEx 调用失败,这是实际问题。如果我评论他们的行,应用程序就会执行。它也很滞后,可能是因为调试循环。有解决方案吗?

Edit2:除了 DEBUG_PROCESS 之外,我按照你的建议编辑了代码,因为它停止了整个进程,我什至无法通过任务管理器杀死它,杀死它的唯一方法是重新启动 PC .也许,我做错了什么,但我不知道是什么。所以消息框出现的顺序是:执行 DebugActiveProcess -> 执行 WaitForDebugEvent -> 执行 CREATE_PROCESS_DEBUG_EVENT -> 最后执行 ContinueDebugEvent。然后等待... -> CloseHandle -> ContinueDebug ...

最佳答案

1) 当你想通过 CreateProcess 创建调试进程时设置 DEBUG_PROCESSdwCreationFlags

2) 在这种情况下永远不要调用 DebugActiveProcess - 这个 api 内部在进程中创建远程线程 - DbgUiRemoteBreakIn - 所以进程不是在它的主线程中而是在这个线程中开始初始化.在 xp 上说这导致进程初始化总是失败。后者(从 win7 开始?)已修复。

3) ContinueDebugEvent 需要在每次调试事件后调用always。在 EXIT_PROCESS_DEBUG_EVENT - also - 需要了解此消息在退出时发送给你的最后一个进程中的线程。并且他等待您的调用 ContinueDebugEvent - 当您收到 EXIT_PROCESS_DEBUG_EVENT 时,进程仍然存在并且没有终止 - 因此您必须在之后对 ContinueDebugEvent 进行一次普通调用开关

4) 你尝试用 VirtualProtectEx 做的事情必须产生无限的 STATUS_GUARD_PAGE_VIOLATION 异常。所以即使不多看——“保护”的主要逻辑是无效的

5) 句柄 LOAD_DLL_DEBUG_EVENT强制性的 - 您必须关闭句柄(hFile 到加载的 DLL)

6) 在 CREATE_PROCESS_DEBUG_EVENT 上,您还必须关闭文件句柄


7) 当我们使用 DEBUG_PROCESS 标志调用 CreateProcess 时 - 使用 CREATE_SUSPENDED - 绝对没有意义 - 为什么??通常这个标志用于处理进程的一些任务,在线程开始以用户模式执行之前和调用 ResumeThread 之后。但是当我们在进程中使用 DEBUG_PROCESS 初始线程时,当它开始执行时(但在内核模式下)发送给我们 CREATE_PROCESS_DEBUG_EVENT 并等待回复(直到调试器不调用 ContinueDebugEvent。因此,我们可以在 CREATE_PROCESS_DEBUG_EVENT 处理程序中完成所有带有进程的特殊任务。对 Windows 内部工作方式的误解会导致严重错误

关于c# - DebugActiveProcess 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44422619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com