Mr20x20/pyshield-incident-analyzer
GitHub: Mr20x20/pyshield-incident-analyzer
PyShield 事件分析器是一个基于规则的多源日志安全事件分析引擎,能解析异构日志、构建攻击时间线、检测威胁模式并提取 IOC 指标,最终输出结构化事件报告。
Stars: 0 | Forks: 0
# 🔎 PyShield 事件分析器
一个基于规则的事件分析引擎,能够解析来自多个来源的日志文件,构建按时间顺序排列的攻击时间线,检测威胁模式,并提取妥协指标 (IOCs)。它是 **PyShield** 安全生态系统的一部分。
## 📸 示例输出
```
=================================================================
PyShield Incident Analyzer — Report
=================================================================
Timestamp : 2026-06-27 10:05:00
Log Sources : linux:auth.log, honeypot:honeypot_report.json
Events : 47 analyzed
Risk Score : 95
Risk Level : CRITICAL
-----------------------------------------------------------------
Alerts : 4 total | CRITICAL:2 HIGH:2 MEDIUM:0 LOW:0
-----------------------------------------------------------------
Malicious IPs : 1
192.168.1.100 failed=5 success=1 ← COMPROMISED
Targeted Users : 2
admin failed=5 success=1 ← COMPROMISED
backdoor failed=0 success=1
-----------------------------------------------------------------
ATTACK TIMELINE:
[001] 2026-06-27 10:00:01 MEDIUM [linux ] Failed SSH password user=admin ip=192.168.1.100
[002] 2026-06-27 10:00:06 INFO [linux ] Accepted SSH password user=admin ip=192.168.1.100
[003] 2026-06-27 10:00:15 HIGH [linux ] Sudo command executed user=admin
[004] 2026-06-27 10:01:00 HIGH [linux ] New user created user=backdoor
[005] 2026-06-27 10:01:30 INFO [linux ] Accepted SSH password user=backdoor
=================================================================
```
## 🏗️ 架构
```
┌─────────────────────────────────────────────────────────────────┐
│ PyShield Incident Analyzer │
│ │
│ Log Sources │
│ ┌──────────────┐ ┌──────────────┐ ┌────────────────────┐ │
│ │ windows.xml │ │ linux.log │ │ honeypot.json │ │
│ └──────┬───────┘ └──────┬───────┘ └─────────┬──────────┘ │
│ └─────────────────┼──────────────────────┘ │
│ ▼ │
│ ┌─────────────────┐ │
│ │ Parser Layer │ protocol-specific │
│ │ windows_parser │ parsing per source │
│ │ linux_parser │ │
│ │ honeypot_parser│ │
│ └────────┬────────┘ │
│ │ raw events │
│ ▼ │
│ ┌─────────────────┐ │
│ │ normalizer.py │ enforces common schema │
│ └────────┬────────┘ │
│ │ normalized events │
│ ▼ │
│ ┌─────────────────┐ │
│ │ timeline.py │ chronological sort │
│ └────────┬────────┘ │
│ │ sorted timeline │
│ ▼ │
│ ┌────────────────────────┐ │
│ │ rules/ │ modular detection │
│ │ brute_force.py │ │
│ │ privilege_escalation │ │
│ │ new_account.py │ │
│ │ lateral_movement.py │ │
│ └────────────┬───────────┘ │
│ │ alerts │
│ ▼ │
│ ┌─────────────────┐ │
│ │ ioc_extractor │ IPs, users, timeline │
│ └────────┬────────┘ │
│ ▼ │
│ ┌─────────────────┐ │
│ │ risk_engine │ scoring + assessment │
│ └────────┬────────┘ │
│ ▼ │
│ ┌─────────────────┐ │
│ │ reporter.py │ JSON + terminal output │
│ └────────┬────────┘ │
│ │ │
└───────────────────────────┼──────────────────────────────────── ┘
▼
incident_report.json + timeline.json
│
▼
PyShield Dashboard (SIEM)
```
## 🔍 检测规则
| 规则 | ID | 检测模式 | 严重程度 |
|---|---|---|---|
| 暴力破解 | RULE-001 | 5分钟内来自同一 IP 的 5 次以上失败登录 | HIGH |
| 暴力破解成功 | RULE-001 | 暴力破解后成功登录 | CRITICAL |
| 添加至管理员组 | RULE-002 | 用户被添加到 Administrators 组 | CRITICAL |
| 登录后提权 | RULE-002 | 登录后 60 秒内使用特殊权限 | HIGH |
| 登录后使用 Sudo | RULE-002 | SSH 登录后 60 秒内使用 Sudo 命令 | HIGH |
| 后门账户 | RULE-003 | 可疑登录后创建新账户 | CRITICAL |
| 非工作时间账户 | RULE-003 | 在非工作时间创建账户 | HIGH |
| 账户即时使用 | RULE-003 | 新账户创建后 10 分钟内被使用 | HIGH |
| 多 IP 登录 | RULE-004 | 同一用户在 1 小时内从 3 个以上 IP 登录 | HIGH |
| 侦察后成功 | RULE-004 | 来自某 IP 的登录失败后成功登录 | CRITICAL |
| 撞库攻击 | RULE-004 | 尝试 5 个以上用户名后其中一个成功 | CRITICAL |
## 🚀 快速开始
### 1. 克隆仓库
```
git clone https://github.com/Mr20x20/pyshield-incident-analyzer.git
cd pyshield-incident-analyzer
```
### 2. 创建虚拟环境
```
python -m venv venv
# Windows
venv\Scripts\activate
# Linux / macOS
source venv/bin/activate
```
### 3. 安装依赖
```
pip install -r requirements.txt
```
### 4. 运行分析
```
# 分析 Linux auth log
python run.py --linux logs/auth.log
# 分析 Windows Event Log(导出为 XML)
python run.py --windows logs/security.xml
# 分析来自 PyShield Honeypot 的 honeypot 报告
python run.py --honeypot logs/honeypot_report.json
# 合并多个来源
python run.py --linux logs/auth.log --honeypot logs/honeypot_report.json
```
### 5. 导出 Windows 事件日志
```
# 在 PowerShell 中以管理员身份运行
Get-WinEvent -LogName Security -MaxEvents 1000 |
Export-Clixml security.xml
```
## 📁 项目结构
```
pyshield-incident-analyzer/
├── run.py # Entry point — CLI argument parser + pipeline
├── normalizer.py # Enforces common event schema
├── timeline.py # Chronological sort + query helpers
├── ioc_extractor.py # Extracts IPs, users, attack timeline
├── risk_engine.py # Scoring + risk level assessment
├── reporter.py # JSON report + visual terminal output
├── config.py # Event IDs, thresholds, scoring rules
├── parser/
│ ├── windows_parser.py # Windows Event Log XML parser
│ ├── linux_parser.py # Linux auth.log regex parser
│ └── honeypot_parser.py # PyShield Honeypot JSON parser
├── rules/
│ ├── brute_force.py # RULE-001: Brute force detection
│ ├── privilege_escalation.py # RULE-002: Privilege escalation
│ ├── new_account.py # RULE-003: Suspicious account creation
│ └── lateral_movement.py # RULE-004: Lateral movement
├── logs/ # Place log files here
└── reports/ # Auto-created output directory
├── incident_report.json
└── timeline.json
```
## 🧠 设计决策
**为什么 Parser 层要与 Normalizer 分离?**
每个日志源的格式完全不同 —— Windows 使用带有命名空间的 XML,Linux 使用正则匹配的纯文本,Honeypot 使用 JSON。将解析与标准化分离意味着每个解析器只专注于一项工作:读取其特定格式。Normalizer 专注于一项工作:强制执行 schema。将它们混在一起会使两者都难以维护。
**为什么 rules/ 是一个包含模块化文件的包?**
添加新的检测规则只需在 `rules/` 中创建一个新文件,并将其添加到 `__init__.py` 中的 `ALL_RULES` 里。其他内容都不需要更改。这与真实 SIEM 产品(如 Sigma 规则)中使用的模式相同 —— 规则是数据,而不是硬编码的逻辑。这也意味着可以独立测试规则,而无需运行整个 pipeline。
**为什么 Timeline 作为一个独立的模块存在?**
Timeline 会被多个消费者查询 —— 每个规则都使用 `get_events_in_window()`、`get_events_by_ip()` 和 `get_events_by_user()`。集中处理这些查询意味着规则本身不需要对原始事件进行排序或过滤。如果查询逻辑需要更改,只需在一个地方修改即可。
**为什么 IOC 的提取要与风险评分分开?**
IOC(恶意 IP、受损用户)是操作层面的产物 —— 安全团队使用它们在防火墙中拦截 IP 并禁用账户。风险评分则是一种管理指标。将它们分开意味着各自可以独立演进。分析师可能只需要原始的 IOC 而不需要风险评分;而管理者可能只需要评分。
**为什么 Normalizer 要清理像 `-` 和 `::1` 这样的占位符值?**
Windows 事件日志在不存远程 IP 时使用 `-` 表示“不适用”,使用 `::1` 表示 localhost。如果不清理这些值,规则引擎会将 localhost 标记为攻击者,并在每个系统事件上产生误报。
## ⚙️ 配置
`config.py` 中的所有设置:
| 设置 | 默认值 | 描述 |
|---|---|---|
| `BRUTE_FORCE_THRESHOLD` | 5 | 触发暴力破解警报的失败登录次数 |
| `BRUTE_FORCE_WINDOW` | 300s | 暴力破解检测的时间窗口 |
| `PRIV_ESC_WINDOW` | 60s | 权限提升检测的时间窗口 |
| `NEW_ACCOUNT_WINDOW` | 600s | 后门账户检测的时间窗口 |
| `LATERAL_MOVEMENT_WINDOW` | 3600s | 横向移动检测的时间窗口 |
## 🔗 PyShield 生态系统集成
```
PyShield Honeypot (project7)
│
└── honeypot_report.json
│
▼
PyShield Incident Analyzer (project10) ← you are here
│
└── incident_report.json
│
▼
PyShield Dashboard (project6)
```
## 🛠️ 技术栈
- **语言:** Python 3.11+
- **日志解析:** `xml.etree.ElementTree`, `re` (标准库)
- **检测:** 带有滑动窗口算法的自定义规则引擎
- **输出:** JSON + ASCII 时间线可视化
## 🔐 法律与道德声明
仅分析您拥有或获得明确授权调查的系统的日志。此工具专为授权的事件响应和安全操作而设计。
## 📜 许可证
MIT License — 详情请参阅 [LICENSE](LICENSE)。
## 👤 作者
**Mr20x20** — 网络与安全爱好者
GitHub: [github.com/Mr20x20](https://github.com/Mr20x20)
## 🔗 相关项目
- [PyShield Dashboard](https://github.com/Mr20x20/PyShield_Dashboard) — 实时 SIEM 仪表板
- [PyShield Honeypot](https://github.com/Mr20x20/pyshield-honeypot) — 攻击者画像分析器
- [PyShield Threat Intel](https://github.com/Mr20x20/pyshield-threat-intel) — CVE 漏洞扫描器
- [PyShield Web Scanner](https://github.com/Mr20x20/pyshield-web-scanner) — Web 错误配置扫描器
标签:AMSI绕过, PB级数据处理, PKI安全, Python, 威胁检测, 安全运维, 文档结构分析, 无后门, 逆向工具