-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProgram.cs
More file actions
499 lines (489 loc) · 27.7 KB
/
Program.cs
File metadata and controls
499 lines (489 loc) · 27.7 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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
using de4dot.blocks;
using de4dot.blocks.cflow;
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.Reflection;
using System.Runtime.InteropServices;
namespace MindSystemCalculator
{
class Program
{
public static int FixedSizeOf = 0;
public static int EmptyTypesFixed = 0;
public static int ParseFixed = 0;
public static int MathsFixed = 0;
public static int OperationFixed = 0;
public static int StringsLengths = 0;
public static int DecimalCompareFixed = 0;
public static List<MethodInfo> ListPlugin = new List<MethodInfo>();
static void Main(string[] args)
{
Console.WriteLine(@" ___ _ _ _ ");
Console.WriteLine(@" / __|__ _| |__ _ _| |__ _| |_ ___ _ _ ");
Console.WriteLine(@"| (__/ _` | / _| || | / _` | _/ _ \ '_|");
Console.WriteLine(@" \___\__,_|_\__|\_,_|_\__,_|\__\___/_| ");
Console.WriteLine(" ");
Console.WriteLine(" - by MindSystem - ");
Console.WriteLine(" ");
ModuleDefMD module = null;
try
{
module = ModuleDefMD.Load(args[0]);
}
catch
{
Console.WriteLine("Please load a correct module into the calculator");
}
LoadPlugin();
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
foreach (TypeDef type in module.Types)
{
foreach (MethodDef method in type.Methods)
{
if (method.HasBody && method.Body.HasInstructions)
{
try
{
SizeOfFixer(method);
EmptyTypesFixer(method);
Cleaner(method);
UnParse(method);
Cleaner(method);
StringsLengthFixer(method);
Cleaner(method);
MathsFixer(method);
Cleaner(method);
if(ListPlugin.Count != 0)
{
foreach(MethodInfo meth in ListPlugin)
{
meth.Invoke(null, new object[] { method });
}
}
}
catch (Exception ex)
{
Console.WriteLine("An error occured, please debug the tool to localize it");
Console.WriteLine("\n");
Console.WriteLine("------------------------------------------------------");
Console.WriteLine("\n");
Console.WriteLine(ex.ToString());
}
}
}
}
stopWatch.Stop();
Console.WriteLine("Done ! Elapsed time : " + stopWatch.Elapsed.TotalSeconds);
string SavingPath = module.Kind == ModuleKind.Dll ? args[0].Replace(".dll", "-Deobfuscated.dll") : args[0].Replace(".exe", "-Deobfuscated.exe");
if (module.IsILOnly)
{
var opts = new ModuleWriterOptions(module);
opts.MetadataOptions.Flags = MetadataFlags.PreserveAll;
opts.Logger = DummyLogger.NoThrowInstance;
module.Write(SavingPath, opts);
}
else
{
var opts = new NativeModuleWriterOptions(module, true);
opts.MetadataOptions.Flags = MetadataFlags.PreserveAll;
opts.Logger = DummyLogger.NoThrowInstance;
module.NativeWrite(SavingPath, opts);
}
Console.WriteLine("[*] X.Parse Fixed :" + ParseFixed.ToString());
Console.WriteLine("[*] EmptyTypes Fixed :" + EmptyTypesFixed.ToString());
Console.WriteLine("[*] Sizeof Fixed :" + FixedSizeOf.ToString());
Console.WriteLine("[*] Math.X Fixed :" + MathsFixed.ToString());
Console.WriteLine("[*] Maths Operations Fixed :" + OperationFixed.ToString());
Console.WriteLine("[*] StringsLength Fixed :" + StringsLengths.ToString());
Console.WriteLine("[*] DecimalCompare Fixed :" + DecimalCompareFixed.ToString());
Console.ReadLine();
}
//This Method solve Decimal.Compare
public static void DecimalCompareFixer(MethodDef method)
{
for (int i = 4; i < method.Body.Instructions.Count - 1; i++)
{
if (method.Body.Instructions[i].OpCode == OpCodes.Call && method.Body.Instructions[i].Operand.ToString().Contains("Compare") && method.Body.Instructions[i - 1].OpCode == OpCodes.Newobj && method.Body.Instructions[i - 2].IsLdcI4() && method.Body.Instructions[i - 3].OpCode == OpCodes.Newobj && method.Body.Instructions[i - 4].IsLdcI4())
{
decimal first = method.Body.Instructions[i - 4].GetLdcI4Value();
decimal second = method.Body.Instructions[i - 2].GetLdcI4Value();
int result = decimal.Compare(first, second);
method.Body.Instructions[i - 1].OpCode = OpCodes.Nop;
method.Body.Instructions[i - 2].OpCode = OpCodes.Nop;
method.Body.Instructions[i - 3].OpCode = OpCodes.Nop;
method.Body.Instructions[i - 4].OpCode = OpCodes.Nop;
method.Body.Instructions[i].OpCode = OpCodes.Ldc_I4;
method.Body.Instructions[i].Operand = result;
DecimalCompareFixed++;
}
}
}
public static void LoadPlugin()
{
List<Assembly> allAssemblies = new List<Assembly>();
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
foreach (string dll in Directory.GetFiles(path, "*.dll"))
{
if (dll.Contains("Plugin"))
{
allAssemblies.Add(Assembly.LoadFile(dll));
Console.WriteLine("Loaded plugin : " + Path.GetFileName(dll));
}
}
if (allAssemblies.Count == 0)
return;
foreach(Assembly asm in allAssemblies)
{
foreach(Type type in asm.GetTypes())
{
if(type.Name == "Calculator")
{
MethodInfo meth = type.GetMethod("Execute");
ListPlugin.Add(meth);
}
}
}
}
//This Method Replace Math.X and solve c# native operations
public static void MathsFixer(MethodDef method)
{
for (int i = 0; i < method.Body.Instructions.Count - 1; i++)
{
if (method.Body.Instructions[i].OpCode == OpCodes.Ldc_R8 && method.Body.Instructions[i + 1].OpCode == OpCodes.Call && method.Body.Instructions[i + 1].Operand.ToString().Contains("Math"))
{
MemberRef MathMethod = (MemberRef)method.Body.Instructions[i+1].Operand;
double argument = (double)method.Body.Instructions[i].Operand;
MethodInfo methodInfo = typeof(Math).GetMethod(MathMethod.Name, new System.Type[] { typeof(double) });
double result = (double)methodInfo.Invoke(null, new object[] { argument });
method.Body.Instructions[i + 1].OpCode = OpCodes.Nop;
method.Body.Instructions[i].Operand = result;
MathsFixed++;
}
else if (method.Body.Instructions[i].OpCode == OpCodes.Ldc_R4 && method.Body.Instructions[i + 1].OpCode == OpCodes.Call && method.Body.Instructions[i + 1].Operand.ToString().Contains("Math"))
{
MemberRef MathMethod = (MemberRef)method.Body.Instructions[i + 1].Operand;
float argument = (float)method.Body.Instructions[i].Operand;
MethodInfo methodInfo = typeof(Math).GetMethod(MathMethod.Name, new System.Type[] { typeof(float) });
float result = (float)methodInfo.Invoke(null, new object[] { argument });
method.Body.Instructions[i + 1].OpCode = OpCodes.Nop;
method.Body.Instructions[i].Operand = result;
MathsFixed++;
}
else if (method.Body.Instructions[i].OpCode == OpCodes.Ldc_I4 && method.Body.Instructions[i + 1].OpCode == OpCodes.Call && method.Body.Instructions[i + 1].Operand.ToString().Contains("Math"))
{
MemberRef MathMethod = (MemberRef)method.Body.Instructions[i + 1].Operand;
int argument = (int)method.Body.Instructions[i].Operand;
MethodInfo methodInfo = typeof(Math).GetMethod(MathMethod.Name, new System.Type[] { typeof(int) });
int result = (int)methodInfo.Invoke(null, new object[] { argument });
method.Body.Instructions[i + 1].OpCode = OpCodes.Nop;
method.Body.Instructions[i].Operand = result;
MathsFixed++;
}
else if (method.Body.Instructions[i].OpCode == OpCodes.Ldc_R8 && method.Body.Instructions[i+1].OpCode == OpCodes.Ldc_R8 && method.Body.Instructions[i + 2].OpCode == OpCodes.Call && method.Body.Instructions[i + 2].Operand.ToString().Contains("Math"))
{
MemberRef MathMethod = (MemberRef)method.Body.Instructions[i + 2].Operand;
double argument = (double)method.Body.Instructions[i].Operand;
MethodInfo methodInfo = typeof(Math).GetMethod(MathMethod.Name, new System.Type[] { typeof(double) });
double result = (double)methodInfo.Invoke(null, new object[] { argument });
method.Body.Instructions[i + 2].OpCode = OpCodes.Nop;
method.Body.Instructions[i + 1].OpCode = OpCodes.Nop;
method.Body.Instructions[i].Operand = result;
MathsFixed++;
}
else if (method.Body.Instructions[i].OpCode == OpCodes.Ldc_R4 && method.Body.Instructions[i + 1].OpCode == OpCodes.Ldc_R4 && method.Body.Instructions[i + 2].OpCode == OpCodes.Call && method.Body.Instructions[i + 2].Operand.ToString().Contains("Math"))
{
MemberRef MathMethod = (MemberRef)method.Body.Instructions[i + 2].Operand;
float argument = (float)method.Body.Instructions[i].Operand;
MethodInfo methodInfo = typeof(Math).GetMethod(MathMethod.Name, new System.Type[] { typeof(float) });
float result = (float)methodInfo.Invoke(null, new object[] { argument });
method.Body.Instructions[i + 2].OpCode = OpCodes.Nop;
method.Body.Instructions[i + 1].OpCode = OpCodes.Nop;
method.Body.Instructions[i].Operand = result;
MathsFixed++;
}
else if (method.Body.Instructions[i].OpCode == OpCodes.Ldc_I4 && method.Body.Instructions[i + 1].OpCode == OpCodes.Ldc_I4 && method.Body.Instructions[i + 2].OpCode == OpCodes.Call && method.Body.Instructions[i + 2].Operand.ToString().Contains("Math"))
{
MemberRef MathMethod = (MemberRef)method.Body.Instructions[i + 2].Operand;
int argument = (int)method.Body.Instructions[i].Operand;
MethodInfo methodInfo = typeof(Math).GetMethod(MathMethod.Name, new System.Type[] { typeof(int) });
int result = (int)methodInfo.Invoke(null, new object[] { argument });
method.Body.Instructions[i + 2].OpCode = OpCodes.Nop;
method.Body.Instructions[i + 1].OpCode = OpCodes.Nop;
method.Body.Instructions[i].Operand = result;
MathsFixed++;
}
else if (method.Body.Instructions[i].IsSub() && method.Body.Instructions[i-1].IsLdcI4() && method.Body.Instructions[i -2].IsLdcI4())
{
int firstarg = method.Body.Instructions[i - 2].GetLdcI4Value();
int secondarg = method.Body.Instructions[i - 1].GetLdcI4Value();
int result = firstarg - secondarg;
method.Body.Instructions[i-1].Operand = result;
method.Body.Instructions[i].OpCode = OpCodes.Nop;
method.Body.Instructions[i -2].OpCode = OpCodes.Nop;
OperationFixed++;
}
else if (method.Body.Instructions[i].IsMul() && method.Body.Instructions[i - 1].IsLdcI4() && method.Body.Instructions[i - 2].IsLdcI4())
{
int firstarg = method.Body.Instructions[i - 2].GetLdcI4Value();
int secondarg = method.Body.Instructions[i - 1].GetLdcI4Value();
int result = firstarg * secondarg;
method.Body.Instructions[i - 1].Operand = result;
method.Body.Instructions[i].OpCode = OpCodes.Nop;
method.Body.Instructions[i - 2].OpCode = OpCodes.Nop;
OperationFixed++;
}
else if (method.Body.Instructions[i].IsDiv() && method.Body.Instructions[i - 1].IsLdcI4() && method.Body.Instructions[i - 2].IsLdcI4())
{
int firstarg = method.Body.Instructions[i - 2].GetLdcI4Value();
int secondarg = method.Body.Instructions[i - 1].GetLdcI4Value();
int result = firstarg / secondarg;
method.Body.Instructions[i - 1].Operand = result;
method.Body.Instructions[i].OpCode = OpCodes.Nop;
method.Body.Instructions[i - 2].OpCode = OpCodes.Nop;
OperationFixed++;
}
else if (method.Body.Instructions[i].OpCode == OpCodes.Xor && method.Body.Instructions[i - 1].IsLdcI4() && method.Body.Instructions[i - 2].IsLdcI4())
{
int firstarg = method.Body.Instructions[i - 2].GetLdcI4Value();
int secondarg = method.Body.Instructions[i - 1].GetLdcI4Value();
int result = firstarg ^ secondarg;
method.Body.Instructions[i - 1].Operand = result;
method.Body.Instructions[i].OpCode = OpCodes.Nop;
method.Body.Instructions[i - 2].OpCode = OpCodes.Nop;
OperationFixed++;
}
else if (method.Body.Instructions[i].IsSub() && method.Body.Instructions[i - 1].IsRem() && method.Body.Instructions[i - 2].IsLdcI4())
{
int firstarg = method.Body.Instructions[i - 2].GetLdcI4Value();
int secondarg = method.Body.Instructions[i - 1].GetLdcI4Value();
int result = firstarg ^ secondarg;
method.Body.Instructions[i - 1].Operand = result;
method.Body.Instructions[i].OpCode = OpCodes.Nop;
method.Body.Instructions[i - 2].OpCode = OpCodes.Nop;
OperationFixed++;
}
else if (method.Body.Instructions[i].OpCode == OpCodes.Shl && method.Body.Instructions[i - 1].IsLdcI4() && method.Body.Instructions[i - 2].IsLdcI4())
{
int firstarg = method.Body.Instructions[i - 2].GetLdcI4Value();
int secondarg = method.Body.Instructions[i - 1].GetLdcI4Value();
int result = firstarg << secondarg;
method.Body.Instructions[i - 1].Operand = result;
method.Body.Instructions[i].OpCode = OpCodes.Nop;
method.Body.Instructions[i - 2].OpCode = OpCodes.Nop;
OperationFixed++;
}
else if (method.Body.Instructions[i].IsShr() && method.Body.Instructions[i - 1].IsRem() && method.Body.Instructions[i - 2].IsLdcI4())
{
int firstarg = method.Body.Instructions[i - 2].GetLdcI4Value();
int secondarg = method.Body.Instructions[i - 1].GetLdcI4Value();
int result = firstarg >> secondarg;
method.Body.Instructions[i - 1].Operand = result;
method.Body.Instructions[i].OpCode = OpCodes.Nop;
method.Body.Instructions[i - 2].OpCode = OpCodes.Nop;
OperationFixed++;
}
else if (method.Body.Instructions[i].OpCode == OpCodes.And && method.Body.Instructions[i - 1].IsLdcI4() && method.Body.Instructions[i - 2].IsLdcI4())
{
int firstarg = method.Body.Instructions[i - 2].GetLdcI4Value();
int secondarg = method.Body.Instructions[i - 1].GetLdcI4Value();
int result = firstarg & secondarg;
method.Body.Instructions[i - 1].Operand = result;
method.Body.Instructions[i].OpCode = OpCodes.Nop;
method.Body.Instructions[i - 2].OpCode = OpCodes.Nop;
OperationFixed++;
}
else if (method.Body.Instructions[i].OpCode == OpCodes.Or && method.Body.Instructions[i - 1].IsLdcI4() && method.Body.Instructions[i - 2].IsLdcI4())
{
int firstarg = method.Body.Instructions[i - 2].GetLdcI4Value();
int secondarg = method.Body.Instructions[i - 1].GetLdcI4Value();
int result = firstarg | secondarg;
method.Body.Instructions[i - 1].Operand = result;
method.Body.Instructions[i].OpCode = OpCodes.Nop;
method.Body.Instructions[i - 2].OpCode = OpCodes.Nop;
OperationFixed++;
}
}
}
public static void Cleaner(MethodDef method)
{
BlocksCflowDeobfuscator blocksCflowDeobfuscator = new BlocksCflowDeobfuscator();
Blocks blocks = new Blocks(method);
blocksCflowDeobfuscator.Initialize(blocks);
blocksCflowDeobfuscator.Deobfuscate();
blocks.RepartitionBlocks();
IList<Instruction> list;
IList<ExceptionHandler> exceptionHandlers;
blocks.GetCode(out list, out exceptionHandlers);
DotNetUtils.RestoreBody(method, list, exceptionHandlers);
}
//This Method Replace X.Parse
public static void UnParse(MethodDef method)
{
for (int i = 1; i < method.Body.Instructions.Count - 1; i++)
{
if (method.Body.Instructions[i].OpCode == OpCodes.Call && method.Body.Instructions[i].Operand.ToString().Contains("Parse") && method.Body.Instructions[i - 1].OpCode == OpCodes.Ldstr)
{
MemberRef Parse = (MemberRef)method.Body.Instructions[i].Operand;
if (Parse.DeclaringType.Name.Contains("Int32"))
{
int result = int.Parse(method.Body.Instructions[i - 1].Operand.ToString());
method.Body.Instructions[i].OpCode = OpCodes.Ldc_I4;
method.Body.Instructions[i].Operand = result;
method.Body.Instructions[i - 1].OpCode = OpCodes.Nop;
ParseFixed++;
}
else if (Parse.DeclaringType.Name.Contains("Single"))
{
float result = float.Parse(method.Body.Instructions[i - 1].Operand.ToString());
method.Body.Instructions[i].OpCode = OpCodes.Ldc_R4;
method.Body.Instructions[i].Operand = result;
method.Body.Instructions[i - 1].OpCode = OpCodes.Nop;
ParseFixed++;
}
else if (Parse.DeclaringType.Name.Contains("Int64"))
{
long result = long.Parse(method.Body.Instructions[i - 1].Operand.ToString());
method.Body.Instructions[i].OpCode = OpCodes.Ldc_I8;
method.Body.Instructions[i].Operand = result;
method.Body.Instructions[i - 1].OpCode = OpCodes.Nop;
ParseFixed++;
}
else if (Parse.DeclaringType.Name.Contains("Double"))
{
double result = double.Parse(method.Body.Instructions[i - 1].Operand.ToString());
method.Body.Instructions[i].OpCode = OpCodes.Ldc_R8;
method.Body.Instructions[i].Operand = result;
method.Body.Instructions[i - 1].OpCode = OpCodes.Nop;
ParseFixed++;
}
else if (Parse.DeclaringType.Name.Contains("Decimal"))
{
Decimal result = Decimal.Parse(method.Body.Instructions[i - 1].Operand.ToString());
method.Body.Instructions[i].OpCode = OpCodes.Ldc_R4;
method.Body.Instructions[i].Operand = (float)result;
method.Body.Instructions[i - 1].OpCode = OpCodes.Nop;
ParseFixed++;
}
else if (Parse.DeclaringType.Name.Contains("UInt32"))
{
uint result = uint.Parse(method.Body.Instructions[i - 1].Operand.ToString());
method.Body.Instructions[i].OpCode = OpCodes.Ldc_I4;
method.Body.Instructions[i].Operand = (int)result;
method.Body.Instructions.Add(OpCodes.Conv_U4.ToInstruction());
method.Body.Instructions[i - 1].OpCode = OpCodes.Nop;
ParseFixed++;
}
else if (Parse.DeclaringType.Name.Contains("UInt64"))
{
ulong result = ulong.Parse(method.Body.Instructions[i - 1].Operand.ToString());
method.Body.Instructions[i].OpCode = OpCodes.Ldc_I8;
method.Body.Instructions[i].Operand = (long)result;
method.Body.Instructions.Add(OpCodes.Conv_U8.ToInstruction());
method.Body.Instructions[i - 1].OpCode = OpCodes.Nop;
ParseFixed++;
}
else if (Parse.DeclaringType.Name.Contains("Int16"))
{
short result = short.Parse(method.Body.Instructions[i - 1].Operand.ToString());
method.Body.Instructions[i].OpCode = OpCodes.Ldc_I4;
method.Body.Instructions[i].Operand = (int)result;
method.Body.Instructions.Add(OpCodes.Conv_I2.ToInstruction());
method.Body.Instructions[i - 1].OpCode = OpCodes.Nop;
ParseFixed++;
}
else if (Parse.DeclaringType.Name.Contains("UInt16"))
{
ushort result = ushort.Parse(method.Body.Instructions[i - 1].Operand.ToString());
method.Body.Instructions[i].OpCode = OpCodes.Ldc_I4;
method.Body.Instructions[i].Operand = (int)result;
method.Body.Instructions.Add(OpCodes.Conv_U2.ToInstruction());
method.Body.Instructions[i - 1].OpCode = OpCodes.Nop;
ParseFixed++;
}
}
}
}
//This Method Remove size.EmptyType
public static void EmptyTypesFixer(MethodDef method)
{
for (int i = 1; i < method.Body.Instructions.Count - 1; i++)
{
if (method.Body.Instructions[i].OpCode == OpCodes.Ldsfld && method.Body.Instructions[i].Operand.ToString().Contains("EmptyTypes") && method.Body.Instructions[i + 1].OpCode == OpCodes.Ldlen)
{
method.Body.Instructions[i].OpCode = OpCodes.Ldc_I4_0;
method.Body.Instructions[i + 1].OpCode = OpCodes.Nop;
EmptyTypesFixed++;
}
}
}
//This Method solve sizeof(X)
public static void SizeOfFixer(MethodDef method)
{
for (int i = 0; i < method.Body.Instructions.Count - 1; i++)
{
Instruction instr = method.Body.Instructions[i];
if (instr.OpCode == OpCodes.Sizeof)
{
Type SizeOfType = Type.GetType(instr.Operand.ToString());
if (SizeOfType != null)
{
instr.OpCode = OpCodes.Ldc_I4;
//See Here : https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/sizeof
instr.Operand = Marshal.SizeOf(SizeOfType);
FixedSizeOf++;
}
}
}
}
//This Method solve string.length
public static void StringsLengthFixer(MethodDef method)
{
for (int i = 1; i < method.Body.Instructions.Count - 1; i++)
{
if (method.Body.Instructions[i].OpCode == OpCodes.Ldstr && method.Body.Instructions[i + 1].OpCode == OpCodes.Call && method.Body.Instructions[i + 1].Operand.ToString().Contains("get_Length"))
{
string stringarg = (string)method.Body.Instructions[i].Operand;
int result = stringarg.Length;
method.Body.Instructions[i].OpCode = OpCodes.Ldc_I4;
method.Body.Instructions[i].Operand = result;
method.Body.Instructions[i + 1].OpCode = OpCodes.Nop;
StringsLengths++;
}
}
}
}
public static class Extensions
{
public static bool IsAdd(this Instruction op)
{
return op.OpCode == OpCodes.Add || op.OpCode == OpCodes.Add_Ovf || op.OpCode == OpCodes.Add_Ovf_Un;
}
public static bool IsSub(this Instruction op)
{
return op.OpCode == OpCodes.Sub || op.OpCode == OpCodes.Sub_Ovf || op.OpCode == OpCodes.Sub_Ovf_Un;
}
public static bool IsMul(this Instruction op)
{
return op.OpCode == OpCodes.Mul || op.OpCode == OpCodes.Mul_Ovf || op.OpCode == OpCodes.Mul_Ovf_Un;
}
public static bool IsDiv(this Instruction op)
{
return op.OpCode == OpCodes.Div || op.OpCode == OpCodes.Div_Un;
}
public static bool IsRem(this Instruction op)
{
return op.OpCode == OpCodes.Rem || op.OpCode == OpCodes.Rem_Un;
}
public static bool IsShr(this Instruction op)
{
return op.OpCode == OpCodes.Shr || op.OpCode == OpCodes.Shr_Un;
}
}
}