0

I am (attempting) to write a program that searches through a hex file for instances of a hex string between two values, eg. Between D4135B and D414AC, incrementing between the first value until the second is reached- D4135B, D4135C, D4135D etc etc. I have managed to get it to increment etc, but it’s the search part I am having trouble with. This is the code I have so far, it's been cobbled together from other places and I need to make it somehow output all search hits into the output file (file_out) I have exceeded the limit of my Python understanding and I'm sure there's probably a much easier way of doing this. I would be very grateful for any help.

def search_process(hx):   # searching for two binary strings
    global FLAG
while threeByteHexPlusOne != threeByteHex2: #Keep incrementing until second value reached
If Flag:
    if hx.find(threeByteHex2) != -1:
    FLAG = False #If threeByteHex = ThreeByteHexPlusOne, end search
    Print (“Reached the end of the search”,hx.find(threeByteHexPlusOne))
    Else:
        If hx.find(threeByteHexPlusOne) != -1:
        FLAG = True
    Return -1 #If no results found

if __name__ == '__main__':
    try:
        file_in = open(FILE_IN, "r")   #opening input file
        file_out = open(FILE_OUT, 'w') #opening output file
        hx_read = file_in.read  #read from input file 
        tmp = ''
        found = ''
        while hx_read:   #reading from file till file is empty
            hx_read = tmp + hx_read
            pos = search_process(hx_read)

            while pos != -1:
                hex_read = hx_read[pos:]

                if FLAG:
                    found = found + hx_read

                pos = search_process(hx_read)   
            tmp = bytes_read[]
            hx_read = file_in.read

        file_out.write(found)  #writing to output file

    except IOError:
        print('FILE NOT FOUND!!! Check your filename or directory/PATH')
crennie
  • 674
  • 7
  • 18
user2188291
  • 149
  • 1
  • 2
  • 9
  • So... what happens when you run it? Does it throw an Exception? Also, the indentation and case is messed up. – Aran-Fey May 03 '13 at 15:39

1 Answers1

0

Here's a program that looks through a hex string from a file 3 bytes at a time and if the 3-byte hex string is between the given hex bounds, it writes it to another file. It makes use of generators to make getting the bytes from the hex string a little cleaner.

import base64
import sys

_usage_string = 'Usage: python {} <input_file> <output_file>'.format(sys.argv[0])

def _to_base_10_int(value):
    return int(value, 16)

def get_bytes(hex_str):
    # Two characters equals one byte
    for i in range(0, len(hex_str), 2):
        yield hex_str[i:i+2]

def get_three_byte_hexes(hex_str):
    bytes = get_bytes(hex_str)
    while True:
        try:
            three_byte_hex = next(bytes) + next(bytes) + next(bytes)
        except StopIteration:
            break
        yield three_byte_hex

def find_hexes_in_range(hex_str, lower_bound_hex, upper_bound_hex):
    lower_bound = _to_base_10_int(lower_bound_hex)
    upper_bound = _to_base_10_int(upper_bound_hex)
    found = []
    for three_byte_hex in get_three_byte_hexes(hex_str):
        hex_value = _to_base_10_int(three_byte_hex)
        if lower_bound <= hex_value < upper_bound:
            found.append(three_byte_hex)
    return found

if __name__ == "__main__":
    try:
        assert(len(sys.argv) == 3)
    except AssertionError:
        print _usage_string
        sys.exit(2)
    file_contents = open(sys.argv[1], 'rb').read()
    hex_str = base64.decodestring(file_contents).encode('hex')
    found = find_hexes_in_range(hex_str, 'D4135B', 'D414AC')
    print('Found:')
    print(found)
    if found:
        with open(sys.argv[2], 'wb') as fout:
            for _hex in found:
                fout.write(_hex)

Check out some more info on generators here

Community
  • 1
  • 1
crennie
  • 674
  • 7
  • 18