Hi. Sorry if the question is confusing or weird but I am trying to find a specific number of bytes after finding a different set of bytes. Hopefully with code and my explaination of code, i'll get my question across.

import base64

find_bytes = bytearray(base64.b16decode('46726F4B6E6F'))
with open("readfile.raw", "rb") as f:
    file_bytes = bytearray(f.read())
    found_pos = file_bytes.find(find_bytes, 0)
    with open("foundhex.txt", "w") as found:
        found.write("Found 46726F4B6E6F at : " + str(found_pos))

Okay so. I have a set of bytes i want to find find_bytes : 46726F4B6E6F, then with those bytes found i want to find the previous 6 bytes before them. In a RL situation, i wouldn't know what those 6 bytes are, I only know that they are 6 bytes and that they are positioned before find_bytes. I am unsure how to achieve this. i was thinking use range() but im not sure how to use it with bytearray or if it is even the correct method.

I hope i explained it properly. please let me know if u need any more clarification.

i just wanted to update my code here as it has changed.

with open(filename, "rb") as binaryfile:
    while True:
        read_file = binaryfile.read()
        if len(read_file):
            for find_bytes in re.finditer(bytes1, read_file):
                with open("foundhex.txt", "a") as found1:
                    found1.write("Found bytes at : " + str(find_bytes.start()) + " " +str(find_bytes.end()) + "\n")
        else:
            break

this code finds all occurences of bytes1. I am still trying to figure out how to find the 6 bytes before bytes1. there was an idea to use slice but im not sure how to use it with the code i have now (for loop). i was thinking something like:

find_prevbytes = read_file[find_bytes -6:6]

but ofc theres the TypeError.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.