Jump to content
Tuts 4 You

(C# , DNLIB) How to replace a byte[] with dnlib?


Recommended Posts

Can you ülease help me with some code snippet or example on how to change a byte[] with dnlib or other libs ? 

thanks

Link to post
kao

Yes, we saw your previous topic, thank you. Honestly, I can't understand what you're trying to do. Perhaps you could explain it in more details ?

 

 

Link to post
Kurapica

maybe he wants to patch an existing array of bytes in some assembly ? using a hex editor ?

Link to post

thank you so far ,) 

i want to know how to change the value of a byte[] with dnlib ...

in assembly there is i.e this code :

byte[] data = { 1, 2, 4, 8, 16, 32 };

and i want to know how i can replace the array with my own values with dnlib or similar

i hope i could explain it better now 

Link to post
kao

Example code just to get you started:

            using (var module = ModuleDefMD.Load(args[0]))
            {
                foreach (var type in module.GetTypes())
                {
                    foreach (FieldDef field in type.Fields)
                    {
                        // this will change all byte[] field values to my own. Make sure to fix the `if`!!!
                        if (field.HasFieldRVA && field.InitialValue != null)
                        {
                            byte[] fake = new byte[] { 0x6B, 0x61, 0x6F };

                            // it's hard to change the size of initialized byte[]. If you really need to have array of different length 
                            // it will be much easier to create a new byte[] and use that instead
                            Array.Resize(ref fake, (int)field.GetFieldSize());  // this line forces new byte[] to be the same length as old one..
                            field.InitialValue = fake;
                        }
                    }
                }
                module.Write(args[1]);

 

You can see more details about how byte[] are implemented internally in my blog: https://lifeinhex.com/how-to-inject-byte-array-using-dnlib/

 

  • Like 2
Link to post

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