使用Sigma构建检测规则 是做什么的?
何时使用
在以下场景使用此技能:
- SOC工程师需要创建可跨多个SIEM平台移植的检测规则
- 威胁情报报告描述了需要新的检测覆盖的TTPs
- 现有的厂商特定规则需要标准化为可共享的格式
- 团队在CI/CD流水线中采用Sigma作为检测即代码标准
不要用于实时流式检测(Sigma适用于批量/计划搜索)或当目标SIEM具有Sigma无法表达的原生检测功能时(例如,Splunk RBA风险评分)。
前提条件
- Python 3.8+,包含
pySigma和相应的后端(pySigma-backend-splunk、pySigma-backend-elasticsearch、pySigma-backend-microsoft365defender) - 克隆的Sigma规则仓库:
git clone https://github.com/SigmaHQ/sigma.git - MITRE ATT&CK框架知识,用于技术映射
- 理解目标SIEM日志源字段映射
工作流程
步骤 1:从威胁情报定义检测逻辑
从威胁报告或ATT&CK技术开始。示例:检测Mimikatz凭证窃取(T1003.001 — LSASS内存):
title: Mimikatz Credential Dumping via LSASS Access
id: 0d894093-71bc-43c3-8d63-bf520e73a7c5
status: stable
level: high
description: Detects process accessing lsass.exe memory, indicative of credential dumping tools like Mimikatz
references:
- https://attack.mitre.org/techniques/T1003/001/
- https://github.com/gentilkiwi/mimikatz
author: mahipal
date: 2024/03/15
modified: 2024/03/15
tags:
- attack.credential_access
- attack.t1003.001
logsource:
category: process_access
product: windows
detection:
selection:
TargetImage|endswith: '\lsass.exe'
GrantedAccess|contains:
- '0x1010'
- '0x1038'
- '0x1fffff'
- '0x40'
filter_main_svchost:
SourceImage|endswith: '\svchost.exe'
filter_main_csrss:
SourceImage|endswith: '\csrss.exe'
filter_main_wininit:
SourceImage|endswith: '\wininit.exe'
condition: selection and not 1 of filter_main_*
falsepositives:
- Legitimate security tools accessing LSASS
- Windows Defender scanning
- CrowdStrike Falcon sensor
步骤 2:验证Sigma规则语法
使用 sigma check 验证规则:
# 安装pySigma和验证器
pip install pySigma pySigma-validators-sigmaHQ
# 验证规则
sigma check rule.yml
或者,使用Python进行验证:
from sigma.rule import SigmaRule
from sigma.validators.core import SigmaValidator
rule = SigmaRule.from_yaml(open("rule.yml").read())
validator = SigmaValidator()
issues = validator.validate_rule(rule)
for issue in issues:
print(f"{issue.severity}: {issue.message}")
步骤 3:转换为目标SIEM查询
转换为Splunk SPL:
from sigma.rule import SigmaRule
from sigma.backends.splunk import SplunkBackend
from sigma.pipelines.splunk import splunk_windows_pipeline
pipeline = splunk_windows_pipeline()
backend = SplunkBackend(pipeline)
rule = SigmaRule.from_yaml(open("rule.yml").read())
splunk_query = backend.convert_rule(rule)
print(splunk_query[0])
输出:
TargetImage="*\lsass.exe" (GrantedAccess="*0x1010*" OR GrantedAccess="*0x1038*"
OR GrantedAccess="*0x1fffff*" OR GrantedAccess="*0x40*")
NOT (SourceImage="*\svchost.exe") NOT (SourceImage="*\csrss.exe")
NOT (SourceImage="*\wininit.exe")
转换为Elastic查询(Lucene):
from sigma.backends.elasticsearch import LuceneBackend
from sigma.pipelines.elasticsearch import ecs_windows_pipeline
pipeline = ecs_windows_pipeline()
backend = LuceneBackend(pipeline)
elastic_query = backend.convert_rule(rule)
print(elastic_query[0])
转换为Microsoft Sentinel KQL:
from sigma.backends.microsoft365defender import Microsoft365DefenderBackend
backend = Microsoft365DefenderBackend()
kql_query = backend.convert_rule(rule)
print(kql_query[0])
步骤 4:映射到MITRE ATT&CK并添加覆盖元数据
在 tags 字段中为每个规则标记ATT&CK技术ID:
tags:
- attack.credential_access # 策略
- attack.t1003.001 # 子技术
- attack.t1003 # 父技术
使用ATT&CK Navigator跟踪检测覆盖范围:
import json
# 从Sigma规则生成ATT&CK Navigator层
layer = {
"name": "SOC检测覆盖",
"versions": {"attack": "14", "navigator": "4.9", "layer": "4.5"},
"domain": "enterprise-attack",
"techniques": []
}
# 解析Sigma规则目录以获取技术标签
import os
from sigma.rule import SigmaRule
for root, dirs, files in os.walk("sigma/rules/windows/"):
for f in files:
if f.endswith(".yml"):
rule = SigmaRule.from_yaml(open(os.path.join(root, f)).read())
for tag in rule.tags:
if str(tag).startswith("attack.t"):
technique_id = str(tag).replace("attack.", "").upper()
layer["techniques"].append({
"techniqueID": technique_id,
"color": "#31a354",
"score": 1
})
with open("coverage_layer.json", "w") as f:
json.dump(layer, f, indent=2)
步骤 5:对样本数据测试规则
创建测试数据并验证规则能够捕获预期事件:
# 使用sigma测试框架
sigma test rule.yml --target splunk --pipeline splunk_windows
# 或使用样本数据在Splunk中手动测试
# 上传具有已知Mimikatz签名的Sysmon process_access日志
通过在非告警的保存搜索中对7天的生产数据运行来验证误报率。
步骤 6:部署到生产SIEM
将转换后的查询部署为计划搜索或关联规则:
Splunk ES关联搜索:
| tstats summariesonly=true count from datamodel=Endpoint.Processes
where Processes.process_name="*\lsass.exe"
by Processes.src, Processes.user, Processes.process_name, Processes.parent_process_name
| `drop_dm_object_name(Processes)`
| where count > 0
Elastic Security规则(TOML格式):
[rule]
name = "LSASS内存访问 - 凭证窃取"
description = "检测对LSASS进程内存的可疑访问"
risk_score = 73
severity = "high"
type = "eql"
query = '''
process where event.action == "access" and
process.name == "lsass.exe" and
not process.executable : ("*\svchost.exe", "*\csrss.exe")
'''
[rule.threat]
framework = "MITRE ATT&CK"
[[rule.threat.technique]]
id = "T1003"
name = "OS凭据窃取"
步骤 7:版本控制与CI/CD集成
将规则存储在Git中,并进行自动化测试:
# .github/workflows/sigma-ci.yml
name: Sigma规则CI
on: [push, pull_request]
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- run: pip install pySigma pySigma-validators-sigmaHQ
- run: sigma check rules/
- run: sigma convert -t splunk -p splunk_windows rules/ > /dev/null
关键概念
| 术语 | 定义 |
|---|---|
| Sigma | 与厂商无关的检测规则格式(基于YAML),通过后端编译为SIEM特定查询 |
| pySigma | Python库,取代旧版sigmac,用于规则转换、验证和流水线处理 |
| 后端 (Backend) | pySigma插件,将Sigma检测逻辑转换为目标平台查询语言(SPL、KQL、Lucene) |
| 流水线 (Pipeline) | 字段映射配置,将通用Sigma字段名称转换为SIEM特定字段名称 |
| 日志源 (Logsource) | Sigma规则部分,定义目标数据的类别(process_creation、network_connection)和产品(windows、linux) |
| 检测即代码 (Detection-as-Code) | 在版本控制中管理检测规则,并配备CI/CD测试和自动化部署的实践 |
工具与系统
- SigmaHQ:官方Sigma规则仓库,在GitHub上包含3000多个社区维护的检测规则
- pySigma:基于Python的Sigma规则处理框架,具有模块化后端和流水线
- ATT&CK Navigator:MITRE工具,用于可视化映射到ATT&CK技术的检测覆盖范围
- Uncoder.IO:基于Web的Sigma规则转换器,支持30多个SIEM平台,用于快速转换
常见场景
- 新CVE检测:编写用于利用指标的Sigma规则(例如,Web日志中的Log4Shell JNDI查找模式)
- 狩猎规则升级:将临时Splunk狩猎查询转换为Sigma规则,用于持续自动化检测
- 多SIEM迁移:将500多个Splunk关联搜索转换为Sigma,以便迁移到Elastic Security
- 紫队输出:将红队发现转换为Sigma规则,以立即获得防御覆盖
- 威胁情报操作化:将基于IOC的威胁报告转换为行为Sigma规则
输出格式
SIGMA规则部署报告
━━━━━━━━━━━━━━━━━━━━━━━━━━━
规则ID: 0d894093-71bc-43c3-8d63-bf520e73a7c5
标题: Mimikatz通过LSASS访问进行凭证窃取
ATT&CK: T1003.001 - LSASS内存
严重性: 高
状态: 已部署到生产环境
转换:
Splunk SPL: 通过 — 已创建保存搜索"sigma_lsass_access"
Elastic EQL: 通过 — 检测规则ID elastic-0d894093已启用
Sentinel KQL: 通过 — 分析规则已通过ARM模板部署
测试:
真正例: 4/4个测试用例匹配
误报: 7天回测中有2个(svchost边缘情况 - 已添加过滤器)
性能: 在每天5000万事件上平均执行3.2秒