Replace a binary block within a large file

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question















  • 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







  • 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















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.







share|improve this question















  • 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







  • 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













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.







share|improve this question











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.









share|improve this question










share|improve this question




share|improve this question









asked May 17 at 12:51









kcghost

584




584







  • 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







  • 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




    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




    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











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





share|improve this answer





















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    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






























    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





    share|improve this answer

























      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





      share|improve this answer























        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





        share|improve this answer













        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






        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered May 18 at 12:12









        kcghost

        584




        584






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay