欢迎光临散文网 会员登陆 & 注册

反调试专题丨反调试之BeingDebugged

2023-06-28 13:18 作者:rkvir逆向工程学院  | 我要投稿

一、x86下IsDebuggerPresent反调试以及反反调试

1、反调试

进程结构体PEB偏移0x2处是一个标志位,当当前程序在调试状态下时,这个标志位就会被改变:

nt!_PEB
   +0x000 InheritedAddressSpace : UChar
   +0x001 ReadImageFileExecOptions : UChar
   +0x002 BeingDebugged    : UChar                isDbg值,8字节
   +0x003 BitField         : UChar
   +0x003 ImageUsesLargePages : Pos 0, 1 Bit
   +0x003 IsProtectedProcess : Pos 1, 1 Bit
   +0x003 IsLegacyProcess  : Pos 2, 1 Bit
   +0x003 IsImageDynamicallyRelocated : Pos 3, 1 Bit
   +0x003 SkipPatchingUser32Forwarders : Pos 4, 1 Bit
   +0x003 SpareBits        : Pos 5, 3 Bits
   +0x004 Mutant           : Ptr32 Void
   +0x008 ImageBaseAddress : Ptr32 Void              镜像基址
   +0x00c Ldr              : Ptr32 _PEB_LDR_DATA             _PEB_LDR_DATA结构
   +0x010 ProcessParameters : Ptr32 _RTL_USER_PROCESS_PARAMETERS
   +0x014 SubSystemData    : Ptr32 Void
   +0x018 ProcessHeap      : Ptr32 Void             
   +0x01c FastPebLock      : Ptr32 _RTL_CRITICAL_SECTION
   +0x020 AtlThunkSListPtr : Ptr32 Void
   +0x024 IFEOKey          : Ptr32 Void
   +0x028 CrossProcessFlags : Uint4B
   .
   .
   .

而API IsdebuggerPresent函数就是检测这个位置(不进内核),FS寄存器偏移0x30的地方就是PEB的地址,此函数的汇编实现如下:

7622C220 mov         eax,dword ptr fs:[00000030h] 
7622C226  movzx       eax,byte ptr [eax+2] 

下面是部分利用代码:

DWORD WINAPI MyIsDebug(
LPVOID lpThreadParameter
)
{
    while (1) {
       if (IsDebuggerPresent())
       {
           MessageBox(NULL, L"警告", L"调试中", MB_OK);
       }
    }
    return 1;
}

int main()
{
    //IsDebuggerPresent();
    CreateThread(NULL, NULL, MyIsDebug, NULL, NULL, NULL);
    std::cout << "Hello World!\n";
    system("pause");
    return 0;
}

也可以自己实现此函数,可以避免对于IsDebuggerPresent函数的Hook或者断点检查:

bool IsDebuggerR()
{
    bool bRet = false;
    __asm {
       mov eax, fs: [0x30]
       mov al, byte ptr[eax + 2]
       mov bRet, al
    }
    return bRet;//返回TRUE是debug
}
DWORD IsDebuggerR()
{
    DWORD MyPeb = __readfsdword(0x30);
    DWORD MyFlag = *(DWORD*)(MyPeb+0x2)
    return MyFlag;
}

效果如下:





2、对于IsdebuggerPresent的反反调试

1. 很多调试器有对应插件,可以过掉此反调试手段。

2. 在调试器中对此函数下断点,修改其函数返回值,达到反反调试。

3. 通过对进程注入DLL,在DLL中Hook函数IsdebuggerPresent。

二、x64下反调试

1、反调试

64位系统下PEB结构有所变化,对于IsDebuggerPresent检测的标志位位置并没有区别,也就是说代码通用,但是自定义由于x64不支持内联汇编,所以引入asm文件,部分代码如下:

mov rax,qword ptr gs:[60h]
movzx eax,byte ptr[rax + 2h]

当然也可以通过函数readgsqword获取PEB地址进而获取标志位结果。

2、反反调试

和x86没有区别
最后,CheckRemoteDebuggerPresent函数也是和IsdebuggerPresent函数类似功能。


反调试专题丨反调试之BeingDebugged的评论 (共 条)

分享到微博请遵守国家法律