• 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.
  • Popular Now

  • Posts

    • Download Natural Language Processing with Python (worth $39.99) for free by Steven Parker Claim your complimentary eBook worth $39.99 for free, before the offer ends on June 25. Begin with foundational concepts like text preprocessing, tokenization, and key Python libraries such as NLTK, spaCy, and TextBlob. Explore the challenges of text data and gain hands-on experience in cleaning, tokenizing, and building basic NLP pipelines. Early chapters provide practical exercises to solidify your understanding of essential techniques. Advance to sophisticated topics like feature engineering using Bag of Words, TF-IDF, and embeddings like Word2Vec and BERT. Delve into language modeling with RNNs, syntax parsing, and sentiment analysis, learning to apply these techniques in real-world scenarios. Chapters on topic modeling and text summarization equip you to extract insights from data, while transformer-based models like BERT take your skills to the next level. Each concept is paired with Python-based examples, ensuring practical mastery. The final chapters focus on real-world projects, such as developing chatbots, sentiment analysis dashboards, and news aggregators. These hands-on applications challenge you to design, train, and deploy robust NLP solutions. With its structured approach and practical focus, this book equips you to confidently tackle real-world NLP challenges and innovate in the field. This free to download offer expires June 25. How to get it Please ensure you read the terms and conditions to claim this offer. Complete and verifiable information is required in order to receive this free offer. If you have previously made use of these free offers, you will not need to re-register. While supplies last! Download Natural Language Processing with Python (worth $39.99) for free Offered by Packt, view other free resources The below offers are also available for free in exchange for your (work) email: AI and Innovation ($21 Value) FREE – Expires 6/11 Unruly: Fighting Back when Politics, AI, and Law Upend [...] ($18 Value) FREE - Expires 6/17 SQL Essentials For Dummies ($10 Value) FREE – Expires 6/17 Continuous Testing, Quality, Security, and Feedback ($27.99 Value) FREE – Expires 6/18 VideoProc Converter AI v7.5 for FREE (worth $78.90) – Expires 6/18 Macxvideo AI ($39.95 Value) Free for a Limited Time – Expires 6/22 Microsoft 365 Copilot At Work ($60 Value) FREE – Expires 6/25 Natural Language Processing with Python ($39.99 Value) FREE – Expires 6/25 Excel Quick and Easy ($12 Value) FREE – Expires 6/24 The Inclusion Equation: Leveraging Data & AI ($21 Value) FREE – Expires 6/24 The Ultimate Linux Newbie Guide – Featured Free content Python Notes for Professionals – Featured Free content Learn Linux in 5 Days – Featured Free content Quick Reference Guide for Cybersecurity – Featured Free content We post these because we earn commission on each lead so as not to rely solely on advertising, which many of our readers block. It all helps toward paying staff reporters, servers and hosting costs. Other ways to support Neowin The above deal not doing it for you, but still want to help? Check out the links below. Check out our partner software in the Neowin Store Buy a T-shirt at Neowin's Threadsquad Subscribe to Neowin - for $14 a year, or $28 a year for an ad-free experience Disclosure: An account at Neowin Deals is required to participate in any deals powered by our affiliate, StackCommerce. For a full description of StackCommerce's privacy guidelines, go here. Neowin benefits from shared revenue of each sale made through the branded deals site.
    • If your SMS service implements RCS E2E encryption, go for it, there's really no difference. But switching to Telegram, for example? Why? Telegram also shows you ads, if you didn't know, and they were doing it before WhatsApp. Also, Telegram isn't E2E encrypted by default, you have to manually enable it for every chat.
  • Recent Achievements

    • Week One Done
      patrickft456 earned a badge
      Week One Done
    • One Month Later
      patrickft456 earned a badge
      One Month Later
    • One Month Later
      Jdoe25 earned a badge
      One Month Later
    • Explorer
      Legend20 went up a rank
      Explorer
    • One Month Later
      jezzzy earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      641
    2. 2
      ATLien_0
      278
    3. 3
      +FloatingFatMan
      171
    4. 4
      Michael Scrip
      156
    5. 5
      Steven P.
      128
  • Tell a friend

    Love Neowin? Tell a friend!