Jump to content
Tuts 4 You

[DNLib] Method is not defined in this Module


Leafy

Recommended Posts

Posted (edited)

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

    }
}
using dnlib.DotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeafObfuscator.Protection
{
//Injecter
    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

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...