Code Monkey home page Code Monkey logo

Comments (1)

 avatar commented on July 25, 2024

I wrote some code that tests this and peverify complains if caller's method signature doesn't include the 0 lower bounds that matches the called method's 0 lower bounds.

The reason that the code you're investigating might work is that it seems from your info above that it's a Babel.NET obfuscated assembly. The obfuscator resolves the correct method and then creates the correct code.

C:\arytest>peverify theexe.exe

Microsoft (R) .NET Framework PE Verifier.  Version  4.0.30319.0
Copyright (c) Microsoft Corporation.  All rights reserved.

[IL]: Error: [C:\arytest\theexe.exe : main.Start::Method][offset 0x00000001] Unable to resolve token.
1 Error(s) Verifying theexe.exe

C:\arytest>theexe.exe

Unhandled Exception: System.MissingMethodException: Method not found: 'Int32 ns.Class1.Method(Double[,])'.
   at main.Start.Method()

Fixing the signature results in everything working again:

C:\arytest>theexe.exe

C:\arytest>peverify theexe.exe

Microsoft (R) .NET Framework PE Verifier.  Version  4.0.30319.0
Copyright (c) Microsoft Corporation.  All rights reserved.

All Classes and Methods in theexe.exe Verified.

Here's the code I used:

using dnlib.DotNet;
using dnlib.DotNet.Emit;

....

            {
                var mod = new ModuleDefUser("themod", Guid.NewGuid());
                var asm = new AssemblyDefUser("themod", new Version(0, 0, 0, 0));
                asm.Modules.Add(mod);
                mod.Kind = ModuleKind.Dll;

                var cl = new TypeDefUser("ns", "Class1", mod.CorLibTypes.Object.TypeDefOrRef);
                mod.Types.Add(cl);
                cl.Visibility = TypeAttributes.Public;

                var arg1 = new ArraySig(mod.CorLibTypes.Double, 2);
                arg1.LowerBounds.Add(0);
                arg1.LowerBounds.Add(0);
                var meth = new MethodDefUser("Method", MethodSig.CreateStatic(mod.CorLibTypes.Int32, arg1),
                    MethodImplAttributes.IL | MethodImplAttributes.Managed,
                    MethodAttributes.Public | MethodAttributes.Static);
                cl.Methods.Add(meth);
                var body = new CilBody();
                meth.Body = body;
                body.Instructions.Add(OpCodes.Ldc_I4_3.ToInstruction());
                body.Instructions.Add(OpCodes.Ret.ToInstruction());

                mod.Write(@"C:\arytest\themod.dll");
            }
            {
                var mod = new ModuleDefUser("theexe", Guid.NewGuid());
                var asm = new AssemblyDefUser("theexe", new Version(0, 0, 0, 0));
                asm.Modules.Add(mod);
                mod.Kind = ModuleKind.Console;

                var cl = new TypeDefUser("main", "Start", mod.CorLibTypes.Object.TypeDefOrRef);
                mod.Types.Add(cl);

                var asmRef = new AssemblyRefUser("themod", new Version(0, 0, 0, 0));
                var tr = new TypeRefUser(mod, "ns", "Class1", asmRef);
                var arg1 = new ArraySig(mod.CorLibTypes.Double, 2);
// Comment these and the program will fail because the correct method can't be found
                arg1.LowerBounds.Add(0);
                arg1.LowerBounds.Add(0);
                var mr = new MemberRefUser(mod, "Method", MethodSig.CreateStatic(mod.CorLibTypes.Int32, arg1), tr);

                var meth = new MethodDefUser("Method", MethodSig.CreateStatic(mod.CorLibTypes.Void),
                    MethodImplAttributes.IL | MethodImplAttributes.Managed,
                    MethodAttributes.Public | MethodAttributes.Static);
                cl.Methods.Add(meth);
                mod.EntryPoint = meth;
                var body = new CilBody();
                meth.Body = body;
                body.Instructions.Add(OpCodes.Ldnull.ToInstruction());
                body.Instructions.Add(OpCodes.Call.ToInstruction(mr));
                body.Instructions.Add(OpCodes.Pop.ToInstruction());
                body.Instructions.Add(OpCodes.Ret.ToInstruction());

                mod.Write(@"C:\arytest\theexe.exe");
            }

from dnlib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.