-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathProgram.cs
More file actions
208 lines (180 loc) · 7.14 KB
/
Program.cs
File metadata and controls
208 lines (180 loc) · 7.14 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
using dnlib.DotNet;
using dnlib.DotNet.Emit;
using dnlib.DotNet.Writer;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Reflection;
namespace ETR
{
class Program
{
static int Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("Usage: ETR.exe <path-to-assembly>");
return 1;
}
try
{
var module = ModuleDefMD.Load(args[0]);
var evalTypes = FindTypes(module);
if (evalTypes.Count == 0)
{
Console.WriteLine("Version not supported.");
return 1;
}
var typeToProcess = evalTypes[0];
Patching(typeToProcess);
// First save as temporary -removed file
string originalPath = args[0];
string tempPath = originalPath.Replace(".exe", "-Removed.exe");
var options = new ModuleWriterOptions(module);
options.MetadataOptions.Flags |= MetadataFlags.PreserveAll;
options.MetadataOptions.Flags |= MetadataFlags.KeepOldMaxStack;
module.Write(tempPath, options);
// Release module
module.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();
// Create backup folder and move original
string backupDir = System.IO.Path.Combine(
System.IO.Path.GetDirectoryName(originalPath),
"Backup"
);
System.IO.Directory.CreateDirectory(backupDir);
string backupPath = System.IO.Path.Combine(
backupDir,
System.IO.Path.GetFileName(originalPath)
);
// Move original to backup, delete original, rename temp to original
System.IO.File.Copy(originalPath, backupPath, true);
System.IO.File.Delete(originalPath);
System.IO.File.Move(tempPath, originalPath);
Console.WriteLine("Successfully patched and backed up original file.");
return 0;
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
return 1;
}
}
private static void Patching(TypeDef evalType)
{
var badMethod = GetStaticMethods(evalType, "System.Boolean", "System.Boolean").ToList();
foreach (var method in badMethod)
{
var instructions = method.Body.Instructions;
instructions.Clear();
instructions.Add(OpCodes.Ldc_I4_1.ToInstruction());
instructions.Add(OpCodes.Ret.ToInstruction());
method.Body.ExceptionHandlers.Clear();
}
}
private static IList<TypeDef> FindTypes(ModuleDefMD module)
{
var evalTypes = new List<TypeDef>();
var types = module.GetTypes();
foreach (var typeDef in types)
{
if (typeDef.Methods.Count == 7
&& CountStaticMethods(typeDef, "System.Boolean", "System.Boolean") == 2
&& CountStaticMethods(typeDef, "System.Void") == 3
&& CountStaticMethods(typeDef, "System.Void", "System.Threading.ThreadStart") == 1
&& CountStaticMethods(typeDef, "System.Boolean") == 1)
{
evalTypes.Add(typeDef);
}
else if (typeDef.Methods.Count == 6
&& CountStaticMethods(typeDef, "System.Boolean", "System.Boolean") == 2
&& CountStaticMethods(typeDef, "System.Void") == 2
&& CountStaticMethods(typeDef, "System.Void", "System.Threading.ThreadStart") == 1
&& CountStaticMethods(typeDef, "System.Boolean") == 1)
{
evalTypes.Add(typeDef);
}
else if (typeDef.Methods.Count == 4
&& CountStaticMethods(typeDef, "System.Boolean", "System.Boolean") == 1
&& CountStaticMethods(typeDef, "System.Void") == 2
&& CountStaticMethods(typeDef, "System.Boolean") == 1)
{
evalTypes.Add(typeDef);
}
else if (typeDef.Methods.Count == 3
&& CountStaticMethods(typeDef, "System.Boolean", "System.Boolean") == 1
&& CountStaticMethods(typeDef, "System.Void") == 1
&& CountStaticMethods(typeDef, "System.Boolean") == 1)
{
evalTypes.Add(typeDef);
}
}
return evalTypes;
}
private static Int32 CountStaticMethods(TypeDef def, String retType, params String[] paramTypes)
{
return GetStaticMethods(def, retType, paramTypes).Count;
}
private static IList<MethodDef> GetStaticMethods(TypeDef def, String retType, params String[] paramTypes)
{
List<MethodDef> methods = new List<MethodDef>();
if (!def.HasMethods)
return methods;
foreach (var method in def.Methods)
{
if (!method.IsStatic)
continue;
if (!method.ReturnType.FullName.Equals(retType))
continue;
if (paramTypes.Length != method.Parameters.Count)
continue;
Boolean paramsMatch = true;
for (Int32 i = 0; i < paramTypes.Length && i < method.Parameters.Count; i++)
{
if (!method.Parameters[i].Type.FullName.Equals(paramTypes[i]))
{
paramsMatch = false;
break;
}
}
if (!paramsMatch)
continue;
methods.Add(method);
}
return methods;
}
private static string GetOutput(string filepath)
{
// Create backup folder if it doesn't exist
string backupDir = System.IO.Path.Combine(
System.IO.Path.GetDirectoryName(filepath),
"Backup"
);
System.IO.Directory.CreateDirectory(backupDir);
// Move original file to backup
string backupFile = System.IO.Path.Combine(
backupDir,
System.IO.Path.GetFileName(filepath)
);
System.IO.File.Copy(filepath, backupFile, true);
// Return original filepath to overwrite it
return filepath;
}
}
}