Romil2112/log-analyzer
GitHub: Romil2112/log-analyzer
结合规则引擎与 Isolation Forest 机器学习的安全日志分析工具,用于检测暴力破解、端口扫描等攻击并生成带 MITRE ATT&CK 映射的可视化报告。
Stars: 0 | Forks: 0
   
# log-analyzer
一个命令行界面(CLI)安全工具,能够解析 SSH `auth.log` 和 Windows Event Log CSV 文件,利用基于规则和机器学习(ML)的检测方法识别攻击,将结果映射到 MITRE ATT&CK,并生成深色主题的 HTML 事件报告。
## 功能
- **多来源解析** — SSH `auth.log`、Windows Event Log CSV 以及 Apache/Nginx 访问日志(自动检测)
- **基于规则的检测** — 滑动窗口暴力破解、端口扫描以及 404 泛洪/Web 扫描告警
- **ML 异常检测** — 基于每个源 IP 的 8 项行为特征运行 Isolation Forest;能够捕捉规则遗漏的低频慢速攻击者
- **MITRE ATT&CK 映射** — 每个事件均标记有技术 ID、战术以及文档链接
- **IP 富化** — 威胁情报信誉(已知恶意 CIDR 源)+ 可选的 MaxMind GeoLite2 GeoIP 国家信息
- **Sigma 导出** — 将检测结果输出为厂商中立的 [Sigma](https://github.com/SigmaHQ/sigma) 规则(`--export-sigma`),适用于 Splunk/Sentinel/Elastic
- **丰富的 CLI** — 彩色表格、严重性徽章(CRITICAL/HIGH/MEDIUM/LOW)以及实时进度条
- **Claude AI 摘要** — 通过 Anthropic API 生成 3 句话的 SOC 执行摘要(`--ai-summary`)
- **HTML 报告** — 包含 Chart.js 仪表盘:时间线、顶级攻击者 IP、事件分类以及 ML 异常分数
- **Docker 支持** — 运行 `docker compose up` 即可同时启动 Postgres 和 analyzer
- **GitHub Actions CI** — 每次推送时运行全部 113 个 pytest 测试并上传示例报告
## 前置条件
| 要求 | 版本 | 备注 |
|---|---|---|
| Python | 3.12+ | |
| PostgreSQL | 14+ | 可选 — 使用 `--no-db` 跳过 |
| Anthropic API key | — | 可选 — 仅在使用 `--ai-summary` 时需要 |
## 展示技能
| 领域 | 详情 |
|---|---|---|
| 安全检测 | 基于 SSH/Windows/Web 日志的滑动窗口暴力破解、端口扫描以及 404 泛洪规则引擎 |
| 检测即代码 | 将检测结果输出为 Sigma 规则(厂商中立),以便部署到 SIEM |
| 威胁情报 / GeoIP | 已知恶意 CIDR 信誉匹配 + 可选的 MaxMind GeoLite2 国家信息富化 |
| ML / 异常检测 | 基于每 IP 8 项行为特征的 Isolation Forest;能够捕捉低频慢速攻击 |
| MITRE ATT&CK | 技术映射(T1110.001, T1046, T1595.002)、战术标记以及可点击的报告链接 |
| PostgreSQL | Schema 设计、psycopg2 批量插入、JSONB 事件详情 |
| Docker | 包含健康检查 Postgres 和卷挂载的多服务 Compose |
| CI/CD | GitHub Actions:每次推送执行 pytest 门禁检查 + HTML 报告制品 |
| Claude AI / Anthropic | 异步 API 集成、SOC 执行摘要生成、prompt 工程 |
## 演示
```
┌─────────────────────────────────────────────────────────────────────┐
│ Log Analyzer │ test_auth_10k.log │ format: ssh │ 10,000 lines │
└─────────────────────────────────────────────────────────────────────┘
[+] Parsed 10,000 events (10,000 lines)
[*] Running rule-based detections...
Detected Incidents
╭──────────────┬─────────────────┬───────┬──────────┬────────────────────╮
│ Type │ Source IP │ Count │ Severity │ MITRE ID │
├──────────────┼─────────────────┼───────┼──────────┼────────────────────┤
│ Brute Force │ 10.99.99.99 │ 2311 │ CRITICAL │ T1110.001 │
│ Port Scan │ 10.99.99.99 │ 512 │ CRITICAL │ T1046 │
│ Port Scan │ 198.51.100.77 │ 87 │ HIGH │ T1046 │
│ Port Scan │ 203.0.113.42 │ 54 │ MEDIUM │ T1046 │
│ Brute Force │ 185.220.101.45 │ 430 │ CRITICAL │ T1110.001 │
│ Brute Force │ 45.33.32.156 │ 218 │ CRITICAL │ T1110.001 │
│ Brute Force │ 198.199.119.48 │ 97 │ HIGH │ T1110.001 │
╰──────────────┴─────────────────┴───────┴──────────┴────────────────────╯
MITRE ATT&CK Coverage
T1110.001 Brute Force: Password Guessing Credential Access (4 incidents)
T1046 Network Service Discovery Discovery (3 incidents)
[+] Isolation Forest — 4 IPs above threshold 0.5
10.99.99.99 score=1.0000 Rule + ML
91.108.4.200 score=0.6196 ML Only
172.16.0.0 score=0.6111 ML Only
203.0.113.42 score=0.5067 Rule + ML
[+] Report written: report.html
```
## 快速开始
### 安装
```
pip install -r requirements.txt
```
### 无数据库模式
```
python log_analyzer.py auth.log --no-db --report report.html
```
### 使用 PostgreSQL
```
python log_analyzer.py auth.log --report report.html
```
### Web 访问日志
```
# 自动检测 access-log 格式并运行 404-flood / web-scan 检测
python log_analyzer.py access.log --no-db --report report.html
```
### 威胁情报 + GeoIP 富化
```
# 默认使用内置的 known-bad CIDR feed;添加您自己的和 GeoIP DB
python log_analyzer.py auth.log --no-db \
--threat-intel-file my_badips.txt \
--geoip-db GeoLite2-Country.mmdb
```
### 导出 Sigma 规则(检测即代码)
```
python log_analyzer.py auth.log --no-db --export-sigma ./sigma_rules
```
### 使用 AI 摘要
```
export ANTHROPIC_API_KEY=sk-ant-your-key-here
python log_analyzer.py auth.log --no-db --ai-summary --report report.html
```
### 通过 Docker 运行
```
docker compose up
```
## CLI 参数
| 参数 | 默认值 | 描述 |
|---|---|---|
| `--report FILE` | `incident_report.html` | 输出 HTML 报告路径 |
| `--no-db` | — | 跳过 PostgreSQL 存储 |
| `--no-ml` | — | 跳过 Isolation Forest |
| `--ai-summary` | — | 生成 Claude AI 执行摘要 |
| `--ml-threshold FLOAT` | `0.5` | 显示异常结果的最低分数 |
| `--brute-force-threshold N` | `5` | 触发告警的失败登录次数 |
| `--brute-force-window MIN` | `10` | 滑动窗口(分钟) |
| `--port-scan-threshold N` | `20` | 触发告警的唯一端口数 |
| `--port-scan-window MIN` | `5` | 滑动窗口(分钟) |
| `--flood-404-threshold N` | `30` | 触发告警的 404 请求数 |
| `--flood-404-window MIN` | `5` | 滑动窗口(分钟) |
| `--allowlist CIDR,...` | — | 以逗号分隔的 IP/CIDR,在检测时排除 |
| `--format {ssh,windows,auto}` | `auto` | 日志格式覆盖 |
| `--init-schema` | — | 创建数据库 schema 并退出 |
## HTML 报告



## 项目结构
```
log-analyzer/
├── log_analyzer.py # Main CLI — parsing, detection, ML, report generation
├── ai_summary.py # Claude API executive summary integration
├── generate_test_logs.py # Synthetic SSH + Windows log generator
├── schema.sql # PostgreSQL schema (log_events, incidents)
├── requirements.txt # Python dependencies
├── config.example.yaml # All detection thresholds and allowlist options
├── Dockerfile # Container image
├── docker-compose.yml # Postgres + analyzer services
├── test_auth_10k.log # 10,000-event SSH fixture
├── test_auth_50k.log # 50,000-event scale fixture
├── test_coordinated.log # Coordinated multi-IP attack
├── test_highvol.log # High-volume mixed attack
├── test_malformed.log # Malformed-line edge cases
├── test_events.csv # Windows Event Log CSV fixture
├── tests/
│ └── test_detection.py # 103 pytest unit + integration tests
└── .github/workflows/
└── ci.yml # GitHub Actions: test + report artifact
```
## 运行测试
```
python -m pytest tests/ -v
```
## 许可证
MIT
标签:Apex, PKI安全, Python, 命令行工具(CLI), 安全规则引擎, 安全运营中心(SOC), 插件系统, 无后门, 机器学习, 测试用例, 请求拦截, 逆向工具