• 0

[VB.Net][C#] Replacing some bytes with another bytes in binary file


Question

  • 0

Simple one byte example:

//
// open file.bin (in local directory) for read/write access
//

BinaryWriter bw = new BinaryWriter(
	File.Open(@"file.binFiFileMode.Open, FileAccess.ReadWrite));

//
// write byte (character 'b') at offset 1234
// and close the file
//

bw.BaseStream.Seek(0x1234, SeekOrigin.Begin);
bw.Write(0x62);
bw.Close();

Simple multi-byte example:

//
// not a real patch, this is JMP EIP;p
//

byte[] patch = { 0xEB, 0xFE };

//
// open file.bin (in local directory) for read/write access
//

BinaryWriter bw = new BinaryWriter(
	File.Open(@"file.binFiFileMode.Open, FileAccess.ReadWrite));

//
// write byte (character 'b') at offset 1234
// and close the file
//

bw.BaseStream.Seek(0x1234, SeekOrigin.Begin);
bw.Write(patch);
bw.Close();

7 answers to this question

Recommended Posts

  • 0

ok never mind, i managed to replace the byte with your code, thanks again :)

However it seems that i MUST replace bytes with same length. I can't replace 2 bytes with 7 bytes for example.

Edited by Elagizy
  • 0
  Elagizy said:
ok never mind, i managed to replace the byte with your code, thanks again :)

However it seems that i MUST replace bytes with same length. I can't replace 2 bytes with 7 bytes for example.

No, BinaryWriter cannot "insert" bytes. Most binary files, like executables, cannot change in size without having to alter a bunch of other information (i.e. PE header). Your best bet is to explain what you're trying to do; we'll try to determine what the best solution is.

  • 0

I managed to record LIVE MMS streams, unfortunately after saving streams, it only works on VLC player. One of the issues i'm working on, is to fix ASF header "play_duration" to get the exact duration of the file. But the file will still be playable on VLC player only, but I can now see the file play length and can drag the seek bar.

Some LIVE MMS streams plays well on all media players, so i'm trying to compare asf headers with each other and fill up the gap.

It seems to be working, i use UltraEdit to mess with ASF header and i got some files partially fixed. All i need at the moment is to edit bytes freely programmatically.

But your code also helps me alot, ASF header has some values with fixed length like "packet length" "play duration" ...etc

  • 0
  Elagizy said:
I managed to record LIVE MMS streams, unfortunately after saving streams, it only works on VLC player. One of the issues i'm working on, is to fix ASF header "play_duration" to get the exact duration of the file. But the file will still be playable on VLC player only, but I can now see the file play length and can drag the seek bar.

Some LIVE MMS streams plays well on all media players, so i'm trying to compare asf headers with each other and fill up the gap.

It seems to be working, i use UltraEdit to mess with ASF header and i got some files partially fixed. All i need at the moment is to edit bytes freely programmatically.

But your code also helps me alot, ASF header has some values with fixed length like "packet length" "play duration" ...etc

You should use a bit stream library which will allow you to write and read stuff at a bit level.

http://www.codeproject.com/csharp/bitstream.asp

This should be pretty straight forward.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.