Replace a binary block within a large file
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have a partition image that contains a 1MB file populated with random data, and I would like to replace the contents of that file with another 1MB file populated with different random data.
I have done something similar before by creating a file with a known unique string and using sed to replace it e.g. sed -i 's/foobar_corruptiontest/barfoo_corruptiontest/' partition.img
For a solution I imagined something along the lines of:
head -c 1M </dev/urandom >fileA
head -c 1M </dev/urandom >fileB
binary_sed -i 's/<fileA/<fileB/' partition.img
I found a tool bbe which purports to be a sedlike editor for binary files, but unless I am reading the man page wrong it doesn't seem to support file input?
Side-note: In case you are very confused why anyone would want to do this, the context is I am testing verity by corrupting a file in a predictable manner, but it should bear no relevance to the question. In fact please do not consider it in an answer.
sed replace binary
add a comment |Â
up vote
1
down vote
favorite
I have a partition image that contains a 1MB file populated with random data, and I would like to replace the contents of that file with another 1MB file populated with different random data.
I have done something similar before by creating a file with a known unique string and using sed to replace it e.g. sed -i 's/foobar_corruptiontest/barfoo_corruptiontest/' partition.img
For a solution I imagined something along the lines of:
head -c 1M </dev/urandom >fileA
head -c 1M </dev/urandom >fileB
binary_sed -i 's/<fileA/<fileB/' partition.img
I found a tool bbe which purports to be a sedlike editor for binary files, but unless I am reading the man page wrong it doesn't seem to support file input?
Side-note: In case you are very confused why anyone would want to do this, the context is I am testing verity by corrupting a file in a predictable manner, but it should bear no relevance to the question. In fact please do not consider it in an answer.
sed replace binary
3
The writing part should be easy asdd if=fileB of=partition.img bs=1 seek=$OFFSET conv=notrunc
It's getting $OFFSET that's the tricky part. Sure , some variation ongrep --byte-offset
could do it, if you wanted to create a pattern that matched a 1M file.
â infixed
May 17 at 13:49
1
that's tricky but you can imagine combining dd if=samedevice skip=x count= ...| bbe ... | dd seek=x of=samedevice . since input is read before being overwritten that should do it (have a backup...)
â A.B
May 17 at 13:53
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a partition image that contains a 1MB file populated with random data, and I would like to replace the contents of that file with another 1MB file populated with different random data.
I have done something similar before by creating a file with a known unique string and using sed to replace it e.g. sed -i 's/foobar_corruptiontest/barfoo_corruptiontest/' partition.img
For a solution I imagined something along the lines of:
head -c 1M </dev/urandom >fileA
head -c 1M </dev/urandom >fileB
binary_sed -i 's/<fileA/<fileB/' partition.img
I found a tool bbe which purports to be a sedlike editor for binary files, but unless I am reading the man page wrong it doesn't seem to support file input?
Side-note: In case you are very confused why anyone would want to do this, the context is I am testing verity by corrupting a file in a predictable manner, but it should bear no relevance to the question. In fact please do not consider it in an answer.
sed replace binary
I have a partition image that contains a 1MB file populated with random data, and I would like to replace the contents of that file with another 1MB file populated with different random data.
I have done something similar before by creating a file with a known unique string and using sed to replace it e.g. sed -i 's/foobar_corruptiontest/barfoo_corruptiontest/' partition.img
For a solution I imagined something along the lines of:
head -c 1M </dev/urandom >fileA
head -c 1M </dev/urandom >fileB
binary_sed -i 's/<fileA/<fileB/' partition.img
I found a tool bbe which purports to be a sedlike editor for binary files, but unless I am reading the man page wrong it doesn't seem to support file input?
Side-note: In case you are very confused why anyone would want to do this, the context is I am testing verity by corrupting a file in a predictable manner, but it should bear no relevance to the question. In fact please do not consider it in an answer.
sed replace binary
asked May 17 at 12:51
kcghost
584
584
3
The writing part should be easy asdd if=fileB of=partition.img bs=1 seek=$OFFSET conv=notrunc
It's getting $OFFSET that's the tricky part. Sure , some variation ongrep --byte-offset
could do it, if you wanted to create a pattern that matched a 1M file.
â infixed
May 17 at 13:49
1
that's tricky but you can imagine combining dd if=samedevice skip=x count= ...| bbe ... | dd seek=x of=samedevice . since input is read before being overwritten that should do it (have a backup...)
â A.B
May 17 at 13:53
add a comment |Â
3
The writing part should be easy asdd if=fileB of=partition.img bs=1 seek=$OFFSET conv=notrunc
It's getting $OFFSET that's the tricky part. Sure , some variation ongrep --byte-offset
could do it, if you wanted to create a pattern that matched a 1M file.
â infixed
May 17 at 13:49
1
that's tricky but you can imagine combining dd if=samedevice skip=x count= ...| bbe ... | dd seek=x of=samedevice . since input is read before being overwritten that should do it (have a backup...)
â A.B
May 17 at 13:53
3
3
The writing part should be easy as
dd if=fileB of=partition.img bs=1 seek=$OFFSET conv=notrunc
It's getting $OFFSET that's the tricky part. Sure , some variation on grep --byte-offset
could do it, if you wanted to create a pattern that matched a 1M file.â infixed
May 17 at 13:49
The writing part should be easy as
dd if=fileB of=partition.img bs=1 seek=$OFFSET conv=notrunc
It's getting $OFFSET that's the tricky part. Sure , some variation on grep --byte-offset
could do it, if you wanted to create a pattern that matched a 1M file.â infixed
May 17 at 13:49
1
1
that's tricky but you can imagine combining dd if=samedevice skip=x count= ...| bbe ... | dd seek=x of=samedevice . since input is read before being overwritten that should do it (have a backup...)
â A.B
May 17 at 13:53
that's tricky but you can imagine combining dd if=samedevice skip=x count= ...| bbe ... | dd seek=x of=samedevice . since input is read before being overwritten that should do it (have a backup...)
â A.B
May 17 at 13:53
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
I found a halfway decent solution, but unfortunately it requires a non-standard tool to search for the file offset. I hope that someone can come up with a better answer that only uses standard tools.
Install the tool SearchBin.
head -c 1M </dev/urandom >fileA
head -c 1M </dev/urandom >fileB
OFFSET=$(searchbin -f fileA -m 1 partition.img | head -1 | awk 'print $4')
dd if=fileB of=paritition.img bs=1 seek=$OFFSET conv=notrunc
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I found a halfway decent solution, but unfortunately it requires a non-standard tool to search for the file offset. I hope that someone can come up with a better answer that only uses standard tools.
Install the tool SearchBin.
head -c 1M </dev/urandom >fileA
head -c 1M </dev/urandom >fileB
OFFSET=$(searchbin -f fileA -m 1 partition.img | head -1 | awk 'print $4')
dd if=fileB of=paritition.img bs=1 seek=$OFFSET conv=notrunc
add a comment |Â
up vote
0
down vote
I found a halfway decent solution, but unfortunately it requires a non-standard tool to search for the file offset. I hope that someone can come up with a better answer that only uses standard tools.
Install the tool SearchBin.
head -c 1M </dev/urandom >fileA
head -c 1M </dev/urandom >fileB
OFFSET=$(searchbin -f fileA -m 1 partition.img | head -1 | awk 'print $4')
dd if=fileB of=paritition.img bs=1 seek=$OFFSET conv=notrunc
add a comment |Â
up vote
0
down vote
up vote
0
down vote
I found a halfway decent solution, but unfortunately it requires a non-standard tool to search for the file offset. I hope that someone can come up with a better answer that only uses standard tools.
Install the tool SearchBin.
head -c 1M </dev/urandom >fileA
head -c 1M </dev/urandom >fileB
OFFSET=$(searchbin -f fileA -m 1 partition.img | head -1 | awk 'print $4')
dd if=fileB of=paritition.img bs=1 seek=$OFFSET conv=notrunc
I found a halfway decent solution, but unfortunately it requires a non-standard tool to search for the file offset. I hope that someone can come up with a better answer that only uses standard tools.
Install the tool SearchBin.
head -c 1M </dev/urandom >fileA
head -c 1M </dev/urandom >fileB
OFFSET=$(searchbin -f fileA -m 1 partition.img | head -1 | awk 'print $4')
dd if=fileB of=paritition.img bs=1 seek=$OFFSET conv=notrunc
answered May 18 at 12:12
kcghost
584
584
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f444360%2freplace-a-binary-block-within-a-large-file%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
The writing part should be easy as
dd if=fileB of=partition.img bs=1 seek=$OFFSET conv=notrunc
It's getting $OFFSET that's the tricky part. Sure , some variation ongrep --byte-offset
could do it, if you wanted to create a pattern that matched a 1M file.â infixed
May 17 at 13:49
1
that's tricky but you can imagine combining dd if=samedevice skip=x count= ...| bbe ... | dd seek=x of=samedevice . since input is read before being overwritten that should do it (have a backup...)
â A.B
May 17 at 13:53