小编Cra*_*ra0的帖子

C#Anti-Debug方法OutputDebugString无法正常工作

所以最近我一直在阅读反调试机制和我遇到的流行方法,以检查当前进程是否正在调试OutputDebugString.我已经编写了这段代码,但它没有完全按照预期工作,有人可以解释为什么或我做错了什么?

private static bool stub_OutputDebugString()
{
    uint ErrCode = 0x12A6;
    Native.SetLastError(ErrCode);
    Native.OutputDebugString("System.Core\n");
    if (Marshal.GetLastWin32Error() == (int)ErrCode)
    {
        //Debugger Detected
        return true;
    }
    else
    {
        //No Debugger Detected
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的P/Invoke签名

[DllImport("kernel32.dll", SetLastError = true)]
public static extern void SetLastError(uint dwErrCode);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern void OutputDebugString(string lpOutputString);
Run Code Online (Sandbox Code Playgroud)

注意我读了如何GetLastError不应该从本机环境调用,因为值可以更改,所以我正在使用Marshal.GetLastWin32Error()

代码应该工作,但当我尝试使用windbg或任何其他调试器调试应用程序时,最后一个错误不会改变.

c# debugging pinvoke windbg native-code

0
推荐指数
1
解决办法
1316
查看次数

标签 统计

c# ×1

debugging ×1

native-code ×1

pinvoke ×1

windbg ×1