-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathAssemblyLoader.cs
228 lines (201 loc) · 8.29 KB
/
AssemblyLoader.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
using dnlib.DotNet;
using dnlib.DotNet.Emit;
using dnlib.DotNet.Writer;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Ether_Obfuscator
{
public class AssemblyLoader
{
public Assembly OriginAssembly;
public AssemblyDef Assembly;
public ModuleDefMD Module{ get; set; }
public string Path;
public string OutputPath;
public AssemblyLoader(string Assembly)
{
Path = Assembly.Replace("\"", "");
OutputPath = System.IO.Path.GetDirectoryName(Path) + "\\[EtherObfuscated]" + System.IO.Path.GetFileName(Path);
LoadAssembly();
LoadModuleDefMD();
//LoadDependencies();
Utils.Importer = new Importer(Module);
}
public AssemblyLoader(byte[] Assembly)
{
LoadAssemblyFromBytes(Assembly);
LoadModuleDefMDFromBytes(Assembly);
//LoadDependencies();
Utils.Importer = new Importer(Module);
}
public AssemblyLoader(string Assembly,bool loaddepend)
{
Path = Assembly.Replace("\"", "");
OutputPath = System.IO.Path.GetDirectoryName(Path) + "\\[EtherObfuscated]" + System.IO.Path.GetFileName(Path);
LoadAssembly();
LoadModuleDefMD();
if(loaddepend)
LoadDependencies();
Utils.Importer = new Importer(Module);
}
public AssemblyLoader(byte[] Assembly,bool loaddepend)
{
LoadAssemblyFromBytes(Assembly);
LoadModuleDefMDFromBytes(Assembly);
if(loaddepend)
LoadDependencies();
Utils.Importer = new Importer(Module);
}
public void LoadAssembly()
{
Console.Write("Loading assembly...\n");
OriginAssembly = System.Reflection.Assembly.UnsafeLoadFrom(Path);
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write(" Assembly: ");
Console.WriteLine(OriginAssembly.FullName);
Console.ForegroundColor = ConsoleColor.White;
}
public void LoadAssemblyFromBytes(byte[] array)
{
Console.Write("Loading assembly...");
OriginAssembly = System.Reflection.Assembly.Load(array);
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write(" Loaded: ");
Console.WriteLine(OriginAssembly.FullName);
Console.ForegroundColor = ConsoleColor.White;
}
public void LoadModuleDefMD()
{
Console.Write("Loading Mono ModuleDefMD...\n");
//ModuleCreationOptions modOpts = new ModuleCreationOptions(CLRRuntimeReaderKind.Mono);
//Module = ModuleDefMD.Load(Path, modOpts);
Module = ModuleDefMD.Load(Path);
Assembly = Module.Assembly;
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write(" Module: ");
Console.WriteLine(Module.FullName);
Console.ForegroundColor = ConsoleColor.White;
}
public void LoadModuleDefMDFromBytes(byte[] array)
{
Console.Write("Loading ModuleDefMD...");
Module = ModuleDefMD.Load(array);
Assembly = Module.Assembly;
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write(" Loaded: ");
Console.WriteLine(Module.FullName);
Console.ForegroundColor = ConsoleColor.White;
}
public void LoadDependencies()
{
Console.WriteLine("Analysing dependencies...");
var asmResolver = new AssemblyResolver();
ModuleContext modCtx = new ModuleContext(asmResolver);
asmResolver.DefaultModuleContext = modCtx;
asmResolver.EnableTypeDefCache = true;
asmResolver.DefaultModuleContext = new ModuleContext(asmResolver);
asmResolver.PostSearchPaths.Insert(0, Path);
if (IsCosturaPresent(Module))
{
foreach (var asm in ExtractCosturaEmbeddedAssemblies(GetEmbeddedCosturaAssemblies(Module), Module))
asmResolver.AddToCache(asm);
}
int i = 0;
foreach (var dependency in Module.GetAssemblyRefs())
{
AssemblyDef assembly = asmResolver.ResolveThrow(dependency, Module);
Console.WriteLine(" dependency["+i+"]:" + dependency.Name);
i++;
}
Module.Context = modCtx;
}
public void LoadDependenciesFromBytes(List<byte[]> files)
{
Console.WriteLine("Resolving dependencies...");
var asmResolver = new AssemblyResolver();
ModuleContext modCtx = new ModuleContext(asmResolver);
asmResolver.DefaultModuleContext = modCtx;
asmResolver.EnableTypeDefCache = true;
asmResolver.DefaultModuleContext = new ModuleContext(asmResolver);
asmResolver.PostSearchPaths.Insert(0, Path);
foreach (var item in files)
{
AssemblyDef assembly = AssemblyDef.Load(item);
asmResolver.AddToCache(assembly);
Console.WriteLine("Resolved " + assembly.Name);
}
Module.Context = modCtx;
}
public bool IsCosturaPresent(ModuleDef module) =>
module.Types.FirstOrDefault(t => t.Name == "AssemblyLoader" && t.Namespace == "Costura") != null;
public string[] GetEmbeddedCosturaAssemblies(ModuleDef module)
{
var list = new List<string>();
var ctor = module.Types.Single(t => t.Name == "AssemblyLoader" && t.Namespace == "Costura").FindStaticConstructor();
var instructions = ctor.Body.Instructions;
for (var i = 1; i < instructions.Count; i++)
{
var curr = instructions[i];
if (curr.OpCode != OpCodes.Ldstr || instructions[i - 1].OpCode != OpCodes.Ldstr)
continue;
var resName = ((string)curr.Operand).ToLowerInvariant();
if (resName.EndsWith(".pdb") || resName.EndsWith(".pdb.compressed"))
{
i++;
continue;
}
list.Add((string)curr.Operand);
}
return list.ToArray();
}
public List<AssemblyDef> ExtractCosturaEmbeddedAssemblies(string[] assemblies, ModuleDef module)
{
var list = new List<AssemblyDef>();
foreach (var assembly in assemblies)
{
EmbeddedResource resource = module.Resources.FindEmbeddedResource(assembly.ToLowerInvariant());
if (resource == null)
throw new Exception("Couldn't find Costura embedded assembly: " + assembly);
if (resource.Name.EndsWith(".compressed"))
{
resource.CreateReader().CopyTo(new MemoryStream());
list.Add(DecompressCosturaAssembly(resource.CreateReader().AsStream()));
continue;
}
list.Add(AssemblyDef.Load(resource.CreateReader().AsStream()));
}
return list;
}
public AssemblyDef DecompressCosturaAssembly(Stream resource)
{
using (var def = new DeflateStream(resource, CompressionMode.Decompress))
{
var ms = new MemoryStream();
def.CopyTo(ms);
ms.Position = 0;
return AssemblyDef.Load(ms);
}
}
public void Save()
{
ModuleWriterOptions opts = new ModuleWriterOptions(Module);
opts.Logger = DummyLogger.NoThrowInstance;
Assembly.Write(System.IO.Path.GetDirectoryName(Path) + "\\[EtherObfuscated]" + System.IO.Path.GetFileName(Path), opts);
Console.WriteLine("Saved.");
}
public void Save(string path)
{
ModuleWriterOptions opts = new ModuleWriterOptions(Module);
opts.Logger = DummyLogger.NoThrowInstance;
Assembly.Write(path, opts);
Console.WriteLine("Saved.");
}
}
}