0

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.

hexdump

attributes screenshot

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.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Bert Pauwels
  • 9
  • 1
  • 3
  • 3
    well, how much of do you know about c# and .net and streams in general? the idea is, create a file stream in open/write mode, seek to the position where you want to write, write the random bytes (NOT string, raw bytes), flush and close the file stream. what part are you stuck at, explicitly? – Can Poyrazoğlu Jan 26 '13 at 15:59
  • 1
    Looks like a GUID format too for the string. – Lloyd Jan 26 '13 at 16:00
  • 1
    You aren't trying to hack something, are you? – Sergey Kalinichenko Jan 26 '13 at 16:03
  • yea it's a guid and I want to be able to change it. I tried to randomise a string, then convert to hex, then write it but it failed converting it correctly, so you suggest writing byte per byte. in this kinda format if (i, i>x,i++) ? – Bert Pauwels Jan 26 '13 at 16:09
  • Possible duplicate with: http://stackoverflow.com/questions/5132890/c-sharp-replace-bytes-in-byte[enter link description here][1] [1]: http://stackoverflow.com/questions/5132890/c-sharp-replace-bytes-in-byte – KF2 Jan 26 '13 at 16:12

1 Answers1

5

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

Lander
  • 3,369
  • 2
  • 37
  • 53
  • it makes sense thanks, now I'm getting an error though: using (System.IO.BinaryWriter bw = new System.IO.BinaryWriter(System.IO.File.Open(path, FileMode.Open, FileAccess.Write, FileShare.None))) bw.BaseStream.Position = 0xB8EB9; // set the offset bw.Write(g1); error: the name 'bw' does not exist is current context at bw.write – Bert Pauwels Jan 26 '13 at 18:04
  • It looks like it's caused by not having the code after the statement scoped. I typed this up on my iPad, but see [this](http://stackoverflow.com/questions/614959/using-the-using-statment-in-c-sharp) question on using the `using` statement. You can discard the `using` statement and just call `Close()` on the `BinaryReader` – Lander Jan 26 '13 at 18:14
  • 2
    This is not going to work, BinaryWriter treats strings specially. It writes the length of the string first. A byte[] is required here, get one from Encoding.ASCII – Hans Passant Jan 26 '13 at 18:16
  • @HansPassant I overlooked that! Thanks, I will update my answer. – Lander Jan 26 '13 at 18:18
  • Allmost there, the problem being when it writes '$' as first char and therefor will overite the last } with a part from the generated guid – Bert Pauwels Jan 26 '13 at 18:34
  • @BertPauwels see the updated answer/Hans's comment. The string must first be converted to a byte array, otherwise it first writes the length of the string. I apologize for that mistake. edit: just saw your your last comment. No problem! Happy coding :) – Lander Jan 26 '13 at 18:50