1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Linq ;
4
+ using System . Text ;
5
+ using System . Runtime . InteropServices ;
6
+
7
+ namespace AntiCrack_DotNet
8
+ {
9
+ public class HooksDetection
10
+ {
11
+ [ DllImport ( "ntdll.dll" , SetLastError = true , CharSet = CharSet . Unicode ) ]
12
+ private static extern void RtlInitUnicodeString ( out Structs . UNICODE_STRING DestinationString , string SourceString ) ;
13
+
14
+ [ DllImport ( "ntdll.dll" , SetLastError = true , CharSet = CharSet . Ansi ) ]
15
+ private static extern void RtlUnicodeStringToAnsiString ( out Structs . ANSI_STRING DestinationString , Structs . UNICODE_STRING UnicodeString , bool AllocateDestinationString ) ;
16
+
17
+ [ DllImport ( "ntdll.dll" , SetLastError = true ) ]
18
+ private static extern uint LdrGetDllHandle ( [ MarshalAs ( UnmanagedType . LPWStr ) ] string DllPath , [ MarshalAs ( UnmanagedType . LPWStr ) ] string DllCharacteristics , Structs . UNICODE_STRING LibraryName , ref IntPtr DllHandle ) ;
19
+
20
+ [ DllImport ( "ntdll.dll" , SetLastError = true , CharSet = CharSet . Ansi ) ]
21
+ private static extern uint LdrGetProcedureAddress ( IntPtr Module , Structs . ANSI_STRING ProcedureName , ushort ProcedureNumber , out IntPtr FunctionHandle ) ;
22
+
23
+ private static IntPtr LowLevelGetModuleHandle ( string Library )
24
+ {
25
+ IntPtr hModule = IntPtr . Zero ;
26
+ Structs . UNICODE_STRING UnicodeString = new Structs . UNICODE_STRING ( ) ;
27
+ RtlInitUnicodeString ( out UnicodeString , Library ) ;
28
+ LdrGetDllHandle ( null , null , UnicodeString , ref hModule ) ;
29
+ return hModule ;
30
+ }
31
+
32
+ private static IntPtr LowLevelGetProcAddress ( IntPtr hModule , string Function )
33
+ {
34
+ IntPtr FunctionHandle = IntPtr . Zero ;
35
+ Structs . UNICODE_STRING UnicodeString = new Structs . UNICODE_STRING ( ) ;
36
+ Structs . ANSI_STRING AnsiString = new Structs . ANSI_STRING ( ) ;
37
+ RtlInitUnicodeString ( out UnicodeString , Function ) ;
38
+ RtlUnicodeStringToAnsiString ( out AnsiString , UnicodeString , true ) ;
39
+ LdrGetProcedureAddress ( hModule , AnsiString , 0 , out FunctionHandle ) ;
40
+ return FunctionHandle ;
41
+ }
42
+
43
+ public static bool DetectBadInstructionsOnCommonAntiDebuggingFunctions ( )
44
+ {
45
+ string [ ] Libraries = { "kernel32.dll" , "kernelbase.dll" , "ntdll.dll" , "user32.dll" , "win32u.dll" } ;
46
+ string [ ] KernelLibAntiDebugFunctions = { "IsDebuggerPresent" , "CheckRemoteDebuggerPresent" , "GetThreadContext" , "CloseHandle" , "OutputDebugStringA" , "GetTickCount" , "SetHandleInformation" } ;
47
+ string [ ] NtdllAntiDebugFunctions = { "NtQueryInformationProcess" , "NtSetInformationThread" , "NtClose" , "NtGetContextThread" , "NtQuerySystemInformation" } ;
48
+ string [ ] User32AntiDebugFunctions = { "FindWindowW" , "FindWindowA" , "FindWindowExW" , "FindWindowExA" , "GetForegroundWindow" , "GetWindowTextLengthA" , "GetWindowTextA" , "BlockInput" } ;
49
+ string [ ] Win32uAntiDebugFunctions = { "NtUserBlockInput" , "NtUserFindWindowEx" , "NtUserQueryWindow" , "NtUserGetForegroundWindow" } ;
50
+ foreach ( string Library in Libraries )
51
+ {
52
+ IntPtr hModule = LowLevelGetModuleHandle ( Library ) ;
53
+ if ( hModule != IntPtr . Zero )
54
+ {
55
+ switch ( Library )
56
+ {
57
+ case "kernel32.dll" :
58
+ {
59
+ try
60
+ {
61
+ foreach ( string AntiDebugFunction in KernelLibAntiDebugFunctions )
62
+ {
63
+ IntPtr Function = LowLevelGetProcAddress ( hModule , AntiDebugFunction ) ;
64
+ byte [ ] FunctionBytes = new byte [ 1 ] ;
65
+ Marshal . Copy ( Function , FunctionBytes , 0 , 1 ) ;
66
+ if ( FunctionBytes [ 0 ] == 0x90 || FunctionBytes [ 0 ] == 0xE9 )
67
+ {
68
+ return true ;
69
+ }
70
+ }
71
+ }
72
+ catch
73
+ {
74
+ continue ;
75
+ }
76
+ }
77
+ break ;
78
+ case "kernelbase.dll" :
79
+ {
80
+ try
81
+ {
82
+ foreach ( string AntiDebugFunction in KernelLibAntiDebugFunctions )
83
+ {
84
+ IntPtr Function = LowLevelGetProcAddress ( hModule , AntiDebugFunction ) ;
85
+ byte [ ] FunctionBytes = new byte [ 1 ] ;
86
+ Marshal . Copy ( Function , FunctionBytes , 0 , 1 ) ;
87
+ if ( FunctionBytes [ 0 ] == 255 || FunctionBytes [ 0 ] == 0x90 || FunctionBytes [ 0 ] == 0xE9 )
88
+ {
89
+ return true ;
90
+ }
91
+ }
92
+ }
93
+ catch
94
+ {
95
+ continue ;
96
+ }
97
+ }
98
+ break ;
99
+ case "ntdll.dll" :
100
+ {
101
+ try
102
+ {
103
+ foreach ( string AntiDebugFunction in NtdllAntiDebugFunctions )
104
+ {
105
+ IntPtr Function = LowLevelGetProcAddress ( hModule , AntiDebugFunction ) ;
106
+ byte [ ] FunctionBytes = new byte [ 1 ] ;
107
+ Marshal . Copy ( Function , FunctionBytes , 0 , 1 ) ;
108
+ if ( FunctionBytes [ 0 ] == 255 || FunctionBytes [ 0 ] == 0x90 || FunctionBytes [ 0 ] == 0xE9 )
109
+ {
110
+ return true ;
111
+ }
112
+ }
113
+ }
114
+ catch
115
+ {
116
+ continue ;
117
+ }
118
+ }
119
+ break ;
120
+ case "user32.dll" :
121
+ {
122
+ try
123
+ {
124
+ foreach ( string AntiDebugFunction in User32AntiDebugFunctions )
125
+ {
126
+ IntPtr Function = LowLevelGetProcAddress ( hModule , AntiDebugFunction ) ;
127
+ byte [ ] FunctionBytes = new byte [ 1 ] ;
128
+ Marshal . Copy ( Function , FunctionBytes , 0 , 1 ) ;
129
+ if ( FunctionBytes [ 0 ] == 0x90 || FunctionBytes [ 0 ] == 0xE9 )
130
+ {
131
+ return true ;
132
+ }
133
+ }
134
+ }
135
+ catch
136
+ {
137
+ continue ;
138
+ }
139
+ }
140
+ break ;
141
+ case "win32u.dll" :
142
+ {
143
+ try
144
+ {
145
+ foreach ( string AntiDebugFunction in Win32uAntiDebugFunctions )
146
+ {
147
+ IntPtr Function = LowLevelGetProcAddress ( hModule , AntiDebugFunction ) ;
148
+ byte [ ] FunctionBytes = new byte [ 1 ] ;
149
+ Marshal . Copy ( Function , FunctionBytes , 0 , 1 ) ;
150
+ if ( FunctionBytes [ 0 ] == 255 || FunctionBytes [ 0 ] == 0x90 || FunctionBytes [ 0 ] == 0xE9 )
151
+ {
152
+ return true ;
153
+ }
154
+ }
155
+ }
156
+ catch
157
+ {
158
+ continue ;
159
+ }
160
+ }
161
+ break ;
162
+ }
163
+ }
164
+ }
165
+ return false ;
166
+ }
167
+ }
168
+ }
0 commit comments