简单的反调试技术实现

admin 2025年1月26日23:20:47评论4 views字数 3072阅读10分14秒阅读模式

 

简单的反调试技术实现

反调试技术非常多,这里只是简单测试了几种在Windows 11上仍有效的方法,更多方法请参考:Anti-Debug Tricks https://anti-debug.checkpoint.com/:

Win32 API

IsDebuggerPresent

Determines whether the calling process is being debugged by a user-mode debugger.

if (IsDebuggerPresent()) {
    printf("attached debugger detected :(n");
    return -2;
}

CheckRemoteDebuggerPresent

The function kernel32!CheckRemoteDebuggerPresent() checks if a debugger (in a different process on the same machine) is attached to the current process.

BOOL bDebuggerPresent;
if (TRUE == CheckRemoteDebuggerPresent(GetCurrentProcess(), &bDebuggerPresent) &&
    TRUE == bDebuggerPresent)
    ExitProcess(-1);

 

NtQueryInformationProcess

The function ntdll!NtQueryInformationProcess() can retrieve a different kind of information from a process. It accepts a ProcessInformationClass parameter which specifies the information you want to get and defines the output type of the ProcessInformation parameter.

ProcessDebugFlags

A kernel structure called EPROCESS, which represents a process object, contains the field NoDebugInherit. The inverse value of this field can be retrieved using an undocumented class ProcessDebugFlags (0x1f). Therefore, if the return value is 0, a debugger is present.

#include <windows.h>

BOOL ProcessDebugFlags_test() ()
{
    typedef NTSTATUS(NTAPI* TNtQueryInformationProcess)(
        IN HANDLE           ProcessHandle,
        IN PROCESSINFOCLASS ProcessInformationClass,
        OUT PVOID           ProcessInformation,
        IN ULONG            ProcessInformationLength,
        OUT PULONG          ReturnLength
    );

    unsigned char func[] = { 'N','t','Q','u','e','r','y','I','n','f','o','r','m','a','t','i','o','n','P','r','o','c','e','s','s','�' };
    auto pfnNtQueryInformationProcess = (TNtQueryInformationProcess)GetProcAddress(ntdll, (PCSTR)func);

    if (pfnNtQueryInformationProcess)
    {
        DWORD dwProcessDebugFlags;
        ULONG dwReturned;
        //const DWORD ProcessDebugFlags = 0x1f;
        NTSTATUS status = pfnNtQueryInformationProcess(
            hProcess,
            ProcessDebugFlags,
            &dwProcessDebugFlags,
            sizeof(DWORD),
            &dwReturned);

        if (NT_SUCCESS(status) && (0 == dwProcessDebugFlags))
            return TRUE;
    }
    return FALSE;
}

ProcessDebugObjectHandle

When debugging begins, a kernel object called “debug object” is created. It is possible to query for the value of this handle by using the undocumented ProcessDebugObjectHandle (0x1e) class.

#include <windows.h>

BOOL ProcessDebugObjectHandle_test()
{
    typedef NTSTATUS(NTAPI* TNtQueryInformationProcess)(
        IN HANDLE           ProcessHandle,
        IN PROCESSINFOCLASS ProcessInformationClass,
        OUT PVOID           ProcessInformation,
        IN ULONG            ProcessInformationLength,
        OUT PULONG          ReturnLength
    );

    unsigned char func[] = { 'N','t','Q','u','e','r','y','I','n','f','o','r','m','a','t','i','o','n','P','r','o','c','e','s','s','�' };
    auto pfnNtQueryInformationProcess = (TNtQueryInformationProcess)GetProcAddress(ntdll, (PCSTR)func);

    if (pfnNtQueryInformationProcess)
    {
        ULONG dwReturned;
        HANDLE hProcessDebugObject = 0;
        //const DWORD ProcessDebugObjectHandle = 0x1e;
        NTSTATUS status = pfnNtQueryInformationProcess(
            hProcess,
            ProcessDebugObjectHandle,
            &hProcessDebugObject,
            sizeof(HANDLE),
            &dwReturned);

        if (NT_SUCCESS(status) && (0 != hProcessDebugObject))
            return TRUE;
    }
    return FALSE;
}

原文始发于微信公众号(红蓝对抗技战术):简单的反调试技术实现

免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2025年1月26日23:20:47
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   简单的反调试技术实现https://cn-sec.com/archives/3678940.html
                  免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉.

发表评论

匿名网友 填写信息