2012年10月9日星期二

DebugBreak.Cpp


/*
以前知道检测调试器用微软的IsDebuggerPresent,这几天又发现微软提供的一个办法:用异常处理。

IsDebuggerPresent()的实现代码是:jmp到这里(下面)。
75D6F41B 64 A1 18 00 00 00 mov         eax,dword ptr fs:[00000018h]
75D6F421 8B 40 30         mov         eax,dword ptr [eax+30h]
75D6F424 0F B6 40 02      movzx       eax,byte ptr [eax+2]
75D6F428 C3               ret

其实DebugBreak()就是几个jmp,然后jmp到这里:
75D93E2C 8B FF            mov         edi,edi
75D93E2E CC               int         3  
75D93E2F C3               ret
*/

#include <windows.h>

BOOL CheckForDebugger()
{
    __try
    {
        DebugBreak();
    }
    __except(GetExceptionCode() == EXCEPTION_BREAKPOINT ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
    {   // No debugger is attached, so return FALSE and continue.
        return FALSE;
    }
    return TRUE;
}

VOID main(VOID)
{
    bool b = IsDebuggerPresent();
    if (b == 0)
    {
        MessageBox(0,L"不在调试器中",0,0);      
    }
    else
    {
        MessageBox(0,L"在调试器中",0,0);      
    }

    b = CheckForDebugger();
    if (b == 0)
    {
        MessageBox(0,L"不在调试器中",0,0);      
    }
    else
    {      
        MessageBox(0,L"在调试器中",0,0);
    }
}
//made by correy
//made at 2012.10.09

没有评论:

发表评论