forked from yck1509/ConfuserEx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAntiDebugProtection.cs
138 lines (117 loc) · 4.04 KB
/
AntiDebugProtection.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using Confuser.Core;
using Confuser.Core.Helpers;
using Confuser.Core.Services;
using Confuser.Renamer;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
namespace Confuser.Protections {
[BeforeProtection("Ki.ControlFlow")]
internal class AntiDebugProtection : Protection {
public const string _Id = "anti debug";
public const string _FullId = "Ki.AntiDebug";
public override string Name {
get { return "Anti Debug Protection"; }
}
public override string Description {
get { return "This protection prevents the assembly from being debugged or profiled."; }
}
public override string Id {
get { return _Id; }
}
public override string FullId {
get { return _FullId; }
}
public override ProtectionPreset Preset {
get { return ProtectionPreset.Minimum; }
}
protected override void Initialize(ConfuserContext context) {
//
}
protected override void PopulatePipeline(ProtectionPipeline pipeline) {
pipeline.InsertPreStage(PipelineStage.ProcessModule, new AntiDebugPhase(this));
}
class AntiDebugPhase : ProtectionPhase {
public AntiDebugPhase(AntiDebugProtection parent)
: base(parent) { }
public override ProtectionTargets Targets {
get { return ProtectionTargets.Modules; }
}
public override string Name {
get { return "Anti-debug injection"; }
}
protected override void Execute(ConfuserContext context, ProtectionParameters parameters) {
var rt = context.Registry.GetService<IRuntimeService>();
var marker = context.Registry.GetService<IMarkerService>();
var name = context.Registry.GetService<INameService>();
foreach (ModuleDef module in parameters.Targets.OfType<ModuleDef>()) {
AntiMode mode = parameters.GetParameter(context, module, "mode", AntiMode.Safe);
TypeDef rtType;
TypeDef attr = null;
const string attrName = "System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptionsAttribute";
switch (mode) {
case AntiMode.Safe:
rtType = rt.GetRuntimeType("Confuser.Runtime.AntiDebugSafe");
break;
case AntiMode.Win32:
rtType = rt.GetRuntimeType("Confuser.Runtime.AntiDebugWin32");
break;
case AntiMode.Antinet:
rtType = rt.GetRuntimeType("Confuser.Runtime.AntiDebugAntinet");
attr = rt.GetRuntimeType(attrName);
module.Types.Add(attr = InjectHelper.Inject(attr, module));
foreach (IDnlibDef member in attr.FindDefinitions()) {
marker.Mark(member, (Protection)Parent);
name.Analyze(member);
}
name.SetCanRename(attr, false);
break;
default:
throw new UnreachableException();
}
IEnumerable<IDnlibDef> members = InjectHelper.Inject(rtType, module.GlobalType, module);
MethodDef cctor = module.GlobalType.FindStaticConstructor();
var init = (MethodDef)members.Single(method => method.Name == "Initialize");
cctor.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Call, init));
foreach (IDnlibDef member in members) {
marker.Mark(member, (Protection)Parent);
name.Analyze(member);
bool ren = true;
if (member is MethodDef) {
var method = (MethodDef)member;
if (method.Access == MethodAttributes.Public)
method.Access = MethodAttributes.Assembly;
if (!method.IsConstructor)
method.IsSpecialName = false;
else
ren = false;
CustomAttribute ca = method.CustomAttributes.Find(attrName);
if (ca != null)
ca.Constructor = attr.FindMethod(".ctor");
}
else if (member is FieldDef) {
var field = (FieldDef)member;
if (field.Access == FieldAttributes.Public)
field.Access = FieldAttributes.Assembly;
if (field.IsLiteral) {
field.DeclaringType.Fields.Remove(field);
continue;
}
}
if (ren) {
member.Name = name.ObfuscateName(member.Name, RenameMode.Unicode);
name.SetCanRename(member, false);
}
}
}
}
enum AntiMode {
Safe,
Win32,
Antinet
}
}
}
}