├── Plugin Base ├── How to use plugin.txt ├── Calculator.cs └── PluginBase.csproj ├── MindSystemCalculator.csproj ├── README.md └── Program.cs /Plugin Base/How to use plugin.txt: -------------------------------------------------------------------------------- 1 | Just add your code to the Execute method 2 | You can use add others method but the methods which will be executed is Execute and take as arg a MethodDef. 3 | Feel free to change that if you're able to ;) 4 | Then generate your dll and place it into the same directory as the calculator. 5 | 6 | If you encounter any issue, please contact me -------------------------------------------------------------------------------- /Plugin Base/Calculator.cs: -------------------------------------------------------------------------------- 1 | using dnlib.DotNet; 2 | using dnlib.DotNet.Emit; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace PluginBase 10 | { 11 | public class Calculator 12 | { 13 | public static void Execute(MethodDef method) 14 | { 15 | for (var i = 0; i < method.Body.Instructions.Count; i++) 16 | { 17 | //Your code :) 18 | } 19 | 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /MindSystemCalculator.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.0 6 | 7 | 8 | 9 | 10 | ..\..\..\..\Desktop\Reverse Engineering\Utils\de4dot.blocks.dll 11 | 12 | 13 | ..\..\..\..\Desktop\Reverse Engineering\Utils\dnlib.dll 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SuperCalculator 2 | Helpful tool which handle most operations used in obfuscation 3 | 4 | # Description 5 | 6 | Yet another calculator? so useless... 7 | I noticed a lot of sizeoffixer, math calculator, ... but none of them was complete so I decided to make mine 8 | 9 | # How to use plugin System ? 10 | 11 | Plugin system allows you to create your own part of code and to execute it from the calculator. Just use 12 | the base I put in the release and place it IN THE SAME directory as the calculator ! 13 | 14 | # Features 15 | 16 | * All sizeof 17 | * All Math.X 18 | * All X.Parse 19 | * Solve Type.EmptyType.Length 20 | * Solve all native msil operations (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/) 21 | * Solve Decimal.Compare 22 | * Solve string.Length 23 | 24 | Fell free to ask for a new feature or add it yourself... 25 | -------------------------------------------------------------------------------- /Plugin Base/PluginBase.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {A658FD94-E11C-46E2-B686-F252EEB95531} 8 | Library 9 | Properties 10 | PluginBase 11 | PluginBase 12 | v4.5 13 | 512 14 | true 15 | 16 | 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | pdbonly 27 | true 28 | bin\Release\ 29 | TRACE 30 | prompt 31 | 4 32 | 33 | 34 | 35 | ..\..\..\..\Desktop\Reverse Engineering\Utils\dnlib.dll 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /Program.cs: -------------------------------------------------------------------------------- 1 | 2 | using de4dot.blocks; 3 | using de4dot.blocks.cflow; 4 | using dnlib.DotNet; 5 | using dnlib.DotNet.Emit; 6 | using dnlib.DotNet.Writer; 7 | using System; 8 | using System.Collections.Generic; 9 | using System.Diagnostics; 10 | using System.IO; 11 | using System.Reflection; 12 | using System.Runtime.InteropServices; 13 | 14 | namespace MindSystemCalculator 15 | { 16 | class Program 17 | { 18 | public static int FixedSizeOf = 0; 19 | public static int EmptyTypesFixed = 0; 20 | public static int ParseFixed = 0; 21 | public static int MathsFixed = 0; 22 | public static int OperationFixed = 0; 23 | public static int StringsLengths = 0; 24 | public static int DecimalCompareFixed = 0; 25 | public static List ListPlugin = new List(); 26 | static void Main(string[] args) 27 | { 28 | Console.WriteLine(@" ___ _ _ _ "); 29 | Console.WriteLine(@" / __|__ _| |__ _ _| |__ _| |_ ___ _ _ "); 30 | Console.WriteLine(@"| (__/ _` | / _| || | / _` | _/ _ \ '_|"); 31 | Console.WriteLine(@" \___\__,_|_\__|\_,_|_\__,_|\__\___/_| "); 32 | Console.WriteLine(" "); 33 | Console.WriteLine(" - by MindSystem - "); 34 | Console.WriteLine(" "); 35 | ModuleDefMD module = null; 36 | try 37 | { 38 | module = ModuleDefMD.Load(args[0]); 39 | } 40 | catch 41 | { 42 | Console.WriteLine("Please load a correct module into the calculator"); 43 | } 44 | LoadPlugin(); 45 | 46 | Stopwatch stopWatch = new Stopwatch(); 47 | stopWatch.Start(); 48 | foreach (TypeDef type in module.Types) 49 | { 50 | foreach (MethodDef method in type.Methods) 51 | { 52 | if (method.HasBody && method.Body.HasInstructions) 53 | { 54 | try 55 | { 56 | SizeOfFixer(method); 57 | EmptyTypesFixer(method); 58 | Cleaner(method); 59 | UnParse(method); 60 | Cleaner(method); 61 | StringsLengthFixer(method); 62 | Cleaner(method); 63 | MathsFixer(method); 64 | Cleaner(method); 65 | if(ListPlugin.Count != 0) 66 | { 67 | foreach(MethodInfo meth in ListPlugin) 68 | { 69 | meth.Invoke(null, new object[] { method }); 70 | } 71 | } 72 | } 73 | catch (Exception ex) 74 | { 75 | Console.WriteLine("An error occured, please debug the tool to localize it"); 76 | Console.WriteLine("\n"); 77 | Console.WriteLine("------------------------------------------------------"); 78 | Console.WriteLine("\n"); 79 | Console.WriteLine(ex.ToString()); 80 | } 81 | 82 | } 83 | } 84 | } 85 | stopWatch.Stop(); 86 | Console.WriteLine("Done ! Elapsed time : " + stopWatch.Elapsed.TotalSeconds); 87 | string SavingPath = module.Kind == ModuleKind.Dll ? args[0].Replace(".dll", "-Deobfuscated.dll") : args[0].Replace(".exe", "-Deobfuscated.exe"); 88 | if (module.IsILOnly) 89 | { 90 | var opts = new ModuleWriterOptions(module); 91 | opts.MetadataOptions.Flags = MetadataFlags.PreserveAll; 92 | opts.Logger = DummyLogger.NoThrowInstance; 93 | module.Write(SavingPath, opts); 94 | } 95 | else 96 | { 97 | var opts = new NativeModuleWriterOptions(module, true); 98 | opts.MetadataOptions.Flags = MetadataFlags.PreserveAll; 99 | opts.Logger = DummyLogger.NoThrowInstance; 100 | module.NativeWrite(SavingPath, opts); 101 | } 102 | Console.WriteLine("[*] X.Parse Fixed :" + ParseFixed.ToString()); 103 | Console.WriteLine("[*] EmptyTypes Fixed :" + EmptyTypesFixed.ToString()); 104 | Console.WriteLine("[*] Sizeof Fixed :" + FixedSizeOf.ToString()); 105 | Console.WriteLine("[*] Math.X Fixed :" + MathsFixed.ToString()); 106 | Console.WriteLine("[*] Maths Operations Fixed :" + OperationFixed.ToString()); 107 | Console.WriteLine("[*] StringsLength Fixed :" + StringsLengths.ToString()); 108 | Console.WriteLine("[*] DecimalCompare Fixed :" + DecimalCompareFixed.ToString()); 109 | Console.ReadLine(); 110 | } 111 | 112 | 113 | //This Method solve Decimal.Compare 114 | public static void DecimalCompareFixer(MethodDef method) 115 | { 116 | for (int i = 4; i < method.Body.Instructions.Count - 1; i++) 117 | { 118 | 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()) 119 | { 120 | decimal first = method.Body.Instructions[i - 4].GetLdcI4Value(); 121 | decimal second = method.Body.Instructions[i - 2].GetLdcI4Value(); 122 | int result = decimal.Compare(first, second); 123 | method.Body.Instructions[i - 1].OpCode = OpCodes.Nop; 124 | method.Body.Instructions[i - 2].OpCode = OpCodes.Nop; 125 | method.Body.Instructions[i - 3].OpCode = OpCodes.Nop; 126 | method.Body.Instructions[i - 4].OpCode = OpCodes.Nop; 127 | method.Body.Instructions[i].OpCode = OpCodes.Ldc_I4; 128 | method.Body.Instructions[i].Operand = result; 129 | DecimalCompareFixed++; 130 | } 131 | } 132 | } 133 | public static void LoadPlugin() 134 | { 135 | List allAssemblies = new List(); 136 | string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); 137 | 138 | foreach (string dll in Directory.GetFiles(path, "*.dll")) 139 | { 140 | if (dll.Contains("Plugin")) 141 | { 142 | allAssemblies.Add(Assembly.LoadFile(dll)); 143 | Console.WriteLine("Loaded plugin : " + Path.GetFileName(dll)); 144 | } 145 | 146 | } 147 | if (allAssemblies.Count == 0) 148 | return; 149 | foreach(Assembly asm in allAssemblies) 150 | { 151 | foreach(Type type in asm.GetTypes()) 152 | { 153 | if(type.Name == "Calculator") 154 | { 155 | MethodInfo meth = type.GetMethod("Execute"); 156 | ListPlugin.Add(meth); 157 | } 158 | } 159 | 160 | } 161 | } 162 | //This Method Replace Math.X and solve c# native operations 163 | public static void MathsFixer(MethodDef method) 164 | { 165 | for (int i = 0; i < method.Body.Instructions.Count - 1; i++) 166 | { 167 | 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")) 168 | { 169 | MemberRef MathMethod = (MemberRef)method.Body.Instructions[i+1].Operand; 170 | double argument = (double)method.Body.Instructions[i].Operand; 171 | MethodInfo methodInfo = typeof(Math).GetMethod(MathMethod.Name, new System.Type[] { typeof(double) }); 172 | double result = (double)methodInfo.Invoke(null, new object[] { argument }); 173 | method.Body.Instructions[i + 1].OpCode = OpCodes.Nop; 174 | method.Body.Instructions[i].Operand = result; 175 | MathsFixed++; 176 | } 177 | 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")) 178 | { 179 | MemberRef MathMethod = (MemberRef)method.Body.Instructions[i + 1].Operand; 180 | float argument = (float)method.Body.Instructions[i].Operand; 181 | MethodInfo methodInfo = typeof(Math).GetMethod(MathMethod.Name, new System.Type[] { typeof(float) }); 182 | float result = (float)methodInfo.Invoke(null, new object[] { argument }); 183 | method.Body.Instructions[i + 1].OpCode = OpCodes.Nop; 184 | method.Body.Instructions[i].Operand = result; 185 | MathsFixed++; 186 | } 187 | 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")) 188 | { 189 | MemberRef MathMethod = (MemberRef)method.Body.Instructions[i + 1].Operand; 190 | int argument = (int)method.Body.Instructions[i].Operand; 191 | MethodInfo methodInfo = typeof(Math).GetMethod(MathMethod.Name, new System.Type[] { typeof(int) }); 192 | int result = (int)methodInfo.Invoke(null, new object[] { argument }); 193 | method.Body.Instructions[i + 1].OpCode = OpCodes.Nop; 194 | method.Body.Instructions[i].Operand = result; 195 | MathsFixed++; 196 | } 197 | 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")) 198 | { 199 | MemberRef MathMethod = (MemberRef)method.Body.Instructions[i + 2].Operand; 200 | double argument = (double)method.Body.Instructions[i].Operand; 201 | MethodInfo methodInfo = typeof(Math).GetMethod(MathMethod.Name, new System.Type[] { typeof(double) }); 202 | double result = (double)methodInfo.Invoke(null, new object[] { argument }); 203 | method.Body.Instructions[i + 2].OpCode = OpCodes.Nop; 204 | method.Body.Instructions[i + 1].OpCode = OpCodes.Nop; 205 | method.Body.Instructions[i].Operand = result; 206 | MathsFixed++; 207 | } 208 | 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")) 209 | { 210 | MemberRef MathMethod = (MemberRef)method.Body.Instructions[i + 2].Operand; 211 | float argument = (float)method.Body.Instructions[i].Operand; 212 | MethodInfo methodInfo = typeof(Math).GetMethod(MathMethod.Name, new System.Type[] { typeof(float) }); 213 | float result = (float)methodInfo.Invoke(null, new object[] { argument }); 214 | method.Body.Instructions[i + 2].OpCode = OpCodes.Nop; 215 | method.Body.Instructions[i + 1].OpCode = OpCodes.Nop; 216 | method.Body.Instructions[i].Operand = result; 217 | MathsFixed++; 218 | } 219 | 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")) 220 | { 221 | MemberRef MathMethod = (MemberRef)method.Body.Instructions[i + 2].Operand; 222 | int argument = (int)method.Body.Instructions[i].Operand; 223 | MethodInfo methodInfo = typeof(Math).GetMethod(MathMethod.Name, new System.Type[] { typeof(int) }); 224 | int result = (int)methodInfo.Invoke(null, new object[] { argument }); 225 | method.Body.Instructions[i + 2].OpCode = OpCodes.Nop; 226 | method.Body.Instructions[i + 1].OpCode = OpCodes.Nop; 227 | method.Body.Instructions[i].Operand = result; 228 | MathsFixed++; 229 | } 230 | else if (method.Body.Instructions[i].IsSub() && method.Body.Instructions[i-1].IsLdcI4() && method.Body.Instructions[i -2].IsLdcI4()) 231 | { 232 | int firstarg = method.Body.Instructions[i - 2].GetLdcI4Value(); 233 | int secondarg = method.Body.Instructions[i - 1].GetLdcI4Value(); 234 | int result = firstarg - secondarg; 235 | method.Body.Instructions[i-1].Operand = result; 236 | method.Body.Instructions[i].OpCode = OpCodes.Nop; 237 | method.Body.Instructions[i -2].OpCode = OpCodes.Nop; 238 | OperationFixed++; 239 | } 240 | else if (method.Body.Instructions[i].IsMul() && method.Body.Instructions[i - 1].IsLdcI4() && method.Body.Instructions[i - 2].IsLdcI4()) 241 | { 242 | int firstarg = method.Body.Instructions[i - 2].GetLdcI4Value(); 243 | int secondarg = method.Body.Instructions[i - 1].GetLdcI4Value(); 244 | int result = firstarg * secondarg; 245 | method.Body.Instructions[i - 1].Operand = result; 246 | method.Body.Instructions[i].OpCode = OpCodes.Nop; 247 | method.Body.Instructions[i - 2].OpCode = OpCodes.Nop; 248 | OperationFixed++; 249 | } 250 | else if (method.Body.Instructions[i].IsDiv() && method.Body.Instructions[i - 1].IsLdcI4() && method.Body.Instructions[i - 2].IsLdcI4()) 251 | { 252 | int firstarg = method.Body.Instructions[i - 2].GetLdcI4Value(); 253 | int secondarg = method.Body.Instructions[i - 1].GetLdcI4Value(); 254 | int result = firstarg / secondarg; 255 | method.Body.Instructions[i - 1].Operand = result; 256 | method.Body.Instructions[i].OpCode = OpCodes.Nop; 257 | method.Body.Instructions[i - 2].OpCode = OpCodes.Nop; 258 | OperationFixed++; 259 | } 260 | else if (method.Body.Instructions[i].OpCode == OpCodes.Xor && method.Body.Instructions[i - 1].IsLdcI4() && method.Body.Instructions[i - 2].IsLdcI4()) 261 | { 262 | int firstarg = method.Body.Instructions[i - 2].GetLdcI4Value(); 263 | int secondarg = method.Body.Instructions[i - 1].GetLdcI4Value(); 264 | int result = firstarg ^ secondarg; 265 | method.Body.Instructions[i - 1].Operand = result; 266 | method.Body.Instructions[i].OpCode = OpCodes.Nop; 267 | method.Body.Instructions[i - 2].OpCode = OpCodes.Nop; 268 | OperationFixed++; 269 | } 270 | else if (method.Body.Instructions[i].IsSub() && method.Body.Instructions[i - 1].IsRem() && method.Body.Instructions[i - 2].IsLdcI4()) 271 | { 272 | int firstarg = method.Body.Instructions[i - 2].GetLdcI4Value(); 273 | int secondarg = method.Body.Instructions[i - 1].GetLdcI4Value(); 274 | int result = firstarg ^ secondarg; 275 | method.Body.Instructions[i - 1].Operand = result; 276 | method.Body.Instructions[i].OpCode = OpCodes.Nop; 277 | method.Body.Instructions[i - 2].OpCode = OpCodes.Nop; 278 | OperationFixed++; 279 | } 280 | else if (method.Body.Instructions[i].OpCode == OpCodes.Shl && method.Body.Instructions[i - 1].IsLdcI4() && method.Body.Instructions[i - 2].IsLdcI4()) 281 | { 282 | int firstarg = method.Body.Instructions[i - 2].GetLdcI4Value(); 283 | int secondarg = method.Body.Instructions[i - 1].GetLdcI4Value(); 284 | int result = firstarg << secondarg; 285 | method.Body.Instructions[i - 1].Operand = result; 286 | method.Body.Instructions[i].OpCode = OpCodes.Nop; 287 | method.Body.Instructions[i - 2].OpCode = OpCodes.Nop; 288 | OperationFixed++; 289 | } 290 | else if (method.Body.Instructions[i].IsShr() && method.Body.Instructions[i - 1].IsRem() && method.Body.Instructions[i - 2].IsLdcI4()) 291 | { 292 | int firstarg = method.Body.Instructions[i - 2].GetLdcI4Value(); 293 | int secondarg = method.Body.Instructions[i - 1].GetLdcI4Value(); 294 | int result = firstarg >> secondarg; 295 | method.Body.Instructions[i - 1].Operand = result; 296 | method.Body.Instructions[i].OpCode = OpCodes.Nop; 297 | method.Body.Instructions[i - 2].OpCode = OpCodes.Nop; 298 | OperationFixed++; 299 | } 300 | else if (method.Body.Instructions[i].OpCode == OpCodes.And && method.Body.Instructions[i - 1].IsLdcI4() && method.Body.Instructions[i - 2].IsLdcI4()) 301 | { 302 | int firstarg = method.Body.Instructions[i - 2].GetLdcI4Value(); 303 | int secondarg = method.Body.Instructions[i - 1].GetLdcI4Value(); 304 | int result = firstarg & secondarg; 305 | method.Body.Instructions[i - 1].Operand = result; 306 | method.Body.Instructions[i].OpCode = OpCodes.Nop; 307 | method.Body.Instructions[i - 2].OpCode = OpCodes.Nop; 308 | OperationFixed++; 309 | } 310 | else if (method.Body.Instructions[i].OpCode == OpCodes.Or && method.Body.Instructions[i - 1].IsLdcI4() && method.Body.Instructions[i - 2].IsLdcI4()) 311 | { 312 | int firstarg = method.Body.Instructions[i - 2].GetLdcI4Value(); 313 | int secondarg = method.Body.Instructions[i - 1].GetLdcI4Value(); 314 | int result = firstarg | secondarg; 315 | method.Body.Instructions[i - 1].Operand = result; 316 | method.Body.Instructions[i].OpCode = OpCodes.Nop; 317 | method.Body.Instructions[i - 2].OpCode = OpCodes.Nop; 318 | OperationFixed++; 319 | } 320 | } 321 | } 322 | public static void Cleaner(MethodDef method) 323 | { 324 | 325 | BlocksCflowDeobfuscator blocksCflowDeobfuscator = new BlocksCflowDeobfuscator(); 326 | Blocks blocks = new Blocks(method); 327 | blocksCflowDeobfuscator.Initialize(blocks); 328 | blocksCflowDeobfuscator.Deobfuscate(); 329 | blocks.RepartitionBlocks(); 330 | IList list; 331 | IList exceptionHandlers; 332 | blocks.GetCode(out list, out exceptionHandlers); 333 | DotNetUtils.RestoreBody(method, list, exceptionHandlers); 334 | } 335 | //This Method Replace X.Parse 336 | public static void UnParse(MethodDef method) 337 | { 338 | for (int i = 1; i < method.Body.Instructions.Count - 1; i++) 339 | { 340 | if (method.Body.Instructions[i].OpCode == OpCodes.Call && method.Body.Instructions[i].Operand.ToString().Contains("Parse") && method.Body.Instructions[i - 1].OpCode == OpCodes.Ldstr) 341 | { 342 | MemberRef Parse = (MemberRef)method.Body.Instructions[i].Operand; 343 | if (Parse.DeclaringType.Name.Contains("Int32")) 344 | { 345 | int result = int.Parse(method.Body.Instructions[i - 1].Operand.ToString()); 346 | method.Body.Instructions[i].OpCode = OpCodes.Ldc_I4; 347 | method.Body.Instructions[i].Operand = result; 348 | method.Body.Instructions[i - 1].OpCode = OpCodes.Nop; 349 | ParseFixed++; 350 | } 351 | else if (Parse.DeclaringType.Name.Contains("Single")) 352 | { 353 | float result = float.Parse(method.Body.Instructions[i - 1].Operand.ToString()); 354 | method.Body.Instructions[i].OpCode = OpCodes.Ldc_R4; 355 | method.Body.Instructions[i].Operand = result; 356 | method.Body.Instructions[i - 1].OpCode = OpCodes.Nop; 357 | ParseFixed++; 358 | } 359 | else if (Parse.DeclaringType.Name.Contains("Int64")) 360 | { 361 | long result = long.Parse(method.Body.Instructions[i - 1].Operand.ToString()); 362 | method.Body.Instructions[i].OpCode = OpCodes.Ldc_I8; 363 | method.Body.Instructions[i].Operand = result; 364 | method.Body.Instructions[i - 1].OpCode = OpCodes.Nop; 365 | ParseFixed++; 366 | } 367 | else if (Parse.DeclaringType.Name.Contains("Double")) 368 | { 369 | double result = double.Parse(method.Body.Instructions[i - 1].Operand.ToString()); 370 | method.Body.Instructions[i].OpCode = OpCodes.Ldc_R8; 371 | method.Body.Instructions[i].Operand = result; 372 | method.Body.Instructions[i - 1].OpCode = OpCodes.Nop; 373 | ParseFixed++; 374 | } 375 | else if (Parse.DeclaringType.Name.Contains("Decimal")) 376 | { 377 | Decimal result = Decimal.Parse(method.Body.Instructions[i - 1].Operand.ToString()); 378 | method.Body.Instructions[i].OpCode = OpCodes.Ldc_R4; 379 | method.Body.Instructions[i].Operand = (float)result; 380 | method.Body.Instructions[i - 1].OpCode = OpCodes.Nop; 381 | ParseFixed++; 382 | } 383 | else if (Parse.DeclaringType.Name.Contains("UInt32")) 384 | { 385 | uint result = uint.Parse(method.Body.Instructions[i - 1].Operand.ToString()); 386 | method.Body.Instructions[i].OpCode = OpCodes.Ldc_I4; 387 | method.Body.Instructions[i].Operand = (int)result; 388 | method.Body.Instructions.Add(OpCodes.Conv_U4.ToInstruction()); 389 | method.Body.Instructions[i - 1].OpCode = OpCodes.Nop; 390 | ParseFixed++; 391 | } 392 | else if (Parse.DeclaringType.Name.Contains("UInt64")) 393 | { 394 | ulong result = ulong.Parse(method.Body.Instructions[i - 1].Operand.ToString()); 395 | method.Body.Instructions[i].OpCode = OpCodes.Ldc_I8; 396 | method.Body.Instructions[i].Operand = (long)result; 397 | method.Body.Instructions.Add(OpCodes.Conv_U8.ToInstruction()); 398 | method.Body.Instructions[i - 1].OpCode = OpCodes.Nop; 399 | ParseFixed++; 400 | } 401 | else if (Parse.DeclaringType.Name.Contains("Int16")) 402 | { 403 | short result = short.Parse(method.Body.Instructions[i - 1].Operand.ToString()); 404 | method.Body.Instructions[i].OpCode = OpCodes.Ldc_I4; 405 | method.Body.Instructions[i].Operand = (int)result; 406 | method.Body.Instructions.Add(OpCodes.Conv_I2.ToInstruction()); 407 | method.Body.Instructions[i - 1].OpCode = OpCodes.Nop; 408 | ParseFixed++; 409 | } 410 | else if (Parse.DeclaringType.Name.Contains("UInt16")) 411 | { 412 | ushort result = ushort.Parse(method.Body.Instructions[i - 1].Operand.ToString()); 413 | method.Body.Instructions[i].OpCode = OpCodes.Ldc_I4; 414 | method.Body.Instructions[i].Operand = (int)result; 415 | method.Body.Instructions.Add(OpCodes.Conv_U2.ToInstruction()); 416 | method.Body.Instructions[i - 1].OpCode = OpCodes.Nop; 417 | ParseFixed++; 418 | } 419 | } 420 | } 421 | } 422 | //This Method Remove size.EmptyType 423 | public static void EmptyTypesFixer(MethodDef method) 424 | { 425 | for (int i = 1; i < method.Body.Instructions.Count - 1; i++) 426 | { 427 | if (method.Body.Instructions[i].OpCode == OpCodes.Ldsfld && method.Body.Instructions[i].Operand.ToString().Contains("EmptyTypes") && method.Body.Instructions[i + 1].OpCode == OpCodes.Ldlen) 428 | { 429 | method.Body.Instructions[i].OpCode = OpCodes.Ldc_I4_0; 430 | method.Body.Instructions[i + 1].OpCode = OpCodes.Nop; 431 | EmptyTypesFixed++; 432 | } 433 | } 434 | } 435 | //This Method solve sizeof(X) 436 | public static void SizeOfFixer(MethodDef method) 437 | { 438 | for (int i = 0; i < method.Body.Instructions.Count - 1; i++) 439 | { 440 | Instruction instr = method.Body.Instructions[i]; 441 | if (instr.OpCode == OpCodes.Sizeof) 442 | { 443 | Type SizeOfType = Type.GetType(instr.Operand.ToString()); 444 | if (SizeOfType != null) 445 | { 446 | instr.OpCode = OpCodes.Ldc_I4; 447 | //See Here : https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/sizeof 448 | instr.Operand = Marshal.SizeOf(SizeOfType); 449 | FixedSizeOf++; 450 | } 451 | 452 | } 453 | } 454 | } 455 | //This Method solve string.length 456 | public static void StringsLengthFixer(MethodDef method) 457 | { 458 | for (int i = 1; i < method.Body.Instructions.Count - 1; i++) 459 | { 460 | 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")) 461 | { 462 | string stringarg = (string)method.Body.Instructions[i].Operand; 463 | int result = stringarg.Length; 464 | method.Body.Instructions[i].OpCode = OpCodes.Ldc_I4; 465 | method.Body.Instructions[i].Operand = result; 466 | method.Body.Instructions[i + 1].OpCode = OpCodes.Nop; 467 | StringsLengths++; 468 | } 469 | } 470 | } 471 | } 472 | public static class Extensions 473 | { 474 | public static bool IsAdd(this Instruction op) 475 | { 476 | return op.OpCode == OpCodes.Add || op.OpCode == OpCodes.Add_Ovf || op.OpCode == OpCodes.Add_Ovf_Un; 477 | } 478 | public static bool IsSub(this Instruction op) 479 | { 480 | return op.OpCode == OpCodes.Sub || op.OpCode == OpCodes.Sub_Ovf || op.OpCode == OpCodes.Sub_Ovf_Un; 481 | } 482 | public static bool IsMul(this Instruction op) 483 | { 484 | return op.OpCode == OpCodes.Mul || op.OpCode == OpCodes.Mul_Ovf || op.OpCode == OpCodes.Mul_Ovf_Un; 485 | } 486 | public static bool IsDiv(this Instruction op) 487 | { 488 | return op.OpCode == OpCodes.Div || op.OpCode == OpCodes.Div_Un; 489 | } 490 | public static bool IsRem(this Instruction op) 491 | { 492 | return op.OpCode == OpCodes.Rem || op.OpCode == OpCodes.Rem_Un; 493 | } 494 | public static bool IsShr(this Instruction op) 495 | { 496 | return op.OpCode == OpCodes.Shr || op.OpCode == OpCodes.Shr_Un; 497 | } 498 | } 499 | } 500 | --------------------------------------------------------------------------------