44using System . Threading ;
55using System . Diagnostics ;
66using System . Runtime . InteropServices ;
7+ using System . Windows . Forms ;
8+ using System . Net ;
9+ using System . Security . Cryptography ;
710
811namespace AntiCrack_DotNet
912{
@@ -62,8 +65,8 @@ internal sealed class AntiDebug
6265 [ DllImport ( "kernelbase.dll" , SetLastError = true ) ]
6366 private static extern int QueryFullProcessImageNameA ( SafeHandle hProcess , uint Flags , byte [ ] lpExeName , Int32 [ ] lpdwSize ) ;
6467
65- [ DllImport ( "user32 .dll" , SetLastError = true ) ]
66- private static extern IntPtr GetForegroundWindow ( ) ;
68+ [ DllImport ( "win32u .dll" , SetLastError = true ) ]
69+ private static extern IntPtr NtUserGetForegroundWindow ( ) ;
6770
6871 [ DllImport ( "user32.dll" , SetLastError = true ) ]
6972 private static extern int GetWindowTextLengthA ( IntPtr HWND ) ;
@@ -93,13 +96,19 @@ internal sealed class AntiDebug
9396
9497 /// <summary>
9598 /// Attempts to close an invalid handle to detect debugger presence.
99+ /// <param name="Syscall">specifies if we should use syscall to call the WinAPI functions.</param>
96100 /// </summary>
97101 /// <returns>Returns true if an exception is caught, indicating no debugger, otherwise false.</returns>
98- public static bool NtCloseAntiDebug_InvalidHandle ( )
102+ public static bool NtCloseAntiDebug_InvalidHandle ( bool Syscall )
99103 {
100104 try
101105 {
102- NtClose ( ( IntPtr ) 0x1231222L ) ;
106+ int RandomInt = new Random ( ) . Next ( int . MinValue , int . MaxValue ) ;
107+ IntPtr RandomIntPtr = new IntPtr ( RandomInt ) ;
108+ if ( Syscall )
109+ Syscalls . SyscallNtClose ( RandomIntPtr ) ;
110+ else
111+ NtClose ( RandomIntPtr ) ;
103112 return false ;
104113 }
105114 catch
@@ -110,17 +119,22 @@ public static bool NtCloseAntiDebug_InvalidHandle()
110119
111120 /// <summary>
112121 /// Attempts to close a protected handle to detect debugger presence.
122+ /// <param name="Syscall">specifies if we should use syscall to call the WinAPI functions.</param>
113123 /// </summary>
114124 /// <returns>Returns true if an exception is caught, indicating no debugger, otherwise false.</returns>
115- public static bool NtCloseAntiDebug_ProtectedHandle ( )
125+ public static bool NtCloseAntiDebug_ProtectedHandle ( bool Syscall )
116126 {
117- IntPtr hMutex = CreateMutexA ( IntPtr . Zero , false , new Random ( ) . Next ( 0 , 9999999 ) . ToString ( ) ) ;
127+ string RandomMutexName = new Random ( ) . Next ( int . MinValue , int . MaxValue ) . ToString ( ) ;
128+ IntPtr hMutex = CreateMutexA ( IntPtr . Zero , false , RandomMutexName ) ;
118129 uint HANDLE_FLAG_PROTECT_FROM_CLOSE = 0x00000002 ;
119130 SetHandleInformation ( hMutex , HANDLE_FLAG_PROTECT_FROM_CLOSE , HANDLE_FLAG_PROTECT_FROM_CLOSE ) ;
120131 bool Result = false ;
121132 try
122133 {
123- NtClose ( hMutex ) ;
134+ if ( Syscall )
135+ Syscalls . SyscallNtClose ( hMutex ) ;
136+ else
137+ NtClose ( hMutex ) ;
124138 Result = false ;
125139 }
126140 catch
@@ -153,45 +167,62 @@ public static bool IsDebuggerPresentCheck()
153167 }
154168
155169 /// <summary>
156- /// Checks if the process has debug flags set using NtQueryInformationProcess.
170+ /// Checks if the process has debug flags set using NtQueryInformationProcess
171+ /// <param name="Syscall">specifies if we should use syscall to call the WinAPI functions.</param>
157172 /// </summary>
158173 /// <returns>Returns true if debug flags are set, otherwise false.</returns>
159- public static bool NtQueryInformationProcessCheck_ProcessDebugFlags ( )
174+ public static bool NtQueryInformationProcessCheck_ProcessDebugFlags ( bool Syscall )
160175 {
161176 uint ProcessDebugFlags = 0 ;
162- NtQueryInformationProcess ( Process . GetCurrentProcess ( ) . SafeHandle , 0x1F , out ProcessDebugFlags , sizeof ( uint ) , 0 ) ;
177+ uint Class = 0x1F ;
178+ uint Size = sizeof ( uint ) ;
179+ uint Result = 0 ;
180+ if ( Syscall )
181+ Syscalls . SyscallNtQueryInformationProcess ( Process . GetCurrentProcess ( ) . SafeHandle , Class , out ProcessDebugFlags , Size , out Result ) ;
182+ else
183+ NtQueryInformationProcess ( Process . GetCurrentProcess ( ) . SafeHandle , 0x1F , out ProcessDebugFlags , sizeof ( uint ) , 0 ) ;
163184 if ( ProcessDebugFlags == 0 )
164185 return true ;
165186 return false ;
166187 }
167188
168189 /// <summary>
169190 /// Checks if the process has a debug port using NtQueryInformationProcess.
191+ /// <param name="Syscall">specifies if we should use syscalls to call the WinAPI functions.</param>.
170192 /// </summary>
171193 /// <returns>Returns true if a debug port is detected, otherwise false.</returns>
172- public static bool NtQueryInformationProcessCheck_ProcessDebugPort ( )
194+ public static bool NtQueryInformationProcessCheck_ProcessDebugPort ( bool Syscall )
173195 {
174196 uint DebuggerPresent = 0 ;
175197 uint Size = sizeof ( uint ) ;
176198 if ( Environment . Is64BitProcess )
177199 Size = sizeof ( uint ) * 2 ;
178- NtQueryInformationProcess ( Process . GetCurrentProcess ( ) . SafeHandle , 7 , out DebuggerPresent , Size , 0 ) ;
200+ uint Result = 0 ;
201+ if ( Syscall )
202+ Syscalls . SyscallNtQueryInformationProcess ( Process . GetCurrentProcess ( ) . SafeHandle , 7 , out DebuggerPresent , Size , out Result ) ;
203+ else
204+ NtQueryInformationProcess ( Process . GetCurrentProcess ( ) . SafeHandle , 7 , out DebuggerPresent , Size , 0 ) ;
179205 if ( DebuggerPresent != 0 )
180206 return true ;
181207 return false ;
182208 }
183209
184210 /// <summary>
185211 /// Checks if the process has a debug object handle using NtQueryInformationProcess.
212+ /// <param name="Syscall">specifies if we should use syscall to call the WinAPI functions.</param>
186213 /// </summary>
187214 /// <returns>Returns true if a debug object handle is detected, otherwise false.</returns>
188- public static bool NtQueryInformationProcessCheck_ProcessDebugObjectHandle ( )
215+ public static bool NtQueryInformationProcessCheck_ProcessDebugObjectHandle ( bool Syscall )
189216 {
190217 IntPtr hDebugObject = IntPtr . Zero ;
191218 uint Size = sizeof ( uint ) ;
192219 if ( Environment . Is64BitProcess )
193220 Size = sizeof ( uint ) * 2 ;
194- NtQueryInformationProcess ( Process . GetCurrentProcess ( ) . SafeHandle , 0x1E , out hDebugObject , Size , 0 ) ;
221+
222+ if ( Syscall )
223+ Syscalls . SyscallNtQueryInformationProcess ( Process . GetCurrentProcess ( ) . SafeHandle , 0x1E , out hDebugObject , Size , 0 ) ;
224+ else
225+ NtQueryInformationProcess ( Process . GetCurrentProcess ( ) . SafeHandle , 0x1E , out hDebugObject , Size , 0 ) ;
195226 if ( hDebugObject != IntPtr . Zero )
196227 return true ;
197228 return false ;
@@ -221,18 +252,31 @@ public static string AntiDebugAttach()
221252 /// <returns>Returns true if a known debugger window is detected, otherwise false.</returns>
222253 public static bool FindWindowAntiDebug ( )
223254 {
255+ string [ ] BadWindowNames = { "x32dbg" , "x64dbg" , "windbg" , "ollydbg" , "dnspy" , "immunity debugger" , "hyperdbg" , "cheat engine" , "cheatengine" , "ida" } ;
224256 Process [ ] GetProcesses = Process . GetProcesses ( ) ;
225257 foreach ( Process GetWindow in GetProcesses )
226258 {
227- string [ ] BadWindowNames = { "x32dbg" , "x64dbg" , "windbg" , "ollydbg" , "dnspy" , "immunity debugger" , "hyperdbg" , "cheat engine" , "cheatengine" , "ida" } ;
228- foreach ( string BadWindows in BadWindowNames )
259+ try
229260 {
230- if ( GetWindow . MainWindowTitle . ToLower ( ) . Contains ( BadWindows ) )
261+ if ( GetWindow . MainWindowHandle != IntPtr . Zero )
231262 {
232- GetWindow . Close ( ) ;
233- return true ;
263+ string title = GetWindow . MainWindowTitle ;
264+ if ( string . IsNullOrEmpty ( title ) ) continue ;
265+
266+ foreach ( string BadWindows in BadWindowNames )
267+ {
268+ if ( title . IndexOf ( BadWindows , StringComparison . OrdinalIgnoreCase ) >= 0 )
269+ {
270+ GetWindow . Close ( ) ;
271+ return true ;
272+ }
273+ }
234274 }
235275 }
276+ catch
277+ {
278+ continue ;
279+ }
236280 }
237281 return false ;
238282 }
@@ -241,10 +285,10 @@ public static bool FindWindowAntiDebug()
241285 /// Checks if the foreground window belongs to a known debugger.
242286 /// </summary>
243287 /// <returns>Returns true if a known debugger window is detected, otherwise false.</returns>
244- public static bool GetForegroundWindowAntiDebug ( )
288+ public static bool NtUserGetForegroundWindowAntiDebug ( )
245289 {
246290 string [ ] BadWindowNames = { "x32dbg" , "x64dbg" , "windbg" , "ollydbg" , "dnspy" , "immunity debugger" , "hyperdbg" , "debug" , "debugger" , "cheat engine" , "cheatengine" , "ida" } ;
247- IntPtr HWND = GetForegroundWindow ( ) ;
291+ IntPtr HWND = NtUserGetForegroundWindow ( ) ;
248292 if ( HWND != IntPtr . Zero )
249293 {
250294 int WindowLength = GetWindowTextLengthA ( HWND ) ;
@@ -254,7 +298,7 @@ public static bool GetForegroundWindowAntiDebug()
254298 GetWindowTextA ( HWND , WindowName , WindowLength + 1 ) ;
255299 foreach ( string BadWindows in BadWindowNames )
256300 {
257- if ( WindowName . ToString ( ) . ToLower ( ) . Contains ( BadWindows ) )
301+ if ( Utils . Contains ( WindowName . ToString ( ) . ToLower ( ) , BadWindows ) )
258302 {
259303 return true ;
260304 }
@@ -353,16 +397,21 @@ public static bool HardwareRegistersBreakpointsDetection()
353397 {
354398 Structs . CONTEXT Context = new Structs . CONTEXT ( ) ;
355399 Context . ContextFlags = CONTEXT_DEBUG_REGISTERS ;
356- IntPtr CurrentThread = GetCurrentThread ( ) ;
357- if ( GetThreadContext ( CurrentThread , ref Context ) )
400+ foreach ( ProcessThread Threads in Process . GetCurrentProcess ( ) . Threads )
358401 {
359- if ( ( Context . Dr1 != 0x00 || Context . Dr2 != 0x00 || Context . Dr3 != 0x00 || Context . Dr4 != 0x00 || Context . Dr5 != 0x00 || Context . Dr6 != 0x00 || Context . Dr7 != 0x00 ) )
402+ uint THREAD_GET_CONTEXT = 0x0008 ;
403+ uint THREAD_QUERY_INFORMATION = 0x0040 ;
404+ IntPtr hThread = OpenThread ( THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION , false , Threads . Id ) ;
405+ if ( GetThreadContext ( hThread , ref Context ) )
360406 {
361- NtClose ( CurrentThread ) ;
362- return true ;
407+ if ( ( Context . Dr1 != 0x00 || Context . Dr2 != 0x00 || Context . Dr3 != 0x00 || Context . Dr6 != 0x00 || Context . Dr7 != 0x00 ) )
408+ {
409+ NtClose ( hThread ) ;
410+ return true ;
411+ }
363412 }
413+ NtClose ( hThread ) ;
364414 }
365- NtClose ( CurrentThread ) ;
366415 return false ;
367416 }
368417
@@ -386,15 +435,17 @@ private static string CleanPath(string Path)
386435
387436 /// <summary>
388437 /// Checks if the parent process is a debugger by querying process information.
438+ /// <param name="Syscall">specifies if we should use syscall to call the WinAPI functions.</param>
389439 /// </summary>
390440 /// <returns>Returns true if the parent process is a debugger, otherwise false.</returns>
391- public static bool ParentProcessAntiDebug ( )
441+ public static bool ParentProcessAntiDebug ( bool Syscall )
392442 {
393443 try
394444 {
395445 Structs . PROCESS_BASIC_INFORMATION PBI = new Structs . PROCESS_BASIC_INFORMATION ( ) ;
396446 uint ProcessBasicInformation = 0 ;
397- if ( NtQueryInformationProcess ( Process . GetCurrentProcess ( ) . SafeHandle , ProcessBasicInformation , ref PBI , ( uint ) Marshal . SizeOf ( typeof ( Structs . PROCESS_BASIC_INFORMATION ) ) , 0 ) == 0 )
447+ uint Result = Syscall ? Syscalls . SyscallNtQueryInformationProcess ( Process . GetCurrentProcess ( ) . SafeHandle , ProcessBasicInformation , ref PBI , ( uint ) Marshal . SizeOf ( typeof ( Structs . PROCESS_BASIC_INFORMATION ) ) , 0 ) : NtQueryInformationProcess ( Process . GetCurrentProcess ( ) . SafeHandle , ProcessBasicInformation , ref PBI , ( uint ) Marshal . SizeOf ( typeof ( Structs . PROCESS_BASIC_INFORMATION ) ) , 0 ) ;
448+ if ( Result == 0 )
398449 {
399450 int ParentPID = PBI . InheritedFromUniqueProcessId . ToInt32 ( ) ;
400451 if ( ParentPID != 0 )
@@ -432,7 +483,8 @@ public static bool NtSetDebugFilterStateAntiDebug()
432483 return true ;
433484 }
434485
435- delegate int ExecutionDelegate ( ) ;
486+ [ UnmanagedFunctionPointer ( CallingConvention . StdCall ) ]
487+ private delegate int ExecutionDelegate ( ) ;
436488
437489 /// <summary>
438490 /// Uses page guard to detect debugger presence by executing a function pointer.
@@ -471,5 +523,4 @@ public static bool PageGuardAntiDebug()
471523 return false ;
472524 }
473525 }
474-
475- }
526+ }
0 commit comments