[英]How to find pattern in file and comment that pattern in file using python
[英]How to find unique pattern of hex from binary file using regex
在这里,我得到了一个 4GB 的二进制文件。 我想在该文件中搜索十六进制模式并获取所需模式的偏移量。
图案看起来像?? ?? ?? E5 00 60 8F E0?? ?? ?? E5 04 00 80 E0
?? ?? ?? E5 00 60 8F E0?? ?? ?? E5 04 00 80 E0
?? ?? ?? E5 00 60 8F E0?? ?? ?? E5 04 00 80 E0
?(问号)是未知点。
Python 代码获取 16 字节十六进制代码是
with open("file.bin", 'rb') as in_file:
while True:
hexdata = in_file.read(16).hex().upper()
if len(hexdata) == 0:
break
返回示例代码,如
17000000ECBDD40617000000F0BDD406
17000000F4BDD40617000000F8BDD406
17000000FCBDD4061700000000BED406
1700000004BED4061700000008BED406
170000000CBED4061700000010BED406
FAEEF0E500608FE0A0EAFFE5040080E0
我正在尝试这种正则表达式模式来找到欲望模式。 模式=“[a-f0-9]{6}[E500608FE0][a-f0-9]{6}[E5040080E0]”
但它返回无。 如果我做错了什么,请告知。
回答
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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.