Jump to content
Tuts 4 You

Cast byte[] to methodbody


noob.exe

Recommended Posts

Posted

Hey,


How can I cast a byte[] into a methodbody by using dnlib?


XenocodeRCE
Posted (edited)

If I were you I would use MethodBody.GetILAsByteArray (System.Reflection)


Edited by SpoonStudio
XenocodeRCE
Posted

My bad.


Posted

@0xd4d : I tried understanding that part of dnlib but I have a bit of trouble.

Could you probably provide an example snippet?

Not that I want to c&p, - but its quiet hard to understand how to use the MethodBodyReader to convert a byte[] to a MethodBody.. :/

Posted (edited)

It assumes you have some basic .NET metadata knowledge. Check de4dot source code if you want to see code calling it. Where? Find All References in VS and you'll find it.


Edited by 0xd4d
  • 1 year later...
Posted (edited)

how can i get MethodBody by MethodDef?

Edited by Avenger
CodeExplorer
Posted
On 5/6/2015 at 0:33 AM, yq8 said:

@0xd4d : I tried understanding that part of dnlib but I have a bit of trouble.

Could you probably provide an example snippet?

Not that I want to c&p, - but its quiet hard to understand how to use the MethodBodyReader to convert a byte[] to a MethodBody.. :/

https://github.com/0xd4d/dnlib/blob/master/src/DotNet/Emit/MethodBody.cs
namespace dnlib.DotNet.Emit {
    /// <summary>
    /// Method body base class
    /// </summary>
    public abstract class MethodBody {
}

Wired abstact classs MethodBody: I mean it has no methods!

    /// <summary>
    /// CIL (managed code) body
    /// </summary>
public sealed class CilBody : MethodBody

This static method is important:
         /// <summary>
        /// Creates a CIL method body or returns an empty one if <paramref name="code"/> is not
        /// a valid CIL method body.
        /// </summary>
        /// <param name="opResolver">The operand resolver</param>
        /// <param name="code">All code</param>
        /// <param name="exceptions">Exceptions or <c>null</c> if all exception handlers are in
        /// <paramref name="code"/></param>
        /// <param name="parameters">Method parameters</param>
        /// <param name="flags">Method header flags, eg. 2 if tiny method</param>
        /// <param name="maxStack">Max stack</param>
        /// <param name="codeSize">Code size</param>
        /// <param name="localVarSigTok">Local variable signature token or 0 if none</param>
        public static CilBody CreateCilBody(IInstructionOperandResolver opResolver, byte[] code, byte[] exceptions, IList<Parameter> parameters, ushort flags, ushort maxStack, uint codeSize, uint localVarSigTok) {
            return CreateCilBody(opResolver, code, exceptions, parameters, flags, maxStack, codeSize, localVarSigTok, new GenericParamContext());
}

MethodBodyReader also contain some statics methods "public static CilBody CreateCilBody"

ModuleDef contains this for resolving tokens:

        /// <summary>
        /// Resolves a token
        /// </summary>
        /// <param name="mdToken">The metadata token</param>
        /// <returns>A <see cref="IMDTokenProvider"/> or <c>null</c> if <paramref name="mdToken"/> is invalid</returns>
        public IMDTokenProvider ResolveToken(MDToken mdToken) {
            return ResolveToken(mdToken.Raw, new GenericParamContext());
        }

 

  • Like 2

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