![](/img/trans.png)
[英]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.