-
Notifications
You must be signed in to change notification settings - Fork 5
/
Program.cs
80 lines (71 loc) · 3.2 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
namespace UniversalMaths
{
internal class Program
{
public static void Main(string[] args)
{
var moduleDefMd = ModuleDefMD.Load(args[0]);
var allMethods = LoadMethods();
foreach (var typeDef in moduleDefMd.GetTypes()
.Where(x => x.HasMethods))
{
foreach (var methodDef in typeDef.Methods
.Where(x => x.HasBody))
{
var instructions = methodDef.Body.Instructions;
for (int i = 0; i < instructions.Count; i++)
{
if (instructions[i].OpCode == OpCodes.Call
&& instructions[i - 1].OpCode == OpCodes.Ldc_R8
&& instructions[i].Operand.ToString().Contains("Math::"))
{
var memberRef = instructions[i].Operand as MemberRef;
if (allMethods.Any(x =>
x.Item1 == memberRef.Name))
{
var resultMethod = allMethods.Find(x =>
x.Item2.ToString() == memberRef.Signature.ToString()
.Replace("System.", string.Empty)
.Replace(" ", $" {memberRef.Name}")).Item2;
if (resultMethod != null)
{
var invokedValue = resultMethod.Invoke(null,
new object[] {(double) instructions[i - 1].Operand});
instructions[i].OpCode = OpCodes.Ldc_R8;
instructions[i].Operand = invokedValue;
instructions[i - 1].OpCode = OpCodes.Nop;
Console.WriteLine($"[+] Math::{resultMethod.Name} returned: {invokedValue}");
}
}
}
}
}
}
moduleDefMd.Write(Path.GetDirectoryName(args[0]) + "\\" +
Path.GetFileNameWithoutExtension(args[0]) +
"_noMaths" + Path.GetExtension(args[0]));
Console.WriteLine($"[+] Saved!");
Console.ReadKey(true);
}
static List<Tuple<string, MethodInfo>> LoadMethods()
{
var sortedList = new List<Tuple<string, MethodInfo>>();
var allMethods = typeof(Math).GetMethods()
.Where(x =>
x.Name != "GetHashCode" && x.Name != "GetType"
&& x.Name != "ToString"
&& x.Name != "Equals")
.ToList();
foreach (var methodInfo in allMethods)
sortedList.Add(new Tuple<string, MethodInfo>(methodInfo.Name, methodInfo));
return sortedList;
}
}
}