mostafa/detection-layer-on-postgres-article
GitHub: mostafa/detection-layer-on-postgres-article
演示如何使用 RSigma 工具将 Sigma 检测规则转换为 PostgreSQL/TimescaleDB 原生 SQL 查询,在数据库层直接构建安全威胁检测与攻击链关联分析能力。
Stars: 0 | Forks: 0
# 使用 Sigma 规则在 PostgreSQL 上构建检测层
博文 [使用 Sigma 规则在 PostgreSQL 上构建检测层](https://mostafa.dev/building-a-detection-layer-on-postgresql-with-sigma-rules-042caeb42b2a) 的配套代码库。
本代码库包含文章中使用的 Sigma 检测规则、处理 pipeline、数据库 schema、示例事件以及生成的 SQL 输出。它们共同演示了 [RSigma](https://github.com/timescale/rsigma) 如何将社区 Sigma 规则转换为原生 PostgreSQL/TimescaleDB SQL 以进行检测,重现了 Okta [2023 年 8 月跨租户模拟 advisory](https://sec.okta.com/articles/2023/08/cross-tenant-impersonation-prevention-and-detection) 中的攻击链。
## 包含内容
```
rules/
okta_user_session_start_via_anonymised_proxy.yml # SigmaHQ – session via proxy
okta_mfa_reset_or_deactivated.yml # SigmaHQ – MFA deactivated
okta_admin_role_assigned_to_user_or_group.yml # SigmaHQ – admin role assigned
okta_identity_provider_created.yml # SigmaHQ – rogue IdP created
okta_proxy_brute_force_correlation.yml # Custom – event_count correlation
okta_cross_tenant_impersonation_correlation.yml # Custom – temporal correlation
pipelines/
okta_postgres.yml # Field mapping for hybrid schema
schema/
jsonb.sql # Pure JSONB table (fastest start)
hybrid.sql # Hybrid table with B-tree indexes
events/
okta_audit.ndjson # Sample Okta System Log events (NDJSON)
output/
default_jsonb.sql # SELECT queries (JSONB mode)
default_hybrid.sql # SELECT queries (hybrid mode with pipeline)
view_jsonb.sql # CREATE VIEW statements
timescaledb_jsonb.sql # time_bucket() queries
continuous_aggregate_jsonb.sql # CREATE MATERIALIZED VIEW (TimescaleDB)
correlation_event_count_jsonb.sql # Event count correlation with CTE
correlation_temporal_jsonb.sql # Temporal correlation (4-rule attack chain)
correlation_sliding_window_jsonb.sql # Sliding window with SQL window functions
```
这四条检测规则来自 [SigmaHQ](https://github.com/SigmaHQ/sigma/tree/master/rules/identity/okta),并使用了原生的 [Okta System Log](https://developer.okta.com/docs/api/openapi/okta-management/management/tag/SystemLog/) 字段名(camelCase),因此在 JSONB 模式下不需要处理 pipeline。这两条 correlation 规则是自定义的。
## 快速开始
安装 [RSigma](https://github.com/timescale/rsigma),然后将这四条检测规则转换为 SQL:
```
rsigma convert rules/okta_*.yml -t postgres \
-O table=okta_events \
-O json_field=data \
-O timestamp_field=time
```
生成 SQL 视图:
```
rsigma convert rules/okta_*.yml -t postgres -f view \
-O table=okta_events \
-O json_field=data \
-O timestamp_field=time
```
使用混合 pipeline(字段名重映射)进行转换:
```
rsigma convert rules/okta_*.yml -t postgres \
-O table=okta_events \
-O timestamp_field=time \
-p pipelines/okta_postgres.yml
```
所有预期的输出都在 `output/` 目录中供参考。
## 输出格式
| 格式 | 命令标志 | 生成内容 |
|--------|-------------|-------------------|
| Default | (none) | `SELECT` 查询 |
| View | `-f view` | `CREATE OR REPLACE VIEW` 语句 |
| TimescaleDB | `-f timescaledb` | 带有 `time_bucket()` 的 `SELECT` |
| Continuous aggregate | `-f continuous_aggregate` | `CREATE MATERIALIZED VIEW ... WITH (timescaledb.continuous)` |
| Sliding window | `-f sliding_window` | 使用 SQL 窗口函数的 Correlation |
## 攻击链
| 步骤 | Okta 事件 | Sigma 规则 | 等级 |
|------|-----------|------------|-------|
| 1 | 来自 proxy 的 `user.session.start` | `okta_user_session_start_via_anonymised_proxy` | high |
| 2 | `user.mfa.factor.deactivate` | `okta_mfa_reset_or_deactivated` | medium |
| 3 | `user.account.privilege.grant` | `okta_admin_role_assigned_to_user_or_group` | medium |
| 4 | `system.idp.lifecycle.create` | `okta_identity_provider_created` | medium |
| **Correlation** | 同一 `actor` 在 30 分钟内的所有四个事件 | `okta_cross_tenant_impersonation_correlation` | **critical** |
## 许可证
SigmaHQ 检测规则在 [DRL](https://github.com/SigmaHQ/sigma/blob/master/LICENSE.Detection.Rules.md) 下许可。本代码库中的所有其他内容均采用 MIT 许可证。
标签:AMSI绕过, IAM, JSONB, MFA绕过, Okta, PostgreSQL, RSigma, Sigma规则, SQL, TimescaleDB, 事件关联, 多线程, 威胁检测, 开源安全工具, 攻击复现, 数据架构, 测试用例, 目标导入, 系统审计, 网络安全, 跨租户模拟, 身份与访问管理, 逆向工程平台, 隐私保护