#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    23
    Rep Power
    0

    Pull bytes strings from binary


    Hi to all in forum,

    My first post here, I hope you can help me.

    I don't know almost anything about Python so far, but I've investigated and it seems that is a language with nice tools
    to handle binary data and less complex and faster than other languages, because of that I ask help in this language.

    I would like help in how to pull bytes strings from the binary in order to have a point of start.

    For example search for the string that begins with:
    Code:
    44 65 07
    If found, print those 3 bytes and the next 11 bytes. (14 bytes in total).
    Finally, from those 14 bytes, print without spaces "bytes 1 to 6", "bytes 6 to 12", and byte "13 to 14".
    Then I would like to print last 2 bytes (13 to 14) joined in decimal format.

    The output would be:
    Code:
    446507c90688 888000800005 21 
    4465072ec918 059173495269 44 
    44650700cc01 01811bc90b00 171
    The hexdump of the input is:
    Code:
    00000000  06 00 00 80 00 00 00 80  09 3c c9 06 88 88 80 00  |.........<......|
    00000010  80 00 44 65 07 c9 06 88  88 80 00 80 00 05 00 15  |..De............|
    00000020  37 06 01 00 00 01 00 65  00 00 00 02 00 00 02 00  |7......e........|
    00000030  18 00 00 00 03 00 00 03  00 17 00 00 00 04 00 00  |................|
    00000040  04 00 01 00 00 00 05 00  00 05 00 15 00 00 00 0a  |................|
    00000050  00 ff ff 00 65 00 00 44  65 07 2e c9 18 05 91 73  |....e..De......s|
    00000060  49 52 69 00 2c ff ff ff  00 cb 41 04 72 23 01 ff  |IRi.,.....A.r#..|
    00000070  ff 00 01 03 ca 03 08 08  fe cb 0a 00 00 00 00 00  |................|
    00000080  44 65 07 00 cc 01 01 81  1b c9 0b 00 00 ab 55 33  |De............U3|
    00000090  01 11 04 b1 7f ff ff ca  06 00 00 00 00 00 00 00  |................|
    000000a0
    PD: I've installed Python 3.3.2 on Windows 7.

    Thanks in advance for any help.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4247
    All right, this code is quick n' dirty and ugly, but should do the trick.
    Code:
    import sys
    
    with open('rambedi.bin', 'rb') as f:
        buf = f.read().decode('UTF-8')
        find_str = '\x44\x65\x07'
        idx = buf.find(find_str)
        while idx > -1:
            data = buf[idx:idx + 14]
    
            for i in range(0, 6):
                sys.stdout.write("%02x" % ord(data[i]))
            sys.stdout.write(' ')
            for i in range(6, 12):
                sys.stdout.write("%02x" % ord(data[i]))
            sys.stdout.write(' ')
            last_two = ''
            for i in range(12, 14):
                last_two += "%02x" % ord(data[i])
            print(int(last_two, 16))
    
            idx = buf.find(find_str, idx + 1)
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    23
    Rep Power
    0
    Thanks for the help.

    I've tried your code but I receive the error below:
    Code:
    >>> exec(open("C:\\Scripts\\Script1.py").read()) 
    Traceback (most recent call last): 
    File "<pyshell#1>", line 1, in <module> 
    exec(open("C:\\Scripts\\Script1.py").read()) 
    File "<string>", line 7, in <module> 
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 3: invalid start byte
    You can download the binsample.txt here:
    http://tinyurl.com/binsample-txt

    Thanks again.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    23
    Rep Power
    0
    Hello to all,

    Somebody could help me with my question.

    Thanks for any help.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    23
    Rep Power
    0
    Hello to all.

    Somebody can help me please?

IMN logo majestic logo threadwatch logo seochat tools logo