How could I use Bash to find 2 bytes in a binary file, increase their values, and replace?

5,916

Solution 1

Testing with this file:

$ echo hello world > test.txt
$ echo -n $'\x1b\x1f' >> test.txt
$ echo whatever >> test.txt
$ hexdump -C test.txt 
00000000  68 65 6c 6c 6f 20 77 6f  72 6c 64 0a 1b 1f 77 68  |hello world...wh|
00000010  61 74 65 76 65 72 0a                              |atever.|
$ grep -a -b --only-matching $'\x1b\x1f' test.txt 
12:

So in this case the 1B 1F is at position 12.

  • Convert to integer (there is probably an easier way)

    $ echo 'ibase=16; '`xxd -u -ps -l 2 -s 12 test.txt`  | bc
    6943
    
  • And the reverse:

    $ printf '%04X' 6943 | xxd -r -ps | hexdump -C
    00000000  1b 1f                                             |..|
    $ printf '%04X' 4242 | xxd -r -ps | hexdump -C
    00000000  10 92                                             |..|
    
  • And putting it back in the file:

    $ printf '%04X' 4242 | xxd -r -ps | dd of=test.txt bs=1 count=2 seek=12 conv=notrunc
    2+0 records in
    2+0 records out
    2 bytes (2 B) copied, 5.0241e-05 s, 39.8 kB/s
    
  • Result:

    $ hexdump -C test.txt
    00000000  68 65 6c 6c 6f 20 77 6f  72 6c 64 0a 10 92 77 68  |hello world...wh|
    00000010  61 74 65 76 65 72 0a                              |atever.|
    

Solution 2

Oh, sorry. This answer is obsolete, because I thought you have hexadecimal values written as ascii in your file.


You can convert the hex numbers to decimal system via printf "%d" 0x1B1F.

If you have saved the bytes in a variable BYTES=1B1F then you get the result with printf "%d" 0x$BYTES.

So, if you want to increase the number do

$ echo $(($(printf "%d" 0x$BYTES) +1))
6944

Then convert it back

printf '%X' $(($(printf "%d" 0x$BYTES) +1))
1B20
Share:
5,916

Related videos on Youtube

piotrekkr
Author by

piotrekkr

Updated on September 18, 2022

Comments

  • piotrekkr
    piotrekkr over 1 year

    I'm trying to find two bytes inside binary file, then increase value of those two bytes and replace them inside file. Those two bytes are on positions 0x82-0x83. For now on I have successfully extracted those two bytes using this:

    #!/usr/bin/env bash
    BYTES=$(tail -c +131 "$1" | head -c 2)
    

    Those bytes have value: 1B 1F. I'm stuck with:

    1. How to convert bytes to integer? It should be 6943 decimal.
    2. How to append / echo binary data to file
    3. How to write increased bytes inside file on positions 0x82-0x83. I could use head -c 130 original.bin >> new_file.bin && magic_command_writing_bytes_to_file >> new_file.bin && tail -c +133 original.bin, but there must be better way.

    I could do that in PHP, it should be easier, but I'm interested how to do this in bash.

  • piotrekkr
    piotrekkr over 10 years
    Yes, It's exactly what I was looking for. Thanks.
  • erik
    erik over 10 years
    @frostschutz: There is a bug —a missing \x— in line two of your example code. It should be echo -n $'\x1b\x1f' >> test.txt.
  • syntaxerror
    syntaxerror over 9 years
    Convert to integer (there is probably an easier way) Oh yes, there is! :) erik chose the best command, so I'll just adapt his line to yours: printf "%d" 0x1B1F will do the job just fine to obtain your 6943 result; going from that, you can use a significantly smarter line like printf "%d" $(xxd -u -ps -l 2 -s 12 test.txt) instead and you won't be needing bc any longer.