AntiDebugging
AntiDebugging
AntiDebugging
Techniques.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <Windows.h>
#include <stdio.h>
#include <TlHelp32.h>
#include "structs.h"
/*
IsdebuggerPresent() API, returns TRUE if a debugger is being attached to the calling process
*/
BOOL IsDebuggerPresent1() {
// Get address PEB (GSx60 is PEB pointer)
PPEB pPeb = (PEB*)(__readgsqword(0x60));
if (pPeb->BeingDebugged == 1)
return TRUE;
return FALSE;
}
/*
NtQueryInformationProcess detects debugging via ProcessDebugPort & ProcessDebugObjectHandle
*/
BOOL NtQInfoProcess() {
NTSTATUS STATUS = NULL;
fnNtQueryInformationProcess pNtQueryInformationProcess = NULL;
DWORD64 dwIsDebuggerPresent = NULL;
DWORD64 hProcessDebugObject = NULL;
// Get Memory Address NtQueryInformationProcess from ntdll.dll
pNtQueryInformationProcess = (fnNtQueryInformationProcess)GetProcAddress(GetModuleHandle(TEXT("NTDLL.DLL")), "NtQueryInformationProcess");
// ProcessDebugPort Method
STATUS = pNtQueryInformationProcess(GetCurrentProcess(), ProcessDebugPort, &dwIsDebuggerPresent, sizeof(DWORD64), NULL);
if (STATUS != 0x0) {
printf("[!] NtQueryInformationProcess Failed: 0x%0.8X \n", STATUS);
return FALSE;
}
if (dwIsDebuggerPresent) {
printf("[+] Debugger Detected!\n");
return TRUE;
}
// ProcessDebugObjectHandle Method
STATUS = pNtQueryInformationProcess(GetCurrentProcess(), ProcessDebugObjectHandle, &hProcessDebugObject, sizeof(DWORD64), NULL);
if (STATUS != 0x0 && STATUS != 0xC0000353) {
printf("[!] NtQueryInformationProcess Failed: 0x%0.8X \n", STATUS);
return FALSE;
}
if (hProcessDebugObject)
return TRUE;
return FALSE;
}
/*
Hardware breakpoint detection, checks if the registers dr0-3 are 0
*/
BOOL HWBP_Check() {
CONTEXT Ctx = { .ContextFlags = CONTEXT_DEBUG_REGISTERS };
// Get current threadcontext
if (!GetThreadContext(GetCurrentThread(), &Ctx)) {
printf("[!] GetThreadContext Failed %d \n", GetLastError());
return FALSE;
}
// Check Hardware Breakpoint by checking registers aren't set to 0
if (Ctx.Dr0 || Ctx.Dr1 || Ctx.Dr2 || Ctx.Dr3) {
printf("[+] Debugger Detected ! \n");
return TRUE;
}
return FALSE;
}
/*
Detect Debuggers Via Name (Array)
*/
WCHAR* g_BlockSoftware[5] = {
L"x64dbg.exe",
L"x32dbg.exe",
L"binaryninja.exe",
L"VsDebugConsole.exe",
L"ida.exe"
};
BOOL BlockSoftware() {
HANDLE hSnapshot = NULL;
PROCESSENTRY32W ProcEntry = { .dwSize = sizeof(PROCESSENTRY32W) };
BOOL bSTATE = FALSE;
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (hSnapshot == INVALID_HANDLE_VALUE) {
printf("[!] CreateToolHelp32Snapshot Failed %d \n", GetLastError());
goto _End;
}
if (!Process32FirstW(hSnapshot, &ProcEntry)) {
printf("[!] Process32FirstW Failed %d \n", GetLastError());
goto _End;
}
do {
for (int i = 0; i < 5; i++) {
if (wcscmp(ProcEntry.szExeFile, g_BlockSoftware[i]) == 0) {
wprintf(L"\t[+] Found \"%ls\" Of Pid %d \n", ProcEntry.szExeFile, ProcEntry.th32ProcessID);
bSTATE = TRUE;
break;
}
}
if (bSTATE)
break;
} while (Process32NextW(hSnapshot, &ProcEntry));
_End:
if (!hSnapshot)
CloseHandle(hSnapshot);
return bSTATE;
}
/*
Detect debugging by evaluating time started and current time, if to long its being debugged GetTickCount64()
*/
BOOL TimeCheck1() {
DWORD dwTime1, dwTime2 = 0;
dwTime1 = GetTickCount64();
dwTime2 = GetTickCount64();
if ((dwTime2 - dwTime1) > 70)
return TRUE;
return FALSE;
}
/*
Send message to debugger is thats succeeds debugging is happening
*/
BOOL SendMessageDbg() {
// Make sure value is non 0 before execution
SetLastError(1);
OutputDebugStringW(L"N0xshell");
if (GetLastError())
return TRUE;
return FALSE;
}
int main() {
printf("[+] Press <Enter> To Start Anti Analysis Techniques! \n");
getchar();
// Method: IsDebuggerPresent
printf("[+] Running: IsDebuggerPresent1 \n");
if (IsDebuggerPresent1()) {
printf("[!] Debugger Detected [IsDebuggerPresent1] \n");
exit(1);
}
else
printf("\t[+] IsDebuggerPresent1 Done! \n");
// Method: NtQueryInformationProcess
printf("[+] Running: NtQInfoProcess \n");
if (NtQInfoProcess()) {
printf("[!] Debugger Detected [NtQInfoProcess] \n");
exit(1);
}
else
printf("\t[+] NtQInfoProcess Done! \n");
// Method: HWBP_Check (Thread Register check)
printf("[+] Running: HWBP_Check \n");
if (HWBP_Check()) {
printf("[!] Debugger Detected [HWBP_Check] \n");
exit(1);
}
else
printf("\t[+] HWBP_Check Done \n");
// Method: BlockSoftware Check
printf("[+] Running: BlockSoftware \n");
if (BlockSoftware()) {
printf("[!] Debugger Detected [BlockSoftware] \n");
exit(1);
}
else
printf("\t[+] BlockSoftware Done \n");
// Method: TimeCheck
printf("[+] Running: TimeCheck1 \n");
if (TimeCheck1()) {
printf("[!] Debugger Detected [TimeCheck1] \n");
exit(1);
}
else
printf("\t[+] TimeCheck1 Done \n");
printf("[+] Press <Enter> To Exit! \n");
getchar();
return 0;
}
This post is licensed under
CC BY 4.0
by the author.