-
NtQueryInformationProcess: You can call NtQueryInformationProcess with the ProcessInformationClass parameter set to ProcessDebugPort or ProcessDebugObjectHandle to determine if the process is being debugged. If a debugger is attached, these calls will return specific information (like a non-zero debug port) indicating the presence of a debugger.
HANDLE hDebugObject = NULL;
NTSTATUS status = NtQueryInformationProcess(GetCurrentProcess(), ProcessDebugObjectHandle, &hDebugObject, sizeof(HANDLE), NULL);
if (NT_SUCCESS(status) && hDebugObject != NULL) {
// Debugger is present
}
-
NtSetInformationThread: Attempt to hide from the debugger by using the ThreadHideFromDebugger thread information class. If this call fails, it might suggest the presence of a debugger.
NTSTATUS status = NtSetInformationThread(GetCurrentThread(), ThreadHideFromDebugger, NULL, 0);
if (!NT_SUCCESS(status)) {
// Debugger might be present
}
-
NtQueryObject: Using the NtQueryObject function with the ObjectTypeInformation class, you can gather information about objects and potentially check for debugger-related objects.
-
NtClose: An attempt to close an invalid handle or a handle twice can sometimes trigger a debugger breakpoint if a debugger is present. This is because the debugger might intercept the invalid handle exception to analyze or log it.
NTSTATUS status = NtClose((HANDLE)0xDEADBEEF);
if (status == STATUS_INVALID_HANDLE) {
// Debugger might be present
}
-
NtYieldExecution: Debuggers may alter the normal context switching behavior of threads. Calling NtYieldExecution in a tight loop and measuring the time taken can sometime reveal if a debugger is affecting the scheduling.
-
NtQuerySystemInformation: This can be used with SystemKernelDebuggerInformation. If the kernel debugger is enabled, this call could reveal it.
SYSTEM_KERNEL_DEBUGGER_INFORMATION dbgInfo;
NTSTATUS status = NtQuerySystemInformation(SystemKernelDebuggerInformation, &dbgInfo, sizeof(dbgInfo), NULL);
if (NT_SUCCESS(status) && dbgInfo.DebuggerEnabled) {
// Kernel debugger is present
}
Please note that the use of undocumented Native API calls can trigger anti-virus software since they are sometimes used by malware to detect the presence of debuggers as well. Make sure that using these methods is done responsibly and with an understanding of the potential implications.
Finally, these methods are not foolproof, as a sophisticated debugger or a kernel debugger may not be detected by these checks. Additionally, debuggers can intercept and fake the results of these calls to avoid detection.