forked from martinkirsche/StrongNameResigner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
384 lines (348 loc) · 15 KB
/
Program.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Security.Cryptography;
using System.Text;
using Mono.Cecil;
using Mono.Options;
namespace StrongNameResigner
{
class Program
{
static int verbosity;
public static string ExecutableFileName
{
get
{
return System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".exe";
}
}
static BaseAssemblyResolver assemblyResolver = new DefaultAssemblyResolver();
public static void Main(string[] args)
{
bool showHelp = false;
bool listDependenciesOnly = false;
foreach (var s in assemblyResolver.GetSearchDirectories())
{
assemblyResolver.RemoveSearchDirectory(s);
}
var p = new OptionSet()
{
{ "a|assembly-search-directory=", "add a search directory into the assembly resolver", v => assemblyResolver.AddSearchDirectory(v) },
{ "d|list-dependencies-only", "output a list of detected dependencies and exit", v => listDependenciesOnly = v != null },
{ "k|key-pair-file=", "resign assembly with the key pair in file (.snk)", v => LoadKeyPairFile(v) },
{ "v", "increase verbosity", v => { if (v != null) ++verbosity; } },
{ "h|help", "show this message and exit", v => showHelp = v != null },
};
if (0 == args.Length)
{
TryHelp();
return;
}
List<string> assemblies;
try
{
assemblies = p.Parse(args);
}
catch (OptionException e)
{
Error(e.Message);
TryHelp();
return;
}
if (showHelp)
{
ShowHelp(p);
return;
}
if (0 == assemblies.Count)
{
Error("No assembly names were given.");
TryHelp();
return;
}
if (0 == assemblyResolver.GetSearchDirectories().Length)
{
assemblyResolver.AddSearchDirectory(".");
}
IEnumerable<AssemblyDefinition> dependendAssemblies;
try
{
dependendAssemblies = FindDependendAssemblies(assemblies).ToList();
}
catch (Exception exception)
{
Error("Unable find dependend assemblies: {0}", exception.ToString());
return;
}
if (listDependenciesOnly)
{
foreach (var a in dependendAssemblies)
{
Console.WriteLine(a.MainModule.FullyQualifiedName);
}
return;
}
foreach (var d in ProcessAssemblies(dependendAssemblies).ToList())
{
try
{
WriteAssembly(d);
}
catch (Exception exception)
{
Error("Unable to write assembly: {0}", exception.ToString());
continue;
}
}
}
static void TryHelp()
{
Console.WriteLine(string.Format("Try »{0} --help« for more information.", ExecutableFileName));
}
static StrongNameKeyPair strongNameKeyPair = null;
static byte[] publicKeyToken = new byte[0];
static void LoadKeyPairFile(string fileName)
{
strongNameKeyPair =
new StrongNameKeyPair(File.ReadAllBytes(fileName));
publicKeyToken = new SHA1Managed().ComputeHash(strongNameKeyPair.PublicKey)
.Reverse().Take(8).ToArray();
}
static void ShowHelp(OptionSet p)
{
Console.WriteLine("Usage: {0} [Option]... AssemblyName...", ExecutableFileName);
Console.WriteLine("Resign a list of assemblies and its dependencies.");
Console.WriteLine("If no key pair is specified, the strong name signature will be removed.");
Console.WriteLine();
Console.WriteLine("Options:");
p.WriteOptionDescriptions(Console.Out);
}
static void Error(string format, params object[] args)
{
Console.Error.WriteLine(format, args);
}
static void Debug(string format, params object[] args)
{
if (verbosity > 1)
{
Console.WriteLine(format, args);
}
}
static void Info(string format, params object[] args)
{
if (verbosity > 0)
{
Console.WriteLine(format, args);
}
}
static IEnumerable<AssemblyDefinition> ProcessAssemblies(IEnumerable<AssemblyDefinition> assemblies)
{
foreach (var d in assemblies)
{
try
{
Info("Processing assembly {0}", d.FullName);
FixAssemblyReferences(d, assemblies);
FixInternalsVisibleToAttribute(d, assemblies);
FixCustomAttributesReferences(d, assemblies);
FixResourceReferences(d, assemblies);
}
catch (Exception exception)
{
Error("Assembly will be skipped: {0}", exception.ToString());
continue;
}
yield return d;
}
}
static AssemblyDefinition LoadAssemblyDefinition(string fileName)
{
Debug("Loading {0}", fileName);
return AssemblyDefinition.ReadAssembly(fileName,
new ReaderParameters { AssemblyResolver = assemblyResolver });
}
static IEnumerable<AssemblyDefinition> FindDependendAssemblies(List<string> assemblyFullNames)
{
List<string> dependencies = new List<string>();
foreach (var fullName in assemblyFullNames)
{
Debug("Loading {0} for dependency searching.", fullName);
var assemblyDefinition = assemblyResolver.Resolve(fullName);
var fileName = assemblyDefinition.MainModule.FullyQualifiedName;
dependencies.Add(assemblyDefinition.FullName);
yield return assemblyDefinition;
}
bool retry = false;
do
{
retry = false;
foreach (var searchDirectory in assemblyResolver.GetSearchDirectories())
{
if (!Directory.Exists(searchDirectory)) { continue; }
foreach (var fileName in Directory.GetFiles(searchDirectory))
{
if ((!fileName.ToLower().EndsWith(".dll") &&
!fileName.ToLower().EndsWith(".exe"))) { continue; }
AssemblyDefinition assemblyDefinition = null;
try
{
assemblyDefinition = LoadAssemblyDefinition(fileName);
}
catch (BadImageFormatException exception)
{
Debug("Unable to load {0}: {1}", fileName, exception.Message);
continue;
}
catch (Exception exception)
{
Error("Unknown error while loading {0}: {1}", fileName, exception.ToString());
continue;
}
if (dependencies.Contains(assemblyDefinition.FullName)) { continue; }
foreach (var r in assemblyDefinition.MainModule.AssemblyReferences)
{
if (!dependencies.Contains(r.FullName)) { continue; }
retry = true;
dependencies.Add(assemblyDefinition.FullName);
yield return assemblyDefinition;
break;
}
}
}
} while (retry);
}
static void FixResourceReferences(AssemblyDefinition assemblyDefinition, IEnumerable<AssemblyDefinition> dependencies)
{
Info("Fixing resource references.");
var searchAndReplace = new List<Tuple<byte[], byte[]>>();
foreach (var d in dependencies)
{
string newName =
new AssemblyNameDefinition(d.Name.Name, d.Name.Version)
{
PublicKeyToken = publicKeyToken
}.FullName;
if (0 == publicKeyToken.Length) { newName += new string(' ', (8 * 2) - "null".Length); }
string oldName = d.FullName;
if (0 == d.Name.PublicKeyToken.Length) { oldName += new string(' ', (8 * 2) - "null".Length); }
if (newName.Length != oldName.Length || newName == oldName) { continue; }
searchAndReplace.Add(
Tuple.Create(Encoding.UTF8.GetBytes(oldName), Encoding.UTF8.GetBytes(newName)));
}
if (0 == searchAndReplace.Count)
{
return;
}
foreach (var resource in assemblyDefinition.MainModule.Resources.ToList())
{
var embeddedResource = resource as EmbeddedResource;
if (embeddedResource == null ||
!embeddedResource.Name.EndsWith(".resources", StringComparison.OrdinalIgnoreCase))
{
continue;
}
using (var resourceOutput = new MemoryStream())
{
using (var resourceInput = embeddedResource.GetResourceStream())
{
BinaryUtility.Replace(new BinaryReader(resourceInput), new BinaryWriter(resourceOutput), searchAndReplace);
assemblyDefinition.MainModule.Resources.Remove(resource);
assemblyDefinition.MainModule.Resources.Add(new EmbeddedResource(embeddedResource.Name, resource.Attributes, resourceOutput.ToArray()));
}
}
}
}
static void FixCustomAttributesReferences(AssemblyDefinition assemblyDefinition, IEnumerable<AssemblyDefinition> dependencies)
{
Info("Fixing type references in custom attributes.");
foreach (var t in assemblyDefinition.MainModule.Types)
{
FixCustomAttributesReferences(t, dependencies);
}
}
static void FixCustomAttributesReferences(TypeDefinition typeDefinition, IEnumerable<AssemblyDefinition> dependencies)
{
foreach(var n in typeDefinition.NestedTypes)
{
FixCustomAttributesReferences(n, dependencies);
}
foreach (var attribute in typeDefinition.CustomAttributes)
{
foreach (var argument in attribute.ConstructorArguments)
{
FixCustomAttributesReferences(argument, dependencies);
}
foreach (var property in attribute.Properties)
{
FixCustomAttributesReferences(property.Argument, dependencies);
}
}
}
static void FixCustomAttributesReferences(CustomAttributeArgument argument, IEnumerable<AssemblyDefinition> dependencies)
{
{
AssemblyDefinition existingDependency;
AssemblyNameReference assemblyNameReference;
if (null != (assemblyNameReference = argument.Type.Scope as AssemblyNameReference) &&
null != (existingDependency = dependencies.SingleOrDefault(x => x.FullName == assemblyNameReference.FullName)))
{
assemblyNameReference.PublicKeyToken = publicKeyToken;
return;
}
}
{
if (argument.Type.FullName != "System.Type") { return; }
if (!(argument.Value is MemberReference)) { return; }
object scope = ((dynamic)argument.Value).Scope;
if (!(scope is AssemblyNameReference)) { return; }
AssemblyNameReference assemblyNameReference = (AssemblyNameReference)scope;
if (!dependencies.Any(x => x.FullName == assemblyNameReference.FullName)) { return; }
assemblyNameReference.PublicKeyToken = publicKeyToken;
}
}
static void FixInternalsVisibleToAttribute(AssemblyDefinition assemblyDefinition, IEnumerable<AssemblyDefinition> dependencies)
{
Info("Fixing InternalsVisibleToAttribute references.");
foreach (var attribute in assemblyDefinition.CustomAttributes)
{
if (attribute.AttributeType.Name != "InternalsVisibleToAttribute") { continue; }
var argument = attribute.ConstructorArguments.First();
var v = argument.Value.ToString().Split(new string[] { "," }, StringSplitOptions.None);
if (2 != v.Length) { continue; }
var name = v[0].Trim();
if (!dependencies.Any(x => x.Name.Name == name)) { continue; }
attribute.ConstructorArguments.Remove(argument);
attribute.ConstructorArguments.Insert(0,
new CustomAttributeArgument(argument.Type, string.Format("{0}, PublicKey={1}", name,
BitConverter.ToString(strongNameKeyPair.PublicKey).Replace("-", ""))));
}
}
static void FixAssemblyReferences(AssemblyDefinition assemblyDefinition, IEnumerable<AssemblyDefinition> dependencies)
{
Info("Fixing assembly references.");
foreach (var r in assemblyDefinition.MainModule.AssemblyReferences)
{
if (!dependencies.Any(x => x.FullName == r.FullName)) { continue; }
Debug("Fixing assembly reference to {0}.", r.FullName);
r.PublicKeyToken = publicKeyToken;
}
}
static void WriteAssembly(AssemblyDefinition assemblyDefinition)
{
Info("Writing assembly to {0}.", assemblyDefinition.MainModule.FullyQualifiedName);
if (0 == publicKeyToken.Length)
{
assemblyDefinition.Name.HasPublicKey = false;
assemblyDefinition.Name.PublicKey = new byte[0];
assemblyDefinition.MainModule.Attributes &= ~ModuleAttributes.StrongNameSigned;
}
assemblyDefinition.Write(assemblyDefinition.MainModule.FullyQualifiedName,
new WriterParameters() { StrongNameKeyPair = strongNameKeyPair });
}
}
}