Using grep to search for hex strings in a file

后端 未结 6 2056
野趣味
野趣味 2020-12-04 17:46

I have been trying all day to get this to work. Does anyone know how to get grep, or something of the like, to retrieve offsets of hex strings in a file?

I have a bu

6条回答
  •  渐次进展
    2020-12-04 18:44

    We tried several things before arriving at an acceptable solution:

    xxd -u /usr/bin/xxd | grep 'DF'
    00017b0: 4010 8D05 0DFF FF0A 0300 53E3 0610 A003  @.........S.....
    
    
    root# grep -ibH "df" /usr/bin/xxd
    Binary file /usr/bin/xxd matches
    xxd -u /usr/bin/xxd | grep -H 'DF'
    (standard input):00017b0: 4010 8D05 0DFF FF0A 0300 53E3 0610 A003  @.........S.....
    

    Then found we could get usable results with

    xxd -u /usr/bin/xxd > /tmp/xxd.hex ; grep -H 'DF' /tmp/xxd
    

    Note that using a simple search target like 'DF' will incorrectly match characters that span across byte boundaries, i.e.

    xxd -u /usr/bin/xxd | grep 'DF'
    00017b0: 4010 8D05 0DFF FF0A 0300 53E3 0610 A003  @.........S.....
    --------------------^^
    

    So we use an ORed regexp to search for ' DF' OR 'DF ' (the searchTarget preceded or followed by a space char).

    The final result seems to be

    xxd -u -ps -c 10000000000 DumpFile > DumpFile.hex
    egrep ' DF|DF ' Dumpfile.hex
    
    0001020: 0089 0424 8D95 D8F5 FFFF 89F0 E8DF F6FF  ...$............
    -----------------------------------------^^
    0001220: 0C24 E871 0B00 0083 F8FF 89C3 0F84 DF03  .$.q............
    --------------------------------------------^^
    

提交回复
热议问题