简体   繁体   English

如何使用正则表达式从二进制文件中找到唯一的十六进制模式

[英]How to find unique pattern of hex from binary file using regex

Here I got a binary file of 4GB.在这里,我得到了一个 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图案看起来像?? ?? ?? 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 the ?(question marks) are unknown points. ?? ?? ?? E5 00 60 8F E0?? ?? ?? E5 04 00 80 E0 ?(问号)是未知点。

Python code to get 16 bytes hex code is Python 代码获取 16 字节十六进制代码是

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]"模式=“[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") 

Try this:尝试这个:

[A-F0-9]{6}(E500608FE0)[A-F0-9]{6}(E5040080E0)

https://regex101.com/r/cBrlcW/1 https://regex101.com/r/cBrlcW/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM