0

I have a very limited linux w/ few basic linux commands. I need to replace a few chars in a hex / binary file:

INPUT:

# hexdump -C block.bin
00000000  11 11 50 04 42 00 00 00  58 00 00 00 3c 0e e2 d4  |..P.B...X...<...|
00000010  50 0b 00 00 00 80 00 00  00 00 00 00 00 00 d0 d7  |P...............|
00000020  1f 09 00 00 00 00 02 00  00 00 00 04 ff ff ff ff  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000040  28 31 98 5b d3 0e 05 00  00 00 00 00 00 00 00 00  |(1.[............|
00000050  00 00 00 00 00 00 00 00  64 00 00 00 00 00 10 00  |........d.......|
00000060  00 ff ff ff ff 00 00 00  00 03 01 0d 03 01 0d 01  |................|
00000070  00 00 00 00 0c 00 01 02  00 00 00 00 00 ff ff ff  |................|
00000080  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
*
00000180  00 00 20 00 00 00 ff ff  ff ff 01 00 00 0c 00 00  |.. .............|
00000190  04 00 00 00 02 00 00 04  00 00 00 00 00 00 ff ff  |................|
000001a0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
*
00000200  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

I need to change the following:

00000060  00 ff ff ff ff 00 00 00  00 03 01 0d 03 01 0d 01  |................|
00000070  00 00 00 00 0c 00 01 02  00 00 00 00 00 ff ff ff  |................|

to (and rewrite back to the same file):

00000060  00 ff ff ff ff 00 00 00  00 03 01 0d 03 01 0d 02  |................|
00000070  01 00 00 00 0c 00 01 02  00 00 00 00 00 ff ff ff  |................|

Like I've said, I have a handful of commands: hexdump, od, vi, nano, awk, sed, python. Looking through the internet, many solutions require 3rd party installs or the use of 'xxd'. Both I cannot use.

Any suggestions?

Thanks!

Maxim_united
  • 1,911
  • 1
  • 14
  • 23
  • Please elaborate on the exact requirements. Do you have to set a byte at a fixed position to a fixed value (`0000006f` to `02`), or is it more complicated? Does the new value depend on the old, are you looking for specific pattern instead of a fixed position, that sort of thing. – Wintermute Feb 12 '15 at 16:16
  • Without `xxd`, looks like you'll have to write that tool yourself - ergo you want a programming language. Check out the answers here: [reading-binary-file-in-python](http://stackoverflow.com/questions/1035340/reading-binary-file-in-python) to get started. – n0741337 Feb 12 '15 at 16:24
  • The positions are fixed. `0000006f` and `00000070` The requirement is to increment both bytes by 1. thx – Maxim_united Feb 12 '15 at 16:25
  • Unfortunately, I have zero knowledge in Python. – Maxim_united Feb 12 '15 at 16:27
  • No perl on this system – Maxim_united Feb 12 '15 at 16:50
  • Which shell is available? And do you have GNU sed or is it something more primitive? – Ben Grimm Feb 12 '15 at 16:58
  • `GNU bash, version 3.2.51(1)-release (x86_64-unknown-linux-gnu)` `GNU sed version 4.1.5.` – Maxim_united Feb 12 '15 at 17:35

1 Answers1

3

I don't think trying this with sed or awk is a sane idea, so we're stuck with python, which is certainly powerful enough for this task. I'm thinking along these lines:

#!/usr/bin/python

# open file in binary mode for reading and writing    
f = open("block.bin", "r+b")

# seek to position and read two bytes
f.seek(0x6f)
data = f.read(2)

# seek to position again
f.seek(0x6f)

# and write the transformed characters back
for d in data:
    f.write(chr(ord(d) + 1))

f.close()
Wintermute
  • 42,983
  • 5
  • 77
  • 80