Communitygithub.com

使用Sigma构建检测规则

构建与SIEM平台无关的检测规则,使用Sigma规则格式。

使用Sigma构建检测规则 是什么?

该技能指导SOC工程师、威胁情报分析师和检测工程师使用Sigma格式构建可移植的检测规则。Sigma是一种与厂商无关的、基于YAML的规则格式,可编译为Splunk SPL、Elastic EQL和Microsoft Sentinel KQL等SIEM特定查询,通过pySigma后端实现。工作流程从威胁情报或MITRE ATT&CK技术(如T1003.001凭证窃取)出发,定义检测逻辑,使用Python库验证规则语法,并利用pySigma后端转换为目标SIEM查询语言。规则通过ATT&CK标签映射覆盖范围,使用测试框架对样本数据进行测试,并作为计划搜索或关联规则部署到生产SIEM中。该技能还涵盖了版本控制、CI/CD集成(如GitHub Actions自动化和规则验证)以及检测即代码实践,使团队能够将检测规则作为软件构件进行管理。通过将规则标准化为Sigma格式,组织可以避免厂商锁定,促进多SIEM迁移,并提高威胁检测能力在整个安全栈中的一致性。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/mukul975/Anthropic-Cybersecurity-Skills/tree/main/skills/building-detection-rules-with-sigma

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

使用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特定查询
pySigmaPython库,取代旧版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秒

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

abusing-dpapi-for-credential-access

Extract and decrypt Windows DPAPI-protected secrets (Credential Manager, browser logins/cookies, Wi-Fi credentials, KeePass keys) online or offline using SharpDPAPI, SharpChrome, Mimikatz, or Impacket

abusing-shadow-credentials-for-privesc

Take over Active Directory accounts by writing attacker-controlled public keys to msDS-KeyCredentialLink (Shadow Credentials) with pyWhisker, Whisker, or Certipy, then authenticate via PKINIT to recover the target

achieving-cmmc-level-2-compliance

>-

acquiring-disk-image-with-dd-and-dcfldd

Create forensically sound bit-for-bit disk images with dd or dcfldd on a Linux forensic workstation, preserving evidence integrity through hash verification (MD5/SHA) during acquisition. Use when imaging a suspect drive, USB device, or memory card for investigation, preserving volatile disk evidence during incident response, or producing a verified copy for legal or law-enforcement proceedings before any destructive analysis.

analyzing-active-directory-acl-abuse

Detect dangerous ACL misconfigurations in Active Directory using ldap3

analyzing-android-malware-with-apktool

Perform static analysis of Android APK malware using apktool for resource decompilation, jadx for Java source recovery, and androguard for manifest inspection, dangerous permission-combination detection, and identification of obfuscated code, dynamic code loading, and reflection-based API calls. Use to statically triage a suspicious APK without executing it or to build mobile malware detection rules.

analyzing-api-gateway-access-logs

Parses API Gateway access logs (AWS API Gateway, Kong, Nginx) to detect

analyzing-apt-group-with-mitre-navigator

Query ATT&CK data with attackcti, mitreattack-python, and stix2, then build MITRE ATT&CK Navigator layers and multi-layer heatmap overlays mapping one or more APT groups

analyzing-azure-activity-logs-for-threats

Queries Azure Monitor activity logs and sign-in logs via azure-monitor-query

analyzing-bootkit-and-rootkit-samples

Analyzes bootkit and advanced rootkit malware infecting the Master

analyzing-browser-forensics-with-hindsight

Parse Chromium-based browser databases with Hindsight to extract and correlate browsing history, downloads, cookies, cached content, autofill data, saved passwords, and extensions from Chrome, Edge, Brave, Opera, and Vivaldi into a unified timeline (XLSX, JSON, or SQLite output). Use during incident response, insider-threat investigations, or criminal cases when you need to reconstruct a user

analyzing-campaign-attribution-evidence

Systematically evaluate cyber-campaign evidence to attribute an operation to a threat actor, using the Diamond Model and Analysis of Competing Hypotheses (ACH) to weigh infrastructure overlaps, TTP consistency, malware code similarity, and timing/language artifacts into confidence-weighted attribution assessments. Use when an incident investigation needs a defensible attribution confidence level.

analyzing-certificate-transparency-for-phishing

Monitor Certificate Transparency logs using crt.sh and Certstream to

analyzing-cloud-storage-access-patterns

Detect abnormal access in AWS S3, GCS, and Azure Blob Storage by analyzing CloudTrail Data Events, GCS audit logs, and Azure Storage Analytics for after-hours bulk downloads, new-IP access, and API-call spikes (e.g. GetObject) via statistical baselines and time-series anomaly detection. Use when investigating suspected cloud data exfiltration or building related detection rules.

analyzing-cobalt-strike-beacon-configuration

Extract and analyze Cobalt Strike beacon configuration from PE files

analyzing-cobaltstrike-malleable-c2-profiles

Parse and analyze Cobalt Strike Malleable C2 profiles with dissect.cobaltstrike (profiles and beacon-payload configs) and pyMalleableC2 (AST parsing) to extract HTTP/DNS transforms, URIs, headers, sleep/jitter, and injection behavior, then generate network detection signatures. Use when reverse-engineering a captured malleable profile or building detections against Cobalt Strike Beacon traffic.

analyzing-command-and-control-communication

Analyzes malware C2 communication over HTTP, HTTPS, DNS, and custom

analyzing-cyber-kill-chain

Analyzes intrusion activity against the Lockheed Martin Cyber Kill Chain

analyzing-disk-image-with-autopsy

Perform comprehensive forensic analysis of raw (dd), E01, or AFF disk images with Autopsy and The Sleuth Kit, recovering deleted files, examining metadata and embedded artifacts, keyword searching, and building investigation timelines with visual reports. Use for structured analysis of a forensic disk image or when stakeholders need visual reports from evidence.

analyzing-dns-logs-for-exfiltration

Analyzes DNS query logs to detect data exfiltration via DNS tunneling,

相关技能