Search code examples
bashgrepdd

Find binary in another binary


I got two binary files (dd images) one huge (around 1 GB) and one small (several MB). I want to get the offset where I can find the small one inside of the lage one. I played around with grep -a -x, xdd and od but had no luck so far.


Solution

  • In case you don't need it as a function to script, but just to handle a 1-off event you can try this:

    mindaugasb@bash:~/workspace $ touch file.txt
    mindaugasb@bash:~/workspace $ echo "0020 0000 0000 0000 0000 0000 0000" > file.txt 
    mindaugasb@bash:~/workspace $ hexdump /bin/ls | grep -f file.txt 
    001a7a0 0020 0000 0000 0000 0000 0000 0000 0000
    001aaf0 0020 0000 0000 0000 0000 0000 0000 0000
    001ad30 0020 0000 0000 0000 0000 0000 0000 0000
    001ad70 0020 0000 0000 0000 0000 0000 0000 0000
    

    The file passed to grep should contain the first line of the smaller file. If there are no duplicates you are in luck.

    First column of hexdump output is the offset.

    If you want the hexdump output for the smaller file - and you should bet it w/o the byte offsets, because then grep will never match since byte offsets are specific to each file - you need to disable the byte offsets, like so:

    mindaugasb@bash:~/workspace $ hexdump -e '16/1 "%02x " "\n"' /bin/ls | head -n1
    7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00