How to overwrite specific bytes in a binary file w

2019-06-11 07:51发布

问题:

I want to overwrite bytes in an exe.

So I need to generate a random string, convert it, and then write it to the exe.

I need to overwrite the 4 hex strings you see there in this format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (8-4-4-4-12) the dashes are needed so that also was a problem for me.

this is the location of the first string.

I absolutely got no idea how to start this, how I can overwrite these 4 strings, in the correct format with random strings (hex, so the random can only be 0123456789abcdef)

any help is much appreciated.

回答1:

The string you want to overwrite is a GUID. You can use theGuid class to generate a new one (see the MSDN Documentation)

As for writing to the file, use the BinaryWriter class.

using (System.IO.BinaryWriter fileWriter = new System.IO.BinaryWriter(System.IO.File.Open("path", System.IO.FileMode.Open)))
{
    fileWriter.BaseStream.Position = 0xB8EB9; // set the offset
    fileWriter.Write(Encoding.ASCII.GetBytes(Guid.NewGuid().ToString()));
}

ideone sample