MIra Posted April 10, 2021 Posted April 10, 2021 Can you ülease help me with some code snippet or example on how to change a byte[] with dnlib or other libs ? thanks
kao Posted April 10, 2021 Posted April 10, 2021 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 ?
Kurapica Posted April 10, 2021 Posted April 10, 2021 maybe he wants to patch an existing array of bytes in some assembly ? using a hex editor ?
DorAEm0nKiNG-TH Posted April 10, 2021 Posted April 10, 2021 (edited) If you are meaning to modifying the byte array in some assemblies just use FileStream https://stackoverflow.com/a/3217953/8902883 Edited April 10, 2021 by DorAEm0nKiNG-TH
MIra Posted April 10, 2021 Author Posted April 10, 2021 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
kao Posted April 11, 2021 Posted April 11, 2021 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/ 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