forked from mkaring/ConfuserEx
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAntiDebug.Win32.cs
More file actions
141 lines (120 loc) · 4.2 KB
/
Copy pathAntiDebug.Win32.cs
File metadata and controls
141 lines (120 loc) · 4.2 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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);
//Anti dnspy
Process here = GetParentProcess();
if (here != null && here.ProcessName.IndexOf("dnspy", StringComparison.OrdinalIgnoreCase) >= 0)
Environment.FailFast("");
// Blocking startup check: fail before any user code runs if a debugger is already
// attached when the module loads (managed or native), instead of only catching it
// later on the async worker thread where the app has already started.
if (Debugger.IsAttached || Debugger.IsLogging() || IsDebuggerPresent())
Environment.FailFast("");
var thread = new Thread(Worker);
thread.IsBackground = true;
thread.Start(null);
}
//https://stackoverflow.com/questions/394816/how-to-get-parent-process-in-net-in-managed-way
private static ParentProcessUtilities PPU;
public static Process GetParentProcess() {
return ParentProcessUtilities.GetParentProcess();
}
/// <summary>
/// A utility class to determine a process parent.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal struct ParentProcessUtilities {
// These members must match PROCESS_BASIC_INFORMATION
internal IntPtr Reserved1;
internal IntPtr PebBaseAddress;
internal IntPtr Reserved2_0;
internal IntPtr Reserved2_1;
internal IntPtr UniqueProcessId;
internal IntPtr InheritedFromUniqueProcessId;
[DllImport("ntdll.dll")]
private static extern int NtQueryInformationProcess(IntPtr processHandle, int processInformationClass, ref ParentProcessUtilities processInformation, uint processInformationLength, out int returnLength);
/// <summary>
/// Gets the parent process of the current process.
/// </summary>
/// <returns>An instance of the Process class.</returns>
internal static Process GetParentProcess() {
return GetParentProcess(Process.GetCurrentProcess().Handle);
}
/// <summary>
/// Gets the parent process of specified process.
/// </summary>
/// <param name="id">The process id.</param>
/// <returns>An instance of the Process class.</returns>
public static Process GetParentProcess(int id) {
Process process = Process.GetProcessById(id);
return GetParentProcess(process.Handle);
}
/// <summary>
/// Gets the parent process of a specified process.
/// </summary>
/// <param name="handle">The process handle.</param>
/// <returns>An instance of the Process class.</returns>
public static Process GetParentProcess(IntPtr handle) {
var pbi = new ParentProcessUtilities();
int status = NtQueryInformationProcess(handle, 0, ref pbi, (uint)Marshal.SizeOf(pbi), out _);
if (status != 0)
return null;
try {
return Process.GetProcessById(pbi.InheritedFromUniqueProcessId.ToInt32());
}
catch (ArgumentException) {
// not found
return 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);
}
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(200);
}
}
}
}