How to overwrite some bytes of a binary file with dd?
Asked Answered
B

2

30

I have a binary file and i want to replace the value A2 at address DEADBEEF with some other value, say A1.

How can I do this with dd? If there are other tools that can do this, please suggest. But I plan to do this on iPhone so I can only work with most basic Unix tools.

Bonspiel answered 3/9, 2011 at 2:37 Comment(0)
C
52
printf '\xa1' | dd of=somefile bs=1 seek=$((0xdeadbeef)) conv=notrunc
Copenhaver answered 3/9, 2011 at 2:41 Comment(2)
This doesn't work on my machine. somefile end up truncated after the modified byte.Confiteor
However, it works when conv=notrunc is at the end of the command. printf '\xa1' | dd of=somefile bs=1 seek=$((0xdeadbeef)) conv=notruncConfiteor
S
0

(Same answer, but doesn't fit into a comment:)

I wanted to preview the file content before applying the patch I wanted; dd uses different sets of arguments for input and output files, so remember to use if and iseek:

% dd if=somefile bs=1 iseek=4488884 count=12 conv=notrunc | hexdump -C
12+0 records in
12+0 records out
12 bytes copied, 0.000131 s, 91.6 kB/s
00000000  1f 04 00 71 40 02 00 54  a1 02 40 f9              |[email protected]..@.|
0000000c

And a multi-byte patch (dd will just overwrite with whatever stdin gives it, so no need to specify length):

% printf '\x40\x02\x00\x54' | dd of=somefile bs=1 seek=4488888 conv=notrunc
4+0 records in
4+0 records out
4 bytes copied, 0.000251 s, 15.9 kB/s
Speiss answered 21/9, 2023 at 18:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.