ilkin1cybsec-cloud/Detection-as-code-pipeline
GitHub: ilkin1cybsec-cloud/Detection-as-code-pipeline
一个将 SOC 检测规则 YAML 化并通过 Python 与 GitHub Actions 实现自动化验证与测试的检测即代码流水线示例项目。
Stars: 0 | Forks: 0
# 检测即代码流水线
## 项目概述
本项目是一个用于 SOC 和检测工程作品集的小型检测即代码流水线。它包含 YAML 检测规则、示例日志、验证检查、测试,以及一个在部署前验证检测逻辑的 GitHub Actions 工作流。
该项目演示了安全团队如何像管理代码一样管理检测:规则存储在代码仓库中,通过样本数据进行测试,验证必填字段,并在 CI 中自动进行检查。
## 展示技能
- 检测工程基础
- 编写基于 YAML 的检测规则
- 示例日志测试
- 规则 schema 验证
- Python 自动化
- GitHub Actions CI 工作流
- MITRE ATT&CK 映射
- 可扩展的安全运营实践
## 使用工具
- Python 3
- YAML 风格的检测规则
- JSON Lines 示例日志
- GitHub Actions
- Markdown 报告
- MITRE ATT&CK 框架
## 目录结构
```
detection-as-code-pipeline/
|-- .github/
| |-- workflows/
| |-- validate-detections.yml
|-- data/
| |-- sample_logs.jsonl
|-- reports/
| |-- validation_report.md
|-- rules/
| |-- linux_privilege_escalation.yml
| |-- windows_bruteforce.yml
| |-- windows_suspicious_powershell.yml
|-- screenshots/
| |-- .gitkeep
|-- src/
| |-- main.py
| |-- validate_rules.py
|-- tests/
| |-- test_rules.py
|-- .gitignore
|-- README.md
|-- requirements.txt
```
## 检测规则
本仓库包含三个示例检测:
| 规则 ID | 规则名称 | 平台 | 严重程度 | MITRE ATT&CK |
|---|---|---|---|---|
| DAC-WIN-001 | 可疑的 PowerShell 执行 | Windows | 高危 | T1059.001 PowerShell |
| DAC-WIN-002 | 多次 Windows 登录失败 | Windows | 中危 | T1110 暴力破解 |
| DAC-LNX-001 | Linux 提权命令 | Linux | 高危 | T1548 滥用提权控制机制 |
## 规则格式
每个规则包含以下必填字段:
- `title`
- `id`
- `status`
- `description`
- `logsource`
- `detection`
- `level`
- `mitre`
示例:
```
title: Suspicious PowerShell Execution
id: DAC-WIN-001
status: experimental
description: Detects PowerShell commands using encoded commands, download cradles, or execution policy bypass.
logsource:
product: windows
service: security
detection:
event_ids:
- 4688
- 4104
match_fields:
- process_name
- command_line
keywords:
- powershell.exe
- encodedcommand
- -enc
level: high
mitre:
tactic: Execution
technique_id: T1059.001
technique: PowerShell
```
## 示例日志
示例日志存储在:
```
data/sample_logs.jsonl
```
示例数据集包括:
- 可疑的 PowerShell 执行
- 反复的 Windows 登录失败
- Linux 提权命令活动
- 良性的 Windows 进程活动
## 验证逻辑
验证脚本会检查每个规则是否:
- 包含所有必填的顶级字段
- 包含必填的 MITRE 字段
- 使用了有效的严重级别
- 定义了事件 ID
- 定义了关键字或聚合检测逻辑
- 按预期与示例日志匹配
## 如何运行
在项目文件夹下:
```
python src/main.py
```
运行测试:
```
python -m unittest discover -s tests
```
## 输出
验证脚本会生成:
```
reports/validation_report.md
```
示例输出:
```
# Detection-as-Code 验证报告
- Rules validated: 3
- Rules passed: 3
- Rules failed: 0
- Total sample matches: 7
```
## GitHub Actions
工作流文件位于:
```
.github/workflows/validate-detections.yml
```
该工作流在 push 和 pull request 时运行。它会:
1. 检出仓库代码
2. 配置 Python 环境
3. 运行检测验证器
4. 运行单元测试
## 未来可能的改进
- 添加更多规则类型
- 添加严重程度评分
- 添加 Sigma 兼容性
- 添加 JSON schema 验证
- 添加规则覆盖率指标
- 添加 Splunk 或 Microsoft Sentinel 查询导出功能
标签:CSV导出, GitHub Actions, OpenCanary, Python, YAML规则, 安全运营, 扫描框架, 无后门, 检测即代码, 自动笔记