mcpField
B
building-detection-rules-with-sigma

building-detection-rules-with-sigma

github.com/mukul975安全35.2k196 次安装8 周前更新
代码仓库

关于 building-detection-rules-with-sigma

面向 SOC 工程师的 Sigma 检测规则编写技能,涵盖规则校验、映射到不同 SIEM 查询、测试和部署。适合将威胁情报或狩猎发现转化为可移植的检测规则。

主要功能

  1. 01编写 YAML 格式的 Sigma 规则
  2. 02校验规则语法
  3. 03转换为 SIEM 查询
  4. 04映射 MITRE ATT&CK 技术
  5. 05测试规则并评估误报
  6. 06通过版本控制和 CI/CD 管理

适用场景

  1. 01将威胁情报转为检测规则
  2. 02把临时狩猎查询推广为持续检测
  3. 03为 SIEM 迁移标准化规则
  4. 04将红队发现转化为防御覆盖

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/mukul975/anthropic-cybersecurity-skills --skill building-detection-rules-with-sigma

安装后在 Claude Code 中输入/building-detection-rules-with-sigma确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

LICENSE
references/
api-reference.md
scripts/
agent.py

详细介绍

使用 Sigma 构建检测规则

何时使用

在以下情况下使用本技能:

  • SOC 工程师需要创建可跨多个 SIEM 平台移植的检测规则
  • 威胁情报报告描述了需要新增检测覆盖的 TTP
  • 需要将现有的厂商特定规则标准化为可共享格式
  • 团队在 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 验证规则:

# Install pySigma and validators
pip install pySigma pySigma-validators-sigmaHQ

# Validate rule
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        # Tactic
    - attack.t1003.001                # Sub-technique
    - attack.t1003                    # Parent technique

使用 ATT&CK Navigator 跟踪检测覆盖:

import json

# Generate ATT&CK Navigator layer from Sigma rules
layer = {
    "name": "SOC Detection Coverage",
    "versions": {"attack": "14", "navigator": "4.9", "layer": "4.5"},
    "domain": "enterprise-attack",
    "techniques": []
}

# Parse Sigma rules directory for technique tags
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:针对样本数据测试规则

创建测试数据并验证规则能够捕获预期事件:

# Use sigma test framework
sigma test rule.yml --target splunk --pipeline splunk_windows

# Or manually test in Splunk with sample data
# Upload Sysmon process_access log with known Mimikatz signature

在非告警的已保存搜索中针对 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 Memory Access - Credential Dumping"
description = "Detects suspicious access to LSASS process memory"
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 Credential Dumping"
步骤 7:版本控制与 CI/CD 集成

将规则存储在 Git 中并进行自动化测试:

# .github/workflows/sigma-ci.yml
name: Sigma Rule 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替代旧版 sigmac 的 Python 库,用于规则转换、验证和流水线处理
后端pySigma 插件,将 Sigma 检测逻辑转换到目标平台查询语言(SPL、KQL、Lucene)
流水线字段映射配置,将通用 Sigma 字段名转换为特定 SIEM 的字段名
日志源Sigma 规则中的部分,定义目标数据的类别(process_creation、network_connection)和产品(windows、linux)
检测即代码在版本控制中管理检测规则,并配合 CI/CD 测试和自动化部署的实践

工具与系统

  • SigmaHQ:官方 Sigma 规则仓库,在 GitHub 上有 3,000+ 社区维护的检测规则
  • 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 RULE DEPLOYMENT REPORT
━━━━━━━━━━━━━━━━━━━━━━━━━━━
Rule ID:      0d894093-71bc-43c3-8d63-bf520e73a7c5
Title:        Mimikatz Credential Dumping via LSASS Access
ATT&CK:       T1003.001 - LSASS Memory
Severity:     High
Status:       Deployed to Production

Conversions:
  Splunk SPL:    PASS — Saved search "sigma_lsass_access" created
  Elastic EQL:   PASS — Detection rule ID elastic-0d894093 enabled
  Sentinel KQL:  PASS — Analytics rule deployed via ARM template

Testing:
  True Positives:    4/4 test cases matched
  False Positives:   2 in 7-day backtest (svchost edge case — filter added)
  Performance:       Avg execution 3.2s on 50M events/day

评论

支持 Markdown