CraftedSignal/sigma-parser

GitHub: CraftedSignal/sigma-parser

一个生产级 Go 语言编写的 Sigma 检测规则解析器,用于从 YAML 规则文件中提取条件表达式、字段映射和检测逻辑。

Stars: 1 | Forks: 0

# Sigma 解析器 [![Go Reference](https://pkg.go.dev/badge/github.com/craftedsignal/sigma-parser.svg)](https://pkg.go.dev/github.com/craftedsignal/sigma-parser) [![Go Report Card](https://goreportcard.com/badge/github.com/craftedsignal/sigma-parser)](https://goreportcard.com/report/github.com/craftedsignal/sigma-parser) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) 一个用于 [Sigma](https://sigmahq.io/) 检测规则的生产级 Go 解析器。从基于 YAML 的 Sigma 规则中提取条件、字段和检测逻辑。使用 `yaml.v3` 和递归下降条件解析器(无需 ANTLR)。 ## 功能特性 - **完整的检测解析**:映射、列表、关键词列表、空值、通配符 - **所有 18 种 Sigma 修饰符**:contains, startswith, endswith, re, cidr, base64, base64offset, wide/utf16, windash, all, exists, fieldref, gt/gte/lt/lte, expand - **条件解析器**:用于 AND/OR/NOT、括号、量词(`all of them`, `1 of selection_*`)的递归下降解析 - **完整的聚合支持**:count/sum/min/max/avg 及 group-by 和比较运算符,near 及 timeframe - **3,100+ 规则语料库**:针对整个 SigmaHQ 规则库进行了测试 - **模糊测试**:在任意输入下无崩溃 ## 安装 ``` go get github.com/craftedsignal/sigma-parser ``` ## 用法 ### 基础条件提取 ``` package main import ( "fmt" sigma "github.com/craftedsignal/sigma-parser" ) func main() { rule := ` title: Mimikatz Usage status: stable level: critical logsource: category: process_creation product: windows detection: selection: Image|endswith: '\mimikatz.exe' CommandLine|contains: - 'sekurlsa::' - 'kerberos::' condition: selection tags: - attack.credential_access - attack.t1003.001 ` result := sigma.ExtractConditions(rule) fmt.Printf("Title: %s (Level: %s)\n", result.Title, result.Level) fmt.Printf("Found %d conditions:\n", len(result.Conditions)) for _, cond := range result.Conditions { fmt.Printf(" Field: %s, Operator: %s, Value: %s\n", cond.Field, cond.Operator, cond.Value) if len(cond.Alternatives) > 0 { fmt.Printf(" Alternatives: %v\n", cond.Alternatives) } } } ``` ### 输出 ``` Title: Mimikatz Usage (Level: critical) Found 2 conditions: Field: Image, Operator: endswith, Value: \mimikatz.exe Field: CommandLine, Operator: contains, Value: sekurlsa:: Alternatives: [sekurlsa:: kerberos::] ``` ### 聚合规则 ``` rule := ` title: Brute Force detection: selection: EventID: 4625 timeframe: 5m condition: selection | count() by SourceIP > 10 ` result := sigma.ExtractConditions(rule) fmt.Println(result.GroupByFields) // [SourceIP] fmt.Println(result.Commands) // [count] ``` ## 支持的 Sigma 特性 | 特性 | 状态 | |---------|--------| | 字段:值映射 | 支持 | | 映射列表 (OR) | 支持 | | 关键词列表 | 支持 | | 空值 | 支持 | | 通配符 (*,?) | 支持 | | 所有 18 种修饰符 | 支持 | | 条件表达式 | 支持 | | 量词 (1 of, all of) | 支持 | | 聚合 (count/sum/min/max/avg) | 支持 | | Near 聚合 | 支持 | | 时间范围 | 支持 | | LogSource 元数据 | 支持 | | 标签 (MITRE ATT&CK) | 支持 | | 多条件字符串 | 支持 | ## API 参考 ### 类型 ``` type Condition struct { Field string // Field name (empty for keywords) Operator string // "=", "contains", "startswith", "endswith", "matches", "cidrmatch", etc. Value string // The condition value Negated bool // True if condition is negated (NOT) LogicalOp string // "AND" or "OR" connecting to previous condition Alternatives []string // Multiple values grouped by OR on same field } type ParseResult struct { Conditions []Condition GroupByFields []string // From aggregation group-by ComputedFields map[string]string // Always empty for Sigma Commands []string // Aggregation functions detected Errors []string // Parse warnings LogSource *LogSource // category/product/service Level string // informational, low, medium, high, critical Status string // experimental, test, stable Title string Tags []string // MITRE ATT&CK tags } ``` ### 函数 ``` func ExtractConditions(yamlContent string) *ParseResult ``` ## 测试 ``` # 运行所有测试(包括 3,100+ SigmaHQ 语料库) make test # 运行 Fuzz 测试 make fuzz # 运行 Benchmarks make benchmark ``` ## 许可证 MIT 许可证 - 详情请参阅 [LICENSE](LICENSE)。 ## 相关项目 - [spl-parser](https://github.com/craftedsignal/spl-parser) - Splunk Processing Language 解析器 - [kql-parser](https://github.com/craftedsignal/kql-parser) - Kusto Query Language 解析器 - [leql-parser](https://github.com/craftedsignal/leql-parser) - Rapid7 LEQL 解析器 - [CraftedSignal](https://craftedsignal.com) - 检测工程平台
标签:AMSI绕过, Cloudflare, EVTX分析, FTP漏洞扫描, Go库, Go语言, MITRE ATT&CK, Sigma规则, SOAR, URL发现, YAML解析器, 云计算, 命令行解析, 威胁检测, 安全检测, 安全编排, 日志审计, 生产可用, 目标导入, 程序破解, 网络安全, 自动化响应, 规则引擎, 解析库, 隐私保护