How to overwrite specific bytes in a binary file with C#?

北城以北 提交于 2019-12-06 14:54:37

问题


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



来源:https://stackoverflow.com/questions/14538620/how-to-overwrite-specific-bytes-in-a-binary-file-with-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!