1
1
using System ;
2
+ using System . Diagnostics ;
3
+
2
4
namespace dnSpyDetector
3
5
{
4
6
class Program
5
7
{
6
-
7
8
[ System . Runtime . InteropServices . DllImport ( "kernel32.dll" ) ]
8
9
public static extern IntPtr LoadLibrary ( string dllToLoad ) ;
9
10
10
11
[ System . Runtime . InteropServices . DllImport ( "kernel32.dll" ) ]
11
12
public static extern IntPtr GetProcAddress ( IntPtr hModule , string procedureName ) ;
12
13
13
- static void Main ( string [ ] args ) {
14
-
14
+ static void Main ( string [ ] args )
15
+ {
16
+ int hookCount = 0 ;
15
17
Console . WriteLine ( "Checking the presence of dnSpy hooks ..." ) ;
16
18
17
19
IntPtr kernel32 = LoadLibrary ( "kernel32.dll" ) ;
@@ -21,25 +23,41 @@ static void Main(string[] args) {
21
23
System . Runtime . InteropServices . Marshal . Copy ( GetProcessId , data , 0 , 1 ) ;
22
24
23
25
//32-bit relative jump = opcode 0xE9
24
- if ( data [ 0 ] == 0xE9 ) {
26
+ if ( data [ 0 ] == 0xE9 )
27
+ {
25
28
Console . WriteLine ( $ "IsDebuggerPresent hook detected ...") ;
26
- Console . ReadKey ( ) ;
27
- return ;
29
+ hookCount ++ ;
28
30
}
29
31
30
32
GetProcessId = GetProcAddress ( kernel32 , "CheckRemoteDebuggerPresent" ) ;
31
- data = new byte [ 1 ] ;
32
33
System . Runtime . InteropServices . Marshal . Copy ( GetProcessId , data , 0 , 1 ) ;
33
34
34
35
//32-bit relative jump = opcode 0xE9
35
- if ( data [ 0 ] == 0xE9 ) {
36
+ if ( data [ 0 ] == 0xE9 )
37
+ {
36
38
Console . WriteLine ( $ "CheckRemoteDebuggerPresent hook detected ...") ;
37
- Console . ReadKey ( ) ;
38
- return ;
39
+ hookCount ++ ;
39
40
}
40
41
42
+ var debuggerType = typeof ( Debugger ) ;
43
+ System . Reflection . MethodInfo [ ] methods = debuggerType . GetMethods ( ) ;
44
+ var getMethod = debuggerType . GetMethod ( "get_IsAttached" ) ;
45
+
46
+ IntPtr targetAddre = getMethod . MethodHandle . GetFunctionPointer ( ) ;
47
+ System . Runtime . InteropServices . Marshal . Copy ( targetAddre , data , 0 , 1 ) ;
41
48
42
- Console . ReadKey ( ) ;
49
+ if ( data [ 0 ] == 0x33 )
50
+ {
51
+ Console . WriteLine ( $ "System.Diagnostics.Debugger hook detected ...") ;
52
+ hookCount ++ ;
53
+ }
54
+
55
+ if ( hookCount == 0 )
56
+ {
57
+ Console . WriteLine ( "No dnSpy hooks found!" ) ;
58
+ }
59
+
60
+ Console . ReadLine ( ) ;
43
61
}
44
62
}
45
63
}
0 commit comments