timescale/rsigma
GitHub: timescale/rsigma
一个用 Rust 实现的 Sigma 检测标准完整工具包,集解析器、评估引擎、linter 和 LSP 于一身,可脱离外部 SIEM 直接对 JSON 日志进行实时检测。
Stars: 1 | Forks: 0
# RSigma
一个完整的 Rust [Sigma](https://github.com/SigmaHQ/sigma) 检测标准工具包 —— 包含解析器、评估引擎、linter、CLI 和 LSP。rsigma 将 Sigma YAML 规则解析为强类型 AST,将其编译为优化的匹配器,并直接针对 JSON 日志事件进行实时评估。它在进程内运行检测和有状态关联逻辑,采用内存高效的压缩事件存储,支持 pySigma 兼容的处理管道以进行字段映射和后端配置,并可从 NDJSON 输入流式传输结果 —— 无需外部 SIEM。内置的 linter 根据 Sigma v2.1.0 规范衍生的 65 项检查来验证规则,具有四个严重级别、完整的抑制系统,并支持针对 13 条安全规则的自动修复 (`--fix`)。LSP 服务器在任何编辑器中提供实时诊断、补全、悬停文档和快速修复代码操作。
| Crate | 描述 |
|-------|-------------|
| [`rsigma-parser`](crates/rsigma-parser/) | 将 Sigma YAML 解析为强类型 AST |
| [`rsigma-eval`](crates/rsigma-eval/) | 编译规则并针对 JSON 事件进行评估 |
| [`rsigma`](crates/rsigma-cli/) | 用于解析、验证、linting、评估规则以及运行检测守护进程的 CLI |
| [`rsigma-lsp`](crates/rsigma-lsp/) | 用于 IDE 支持的 Language Server Protocol (LSP) 服务器 |
## 安装
```
# 构建所有 crates
cargo build --release
# 安装 CLI
cargo install rsigma
# 安装 LSP server
cargo install --path crates/rsigma-lsp
```
## 快速开始
从命令行针对 Sigma 规则评估事件:
```
# 单个事件(内联 JSON)
rsigma eval -r path/to/rules/ -e '{"CommandLine": "cmd /c whoami"}'
# 从 stdin 流式传输 NDJSON
cat events.ndjson | rsigma eval -r path/to/rules/
# 具有 hot-reload 和 Prometheus metrics 的长期运行 daemon
hel run | rsigma daemon -r rules/ -p ecs.yml --api-addr 0.0.0.0:9090
# 带有用于 field mapping 的处理流水线
rsigma eval -r rules/ -p pipelines/ecs.yml -e '{"process.command_line": "whoami"}'
```
或者直接使用该库:
```
use rsigma_parser::parse_sigma_yaml;
use rsigma_eval::{Engine, Event};
use serde_json::json;
let yaml = r#"
title: Detect Whoami
logsource:
product: windows
category: process_creation
detection:
selection:
CommandLine|contains: 'whoami'
condition: selection
level: medium
"#;
let collection = parse_sigma_yaml(yaml).unwrap();
let mut engine = Engine::new();
engine.add_collection(&collection).unwrap();
let event = Event::from_value(&json!({"CommandLine": "cmd /c whoami"}));
let matches = engine.evaluate(&event);
assert_eq!(matches[0].rule_title, "Detect Whoami");
```
## 架构
```
┌──────────────────┐
YAML input ───> │ serde_yaml │──> Raw YAML Value
└──────────────────┘
│
▼
┌──────────────────┐
│ parser.rs │──> Typed AST
│ (YAML → AST) │ (SigmaRule, CorrelationRule,
└──────────────────┘ FilterRule, SigmaCollection)
│
┌────────────────┼──────────────┐
▼ ▼ ▼
┌────────────┐ ┌────────────┐ ┌────────────┐
│ sigma.pest │ │ value.rs │ │ ast.rs │
│ (PEG │ │ (SigmaStr, │ │ (AST types │
│ grammar) │ │ wildcards,│ │ modifiers,│
│ + │ │ timespan) │ │ enums) │
│condition.rs│ └────────────┘ └────────────┘
│ (Pratt │
│ parser) │
└────────────┘
│
┌─────┴───────────────────────────────────────────────┐
│ │
▼ ▼
┌──────────────────────────────────────────┐ ┌────────────────────┐
│ rsigma-eval │ │ rsigma-lsp │
│ │ │ │
│ pipeline/ ──> Pipeline (YAML parsing, │ │ LSP server over │
│ conditions, transformations, state) │ │ stdio (tower-lsp) │
│ ↓ transforms SigmaRule AST │ │ │
│ │ │ • diagnostics │
│ compiler.rs ──> CompiledRule │ │ (lint + parse │
│ matcher.rs ──> CompiledMatcher │ │ + compile) │
│ engine.rs ──> Engine (stateless) │ │ • completions │
│ │ │ • hover │
│ correlation.rs ──> CompiledCorrelation │ │ • document │
│ + EventBuffer (deflate-compressed) │ │ symbols │
│ correlation_engine.rs ──> (stateful) │ │ │
│ sliding windows, group-by, chaining, │ │ Editors: │
│ alert suppression, action-on-fire, │ │ VSCode, Neovim, │
│ memory management, event inclusion │ │ Helix, Zed, ... │
│ │ └────────────────────┘
│ rsigma.* custom attributes ─────────> │
│ engine config from pipelines │
└──────────────────────────────────────────┘
│
▼
┌────────────────────┐
│ MatchResult │──> rule title, id, level, tags,
│ CorrelationResult │ matched selections, field matches,
└────────────────────┘ aggregated values, optional events
```
## 参考
- [pySigma](https://github.com/SigmaHQ/pySigma) — 参考 Python 实现
- [Sigma 规范 V2.0.0](https://github.com/SigmaHQ/sigma-specification) — 正式规范
- [sigma-rust](https://github.com/jopohl/sigma-rust) — Pratt 解析方法
- [sigmars](https://github.com/crowdalert/sigmars) — 关联支持模式
## 发布
所有四个 crate 共享一个版本号(在工作区 `Cargo.toml` 中设置)并同时发布。
### 发布新版本
1. 在根 `Cargo.toml` 中升级版本号。
2. 提交并推送到 `main`。
3. 创建一个 GitHub Release(例如标签 `v0.2.0`)。`publish.yml` 工作流会自动触发并按依赖顺序发布所有 crate。
### 演练
通过 **Actions → Publish to crates.io → Run workflow** 手动触发工作流。
手动运行会自动向每次 `cargo publish` 调用传递 `--dry-run`。
### 从部分失败中恢复
如果工作流中途失败(例如 `rsigma-parser` 已发布但 `rsigma-eval` 失败),重新运行工作流将在已发布的 crate 处失败。
要恢复,请按顺序手动发布剩余的 crate:
```
# 跳过已成功发布的 crates
cargo publish -p rsigma-eval && sleep 30
cargo publish -p rsigma
cargo publish -p rsigma-lsp
```
## 许可证
MIT
标签:AMSI绕过, CLI, DevSecOps, ECS, EDR, Homebrew安装, JSON, Linter, LSP, pptx, pySigma, Rust, Terraform, URL发现, WiFi技术, YAML, 上游代理, 云安全监控, 云计算, 代码补全, 可视化界面, 后端开发, 威胁检测, 安全库, 安全检测, 性能优化, 检测绕过, 相关性分析, 网络安全, 网络流量审计, 脆弱性评估, 自动化响应, 自定义请求头, 规则引擎, 解析器, 隐私保护, 静态分析