第 50 章 YARA / Sigma 规则工程
规则 = 把分析经验变成"自动识别能力"。YARA 识别"样本是什么"(文件特征),Sigma 识别"系统发生了什么"(行为特征)。本章讲规则的工程化编写:从语法到设计哲学到测试验证。
📍 知识点地图 | 主题:YARA/Sigma规则 | 前置:第17章 | 后续:第83章 | 核心概念:规则语法、设计哲学、SIEM转换
50.1 YARA 规则(恶意样本识别)
语法速成(核心)
rule Example_Detection {
meta:
author = "analyst"
description = "识别示例木马"
hash = "sha256:..."
strings:
$s1 = "http://c2.example.com" ascii
$s2 = "cmd.exe /c" ascii wide nocase
$s3 = { 6A 00 6A 00 68 00 00 00 00 } // 字节序列
$s4 = /https?:\/\/[a-z0-9\.]+\.com/ // 正则
$s5 = "GetProcAddress" ascii fullword
condition:
// 逻辑: 至少 2 个字符串 + 特定字节模式
2 of ($s*) and $s3 and filesize > 100KB
}
关键语法点
字符串修饰符:
ascii / wide(宽字符)/ nocase(忽略大小写)/ fullword(整词)
字节串: { ... } 支持 ?? 通配符和跳变 { 6A 00 [4-8] 68 00 }
正则: /pattern/
条件逻辑:
all of them / any of them / 2 of them
$a in (0..100)(字符串位置范围)
filesize / uint16(0)(PE 头判断)/ entrypoint
与或非: and/or/not,括号分组
常用模块:
PE 模块: pe.imphash() / pe.sections[0].name
ELF 模块: elf.arch
Cuckoo/马哈模块(行为特征,较少用)
50.2 YARA 规则设计哲学(工程化)
□ 稳定性优先: 特征选"难变的部分"(API 名/协议格式/固定字节)
避免: 频繁变动的字符串/URL(家族换域名就失效)
□ 分层策略:
强特征(唯一字节模式)→ 家族级精准
弱特征(通用 API 组合)→ 泛化兜底
□ 避免误报:
避免纯 URL/纯文件名(太通用)
结合结构特征(PE 入口点+导入+字节)
用条件限制(filesize/多个特征同时)
□ 避免规避:
避免明文长字符串(攻击者会改)
用字节串/正则/逻辑组合代替单点特征
□ 性能: 规则多时注意匹配效率(先快速条件后慢速条件)
50.3 YARA 编写流程
1. 分析样本 → 提取稳定特征(去混淆后字符串/结构/字节模式)
2. 起草规则(分层: 家族级 + 变种级)
3. 测试: yara rule.yar sample(命中)
4. 反例测试: 跑干净样本库(无误报)
5. 迭代: 调条件降误报/升召回
6. 发布: 规范 meta(作者/描述/参考)
工具:
yara (CLI): yara -s rule.yar sample
yara-python: 集成进自动化管道
规则管理: 团队规则库 + CI 验证
50.4 Sigma 规则(行为检测)
与 YARA 的区别(核心认知)
YARA: 描述"文件长什么样" → 静态文件扫描
Sigma: 描述"日志里发生了什么" → SIEM 检测(Windows EventLog/Sysmon 等)
语法速成
title: 可疑 PowerShell 编码执行
id: 6e2730a9-...
status: test
logsource:
category: process_creation
product: windows
detection:
selection:
Image|endswith: '\powershell.exe'
CommandLine|contains|all:
- '-enc'
- 'JAB'
filter:
Image|startswith: 'C:\Windows\System32\'
condition: selection and not filter
fields:
- CommandLine
- ParentImage
level: high
编写要点
logsource(日志源): 最关键的一步——确定事件在哪
category: process_creation/file_event/registry_event/network_connection
product: windows/linux
service: sysmon/security/powershell
字段修饰符:
|contains / |startswith / |endswith / |all / |any
|contains|all: 多个都要包含
|re: 正则
|field: 引用另一个字段
condition:
selection / filter(排除白名单)
selection and not filter
时间窗口(correlation 规则)
50.5 Sigma → SIEM 转换
# Sigma 是"通用语言",可转换到各家 SIEM
sigmac -t splunk -c tools/config rule.yml
sigmac -t elasticsearch -t rule.yml
sigmac -t qradar / microsoft365defender / sentinel ...
# Sigma CLI(新生态)
sigma convert -t splunk rule.yml
sigma check rule.yml # 语法校验
50.6 规则工程化实践(完整流水线)
样本/事件分析
→ 特征提取(YARA: 文件特征;Sigma: 日志特征)
→ 规则编写(分层/防误报)
→ 自动化测试(命中率 + 误报率)
→ 版本管理(规则库 Git + 评审)
→ 发布/部署(扫描器/SIEM)
→ 定期更新(新变种出现时迭代)
50.7 常见检测场景的规则思路
| 场景 | YARA 思路 | Sigma 思路 |
|---|---|---|
| 已知家族变种 | 家族稳定字节+API 组合 | 对应行为的日志模式 |
| 加密 shellcode | 高熵+无合法 PE 结构 | 进程内存异常(有数据时) |
| C2 通信 | 固定协议头/域名模式 | DNS/网络连接异常 |
| 挖矿 | 矿池地址+worker 格式 | 进程+网络组合 |
| 窃密 | 浏览器文件访问特征 | 文件访问异常序列 |
| 供应链投毒 | 包内恶意字节 | 安装后执行行为 |
动手练习
- 给一个样本写 3 版 YARA 规则(弱特征/强特征/分层),用正反样本测试召回率和误报率。
- 把一条规则从"明文 URL"改造成"协议字节+结构"(防规避练习)。
- 用 sigmac 把一条 Sigma 规则转换到两种 SIEM 格式,对比差异。
- 为 4 种检测场景各写一条 Sigma 规则,用 sigma check 校验语法。
深入阅读
- 仓库:
skills/malware-analysis/references/yara-sigma-rules.md(规则编写方法论) - 仓库:
skills/malware-analysis/SKILL.md(六阶段分析,规则是产出之一) - 仓库:
skills/malware-analysis/references/anti-analysis-techniques.md(对抗 → 规则稳定性考虑) - 参考库: SigmaHQ / YARA-Rules(GitHub 公开规则库)