KartikRajOfficial/behavioral-anomaly-detection-system

GitHub: KartikRajOfficial/behavioral-anomaly-detection-system

基于 Isolation Forest 与 Random Forest 两阶段策略的 AI 驱动 UEBA 平台,提供实时行为异常检测、攻击分类与可解释的风险告警。

Stars: 0 | Forks: 0

# 🛡️ AI 驱动的行为异常检测系统 ## 🏗️ 架构 ``` ┌──────────────────────────────────────────────────────────────────────────┐ │ PIPELINE ARCHITECTURE │ ├──────────────────────────────────────────────────────────────────────────┤ │ │ │ ┌─────────────────┐ ┌──────────────────┐ ┌────────────────────┐ │ │ │ DATA GENERATOR │───▶│ BASELINE PROFILER │───▶│ DETECTION MODEL │ │ │ │ synthetic_data_ │ │ baseline_ │ │ detection_ │ │ │ │ generator.py │ │ profiler.py │ │ model.py │ │ │ │ │ │ │ │ │ │ │ │ • 75 entities │ │ • Per-entity │ │ • Isolation Forest│ │ │ │ • 8000+ events │ │ statistical │ │ (primary) │ │ │ │ • 7 anomaly types│ │ profiles │ │ • LSTM autoencoder│ │ │ │ • 30-day sim │ │ • Cold-start │ │ (if available) │ │ │ └─────────────────┘ │ fallback to │ │ • 19 features │ │ │ │ population prior│ └────────┬───────────┘ │ │ └──────────────────┘ │ │ │ ▼ │ │ ┌─────────────────┐ ┌──────────────────┐ ┌────────────────────┐ │ │ │ DASHBOARD │◀───│ EXPLAINABILITY │◀───│ ANOMALY CLASSIFIER│ │ │ │ dashboard.py │ │ explainability.py│ │ anomaly_ │ │ │ │ │ │ │ │ classifier.py │ │ │ │ • Ranked alerts │ │ • SHAP values │ │ │ │ │ │ • Risk scoring │ │ (if available) │ │ • Random Forest │ │ │ │ • Entity drilldown│ │ • Rule-based │ │ • SMOTE balancing │ │ │ │ • Timeline view │ │ fallback │ │ • 7-class output │ │ │ └─────────────────┘ └──────────────────┘ └────────────────────┘ │ │ │ │ Orchestrated by: main.py │ └──────────────────────────────────────────────────────────────────────────┘ ``` ## 📁 项目结构 ``` anomaly-detection-system/ ├── main.py # Pipeline orchestrator (run this first) ├── synthetic_data_generator.py # Generates realistic access log data ├── baseline_profiler.py # Builds per-entity behavioral profiles ├── detection_model.py # Anomaly detection (Isolation Forest + optional LSTM) ├── anomaly_classifier.py # Multi-class anomaly type classification ├── explainability.py # SHAP / rule-based alert explanations ├── dashboard.py # Streamlit interactive dashboard ├── requirements.txt # Python dependencies ├── README.md # This file ├── data/ # Generated data artifacts │ ├── synthetic_access_logs.csv │ ├── entity_profiles.pkl │ ├── isolation_forest_model.pkl │ ├── feature_scaler.pkl │ ├── anomaly_classifier.pkl │ └── anomaly_label_encoder.pkl └── outputs/ # Pipeline outputs ├── alerts.csv ├── flagged_alerts.csv └── confusion_matrix.png ``` ## 🚀 快速开始 ``` # 安装依赖 pip install -r requirements.txt # 运行完整 pipeline python main.py # 启动交互式 dashboard streamlit run dashboard.py ``` ## 📊 运行单个组件 ``` # 仅生成 synthetic data python synthetic_data_generator.py # 构建 baseline profiles python baseline_profiler.py # 训练 detection model python detection_model.py # 训练 anomaly classifier python anomaly_classifier.py ``` ## 🎯 检测到的异常类型 | 异常类型 | 描述 | 比例 | |---|---|---| | **暴力破解 (Brute Force)** | 来自单一源的快速重复身份验证失败 | 1.5% | | **不可能的旅行 (Impossible Travel)** | 在不合理的时间内从相距甚远的地点登录 | 1.0% | | **凭证填充 (Credential Stuffing)** | 大量实体,少量源 IP,高失败率 | 2.0% | | **横向移动 (Lateral Movement)** | 访问异常广泛的资源 | 1.5% | | **设备欺骗 (Device Spoofing)** | 设备 ID 与不匹配的指纹 | 1.0% | | **低速慢速数据窃取 (Low & Slow Exfiltration)** | 连续几天在非工作时间进行渐进式数据访问 | 1.0% | | **内部人员漂移 (Insider Drift)** | 权限/资源范围渐进式扩大 | 0.5% | ## 🧠 技术方案 ### 检测策略(两阶段) 1. **无监督异常检测** — Isolation Forest 仅在正常行为数据上进行训练,以计算异常分数。该模型学习正常活动的“形态”并标记偏差。 2. **有监督分类** — 对于被标记的事件,Random Forest 分类器会预测该事件类似于哪种异常类型;该模型在使用 SMOTE 过采样进行类别平衡的标注异常样本上进行训练。 ### 特征工程(19 个特征) - **时间 (Temporal)**:登录小时、星期几、周末标志 - **地理 (Geographic)**:纬度、经度 - **行为 (Behavioral)**:会话持续时间、命令序列长度、资源编码 - **偏离配置文件**:小时偏差、地理偏差、会话偏差、资源新颖性、指纹不匹配 - **网络 (Network)**:IP 地址段、实体类型 - **滚动 (Rolling)**:最近时间窗口内的事件频率 ### 可解释性 - 主要:使用 SHAP TreeExplainer 进行特征归因 - 备用:基于规则的解释,配合特定异常类型的模板 ## ⚙️ 设计决策与假设 1. **选择 Isolation Forest 而非 One-Class SVM**:在高维数据上具有更好的性能,且训练时间更快 2. **LSTM 作为可选**:尝试了深度学习,但 pipeline 不会因此阻塞 — 优雅降级确保系统始终正常运行 3. **按实体进行画像**:每个实体都有统计基线,从而支持个性化的异常阈值,而不是一刀切 4. **冷启动处理**:历史事件少于 10 个的实体会退回到群体级别的先验值(按 entity_type 分组) 5. **最高百分位阈值设定**:警报按风险分数排序;前 5% 被标记,以匹配真实的 SOC 警报预算 ## ⚠️ 已知局限性 | 局限性 | 缓解策略 | |---|---| | **概念漂移** | 应定期(例如每周)重建画像。生产系统会使用在线学习或滑动窗口重训练。 | | **冷启动** | 群体级别的先验值提供了合理的基线,但在积累足够历史记录之前,新实体可能会有较高的误报率。 | | **标签可用性** | 系统专为半监督操作而设计 — 检测是完全无监督的,分类需要一些标注样本。 | | **时间模式** | 目前的特征只能捕获时间点的行为;生产系统将受益于使用序列模型(LSTM/Transformer)进行时间模式检测。 | | **对抗性逃逸** | 复杂的攻击者可能会故意模仿正常行为。集成方法和跨实体的行为分析将有所帮助。 | | **规模** | 专为在笔记本 CPU 上执行而设计。生产部署将需要分布式特征工程和模型服务。 | ## 📈 预期指标 - **Precision @ top-1% 警报预算**:~0.50–0.85(因运行而异) - **总体检测 F1**:~0.30–0.60(在第 95 个百分位阈值处) - **分类器准确率**:每种异常类型约 0.70–0.90 - **Pipeline 运行时间**:在笔记本 CPU 上 < 60 秒 ## 🔧 依赖项 - Python 3.8+ - numpy, pandas, faker(数据生成) - scikit-learn, imbalanced-learn(ML 模型) - matplotlib, seaborn(可视化) - streamlit, plotly(仪表盘) - shap(可解释性,可选) *专为网络安全黑客松而构建 — 优先保证端到端 pipeline 的可用性,而非模型的复杂度。*
标签:AMSI绕过, Apex, Kubernetes, UEBA, 威胁检测, 安全运营, 扫描框架, 机器学习, 行为异常检测, 逆向工具