13.1. Hyperscan¶
13.1.1. 简介¶
"Hyperscan是一个高性能的正则表达式匹配库(...)" (https://www.intel.com/content/www/us/en/developer/articles/technical/introduction-to-hyperscan.html)
在Suricata中,它可用于执行多模式匹配(mpm)或单模式匹配(spm)。
Suricata对hyperscan的支持最初由Intel的Justin Viiret和Jim Xu通过https://github.com/OISF/suricata/pull/1965实现。
目前Hyperscan仅适用于基于Intel x86的处理器架构。对于ARM处理器,vectorscan可作为hyperscan的直接替代品,详见https://github.com/VectorCamp/vectorscan。
13.1.2. 基础安装(软件包)¶
部分Linux发行版在其软件包集合中包含了hyperscan。
Fedora 37+/Centos 8+: sudo dnf install hyperscan-devel Ubuntu/Debian: sudo apt-get install libhyperscan-dev
13.1.3. 高级安装(源码编译)¶
从源码编译Hyperscan需要以下依赖项:
boost开发库(最低boost库版本为1.58)
cmake
C++编译器(如gcc-c++)
libpcap开发库
pcre2开发库
python3
ragel
sqlite开发库
注意: 如果克隆hyperscan的GitHub仓库,git是额外依赖项。也可以直接从GitHub仓库下载hyperscan的zip包。
编译安装步骤:
git clone https://github.com/intel/hyperscan
cd hyperscan
cmake -DBUILD_STATIC_AND_SHARED=1
cmake --build ./
sudo cmake --install ./
注意: Hyperscan的编译过程可能耗时较长。
注意: 可能需要将/usr/local/lib或/usr/local/lib64添加到`ld`搜索路径。通常通过在/etc/ld.so.conf.d/下创建包含libhs.so.5所在目录路径的文件来实现(针对hyperscan 5.x版本)。
13.1.4. 使用Hyperscan¶
确认已安装的suricata版本启用了hyperscan支持。
suricata --build-info | grep Hyperscan
Hyperscan支持: 是
要启用hyperscan支持,编辑suricata.yaml文件。 将mpm-algo和spm-algo的值改为'hs'。
或者使用命令行选项:--set mpm-algo=hs --set spm-algo=hs
注意:suricata.yaml中mpm-algo和spm-algo的默认设置为"auto"。当设置为"auto"时,如果系统存在hyperscan,Suricata将自动使用它。
如果当前suricata安装不支持hyperscan,请参考 installation
13.1.5. Hyperscan缓存机制¶
启动时,Hyperscan会将规则集编译优化为内部数据结构。Suricata通过将这些内部结构保存到磁盘并在下次启动时加载来优化启动过程。这避免了规则集的重新编译,从而实现更快的初始化。如果规则集发生变化,系统会自动生成新的缓存文件。
启用此功能需在`suricata.yaml`中配置:
detect:
# 将MPM上下文缓存到磁盘以避免启动时重新编译规则
# 缓存文件保存在标准库目录中
sgh-mpm-caching: 是
sgh-mpm-caching-path: /var/lib/suricata/cache/hs
注意: 可能需要创建并调整默认缓存文件夹的权限,特别是当以非root用户身份运行Suricata时。