5
5
using System . Runtime . InteropServices ;
6
6
using System . Diagnostics ;
7
7
using System . IO ;
8
+ using System . Threading ;
9
+ using static System . Net . WebRequestMethods ;
10
+ using System . Windows . Forms ;
11
+ using System . ServiceProcess ;
12
+ using System . Runtime . CompilerServices ;
8
13
9
14
namespace AntiCrack_DotNet
10
15
{
11
16
class AntiDebug
12
17
{
13
- [ DllImport ( "kernel32 .dll" , SetLastError = true ) ]
18
+ [ DllImport ( "kernelbase .dll" , SetLastError = true ) ]
14
19
private static extern bool SetHandleInformation ( IntPtr hObject , uint dwMask , uint dwFlags ) ;
15
20
16
21
[ DllImport ( "ntdll.dll" , SetLastError = true ) ]
17
22
private static extern bool NtClose ( IntPtr Handle ) ;
18
23
19
- [ DllImport ( "kernel32 .dll" , SetLastError = true ) ]
24
+ [ DllImport ( "kernelbase .dll" , SetLastError = true ) ]
20
25
private static extern IntPtr CreateMutexA ( IntPtr lpMutexAttributes , bool bInitialOwner , string lpName ) ;
21
26
22
- [ DllImport ( "kernel32 .dll" , SetLastError = true ) ]
27
+ [ DllImport ( "kernelbase .dll" , SetLastError = true ) ]
23
28
private static extern bool IsDebuggerPresent ( ) ;
24
29
25
- [ DllImport ( "kernel32.dll" , SetLastError = true ) ]
26
- private static extern bool CheckRemoteDebuggerPresent ( IntPtr Handle , ref bool CheckBool ) ;
27
-
28
- [ DllImport ( "kernel32.dll" , SetLastError = true ) ]
30
+ [ DllImport ( "kernelbase.dll" , SetLastError = true ) ]
29
31
private static extern IntPtr GetModuleHandle ( string lib ) ;
30
32
31
- [ DllImport ( "kernel32 .dll" , SetLastError = true ) ]
33
+ [ DllImport ( "kernelbase .dll" , SetLastError = true ) ]
32
34
private static extern IntPtr GetProcAddress ( IntPtr ModuleHandle , string Function ) ;
33
35
34
- [ DllImport ( "kernel32.dll" , SetLastError = true ) ]
35
- private static extern bool WriteProcessMemory ( SafeHandle ProcHandle , IntPtr BaseAddress , byte [ ] Buffer , uint size , int NumOfBytes ) ;
36
+ [ DllImport ( "kernelbase.dll" , SetLastError = true ) ]
37
+ private static extern bool WriteProcessMemory ( SafeHandle hProcess , IntPtr BaseAddress , byte [ ] Buffer , uint size , int NumOfBytes ) ;
38
+
39
+ [ DllImport ( "kernelbase.dll" , SetLastError = true ) ]
40
+ private static extern bool ReadProcessMemory ( SafeHandle hProcess , IntPtr BaseAddress , out byte [ ] Buffer , uint size , out int NumOfBytes ) ;
36
41
37
42
[ DllImport ( "ntdll.dll" , SetLastError = true ) ]
38
43
private static extern uint NtSetInformationThread ( IntPtr ThreadHandle , uint ThreadInformationClass , IntPtr ThreadInformation , int ThreadInformationLength ) ;
39
44
40
- [ DllImport ( "kernel32 .dll" , SetLastError = true ) ]
45
+ [ DllImport ( "kernelbase .dll" , SetLastError = true ) ]
41
46
private static extern IntPtr OpenThread ( uint DesiredAccess , bool InheritHandle , int ThreadId ) ;
42
47
43
- [ DllImport ( "kernel32 .dll" , SetLastError = true ) ]
48
+ [ DllImport ( "kernelbase .dll" , SetLastError = true ) ]
44
49
private static extern uint GetTickCount ( ) ;
45
50
46
- [ DllImport ( "kernel32.dll" , SetLastError = true ) ]
47
- private static extern void OutputDebugStringA ( string Text ) ;
48
-
49
- [ DllImport ( "kernel32.dll" , SetLastError = true ) ]
51
+ [ DllImport ( "kernelbase.dll" , SetLastError = true ) ]
50
52
private static extern IntPtr GetCurrentThread ( ) ;
51
53
52
- [ DllImport ( "kernel32 .dll" , SetLastError = true ) ]
54
+ [ DllImport ( "kernelbase .dll" , SetLastError = true ) ]
53
55
private static extern bool GetThreadContext ( IntPtr hThread , ref Structs . CONTEXT Context ) ;
54
56
55
57
[ DllImport ( "ntdll.dll" , SetLastError = true ) ]
@@ -61,7 +63,7 @@ class AntiDebug
61
63
[ DllImport ( "ntdll.dll" , SetLastError = true ) ]
62
64
private static extern uint NtQueryInformationProcess ( SafeHandle hProcess , uint ProcessInfoClass , ref Structs . PROCESS_BASIC_INFORMATION ProcessInfo , uint nSize , uint ReturnLength ) ;
63
65
64
- [ DllImport ( "kernel32 .dll" , SetLastError = true ) ]
66
+ [ DllImport ( "kernelbase .dll" , SetLastError = true ) ]
65
67
private static extern int QueryFullProcessImageNameA ( SafeHandle hProcess , uint Flags , byte [ ] lpExeName , Int32 [ ] lpdwSize ) ;
66
68
67
69
[ DllImport ( "user32.dll" , SetLastError = true ) ]
@@ -73,6 +75,24 @@ class AntiDebug
73
75
[ DllImport ( "user32.dll" , SetLastError = true ) ]
74
76
private static extern int GetWindowTextA ( IntPtr HWND , StringBuilder WindowText , int nMaxCount ) ;
75
77
78
+ [ DllImport ( "ntdll.dll" , SetLastError = true ) ]
79
+ private static extern uint NtSetDebugFilterState ( ulong ComponentId , uint Level , bool State ) ;
80
+
81
+ [ DllImport ( "kernelbase.dll" , SetLastError = true ) ]
82
+ private static extern void GetSystemInfo ( out Structs . SYSTEM_INFO lpSystemInfo ) ;
83
+
84
+ [ DllImport ( "kernelbase.dll" , SetLastError = true ) ]
85
+ private static extern IntPtr VirtualAlloc ( IntPtr lpAddress , uint dwSize , uint flAllocationType , uint flProtect ) ;
86
+
87
+ [ DllImport ( "ntdll.dll" , SetLastError = true ) ]
88
+ private static extern IntPtr memset ( IntPtr Dst , int val , uint size ) ;
89
+
90
+ [ DllImport ( "kernelbase.dll" , SetLastError = true ) ]
91
+ private static extern bool VirtualProtect ( IntPtr lpAddress , uint dwSize , uint flNewProtect , out uint lpflOldProtect ) ;
92
+
93
+ [ DllImport ( "kernelbase.dll" , SetLastError = true ) ]
94
+ private static extern bool VirtualFree ( IntPtr lpAddress , uint dwSize , uint dwFreeType ) ;
95
+
76
96
public static bool NtCloseAntiDebug_InvalidHandle ( )
77
97
{
78
98
try
@@ -91,15 +111,19 @@ public static bool NtCloseAntiDebug_ProtectedHandle()
91
111
IntPtr hMutex = CreateMutexA ( IntPtr . Zero , false , new Random ( ) . Next ( 0 , 9999999 ) . ToString ( ) ) ;
92
112
uint HANDLE_FLAG_PROTECT_FROM_CLOSE = 0x00000002 ;
93
113
SetHandleInformation ( hMutex , HANDLE_FLAG_PROTECT_FROM_CLOSE , HANDLE_FLAG_PROTECT_FROM_CLOSE ) ;
114
+ bool Result = false ;
94
115
try
95
116
{
96
117
NtClose ( hMutex ) ;
97
- return false ;
118
+ Result = false ;
98
119
}
99
120
catch
100
121
{
101
- return true ;
122
+ Result = true ;
102
123
}
124
+ SetHandleInformation ( hMutex , HANDLE_FLAG_PROTECT_FROM_CLOSE , 0 ) ;
125
+ NtClose ( hMutex ) ;
126
+ return Result ;
103
127
}
104
128
105
129
public static bool DebuggerIsAttached ( )
@@ -170,7 +194,10 @@ public static bool FindWindowAntiDebug()
170
194
foreach ( string BadWindows in BadWindowNames )
171
195
{
172
196
if ( GetWindow . MainWindowTitle . ToLower ( ) . Contains ( BadWindows ) )
197
+ {
198
+ GetWindow . Close ( ) ;
173
199
return true ;
200
+ }
174
201
}
175
202
}
176
203
return false ;
@@ -180,15 +207,20 @@ public static bool GetForegroundWindowAntiDebug()
180
207
{
181
208
string [ ] BadWindowNames = { "x32dbg" , "x64dbg" , "windbg" , "ollydbg" , "dnspy" , "immunity debugger" , "hyperdbg" , "debug" , "debugger" , "cheat engine" , "cheatengine" , "ida" } ;
182
209
IntPtr HWND = GetForegroundWindow ( ) ;
183
- int WindowLength = GetWindowTextLengthA ( HWND ) ;
184
- if ( WindowLength != 0 )
210
+ if ( HWND != IntPtr . Zero )
185
211
{
186
- StringBuilder WindowName = new StringBuilder ( WindowLength + 1 ) ;
187
- GetWindowTextA ( HWND , WindowName , WindowLength + 1 ) ;
188
- foreach ( string BadWindows in BadWindowNames )
212
+ int WindowLength = GetWindowTextLengthA ( HWND ) ;
213
+ if ( WindowLength != 0 )
189
214
{
190
- if ( WindowName . ToString ( ) . ToLower ( ) . Contains ( BadWindows ) )
191
- return true ;
215
+ StringBuilder WindowName = new StringBuilder ( WindowLength + 1 ) ;
216
+ GetWindowTextA ( HWND , WindowName , WindowLength + 1 ) ;
217
+ foreach ( string BadWindows in BadWindowNames )
218
+ {
219
+ if ( WindowName . ToString ( ) . ToLower ( ) . Contains ( BadWindows ) )
220
+ {
221
+ return true ;
222
+ }
223
+ }
192
224
}
193
225
}
194
226
return false ;
@@ -224,20 +256,20 @@ public static string HideThreadsAntiDebug()
224
256
public static bool GetTickCountAntiDebug ( )
225
257
{
226
258
uint Start = GetTickCount ( ) ;
259
+ Thread . Sleep ( 0x10 ) ;
227
260
return ( GetTickCount ( ) - Start ) > 0x10 ;
228
261
}
229
-
230
262
public static bool OutputDebugStringAntiDebug ( )
231
263
{
232
- OutputDebugStringA ( "just testing some stuff..." ) ;
264
+ Debugger . Log ( 0 , null , "just testing some stuff..." ) ;
233
265
if ( Marshal . GetLastWin32Error ( ) == 0 )
234
266
return true ;
235
267
return false ;
236
268
}
237
269
238
270
public static void OllyDbgFormatStringExploit ( )
239
271
{
240
- OutputDebugStringA ( " %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
+ 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") ;
241
273
}
242
274
243
275
public static bool DebugBreakAntiDebug ( )
@@ -259,16 +291,18 @@ public static bool HardwareRegistersBreakpointsDetection()
259
291
{
260
292
Structs . CONTEXT Context = new Structs . CONTEXT ( ) ;
261
293
Context . ContextFlags = CONTEXT_DEBUG_REGISTERS ;
262
- if ( GetThreadContext ( GetCurrentThread ( ) , ref Context ) )
294
+ IntPtr CurrentThread = GetCurrentThread ( ) ;
295
+ if ( GetThreadContext ( CurrentThread , ref Context ) )
263
296
{
264
297
if ( ( Context . Dr1 != 0x00 || Context . Dr2 != 0x00 || Context . Dr3 != 0x00 || Context . Dr4 != 0x00 || Context . Dr5 != 0x00 || Context . Dr6 != 0x00 || Context . Dr7 != 0x00 ) )
265
298
{
299
+ NtClose ( CurrentThread ) ;
266
300
return true ;
267
301
}
268
302
}
303
+ NtClose ( CurrentThread ) ;
269
304
return false ;
270
305
}
271
-
272
306
private static string CleanPath ( string Path )
273
307
{
274
308
string CleanedPath = null ;
@@ -314,5 +348,46 @@ public static bool ParentProcessAntiDebug()
314
348
catch { } ;
315
349
return false ;
316
350
}
351
+
352
+ public static bool NtSetDebugFilterStateAntiDebug ( )
353
+ {
354
+ if ( NtSetDebugFilterState ( 0 , 0 , true ) != 0 )
355
+ return false ;
356
+ return true ;
357
+ }
358
+
359
+ delegate int ExecutionDelegate ( ) ;
360
+ public static bool PageGuardAntiDebug ( )
361
+ {
362
+ Structs . SYSTEM_INFO SysInfo = new Structs . SYSTEM_INFO ( ) ;
363
+ GetSystemInfo ( out SysInfo ) ;
364
+ uint MEM_COMMIT = 0x00001000 ;
365
+ uint MEM_RESERVE = 0x00002000 ;
366
+ uint PAGE_EXECUTE_READWRITE = 0x40 ;
367
+ uint PAGE_GUARD = 0x100 ;
368
+ uint MEM_RELEASE = 0x00008000 ;
369
+ IntPtr AllocatedSpace = VirtualAlloc ( IntPtr . Zero , SysInfo . PageSize , MEM_COMMIT | MEM_RESERVE , PAGE_EXECUTE_READWRITE ) ;
370
+ if ( AllocatedSpace != IntPtr . Zero )
371
+ {
372
+ memset ( AllocatedSpace , 1 , 0xC3 ) ;
373
+ uint OldProtect = 0 ;
374
+ if ( VirtualProtect ( AllocatedSpace , SysInfo . PageSize , PAGE_EXECUTE_READWRITE | PAGE_GUARD , out OldProtect ) )
375
+ {
376
+ try
377
+ {
378
+ ExecutionDelegate IsDebugged = Marshal . GetDelegateForFunctionPointer < ExecutionDelegate > ( AllocatedSpace ) ;
379
+ int Result = IsDebugged ( ) ;
380
+ }
381
+ catch
382
+ {
383
+ VirtualFree ( AllocatedSpace , SysInfo . PageSize , MEM_RELEASE ) ;
384
+ return false ;
385
+ }
386
+ VirtualFree ( AllocatedSpace , SysInfo . PageSize , MEM_RELEASE ) ;
387
+ return true ;
388
+ }
389
+ }
390
+ return false ;
391
+ }
317
392
}
318
- }
393
+ }
0 commit comments