1
1
using System ;
2
- using System . Collections . Generic ;
3
- using System . Linq ;
4
- using System . Text ;
5
- using System . Runtime . InteropServices ;
6
- using System . Diagnostics ;
7
2
using System . IO ;
3
+ using System . Text ;
8
4
using System . Threading ;
9
- using System . Windows . Forms ;
10
- using System . ServiceProcess ;
11
- using System . Runtime . CompilerServices ;
5
+ using System . Diagnostics ;
6
+ using System . Runtime . InteropServices ;
12
7
13
8
namespace AntiCrack_DotNet
14
9
{
15
- class AntiDebug
10
+ internal sealed class AntiDebug
16
11
{
12
+ #region WinApi
13
+
17
14
[ DllImport ( "kernelbase.dll" , SetLastError = true ) ]
18
15
private static extern bool SetHandleInformation ( IntPtr hObject , uint dwMask , uint dwFlags ) ;
19
16
@@ -92,6 +89,12 @@ class AntiDebug
92
89
[ DllImport ( "kernelbase.dll" , SetLastError = true ) ]
93
90
private static extern bool VirtualFree ( IntPtr lpAddress , uint dwSize , uint dwFreeType ) ;
94
91
92
+ #endregion
93
+
94
+ /// <summary>
95
+ /// Attempts to close an invalid handle to detect debugger presence.
96
+ /// </summary>
97
+ /// <returns>Returns true if an exception is caught, indicating no debugger, otherwise false.</returns>
95
98
public static bool NtCloseAntiDebug_InvalidHandle ( )
96
99
{
97
100
try
@@ -105,6 +108,10 @@ public static bool NtCloseAntiDebug_InvalidHandle()
105
108
}
106
109
}
107
110
111
+ /// <summary>
112
+ /// Attempts to close a protected handle to detect debugger presence.
113
+ /// </summary>
114
+ /// <returns>Returns true if an exception is caught, indicating no debugger, otherwise false.</returns>
108
115
public static bool NtCloseAntiDebug_ProtectedHandle ( )
109
116
{
110
117
IntPtr hMutex = CreateMutexA ( IntPtr . Zero , false , new Random ( ) . Next ( 0 , 9999999 ) . ToString ( ) ) ;
@@ -125,18 +132,30 @@ public static bool NtCloseAntiDebug_ProtectedHandle()
125
132
return Result ;
126
133
}
127
134
135
+ /// <summary>
136
+ /// Checks if a debugger is attached to the process.
137
+ /// </summary>
138
+ /// <returns>Returns true if a debugger is attached, otherwise false.</returns>
128
139
public static bool DebuggerIsAttached ( )
129
140
{
130
141
return Debugger . IsAttached ;
131
142
}
132
143
144
+ /// <summary>
145
+ /// Checks if a debugger is present using the IsDebuggerPresent API.
146
+ /// </summary>
147
+ /// <returns>Returns true if a debugger is present, otherwise false.</returns>
133
148
public static bool IsDebuggerPresentCheck ( )
134
149
{
135
150
if ( IsDebuggerPresent ( ) )
136
151
return true ;
137
152
return false ;
138
153
}
139
154
155
+ /// <summary>
156
+ /// Checks if the process has debug flags set using NtQueryInformationProcess.
157
+ /// </summary>
158
+ /// <returns>Returns true if debug flags are set, otherwise false.</returns>
140
159
public static bool NtQueryInformationProcessCheck_ProcessDebugFlags ( )
141
160
{
142
161
uint ProcessDebugFlags = 0 ;
@@ -146,6 +165,10 @@ public static bool NtQueryInformationProcessCheck_ProcessDebugFlags()
146
165
return false ;
147
166
}
148
167
168
+ /// <summary>
169
+ /// Checks if the process has a debug port using NtQueryInformationProcess.
170
+ /// </summary>
171
+ /// <returns>Returns true if a debug port is detected, otherwise false.</returns>
149
172
public static bool NtQueryInformationProcessCheck_ProcessDebugPort ( )
150
173
{
151
174
uint DebuggerPresent = 0 ;
@@ -158,6 +181,10 @@ public static bool NtQueryInformationProcessCheck_ProcessDebugPort()
158
181
return false ;
159
182
}
160
183
184
+ /// <summary>
185
+ /// Checks if the process has a debug object handle using NtQueryInformationProcess.
186
+ /// </summary>
187
+ /// <returns>Returns true if a debug object handle is detected, otherwise false.</returns>
161
188
public static bool NtQueryInformationProcessCheck_ProcessDebugObjectHandle ( )
162
189
{
163
190
IntPtr hDebugObject = IntPtr . Zero ;
@@ -170,6 +197,10 @@ public static bool NtQueryInformationProcessCheck_ProcessDebugObjectHandle()
170
197
return false ;
171
198
}
172
199
200
+ /// <summary>
201
+ /// Patches the DbgUiRemoteBreakin and DbgBreakPoint functions to prevent debugger attachment.
202
+ /// </summary>
203
+ /// <returns>Returns "Success" if the patching was successful, otherwise "Failed".</returns>
173
204
public static string AntiDebugAttach ( )
174
205
{
175
206
IntPtr NtdllModule = GetModuleHandle ( "ntdll.dll" ) ;
@@ -184,6 +215,10 @@ public static string AntiDebugAttach()
184
215
return "Failed" ;
185
216
}
186
217
218
+ /// <summary>
219
+ /// Checks for the presence of known debugger windows.
220
+ /// </summary>
221
+ /// <returns>Returns true if a known debugger window is detected, otherwise false.</returns>
187
222
public static bool FindWindowAntiDebug ( )
188
223
{
189
224
Process [ ] GetProcesses = Process . GetProcesses ( ) ;
@@ -202,6 +237,10 @@ public static bool FindWindowAntiDebug()
202
237
return false ;
203
238
}
204
239
240
+ /// <summary>
241
+ /// Checks if the foreground window belongs to a known debugger.
242
+ /// </summary>
243
+ /// <returns>Returns true if a known debugger window is detected, otherwise false.</returns>
205
244
public static bool GetForegroundWindowAntiDebug ( )
206
245
{
207
246
string [ ] BadWindowNames = { "x32dbg" , "x64dbg" , "windbg" , "ollydbg" , "dnspy" , "immunity debugger" , "hyperdbg" , "debug" , "debugger" , "cheat engine" , "cheatengine" , "ida" } ;
@@ -225,6 +264,10 @@ public static bool GetForegroundWindowAntiDebug()
225
264
return false ;
226
265
}
227
266
267
+ /// <summary>
268
+ /// Hides threads from the debugger by setting the NtSetInformationThread.
269
+ /// </summary>
270
+ /// <returns>Returns "Success" if the threads were hidden successfully, otherwise "Failed".</returns>
228
271
public static string HideThreadsAntiDebug ( )
229
272
{
230
273
try
@@ -252,12 +295,21 @@ public static string HideThreadsAntiDebug()
252
295
}
253
296
}
254
297
298
+ /// <summary>
299
+ /// Uses GetTickCount to detect debugger presence.
300
+ /// </summary>
301
+ /// <returns>Returns true if debugger presence is detected, otherwise false.</returns>
255
302
public static bool GetTickCountAntiDebug ( )
256
303
{
257
304
uint Start = GetTickCount ( ) ;
258
305
Thread . Sleep ( 0x10 ) ;
259
306
return ( GetTickCount ( ) - Start ) > 0x10 ;
260
307
}
308
+
309
+ /// <summary>
310
+ /// Uses OutputDebugString to detect debugger presence.
311
+ /// </summary>
312
+ /// <returns>Returns true if debugger presence is detected, otherwise false.</returns>
261
313
public static bool OutputDebugStringAntiDebug ( )
262
314
{
263
315
Debugger . Log ( 0 , null , "just testing some stuff..." ) ;
@@ -266,11 +318,18 @@ public static bool OutputDebugStringAntiDebug()
266
318
return false ;
267
319
}
268
320
321
+ /// <summary>
322
+ /// Exploits a format string vulnerability in OllyDbg.
323
+ /// </summary>
269
324
public static void OllyDbgFormatStringExploit ( )
270
325
{
271
326
Debugger . Log ( 0 , null , "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s" ) ;
272
327
}
273
328
329
+ /// <summary>
330
+ /// Triggers a debug break to detect debugger presence.
331
+ /// </summary>
332
+ /// <returns>Returns true if an exception is caught, indicating no debugger, otherwise false.</returns>
274
333
public static bool DebugBreakAntiDebug ( )
275
334
{
276
335
try
@@ -286,6 +345,10 @@ public static bool DebugBreakAntiDebug()
286
345
287
346
private static long CONTEXT_DEBUG_REGISTERS = 0x00010000L | 0x00000010L ;
288
347
348
+ /// <summary>
349
+ /// Detects hardware breakpoints by checking debug registers.
350
+ /// </summary>
351
+ /// <returns>Returns true if hardware breakpoints are detected, otherwise false.</returns>
289
352
public static bool HardwareRegistersBreakpointsDetection ( )
290
353
{
291
354
Structs . CONTEXT Context = new Structs . CONTEXT ( ) ;
@@ -302,6 +365,12 @@ public static bool HardwareRegistersBreakpointsDetection()
302
365
NtClose ( CurrentThread ) ;
303
366
return false ;
304
367
}
368
+
369
+ /// <summary>
370
+ /// Cleans the specified path by removing null characters.
371
+ /// </summary>
372
+ /// <param name="Path">The path to clean.</param>
373
+ /// <returns>The cleaned path.</returns>
305
374
private static string CleanPath ( string Path )
306
375
{
307
376
string CleanedPath = null ;
@@ -315,6 +384,10 @@ private static string CleanPath(string Path)
315
384
return CleanedPath ;
316
385
}
317
386
387
+ /// <summary>
388
+ /// Checks if the parent process is a debugger by querying process information.
389
+ /// </summary>
390
+ /// <returns>Returns true if the parent process is a debugger, otherwise false.</returns>
318
391
public static bool ParentProcessAntiDebug ( )
319
392
{
320
393
try
@@ -348,6 +421,10 @@ public static bool ParentProcessAntiDebug()
348
421
return false ;
349
422
}
350
423
424
+ /// <summary>
425
+ /// Uses NtSetDebugFilterState to prevent debugging.
426
+ /// </summary>
427
+ /// <returns>Returns true if the filter state was set successfully, otherwise false.</returns>
351
428
public static bool NtSetDebugFilterStateAntiDebug ( )
352
429
{
353
430
if ( NtSetDebugFilterState ( 0 , 0 , true ) != 0 )
@@ -356,6 +433,11 @@ public static bool NtSetDebugFilterStateAntiDebug()
356
433
}
357
434
358
435
delegate int ExecutionDelegate ( ) ;
436
+
437
+ /// <summary>
438
+ /// Uses page guard to detect debugger presence by executing a function pointer.
439
+ /// </summary>
440
+ /// <returns>Returns true if debugger presence is detected, otherwise false.</returns>
359
441
public static bool PageGuardAntiDebug ( )
360
442
{
361
443
Structs . SYSTEM_INFO SysInfo = new Structs . SYSTEM_INFO ( ) ;
@@ -389,4 +471,5 @@ public static bool PageGuardAntiDebug()
389
471
return false ;
390
472
}
391
473
}
392
- }
474
+
475
+ }
0 commit comments