cognis-digital/detectionkit
GitHub: cognis-digital/detectionkit
一个将中性检测规则格式编译为 Sigma、Elastic 和 Splunk 语法的「检测即代码」工具,支持在 CI 中自动验证规则。
Stars: 0 | Forks: 0
# detectionkit
**用于 SIEM 规则的检测即代码(Detection-as-code)。** 使用简单、中立的格式一次性编写检测规则,然后将其编译为多种 SIEM 方言 —— [Sigma](https://github.com/SigmaHQ/sigma)
YAML、Elastic 检测规则(KQL + JSON)以及 Splunk SPL 搜索 ——
并在发布前于 CI 中验证规则。
在一个地方编写逻辑;让 detectionkit 输出每个平台的语法。
这是一种防御性、分析性的工具:它只描述在日志中*需要寻找什么*,
仅此而已。
- 单一的中立规则模型:id、title、logsource、severity 以及由
字段选择器组成的布尔条件树。
- 操作符:`equals`、`contains`、`in`、`regex`,通过 `and` / `or` 组合。
- 三个编译器目标,确定性输出(经过 golden-tested)。
- 适合作为 CI 门禁的 `validate` 命令(出现任何问题均以非零状态退出)。
- 仅使用标准库。无外部依赖。
许可证:COCL 1.0
## 安装 / 构建
需要 Go 1.22+。
```
git clone https://github.com/cognis-digital/detectionkit
cd detectionkit
go build -o detectionkit ./cmd/detectionkit # build the binary
go test ./... # run the test suite
go install ./cmd/detectionkit # optional: install to $GOBIN
```
## 用法
### 将规则编译为目标
```
detectionkit compile --target sigma|elastic|splunk
```
**Sigma:**
```
$ detectionkit compile examples/windows_failed_logons.json --target sigma
title: Multiple Windows Failed Logons From Single Source
id: ckd-0001-win-failed-logons
description: "Detects Windows Security event 4625 (failed logon) with a non-interactive logon type, indicative of password spraying or brute force."
author: Cognis Digital
logsource:
category: authentication
product: windows
service: security
detection:
sel0:
EventID: 4625
sel1:
LogonType:
- 3
- 10
condition: (sel0 and sel1)
tags:
- attack.credential_access
- attack.t1110
level: medium
```
**Splunk (SPL):**
```
$ detectionkit compile examples/suspicious_powershell.json --target splunk
search sourcetype=windows (Image=*powershell* AND (CommandLine=*-EncodedCommand* OR CommandLine=*DownloadString* OR | regex CommandLine="(?i)-e(nc)?\s+[A-Za-z0-9+/=]{40,}")) | eval detection_id=ckd-0002-suspicious-powershell, severity=high
```
**Elastic(包含在 detection-rule JSON 中的 KQL):**
```
$ detectionkit compile examples/windows_failed_logons.json --target elastic
{
"rule_id": "ckd-0001-win-failed-logons",
"name": "Multiple Windows Failed Logons From Single Source",
"description": "Detects Windows Security event 4625 ...",
"severity": "medium",
"type": "query",
"language": "kuery",
"query": "(EventID : \"4625\" and LogonType : (\"3\" or \"10\"))",
"index": [
"winlogbeat-*",
"logs-windows.*"
],
"tags": [
"attack.credential_access",
"attack.t1110"
]
}
```
### 验证规则(CI 门禁)
检查必填字段、有效的 severity、log-source 的完整性,
以及格式良好的 condition(operator/value 配对、regex 编译、字段名称)。
出现任何问题都会以非零状态退出,从而中断 pipeline。
```
$ detectionkit validate examples/windows_failed_logons.json
OK examples/windows_failed_logons.json (medium, 2 field(s))
$ echo $?
0
```
无效的规则会一次性报告所有问题并退出状态 1:
```
$ detectionkit validate broken.json
FAIL broken.json: 2 validation problem(s):
- invalid severity "spicy" (want one of: critical, high, informational, low, medium)
- condition.and[0]: operator "in" requires non-empty values list
```
### 列出带有覆盖率的规则目录
```
$ detectionkit list examples
ID TITLE SEVERITY FIELDS VALID TARGETS
------------------------------------------------------------------------------------------------
ckd-0003-linux-sshd-b… Linux SSHD Repeated Authentic… low 2 yes sigma,elastic,splunk
ckd-0002-suspicious-p… Suspicious PowerShell Encoded… high 2 yes sigma,elastic,splunk
ckd-0001-win-failed-l… Multiple Windows Failed Logon… medium 2 yes sigma,elastic,splunk
3 rule(s), 3 valid
```
## 规则格式
规则为 JSON 格式。condition 是一棵树:每个节点要么是一个 **selector**
叶子节点,要么是一个由子节点组成的 **and**/**or** 组。
```
{
"id": "ckd-0001-win-failed-logons",
"title": "Multiple Windows Failed Logons From Single Source",
"description": "...",
"author": "Cognis Digital",
"severity": "medium",
"tags": ["attack.credential_access", "attack.t1110"],
"logsource": { "category": "authentication", "product": "windows", "service": "security" },
"condition": {
"and": [
{ "selector": { "field": "EventID", "operator": "equals", "value": "4625" } },
{ "selector": { "field": "LogonType", "operator": "in", "values": ["3", "10"] } }
]
}
}
```
| 字段 | 备注 |
|--------------|-----------------------------------------------------------------------|
| `id` | 必填。稳定的标识符。 |
| `title` | 必填。人类可读的名称。 |
| `severity` | 必填。以下之一:informational, low, medium, high, critical。 |
| `logsource` | 至少包含 category / product / service 中的一项。 |
| `condition` | 一个 selector 叶子节点,或一个由条件组成的 `and`/`or` 组。 |
**selector 操作符:**
| 操作符 | 操作数 | 含义 |
|------------|------------|------------------------------------------|
| `equals` | `value` | 精确字段匹配。 |
| `contains` | `value` | 子字符串 / 通配符匹配。 |
| `in` | `values[]` | 字段匹配列表中的任意值。 |
| `regex` | `value` | 正则表达式匹配(Go RE2)。 |
请参阅 [`examples/`](examples/) 中的三个已编写规则,涵盖了 Windows 登录失败、
可疑的 PowerShell 以及 Linux SSH 暴力破解。
## 功能
- **单一来源,三个目标** —— Sigma YAML、Elastic 检测规则(KQL +
index 提示)以及 Splunk SPL,全部源自单一的中立规则。
- **条件模型** —— 带有 `equals` / `contains` / `in` /
`regex` 的字段 selector,通过任意嵌套的 `and` / `or` 组合。
- **作为 CI 门禁的验证** —— 必填字段、severity 词汇表、
log-source 存在性、operator/value 配对、regex 编译以及字段名称
合理性检查;汇总所有问题并在失败时以非零状态退出。
- **覆盖率列表** —— 规则目录的表格视图,包含每个规则的计数字段、
有效性以及可编译的目标。
- **确定性输出** —— 稳定的键/选择顺序,由
每个目标的 golden-string 测试验证。
- **零依赖** —— 纯 Go 标准库。
## 项目结构
```
cmd/detectionkit/ CLI entry point (compile / validate / list)
internal/rule/ neutral rule model, parsing, validation
internal/compile/ sigma / elastic / splunk compilers
internal/listing/ directory scan + coverage table
examples/ authored sample detection rules
```
## 测试
```
go test ./...
```
测试套件通过 golden-output 断言覆盖了每个编译器目标,涵盖了格式错误规则的验证
通过/失败情况、目录列表/覆盖率扫描以及 CLI
退出代码(包括 `validate` CI 门禁)。
维护者:**Cognis Digital**
许可证:**COCL 1.0**
标签:EVTX分析, Go语言, Homebrew安装, 安全规则编译器, 安全运营, 扫描框架, 文档结构分析, 日志审计, 检测即代码, 程序破解