-
Notifications
You must be signed in to change notification settings - Fork 0
/
automedic.cs
192 lines (171 loc) · 7.71 KB
/
automedic.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
using System;
using System.Reflection;
using System.IO;
using System.Collections.Generic;
using dnlib;
using dnlib.DotNet;
using dnlib.DotNet.Writer;
using de4dot.cui;
using de4dot.code;
/// <summary>
/// Inherit FilesDeobfuscator and modify it's functionality a bit.
/// </summary>
class AutoMedic : FilesDeobfuscator
{
public ModuleDefMD module;
public static int correctChecksum = -1;
public static bool bPrintedVersion = false;
public delegate int closure(ModuleDef module, MethodDef method);
public static List<closure> modifiers = new List<closure>();
string filename;
string filenameBackup;
string[] arguments;
string verLow;
string verHigh;
static bool Try(Action action) { try { action(); return true; } catch { return false; } }
static T Try<T>(Func<T> func) { try { return func(); } catch { return default(T); } }
static bool True(Action action) { action(); return true; }
static bool False(Action action) { action(); return false; }
AutoMedic(FilesDeobfuscator.Options options, string filename, string filenameBackup, string[] arguments, string verLow, string verHigh) : base(options)
{
this.filename = filename;
this.filenameBackup = filenameBackup;
this.arguments = arguments;
this.verLow = verLow;
this.verHigh = verHigh;
}
static void Write(ConsoleColor color, String txt)
{
Console.ForegroundColor = color;
Console.Write(txt);
Console.ResetColor();
}
static void WriteLine(dynamic prefix, dynamic suffix)
{
Write(ConsoleColor.Cyan, prefix + ": ");
Console.WriteLine(suffix);
}
void WriteLine(dynamic suffix)
{
WriteLine(filename, suffix);
}
/// <summary>
/// Print Program Version
/// </summary>
static void version()
{
if(bPrintedVersion == false) {
Write(ConsoleColor.Red, "======");
Write(ConsoleColor.White, "=====================");
Write(ConsoleColor.Red, "======\n");
Write(ConsoleColor.Red, "====");
Write(ConsoleColor.White, "====");
Write(ConsoleColor.Cyan, " Auto");
Write(ConsoleColor.Red, "-");
Write(ConsoleColor.Cyan, "Medic");
Write(ConsoleColor.Yellow, " v4.7 ");
Write(ConsoleColor.White, "====");
Write(ConsoleColor.Red, "====\n");
Write(ConsoleColor.Red, "======");
Write(ConsoleColor.White, "=====================");
Write(ConsoleColor.Red, "======\n");
}
bPrintedVersion = true;
}
/// <summary>
/// Deobfuscate files ands return an instance to the module definition of the first one.
/// Adapted from de4dot.cui.FileDeofuscator.deobfuscateAll()
///
/// </summary>
/// <returns>The module definition of the first file.</returns>
void deobfuscate()
{
var stdOut = Console.Out;
try {
Console.SetOut(new StringWriter()); //redirect stdout to nothing.
List<IObfuscatedFile> allFiles = new List<IObfuscatedFile>(LoadAllFiles()); //Load the files.
DeobfuscateAllFiles(allFiles); //Deobfuscate the files.
Rename(allFiles); //Rename methods/classes/etc.
module = allFiles[0].ModuleDefMD; //Return the module definition of the first file.
}
finally { Console.SetOut(stdOut); } // restore stdout.
}
/// <summary>
/// Backs up the binary provided.
/// </summary>
/// <param name="from">The binary to backup. Must exist.</param>
/// <param name="to">The name to use for the backup.</param>
/// <returns>false on failure and true on success.</returns>
static bool BackupBinary(string from, string to)
{
WriteLine(from, "creating backup binary...");
//check the versions of the assembly to see if the one we are going to patch is newer.
Version newVersion = AssemblyName.GetAssemblyName(from).Version;
Version oldVersion = Try(()=>AssemblyName.GetAssemblyName(to).Version); // Implicitly null if doesn't exist.
string ret = (newVersion == oldVersion) switch {
true => "backup binary exists already, aborting file write.",
_ when !Try(() => File.Delete(to)) => "failed to remove stale backup binary, aborting execution.",
_ when !Try(() => File.Copy(from, to)) => "failed to create backup binary, aborting execution.",
_ => null
};
return ret switch {
null => true,
_ when True(() => WriteLine(from, ret)) => false
};
}
public static void DoPatch(string filename, string[] arguments, string verLow = "0.0.0.0", string verHigh = "2147483647.2147483647.2147483647.2147483647")
{
//print version.
AutoMedic.version();
//create a backup.
string filenameBackup = filename + ".bak";
if (BackupBinary(filename, filenameBackup)) {
//file argument to run de4dot (the deobfuscator) with.
List<string> argumentList = new List<string>(arguments);
argumentList.Add("-f");
argumentList.Add(filenameBackup);
//populate the options using the commandline arguments passed to de4dot (code based actual on de4dot code).
FilesDeobfuscator.Options options = new FilesDeobfuscator.Options();
de4dot.cui.Program.ParseCommandLine(argumentList.ToArray(), options);
if(!(new AutoMedic(options, filename, filenameBackup, arguments, verLow, verHigh)).DoPatch())
File.Delete(filenameBackup);
}
Console.WriteLine("Press Enter to exit...");
Console.ReadLine();
}
bool DoPatch()
{
int checksum = 0;
Version binaryVersion = Try(() => AssemblyName.GetAssemblyName(filename).Version);
Version low = Version.Parse(verLow);
Version high = Version.Parse(verHigh);
string ret = null;
if ((binaryVersion == null) switch {
true => "No binaries with matching names found.",
_ when binaryVersion < low || binaryVersion > high => "Binary version does not match, aborting patch.",
_ when False(() => WriteLine("deobfuscating binary...")) => "",
_ when !Try(() => deobfuscate()) => "error deobfuscating, aborting file write.",
_ => null
} != null) {
WriteLine(ret);
return false;
}
//iterate through all classes with user code delegates.
WriteLine("patching binary...");
foreach (TypeDef type in module.GetTypes())
foreach (MethodDef method in type.Methods)
foreach (closure modifier in AutoMedic.modifiers)
checksum += modifier(module, method);
//checksum check.
if (checksum != AutoMedic.correctChecksum) {
WriteLine("checksum incorrect, aborting file write.");
return false;
}
// Save file.
var options = new ModuleWriterOptions(module);
options.MetadataOptions.Flags |= MetadataFlags.PreserveAll & MetadataFlags.KeepOldMaxStack;
options.Logger = DummyLogger.NoThrowInstance;
module.Write(filename, options); //write the file (hopefully).
return true;
}
}