反调试 - r3 使用 NtQuerySystemInformation 获取 KdKdDebuggerEnable 和 KdDebuggerNotPresent

原理

NtQuerySystemInformation 被 ntdll.dll 导出,当第一个参数传入 0x23 (SystemInterruptInformation) 时,会返回一个 SYSTEM_KERNEL_DEBUGGER_INFORMATION 结构,里面的成员KdKdDebuggerEnable 和 KdDebuggerNotPresent 标志系统是否启用内核调试。

代码示例

// Test_Console_1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <Windows.h>
#include <intrin.h>

using namespace std;

typedef struct _SYSTEM_KERNEL_DEBUGGER_INFORMATION{
    
    
	BOOLEAN KernelDebuggerEnabled;
	BOOLEAN KernelDebuggerNotPresent;
} SYSTEM_KERNEL_DEBUGGER_INFORMATION, * PSYSTEM_KERNEL_DEBUGGER_INFORMATION;
typedef NTSTATUS(WINAPI* pNtQuerySystemInformation)(IN UINT SystemInformationClass,OUT PVOID SystemInformation,IN ULONG SystemInformationLength,OUT PULONG ReturnLength);

int main()
{
    
    
   // 取 NtQuerySystemInformation 地址
	pNtQuerySystemInformation NtQuerySystemInformation = (pNtQuerySystemInformation)GetProcAddress(LoadLibrary(L"ntdll.dll"), "ZwQuerySystemInformation");
	if (NtQuerySystemInformation == NULL) {
    
    
		goto main_end;
	}

	// 获取系统信息
	SYSTEM_KERNEL_DEBUGGER_INFORMATION KdDebuggerInfo;		
	if (NtQuerySystemInformation(
		0x23,																							// 要检索的系统信息的类型: SystemInterruptInformation
		&KdDebuggerInfo,																	// 接受请求信息
		sizeof(SYSTEM_KERNEL_DEBUGGER_INFORMATION),		// 请求信息的字节大小
		NULL
	) != 0) {
    
    
		goto main_end;
	}

	// 判断调试器
	if (KdDebuggerInfo.KernelDebuggerEnabled || !KdDebuggerInfo.KernelDebuggerNotPresent) {
    
    
		cout << "发现调试器!" << endl;
	}
	else {
    
    
		cout << "没有调试器" << endl;
	}

main_end:
    getchar();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Simon798/article/details/107344650