Jump to content
Tuts 4 You

[DNLib] Method is not defined in this Module


Leafy

Recommended Posts

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;
        }

    }
}
 

 

Link to comment

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 by kao
Link to comment
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 by Leafy
Link to comment

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.

Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...