Jump to content
Tuts 4 You

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


Recommended Posts

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 comment

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;


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 comment

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