noob.exe Posted May 5, 2015 Posted May 5, 2015 Hey,How can I cast a byte[] into a methodbody by using dnlib?
XenocodeRCE Posted May 5, 2015 Posted May 5, 2015 (edited) If I were you I would use MethodBody.GetILAsByteArray (System.Reflection) Edited May 5, 2015 by SpoonStudio
0xd4d Posted May 5, 2015 Posted May 5, 2015 Hey, How can I cast a byte[] into a methodbody by using dnlib? You can't cast a byte[] to any other incompatible type. You must parse the bytes. There's a class for that already: https://github.com/0xd4d/dnlib/blob/master/src/DotNet/Emit/MethodBodyReader.cs#L42 If I were you I would use MethodBody.GetILAsByteArray (System.Reflection) He already has a byte[]. 2
noob.exe Posted May 5, 2015 Author Posted May 5, 2015 @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.. :/
0xd4d Posted May 6, 2015 Posted May 6, 2015 (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 May 6, 2015 by 0xd4d
Avenger Posted March 9, 2017 Posted March 9, 2017 (edited) how can i get MethodBody by MethodDef? Edited March 9, 2017 by Avenger
CodeExplorer Posted March 9, 2017 Posted March 9, 2017 https://github.com/0xd4d/dnlib/blob/master/src/DotNet/MethodDef.cs Dnlib has in MethodDef a property called MethodBody. public MDToken MDToken { get { return new MDToken(Table.Method, rid); } } This is how methods are identified, so you resolve the method using Module.ResolveMethod(int int metadataToken):https://msdn.microsoft.com/en-us/library/k16h33dz(v=vs.110).aspx 1
CodeExplorer Posted March 9, 2017 Posted March 9, 2017 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()); } 2
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now