40u5/login-anomaly-pipeline
GitHub: 40u5/login-anomaly-pipeline
基于 Kafka、PySpark Structured Streaming 和机器学习的实时登录异常检测管线,内置五类检测规则并配有可视化仪表板。
Stars: 0 | Forks: 0
# loganom
**实时登录异常检测。**
模拟认证事件 → Kafka → PySpark Structured Streaming → Postgres 告警 → Streamlit 实时仪表板。
## 已实现检测规则
| 规则 | 捕获内容 |
|---|---|
| **velocity** | 在短时间窗口内,单个用户出现 ≥ N 次失败登录 —— 典型的暴力破解 |
| **impossible_travel** | 同一用户在短时间窗口内从两个地理位置相距甚远的 IP 成功登录 |
| **credential_stuffing** | 同一 IP 在短时间窗口内尝试大量不同的 user_id |
| **aggregator_fingerprint** | 单个设备指纹在较长时间窗口内涉及大量 user_id |
| **isolation_forest** | 基于每个用户时间窗口的特征(failures、unique_ips、unique_fps、hour),由离线训练的 Isolation Forest 进行评分 |
请参阅 [`docs/detection_rules.md`](docs/detection_rules.md) 了解关于时间窗口、阈值和误报的说明。
## 架构
```
┌──────────────┐ produce ┌─────────┐ subscribe ┌──────────────────┐ write ┌──────────────────┐
│ GENERATOR │───────────▶│ KAFKA │──────────────▶│ SPARK STREAMING │────────────▶│ POSTGRES │
│ normal + │ │ topic: │ │ rules + iso-fst │ │ events + alerts │
│ injected │ │ logins │ │ │ │ │
│ attacks │ └─────────┘ └──────────────────┘ └────────┬─────────┘
└──────────────┘ │
▼
┌──────────────────┐
│ STREAMLIT │
│ live dashboard │
└──────────────────┘
```
## 快速开始
前置条件:Docker Desktop、Python **3.11**、Java 17/21。
```
cp .env.example .env
docker compose up -d # postgres on 5434, kafka on 9092
py -3.11 -m venv .venv
.venv/Scripts/activate
pip install -e ".[dev]"
# Terminal A — 启动流式检测器
loganom detect
# Terminal B — 向 Kafka 生产实时事件
loganom stream --rate 50 --attacks on
# Terminal C — 打开仪表板
loganom dashboard
# → http://localhost:8501
```
如需在不使用 Kafka 的情况下进行离线/批处理运行,并同时训练 Isolation Forest:
```
loganom generate --events 50000 --attacks on # writes to Postgres
loganom train # fits + serializes the iso-fst
loganom score-batch # scores the same data, writes alerts
```
## 文档
- [`docs/architecture.md`](docs/architecture.md)
- [`docs/detection_rules.md`](docs/detection_rules.md)
- [`docs/demo.md`](docs/demo.md)
标签:Apex, Kafka, Kubernetes, PMD, PySpark, SonarQube插件, 安全数据分析, 实时流处理, 异常检测, 机器学习, 测试用例, 红队行动, 请求拦截, 逆向工具