0

I tried to use python script to find a repeated copies of the configuration section within the binary dump 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 config section within the partition and print the addresses where they start.

#!/usr/bin/env python3
import re
import mmap
import sys

magic = '\xff' * 202
pattern = magic + '\x00\x00\x23\x27'

fh = open(sys.argv[1], "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()

I got the error:

$ ./matcher.py dump.bin
Traceback (most recent call last):
  File "/home/eviecomp/BC2UTILS/dump_previous_profile/./matcher.py", line 13, in <module>
    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

Please advice how to fix script.

7
  • mf is a bytes-like object. re.finditer needs a string as its second argument. Commented Jul 5, 2023 at 19:04
  • What happens if you replace mf in re.finditer(...) with mf.decode()? See here for why you may want to do that: docs.python.org/3/library/stdtypes.html#bytes.decode Commented Jul 5, 2023 at 19:10
  • @jjramsey tried, got another error: ./matcher2.py dump.bin Traceback (most recent call last): File "/home/eviecomp/BC2UTILS/dump_previous_profile/./matcher2.py", line 13, in <module> for occurence in re.finditer(pattern, mf.decode()): AttributeError: 'mmap.mmap' object has no attribute 'decode' Commented Jul 5, 2023 at 19:22
  • What about trying mf[:].decode() instead? Commented Jul 5, 2023 at 20:15
  • ./matcher2.py dump.bin Traceback (most recent call last): File "/home/eviecomp/BC2UTILS/dump_previous_profile/./matcher2.py", line 13, in <module> for occurence in re.finditer(pattern, mf[:].decode()): UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte Commented Jul 5, 2023 at 21:05

1 Answer 1

0

So I find the answer myself: I have to use b'' literals:

magic = b'\xff' * 202
pattern = magic + b'\x00\x00\x23\x27'

so the script is:

#!/usr/bin/env python3
import re
import mmap
import sys

magic = b'\xff' * 202
pattern = magic + b'\x00\x00\x23\x27'

fh = open(sys.argv[1], "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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.