This repository was archived by the owner on Jan 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathAntiDebug.Win32.cs
70 lines (58 loc) · 1.59 KB
/
AntiDebug.Win32.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
namespace Confuser.Runtime {
internal static class AntiDebugWin32 {
static void Initialize() {
string x = "COR";
if (Environment.GetEnvironmentVariable(x + "_PROFILER") != null ||
Environment.GetEnvironmentVariable(x + "_ENABLE_PROFILING") != null)
Environment.FailFast(null);
var thread = new Thread(Worker);
thread.IsBackground = true;
thread.Start(null);
}
[DllImport("kernel32.dll")]
static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll")]
static extern bool IsDebuggerPresent();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern int OutputDebugString(string str);
static void Worker(object thread) {
var th = thread as Thread;
if (th == null) {
th = new Thread(Worker);
th.IsBackground = true;
th.Start(Thread.CurrentThread);
Thread.Sleep(500);
}
while (true) {
// Managed
if (Debugger.IsAttached || Debugger.IsLogging())
Environment.FailFast("");
// IsDebuggerPresent
if (IsDebuggerPresent())
Environment.FailFast("");
// OpenProcess
Process ps = Process.GetCurrentProcess();
if (ps.Handle == IntPtr.Zero)
Environment.FailFast("");
ps.Close();
// OutputDebugString
if (OutputDebugString("") > IntPtr.Size)
Environment.FailFast("");
// CloseHandle
try {
CloseHandle(IntPtr.Zero);
}
catch {
Environment.FailFast("");
}
if (!th.IsAlive)
Environment.FailFast("");
Thread.Sleep(1000);
}
}
}
}