Sample Header Ad - 728x90

Searching for a pattern in a binary file using python script

1 vote
1 answer
3627 views
I want to find repeated copies of the config section within the partition dump (binary file), using pattern and 'magic' header. The config section always starts with 202 '0xff' bytes followed by 4 bytes '\x00\x00\x23\x27'. The script should identify different copies of configuration within the partition and print addresses (in bytes) where occurrences of the pattern starting. I adjusted an existing python script for my pattern, but it doesn't works, just throws errors, due to mixing bytes with strings. How to fix this script? #!/usr/bin/env python3 import re import mmap import sys magic = '\xff' * 202 pattern = magic + '\x00\x00\x23\x27' fh = open(sys.argvReferenced image, "r+b") mf = mmap.mmap(fh.fileno(), 0) mf.seek(0) fh.seek(0) for occurence in re.finditer(pattern, mf): print(occurence.start()) mf.close() fh.close() _errors:_ $ ./matcher.py dump.bin Traceback (most recent call last): File "/home/eviecomp/BC2UTILS/dump_previous_profile/./matcher.py", line 13, in for occurence in re.finditer(pattern, mf): File "/usr/lib/python3.9/re.py", line 248, in finditer return _compile(pattern, flags).finditer(string) TypeError: cannot use a string pattern on a bytes-like object pattern and magic: enter image description here
Asked by minto (575 rep)
Jul 6, 2023, 09:12 PM
Last activity: Jul 6, 2023, 10:56 PM