Leafy Posted February 3, 2018 Posted February 3, 2018 If i try to insert a call after a Instruction im getting a Method is not defined in this Module Exception using dnlib.DotNet; using dnlib.DotNet.Emit; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace LeafObfuscator.Protection { //TODO: Fix class Int32 { public static void encode(ModuleDefMD md) { Inject.injectmethods(md,Int(md)); foreach (TypeDef type in md.Types) { foreach (MethodDef method in type.Methods) { CilBody body = method.Body; if (!method.HasBody) { continue; } for (int i = 0; i < body.Instructions.Count; i++) { if (method.Name != "dec") { if (body.Instructions[i].OpCode == OpCodes.Ldc_I4) { body.Instructions[i].Operand = Convert.ToInt32(body.Instructions[i].Operand) - 10; body.Instructions.Insert(i + 1, Instruction.Create(OpCodes.Call, Int(md))); } } } } } } private static MethodDef Int(ModuleDefMD md) { MethodImplAttributes methImplFlags = MethodImplAttributes.IL | MethodImplAttributes.Managed; MethodAttributes methFlags = MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.HideBySig | MethodAttributes.ReuseSlot; MethodDef inthider = new MethodDefUser("dec", MethodSig.CreateStatic(md.CorLibTypes.Int32, md.CorLibTypes.Int32), methImplFlags, methFlags); CilBody body = new CilBody(); inthider.Body = body; body.Instructions.Add(Instruction.Create(OpCodes.Ldc_I4, 10)); body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); body.Instructions.Add(Instruction.Create(OpCodes.Add)); body.Instructions.Add(Instruction.Create(OpCodes.Ret)); return inthider; } } }
kao Posted February 3, 2018 Posted February 3, 2018 (edited) You did not add your method to the appropriate class (main module in your case). See dnlib\Examples\Example2.cs, line 35. EDIT: your coding is terrible. You added one method to the module in the very beginning. But then you call "Int(md)" again in the loop - which creates a new method every time. And these new methods are not added to any class. Edited February 3, 2018 by kao
Leafy Posted February 3, 2018 Author Posted February 3, 2018 (edited) 18 minutes ago, kao said: You did not add your method to the appropriate class (main module in your case). See dnlib\Examples\Example2.cs, line 35. EDIT: your coding is terrible. You added one method to the module in the very beginning. But then you call "Int(md)" again in the loop - which creates a new method every time. And these new methods are not added to any class. My injecter injects the method into <Module> class Inject { public static MethodDef injectmethods(ModuleDefMD md, MethodDef method) { foreach (TypeDef type in md.Types) { if (type.Name == "<Module>") { MethodDef decmethod = method; type.Methods.Add(decmethod); return decmethod; } } throw new Exception("failed"); } } } Edited February 3, 2018 by Leafy
Etor Madiv Posted February 5, 2018 Posted February 5, 2018 A little fix would be to not create a new dec every time you encouter an ldc.i4 opcode, you create the dec method just one time and then reuse it across the module public static void encode(ModuleDefMD md) { MethodDef intDecoder = Int(md); //Create just one dec method and reuse it. Inject.injectmethods(md, intDecoder); foreach (TypeDef type in md.Types) { foreach (MethodDef method in type.Methods) { CilBody body = method.Body; if (!method.HasBody) { continue; } for (int i = 0; i < body.Instructions.Count; i++) { if (method.Name != "dec") { if (body.Instructions[i].OpCode == OpCodes.Ldc_I4) { body.Instructions[i].Operand = Convert.ToInt32(body.Instructions[i].Operand) - 10; body.Instructions.Insert(i + 1, Instruction.Create(OpCodes.Call, intDecoder)); //Do not create a new one every time, they are not the same. } } } } } } You should also note that not all int values will get affected by your code, you should consider supporting ldc.i4.x opcodes in addition.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now