Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[DNLib] Method is not defined in this Module

Featured Replies

Posted

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

    }
}
 

 

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

  • Author
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

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.

Create an account or sign in to comment

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.