Repository navigation
Expand file tree
/
Copy pathAntiDebug.cpp
More file actions
539 lines (461 loc) · 21.4 KB
/
Copy pathAntiDebug.cpp
File metadata and controls
539 lines (461 loc) · 21.4 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#include "AntiDebug.h"
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <set>
#include "ProcessInfo.h"
#include "Util.h"
#include "TraceLog.h"
#include "Settings.h"
#include "PinLocker.h"
#include "TinyTracer.h"
#include "ModuleInfo.h"
#ifdef USE_WINDOWS_HDR
#include "win/win_paths.h"
#endif
#define ANTIDBG_LABEL "[ANTIDEBUG] --> "
using namespace LEVEL_PINCLIENT;
/* ================================================================== */
// Global variables used by AntiDebug
/* ================================================================== */
ADDRINT pebAddr = 0;
ADDRINT heapFlags = 0;
ADDRINT heapForceFlags = 0;
std::vector<std::string> loadedLib;
std::map<std::string, std::string> funcToLink;
typedef VOID AntiDBGCallBack(const ADDRINT Address, const CHAR* name, uint32_t argCount, VOID* arg1, VOID* arg2, VOID* arg3, VOID* arg4, VOID* arg5);
/* ==================================================================== */
// Leveraging the existing paramToStr, extracts only the string after '->'
/* ==================================================================== */
std::wstring paramToStrSplit(VOID* arg1)
{
if (arg1 == NULL) {
return L"0";
}
std::wstring inStr = paramToStr(arg1);
size_t arrowPos = inStr.find(L"->");
if (arrowPos != 0) {
// Extract the substring after the arrow symbol and contained in quotes
std::wstring secondPart = inStr.substr(arrowPos + 3);
size_t startPos = secondPart.find('"');
if (startPos != std::string::npos) {
size_t endPos = secondPart.rfind('"');
if (endPos != std::string::npos) {
secondPart = secondPart.substr(startPos + 1, endPos - startPos - 1);
}
}
return secondPart;
}
return L"0";
}
/* ==================================================================== */
// System information
/* ==================================================================== */
BOOL WinIsNativeOs32(void)
{
BOOL isNativeOs32 = FALSE;
#ifndef _WIN64
OS_HOST_CPU_ARCH_TYPE arch = OS_HOST_CPU_ARCH_TYPE_INVALID;
OS_RETURN_CODE code = OS_GetHostCPUArch(&arch);
if (code.generic_err != OS_RETURN_CODE_NO_ERROR) {
return TRUE; // assume 32 bit
}
if (arch == OS_HOST_CPU_ARCH_TYPE_IA32) {
isNativeOs32 = TRUE;
}
#endif
return isNativeOs32;
}
BOOL WinIsWindowsVistaOrGreater(void)
{
const USIZE buf_size = 300;
CHAR buf[buf_size] = { 0 };
OS_RETURN_CODE code = OS_GetKernelRelease(buf, buf_size);
if (code.generic_err != OS_RETURN_CODE_NO_ERROR) {
return TRUE; // assume greater than Vista
}
std::vector<std::string> args;
util::splitList(buf, '.', args);
if (args.size() >= 2) {
int dwMajorVersion = util::loadInt(args[0], false);
if (dwMajorVersion >= 6) {
return TRUE;
}
}
return FALSE;
}
/* ==================================================================== */
// Log info with antidebug label
/* ==================================================================== */
VOID LogAntiDbg(const WatchedType wType, const ADDRINT Address, const char* msg, const char *link=nullptr)
{
LogMsgAtAddress(wType, Address, ANTIDBG_LABEL, msg, link);
}
/* ==================================================================== */
// Callback function to be executed when memory is accessed
/* ==================================================================== */
VOID AntiDbg::WatchMemoryAccess(ADDRINT addr, UINT32 size, const ADDRINT insAddr)
{
PinLocker locker;
const WatchedType wType = isWatchedAddress(insAddr);
if (wType == WatchedType::NOT_WATCHED) return;
if (!pebAddr) return;
// Check the accessed memory address for antidebug tricks
if (addr == pebAddr + 2) {
return LogAntiDbg(wType, insAddr, "PEB!BeingDebugged accessed");
}
if (addr == 0x7ffe02d4) {
return LogAntiDbg(wType, insAddr, "KUSER_SHARED_DATA accessed",
"https://anti-debug.checkpoint.com/techniques/debug-flags.html#kuser_shared_data");
}
#ifdef _WIN64
if (addr == pebAddr + 0xBC) {
return LogAntiDbg(wType, insAddr, "PEB!NtGlobalFlag accessed");
}
if (addr == heapFlags || addr == heapForceFlags) {
return LogAntiDbg(wType, insAddr, "Heap Flags accessed",
"https://anti-debug.checkpoint.com/techniques/debug-flags.html#manual-checks-heap-flags");
}
#else
if (addr == pebAddr + 0x68) {
return LogAntiDbg(wType, insAddr, "PEB!NtGlobalFlag accessed");
}
if (addr == heapFlags || addr == heapForceFlags) {
return LogAntiDbg(wType, insAddr, "Heap Flags accessed",
"https://anti-debug.checkpoint.com/techniques/debug-flags.html#manual-checks-heap-flags");
}
#endif
}
std::set<THREADID> popfThreads;
#define CLEAR_TRAP
VOID AntiDbg::FlagsCheck(const CONTEXT* ctxt, THREADID tid)
{
PinLocker locker;
const ADDRINT Address = (ADDRINT)PIN_GetContextReg(ctxt, REG_INST_PTR);
const WatchedType wType = isWatchedAddress(Address);
if (wType == WatchedType::NOT_WATCHED) return;
ADDRINT pushedVal = UNKNOWN_ADDR;
const ADDRINT* stackPtr = reinterpret_cast<ADDRINT*>(PIN_GetContextReg(ctxt, REG_STACK_PTR));
size_t copiedSize = PIN_SafeCopy(&pushedVal, stackPtr, sizeof(pushedVal));
if (copiedSize != sizeof(pushedVal)) {
return;
}
const bool isTrap = (pushedVal & 0x100) ? true : false;
if (!isTrap) return;
LogAntiDbg(wType, Address, "Trap Flag set",
"https://anti-debug.checkpoint.com/techniques/assembly.html#popf_and_trap_flag");
#ifdef CLEAR_TRAP
pushedVal ^= 0x100;
::memcpy((void*)stackPtr, &pushedVal, sizeof(pushedVal));
popfThreads.insert(tid);
#endif
}
VOID AntiDbg::FlagsCheck_after(const CONTEXT* ctxt, THREADID tid, ADDRINT eip)
{
{
PinLocker locker;
if (popfThreads.find(tid) == popfThreads.end()) {
return; // trap flag wasn't set in this thread
}
popfThreads.erase(tid); // erase the stored TID
}
EXCEPTION_INFO exc;
PIN_InitWindowsExceptionInfo(&exc, 0x80000004L, eip); // NTSTATUS_STATUS_SINGLE_STEP
PIN_RaiseException(ctxt, tid, &exc);
}
VOID AntiDbg::InterruptCheck(const CONTEXT* ctxt)
{
PinLocker locker;
const ADDRINT Address = (ADDRINT)PIN_GetContextReg(ctxt, REG_INST_PTR);
const WatchedType wType = isWatchedAddress(Address);
if (wType == WatchedType::NOT_WATCHED) return;
int interruptID = 0;
if (!fetchInterruptID(Address, interruptID)) return;
if (interruptID == 3) {
LogAntiDbg(wType, Address, "INT3",
"https://anti-debug.checkpoint.com/techniques/assembly.html#int3");
}
if (interruptID == 0x2d) {
LogAntiDbg(wType, Address, "INT2D",
"https://anti-debug.checkpoint.com/techniques/assembly.html#int2d");
}
}
/* ==================================================================== */
// Process API calls (related to AntiDebug techniques)
/* ==================================================================== */
VOID AntiDbgLogFuncOccurrence(const ADDRINT Address, const CHAR* name, uint32_t argCount, VOID* arg1, VOID* arg2, VOID* arg3, VOID* arg4, VOID* arg5)
{
PinLocker locker;
const WatchedType wType = isWatchedAddress(Address);
if (wType == WatchedType::NOT_WATCHED) return;
std::stringstream ss;
ss << "^ " << name;
auto itr = funcToLink.find(name);
if (itr != funcToLink.end()) {
return LogAntiDbg(wType, Address, ss.str().c_str(),
itr->second.c_str());
}
return LogAntiDbg(wType, Address, ss.str().c_str());
}
VOID AntiDbg_LoadLibrary(const ADDRINT Address, const CHAR* name, uint32_t argCount, VOID* arg1, VOID* arg2, VOID* arg3, VOID* arg4, VOID* arg5)
{
if (!argCount) return;
PinLocker locker;
if (isWatchedAddress(Address) == WatchedType::NOT_WATCHED) return;
if (!arg1 || !PIN_CheckReadAccess(arg1)) return;
// Track LoadLibraryX to detect access to LOAD_DLL_DEBUG_INFO
// Get the library name from argument
std::wstring argStr = paramToStrSplit(arg1);
// Convert from wide string for comparison
std::string _argStr(argStr.begin(), argStr.end());
loadedLib.push_back(_argStr);
}
VOID AntiDbg_RaiseException(const ADDRINT Address, const CHAR* name, uint32_t argCount, VOID* arg1, VOID* arg2, VOID* arg3, VOID* arg4, VOID* arg5)
{
if (!argCount) return;
PinLocker locker;
const WatchedType wType = isWatchedAddress(Address);
if (wType == WatchedType::NOT_WATCHED) return;
// RaiseException constants
enum ExceptionCode { kDBG_CONTROL_C = 0x40010005, kDBG_RIPEVENT = 0x40010007 };
// kernel32!RaiseException() with DBG_CONTROL_C or DBG_RIPEVENT
if (int((size_t)arg1) == ExceptionCode::kDBG_CONTROL_C || int((size_t)arg1) == ExceptionCode::kDBG_RIPEVENT) {
return LogAntiDbg(wType, Address, "^ kernel32!RaiseException()",
"https://anti-debug.checkpoint.com/techniques/exceptions.html#raiseexception");
}
}
VOID AntiDbg_NtQuerySystemInformation(const ADDRINT Address, const CHAR* name, uint32_t argCount, VOID* arg1, VOID* arg2, VOID* arg3, VOID* arg4, VOID* arg5)
{
if (!argCount) return;
PinLocker locker;
const WatchedType wType = isWatchedAddress(Address);
if (wType == WatchedType::NOT_WATCHED) return;
enum SystemInformationClass { SystemKernelDebuggerInformation = 0x23 };
// function ntdll!NtQuerySystemInformation() with first parameter set to 0x23 (SystemKernelDebuggerInformation)
if (int((size_t)arg1) == SystemInformationClass::SystemKernelDebuggerInformation) {
return LogAntiDbg(wType, Address, "^ ntdll!NtQuerySystemInformation (SystemKernelDebuggerInformation)",
"https://anti-debug.checkpoint.com/techniques/debug-flags.html#using-win32-api-checks-ntquerysysteminformation");
}
}
VOID AntiDbg_NtQueryInformationProcess(const ADDRINT Address, const CHAR* name, uint32_t argCount, VOID* arg1, VOID* arg2, VOID* arg3, VOID* arg4, VOID* arg5)
{
if (argCount < 2) return;
PinLocker locker;
const WatchedType wType = isWatchedAddress(Address);
if (wType == WatchedType::NOT_WATCHED) return;
enum ProcessInformationClass { ProcessDebugPort = 0x7, ProcessDebugFlags = 0x1f, ProcessDebugObjectHandle = 0x1e };
// function ntdll!NtQueryInformationProcess with ProcessInformationClass == 7 (ProcessDebugPort)
if (int((size_t)arg2) == ProcessInformationClass::ProcessDebugPort) {
return LogAntiDbg(wType, Address, "^ ntdll!NtQueryInformationProcess (ProcessDebugPort)",
"https://anti-debug.checkpoint.com/techniques/debug-flags.html#using-win32-api-ntqueryinformationprocess-processdebugport");
}
// function ntdll!NtQueryInformationProcess with ProcessInformationClass == 0x1f (ProcessDebugFlags)
if (int((size_t)arg2) == ProcessInformationClass::ProcessDebugFlags) {
return LogAntiDbg(wType, Address, "^ ntdll!NtQueryInformationProcess (ProcessDebugFlags)",
"https://anti-debug.checkpoint.com/techniques/debug-flags.html#using-win32-api-ntqueryinformationprocess-processdebugflags");
}
// function ntdll!NtQueryInformationProcess with ProcessInformationClass == 0x1e (ProcessDebugObjectHandle)
if (int((size_t)arg2) == ProcessInformationClass::ProcessDebugObjectHandle) {
return LogAntiDbg(wType, Address, "^ ntdll!NtQueryInformationProcess (ProcessDebugObjectHandle)",
"https://anti-debug.checkpoint.com/techniques/debug-flags.html#using-win32-api-ntqueryinformationprocess-processdebugobjecthandle");
}
}
VOID AntiDbg_NtQueryObject(const ADDRINT Address, const CHAR* name, uint32_t argCount, VOID* arg1, VOID* arg2, VOID* arg3, VOID* arg4, VOID* arg5)
{
if (argCount < 2) return;
PinLocker locker;
const WatchedType wType = isWatchedAddress(Address);
if (wType == WatchedType::NOT_WATCHED) return;
enum ObjectInformationClass { ObjectAllTypesInformation = 3 };
// ntdll!NtQueryObject() to access DebugObject (with ObjectAllTypesInformation as 2nd argument)
if (int((size_t)arg2) == ObjectInformationClass::ObjectAllTypesInformation) {
return LogAntiDbg(wType, Address, "^ ntdll!NtQueryObject (ObjectAllTypesInformation)",
"https://anti-debug.checkpoint.com/techniques/object-handles.html#ntqueryobject");
}
}
VOID AntiDbg_CreateFile(const ADDRINT Address, const CHAR* name, uint32_t argCount, VOID* arg1, VOID* arg2, VOID* arg3, VOID* arg4, VOID* arg5)
{
if (argCount < 3) return;
PinLocker locker;
const WatchedType wType = isWatchedAddress(Address);
if (wType == WatchedType::NOT_WATCHED) return;
// kernel32!CreateFileX called on the module itself with Exclusive access, or on loaded libraries
// Check only exclusive accesses for optimization
if (int((size_t)arg3) == 0) {
IMG img = IMG_FindByAddress(Address);
if (!IMG_Valid(img)) return;
if (!arg1 || !PIN_CheckReadAccess(arg1)) return;
// Get the module name from image
std::string moduleName = IMG_Name(img);
// Get the module name from arguments
std::wstring argStr = paramToStrSplit(arg1);
// Convert from wide string for comparison
std::string _argStr(argStr.begin(), argStr.end());
// Check if open is done on module
if (util::isStrEqualI(_argStr, moduleName)) {
return LogAntiDbg(wType, Address, "^ kernel32!CreateFile on module",
"https://anti-debug.checkpoint.com/techniques/object-handles.html#createfile");
}
// Check if open is done on loaded libraries
for (size_t i = 0; i < loadedLib.size(); i++) {
if (util::isStrEqualI(_argStr, loadedLib[i])) {
return LogAntiDbg(wType, Address, "^ kernel32!CreateFile on loaded lib",
"https://anti-debug.checkpoint.com/techniques/object-handles.html#loadlibrary");
}
}
}
}
/* ==================================================================== */
// Collect some infos at Thread start, to be used later in checks
/* ==================================================================== */
BOOL getPEB(CONTEXT* ctxt, ADDRINT& pebAddr)
{
BOOL is_ok = FALSE;
#ifdef _WIN64
// Read the value from the memory address pointed by GS:[60h] and save it in the global variable
ADDRINT gsValue;
PIN_GetContextRegval(ctxt, REG_SEG_GS_BASE, reinterpret_cast<UINT8*>(&gsValue));
gsValue += 0x60;
// Save PEB Address
if (PIN_SafeCopy(&pebAddr, reinterpret_cast<VOID*>(gsValue), sizeof(pebAddr)))is_ok = TRUE;
#else
// Read the value from the memory address pointed by FS:[30h] and save it in the global variable
ADDRINT fsValue;
PIN_GetContextRegval(ctxt, REG_SEG_FS_BASE, reinterpret_cast<UINT8*>(&fsValue));
fsValue += 0x30;
// Save PEB Address
if (PIN_SafeCopy(&pebAddr, reinterpret_cast<VOID*>(fsValue), sizeof(pebAddr))) is_ok = TRUE;
#endif
return is_ok;
}
VOID AntiDbg::WatchThreadStart(THREADID threadid, CONTEXT* ctxt, INT32 flags, VOID* v)
{
PinLocker locker;
// Checks only the first thread
if (threadid != 0) {
return;
}
#ifdef _WIN64
// Read the value from the memory address pointed by GS:[60h] and save it in the global variable
if (!getPEB(ctxt, pebAddr)) return;
// Get Heap flags addresses (https://anti-debug.checkpoint.com/techniques/debug-flags.html#manual-checks-heap-flags)
ADDRINT heapBase;
PIN_SafeCopy(&heapBase, reinterpret_cast<VOID*>(pebAddr + 0x30), sizeof(heapBase));
ADDRINT heapFlagsOffset = WinIsWindowsVistaOrGreater()
? 0x70
: 0x14;
ADDRINT heapForceFlagsOffset = WinIsWindowsVistaOrGreater()
? 0x74
: 0x18;
heapFlags = heapBase + heapFlagsOffset;
heapForceFlags = heapBase + heapForceFlagsOffset;
#else
// Read the value from the memory address pointed by FS:[30h] and save it in the global variable
if (!getPEB(ctxt, pebAddr)) return;
// Get Heap flags addresses (https://anti-debug.checkpoint.com/techniques/debug-flags.html#manual-checks-heap-flags)
ADDRINT heapBase;
ADDRINT heapBaseOffset = WinIsNativeOs32()
? 0x18
: 0x1030;
PIN_SafeCopy(&heapBase, reinterpret_cast<VOID*>(pebAddr + heapBaseOffset), sizeof(heapBase));
ADDRINT heapFlagsOffset = WinIsWindowsVistaOrGreater()
? 0x40
: 0x0C;
ADDRINT heapForceFlagsOffset = WinIsWindowsVistaOrGreater()
? 0x44
: 0x10;
heapFlags = heapBase + heapFlagsOffset;
heapForceFlags = heapBase + heapForceFlagsOffset;
#endif
}
//* ==================================================================== */
// "CloseHandle" instrumentation, detects invalid handlers
/* ===================================================================== */
VOID AntiDbg_After_CloseHandle(ADDRINT Address, ADDRINT result)
{
PinLocker locker;
const WatchedType wType = isWatchedAddress(Address);
if (wType == WatchedType::NOT_WATCHED) return;
if (!result) {
// Invalid closure
return LogAntiDbg(wType, Address, "^ kernel32!CloseHandle (INVALID_HNDL_VAL)",
"https://anti-debug.checkpoint.com/techniques/object-handles.html#closehandle");
}
}
/* ==================================================================== */
// Add single function
/* ==================================================================== */
bool AntiDbgAddCallbackBefore(IMG Image, char* fName, uint32_t argNum, AntiDBGCallBack callback)
{
const size_t argMax = 5;
if (argNum > argMax) argNum = argMax;
RTN funcRtn = find_by_unmangled_name(Image, fName);
if (RTN_Valid(funcRtn)) {
RTN_Open(funcRtn);
RTN_InsertCall(funcRtn, IPOINT_BEFORE, AFUNPTR(callback),
IARG_RETURN_IP,
IARG_ADDRINT, fName,
IARG_UINT32, argNum,
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
IARG_FUNCARG_ENTRYPOINT_VALUE, 1,
IARG_FUNCARG_ENTRYPOINT_VALUE, 2,
IARG_FUNCARG_ENTRYPOINT_VALUE, 3,
IARG_FUNCARG_ENTRYPOINT_VALUE, 4,
IARG_END
);
RTN_Close(funcRtn);
return true;
}
return false;
}
/* ==================================================================== */
// Add to monitored functions all the API needed for AntiDebug.
// Called by ImageLoad
/* ==================================================================== */
VOID AntiDbg::MonitorAntiDbgFunctions(IMG Image)
{
funcToLink["IsDebuggerPresent"] = "https://anti-debug.checkpoint.com/techniques/debug-flags.html#using-win32-api-isdebuggerpresent";
funcToLink["CheckRemoteDebuggerPresent"] = "https://anti-debug.checkpoint.com/techniques/debug-flags.html#using-win32-api-checkremotedebuggerpresent";
funcToLink["RtlQueryProcessHeapInformation"] = "https://anti-debug.checkpoint.com/techniques/debug-flags.html#using-win32-api-checks-rtlqueryprocessheapinformation";
funcToLink["RtlQueryProcessDebugInformation"] = "https://anti-debug.checkpoint.com/techniques/debug-flags.html#using-win32-api-checks-rtlqueryprocessdebuginformation";
funcToLink["HeapWalk"] = "https://anti-debug.checkpoint.com/techniques/debug-flags.html#manual-checks-heap-protection";
funcToLink["CsrGetProcessId"] = "https://anti-debug.checkpoint.com/techniques/object-handles.html#openprocess";
funcToLink["SetUnhandledExceptionFilter"] = "https://anti-debug.checkpoint.com/techniques/exceptions.html#unhandledexceptionfilter";
funcToLink["RaiseException"] = "https://anti-debug.checkpoint.com/techniques/exceptions.html#raiseexception";
// API needed for Antidebug
const std::string dllName = util::getDllName(IMG_Name(Image));
if (util::iequals(dllName, "ntdll")) {
AntiDbgAddCallbackBefore(Image, "CsrGetProcessId", 0, AntiDbgLogFuncOccurrence);
AntiDbgAddCallbackBefore(Image, "RtlQueryProcessHeapInformation", 1, AntiDbgLogFuncOccurrence);
AntiDbgAddCallbackBefore(Image, "RtlQueryProcessDebugInformation", 3, AntiDbgLogFuncOccurrence);
AntiDbgAddCallbackBefore(Image, "NtQueryInformationProcess", 5, AntiDbg_NtQueryInformationProcess);
AntiDbgAddCallbackBefore(Image, "NtQuerySystemInformation", 4, AntiDbg_NtQuerySystemInformation);
////////////////////////////////////
// If AntiDebug level is Deep
////////////////////////////////////
if (m_Settings.antidebug >= ANTIDEBUG_DEEP) {
AntiDbgAddCallbackBefore(Image, "NtQueryObject", 5, AntiDbg_NtQueryObject);
}
}
if (util::iequals(dllName, "kernel32")) {
AntiDbgAddCallbackBefore(Image, "LoadLibraryW", 1, AntiDbg_LoadLibrary);
AntiDbgAddCallbackBefore(Image, "LoadLibraryA", 1, AntiDbg_LoadLibrary);
AntiDbgAddCallbackBefore(Image, "CreateFileW", 5, AntiDbg_CreateFile);
AntiDbgAddCallbackBefore(Image, "CreateFileA", 5, AntiDbg_CreateFile);
AntiDbgAddCallbackBefore(Image, "IsDebuggerPresent", 0, AntiDbgLogFuncOccurrence);
AntiDbgAddCallbackBefore(Image, "CheckRemoteDebuggerPresent", 2, AntiDbgLogFuncOccurrence);
AntiDbgAddCallbackBefore(Image, "HeapWalk", 2, AntiDbgLogFuncOccurrence);
AntiDbgAddCallbackBefore(Image, "SetUnhandledExceptionFilter", 1, AntiDbgLogFuncOccurrence);
AntiDbgAddCallbackBefore(Image, "RaiseException", 4, AntiDbg_RaiseException);
}
// CloseHandle return value hook
RTN funcRtn = find_by_unmangled_name(Image, "CloseHandle");
if (!RTN_Valid(funcRtn)) return; // failed
RTN_Open(funcRtn);
RTN_InsertCall(funcRtn, IPOINT_AFTER, AFUNPTR(AntiDbg_After_CloseHandle),
IARG_RETURN_IP,
IARG_FUNCRET_EXITPOINT_VALUE,
IARG_END);
RTN_Close(funcRtn);
}