Here I got a binary file of 4GB. And I want to search for hex pattern in That file and get offset of required pattern.
The pattern look like ?? ?? ?? E5 00 60 8F E0 ?? ?? ?? E5 04 00 80 E0
the ?(question marks) are unknown points.
Python code to get 16 bytes hex code is
with open("file.bin", 'rb') as in_file:
while True:
hexdata = in_file.read(16).hex().upper()
if len(hexdata) == 0:
break
Returns sample code like
17000000ECBDD40617000000F0BDD406
17000000F4BDD40617000000F8BDD406
17000000FCBDD4061700000000BED406
1700000004BED4061700000008BED406
170000000CBED4061700000010BED406
FAEEF0E500608FE0A0EAFFE5040080E0
I am trying this regex pattern to find desire pattern. pattern = "[a-f0-9]{6}[E500608FE0][a-f0-9]{6}[E5040080E0]"
But it return None. If I am doing something wrong let be know.
Answer
import re
pattern = "[A-F0-9]{6}(E500608FE0)[A-F0-9]{6}(E5040080E0)"
with open("file.bin", 'rb') as in_file:
while True:
hexdata = in_file.read(16).hex().upper()
if len(hexdata) == 0:
break
if re.search(pattern, hexdata):
print("found")
CodePudding user response:
Try this:
[A-F0-9]{6}(E500608FE0)[A-F0-9]{6}(E5040080E0)