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
Expand file tree
/
Copy pathAnalyzePhase.cs
More file actions
235 lines (195 loc) · 7.96 KB
/
Copy pathAnalyzePhase.cs
File metadata and controls
235 lines (195 loc) · 7.96 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using System;
using System.Collections.Generic;
using Confuser.Core;
using Confuser.Renamer.Analyzers;
using dnlib.DotNet;
namespace Confuser.Renamer {
internal class AnalyzePhase : ProtectionPhase {
public AnalyzePhase(NameProtection parent)
: base(parent) { }
public override bool ProcessAll {
get { return true; }
}
public override ProtectionTargets Targets {
get { return ProtectionTargets.AllDefinitions; }
}
public override string Name {
get { return "Name analysis"; }
}
void ParseParameters(IDnlibDef def, ConfuserContext context, NameService service, ProtectionParameters parameters) {
var mode = parameters.GetParameter<RenameMode?>(context, def, "mode", null);
if (mode != null)
service.SetRenameMode(def, mode.Value);
}
protected override void Execute(ConfuserContext context, ProtectionParameters parameters) {
var service = (NameService)context.Registry.GetService<INameService>();
context.Logger.Debug("Building VTables & identifier list...");
foreach (IDnlibDef def in parameters.Targets.WithProgress(context.Logger)) {
ParseParameters(def, context, service, parameters);
if (def is ModuleDef) {
var module = (ModuleDef)def;
foreach (Resource res in module.Resources)
service.SetOriginalName(res, res.Name);
}
else
service.SetOriginalName(def, def.Name);
if (def is TypeDef) {
service.GetVTables().GetVTable((TypeDef)def);
service.SetOriginalNamespace(def, ((TypeDef)def).Namespace);
}
context.CheckCancellation();
}
context.Logger.Debug("Analyzing...");
RegisterRenamers(context, service);
IList<IRenamer> renamers = service.Renamers;
foreach (IDnlibDef def in parameters.Targets.WithProgress(context.Logger)) {
Analyze(service, context, parameters, def, true);
context.CheckCancellation();
}
}
void RegisterRenamers(ConfuserContext context, NameService service) {
bool wpf = false,
caliburn = false,
winforms = false,
json = false;
foreach (var module in context.Modules)
foreach (var asmRef in module.GetAssemblyRefs()) {
if (asmRef.Name == "WindowsBase" || asmRef.Name == "PresentationCore" ||
asmRef.Name == "PresentationFramework" || asmRef.Name == "System.Xaml") {
wpf = true;
}
else if (asmRef.Name == "Caliburn.Micro") {
caliburn = true;
}
else if (asmRef.Name == "System.Windows.Forms") {
winforms = true;
}
else if (asmRef.Name == "Newtonsoft.Json") {
json = true;
}
}
if (wpf) {
var wpfAnalyzer = new WPFAnalyzer();
context.Logger.Debug("WPF found, enabling compatibility.");
service.Renamers.Add(wpfAnalyzer);
if (caliburn) {
context.Logger.Debug("Caliburn.Micro found, enabling compatibility.");
service.Renamers.Add(new CaliburnAnalyzer(wpfAnalyzer));
}
}
if (winforms) {
var winformsAnalyzer = new WinFormsAnalyzer();
context.Logger.Debug("WinForms found, enabling compatibility.");
service.Renamers.Add(winformsAnalyzer);
}
if (json) {
var jsonAnalyzer = new JsonAnalyzer();
context.Logger.Debug("Newtonsoft.Json found, enabling compatibility.");
service.Renamers.Add(jsonAnalyzer);
}
}
internal void Analyze(NameService service, ConfuserContext context, ProtectionParameters parameters, IDnlibDef def, bool runAnalyzer) {
if (def is TypeDef)
Analyze(service, context, parameters, (TypeDef)def);
else if (def is MethodDef)
Analyze(service, context, parameters, (MethodDef)def);
else if (def is FieldDef)
Analyze(service, context, parameters, (FieldDef)def);
else if (def is PropertyDef)
Analyze(service, context, parameters, (PropertyDef)def);
else if (def is EventDef)
Analyze(service, context, parameters, (EventDef)def);
else if (def is ModuleDef) {
var pass = parameters.GetParameter<string>(context, def, "password", null);
if (pass != null)
service.reversibleRenamer = new ReversibleRenamer(pass);
service.SetCanRename(def, false);
}
if (!runAnalyzer || parameters.GetParameter(context, def, "forceRen", false))
return;
foreach (IRenamer renamer in service.Renamers)
renamer.Analyze(context, service, parameters, def);
}
static bool IsVisibleOutside(ConfuserContext context, ProtectionParameters parameters, IMemberDef def) {
var type = def as TypeDef;
if (type == null)
type = def.DeclaringType;
var renPublic = parameters.GetParameter<bool?>(context, def, "renPublic", null);
if (renPublic == null)
return type.IsVisibleOutside();
else
return type.IsVisibleOutside(false) && !renPublic.Value;
}
void Analyze(NameService service, ConfuserContext context, ProtectionParameters parameters, TypeDef type) {
if (IsVisibleOutside(context, parameters, type)) {
service.SetCanRename(type, false);
}
else if (type.IsRuntimeSpecialName || type.IsGlobalModuleType) {
service.SetCanRename(type, false);
}
else if (type.FullName == "ConfusedByAttribute") {
// Courtesy
service.SetCanRename(type, false);
}
if (parameters.GetParameter(context, type, "forceRen", false))
return;
if (type.InheritsFromCorlib("System.Attribute")) {
service.ReduceRenameMode(type, RenameMode.ASCII);
}
if (type.InheritsFrom("System.Configuration.SettingsBase")) {
service.SetCanRename(type, false);
}
}
void Analyze(NameService service, ConfuserContext context, ProtectionParameters parameters, MethodDef method) {
if (IsVisibleOutside(context, parameters, method.DeclaringType) &&
(method.IsFamily || method.IsFamilyOrAssembly || method.IsPublic) &&
IsVisibleOutside(context, parameters, method))
service.SetCanRename(method, false);
else if (method.IsRuntimeSpecialName)
service.SetCanRename(method, false);
else if (parameters.GetParameter(context, method, "forceRen", false))
return;
else if (method.DeclaringType.IsComImport() && !method.HasAttribute("System.Runtime.InteropServices.DispIdAttribute"))
service.SetCanRename(method, false);
else if (method.DeclaringType.IsDelegate())
service.SetCanRename(method, false);
}
void Analyze(NameService service, ConfuserContext context, ProtectionParameters parameters, FieldDef field) {
if (IsVisibleOutside(context, parameters, field.DeclaringType) &&
(field.IsFamily || field.IsFamilyOrAssembly || field.IsPublic) &&
IsVisibleOutside(context, parameters, field))
service.SetCanRename(field, false);
else if (field.IsRuntimeSpecialName)
service.SetCanRename(field, false);
else if (parameters.GetParameter(context, field, "forceRen", false))
return;
else if (field.DeclaringType.IsSerializable && !field.IsNotSerialized)
service.SetCanRename(field, false);
else if (field.IsLiteral && field.DeclaringType.IsEnum &&
!parameters.GetParameter(context, field, "renEnum", false))
service.SetCanRename(field, false);
}
void Analyze(NameService service, ConfuserContext context, ProtectionParameters parameters, PropertyDef property) {
if (IsVisibleOutside(context, parameters, property.DeclaringType) &&
property.IsPublic() &&
IsVisibleOutside(context, parameters, property))
service.SetCanRename(property, false);
else if (property.IsRuntimeSpecialName)
service.SetCanRename(property, false);
else if (parameters.GetParameter(context, property, "forceRen", false))
return;
else if (property.DeclaringType.Implements("System.ComponentModel.INotifyPropertyChanged"))
service.SetCanRename(property, false);
else if (property.DeclaringType.Name.String.Contains("AnonymousType"))
service.SetCanRename(property, false);
}
void Analyze(NameService service, ConfuserContext context, ProtectionParameters parameters, EventDef evt) {
if (IsVisibleOutside(context, parameters, evt.DeclaringType) &&
evt.IsPublic() &&
IsVisibleOutside(context, parameters, evt))
service.SetCanRename(evt, false);
else if (evt.IsRuntimeSpecialName)
service.SetCanRename(evt, false);
}
}
}