RITHIKKRAMESH/rithikkramesh-icmp-flood-detection-iiitdm-kancheepuram

GitHub: RITHIKKRAMESH/rithikkramesh-icmp-flood-detection-iiitdm-kancheepuram

基于集成机器学习的 SDN 网络 DDoS 攻击检测与分类系统,通过跨域数据验证实现真实网络环境下的鲁棒泛洪流量识别。

Stars: 0 | Forks: 0

# 使用集成机器学习进行 DDoS 攻击检测与分类 [![Python Version](https://img.shields.io/badge/python-3.10%20%7C%203.11%20%7C%203.12-blue)](https://www.python.org/) [![Scikit-Learn](https://img.shields.io/badge/scikit--learn-1.4%2B-orange)](https://scikit-learn.org/) [![LightGBM](https://img.shields.io/badge/LightGBM-4.3%2B-green)](https://lightgbm.readthedocs.io/) [![XGBoost](https://img.shields.io/badge/XGBoost-2.0%2B-red)](https://xgboost.readthedocs.io/) 本仓库实现了一个严格的、端到端的机器学习 pipeline,用于**DDoS 泛洪攻击检测**,并根据学术项目和评估指南进行设计。本项目侧重于跨域泛化、时间感知的数据完整性、信息泄露预防以及实时推理。 ## 📖 项目目标 在单一实验室数据集上训练的网络安全检测模型通常会面临过拟合问题,并且在真实环境中的表现不佳。本项目的目标是: 1. 在内部 SDN 模拟数据集(**Dataset A3**)上训练稳健的集成分类器。 2. 在完全未见的物理网络抓包数据集(**Dataset B3**)上验证泛化能力,且无需重新训练。 3. 将网络特征对齐到高度泛化的 **10 个 Layer-3/Layer-4 数据包头 schema**,以实现跨不同环境的稳健运行。 4. 开发一个**实时检测适配器**,将机器学习模型与实时网络接口连接起来。 ## 📂 项目目录结构 ``` ├── dataset/ # Raw network datasets (Dataset_A3.csv, Dataset_B3.csv) ├── preprocessing/ # Data cleaning and binary protocol mapping scripts │ └── cleaned_dataset/ # Output location for clean CSV files ├── feature_engineering/ # Feature selection and label encoding modules │ └── output/ # Formatted training matrices (Features_A, Labels_A) ├── training/ # Model training and data balancing scripts │ ├── training_data/ # Chronological splits (70/10/20) & SMOTE balanced sets │ ├── split_dataset.py # Preserves temporal integrity during splits │ ├── apply_smote.py # Balances minority benign classes using SMOTE │ ├── train_random_forest.py # Baseline RF model │ ├── train_xgboost.py # Advanced XGBoost classifier │ ├── train_lightgbm.py # Advanced LightGBM classifier │ └── train_catboost.py # Advanced CatBoost classifier ├── models/ # Saved weights (.pkl) for best and individual classifiers │ └── save_best_model.py # Script to stage LightGBM as the production model ├── feature_selection/ # Feature importance ranking script based on LightGBM │ └── output/ # Feature importance CSV and plotted horizontal bar chart ├── dataset_B/ # Cross-domain verification scripts │ └── evaluate_datasetB.py # Pipeline to test models against the unseen B3 dataset ├── multiclass_pipeline/ # Self-contained folder archiving the multiclass pipeline ├── reports/ # Saved ablation and generalization text reports ├── realtime_adapter.py # Live sniffing and packet simulation alert adapter └── README.md # Comprehensive project documentation ``` ## ⚙️ 特征 Schema 对齐 为了解决 SDN 模拟(Dataset A3)和物理网络抓包(Dataset B3)之间的域不匹配问题,特征 pipeline 提取并对齐了 **10 个核心数据包头特征**: 1. **`proto`**:协议 ID(1: ICMP,6: TCP,17: UDP) 2. **`total_length`**:数据包总长度(字节) 3. **`src_port`**:源端口号(ICMP 为 0) 4. **`dst_port`**:目的端口号(ICMP 为 0) 5. **`fin`**:TCP FIN 标志指示符 6. **`syn`**:TCP SYN 标志指示符 7. **`rst`**:TCP RST 标志指示符 8. **`psh`**:TCP PSH 标志指示符 9. **`ack`**:TCP ACK 标志指示符 10. **`urg`**:TCP URG 标志指示符 ## 🛡️ 防止泄露与数据划分 * **时间划分**:为了保留真实的部署场景,严格禁止在划分之前进行随机打乱。数据集按时间顺序排列:**70% 训练集**,**10% 验证集**,**20% 测试集**。仅对已分离出的训练集划分执行打乱操作。 * **泄露控制**:SMOTE 过采样和特征工程的拟合**仅在训练子集上**进行计算,随后应用于验证/测试切片,以防止数据污染。 * **类别平衡**:在训练集上通过 **SMOTE (k_neighbors=5)** 实现,将 Benign (3,033) 和 Attack (136,420) 类别平衡至稳定的 **136,420 vs 136,420** 分布。 ## 📊 实验结果与评估 ### 1. 在未见的 Dataset B3 上的跨域泛化(二分类) 所有分类器均在独立的 **Dataset B3**(210,000 个 Benign / 210,000 个 Attack 样本)上进行了评估,且未进行任何重新训练。**LightGBM 是表现最佳的模型**: | 模型 | 准确率 | Benign Recall | Attack Recall | F1-Score | 结果 | | :--- | :---: | :---: | :---: | :---: | :---: | | **Random Forest**(基线) | 71.89% | 81.02% | 62.76% | 0.6907 | 稳定可泛化 | | **XGBoost** | 64.80% | 29.61% | 100.00% | 0.7397 | 高攻击召回率 | | **LightGBM** | **73.61%** | **47.22%** | **100.00%** | **0.7912** | **最佳模型(胜出者)** | | **CatBoost** | 48.63% | 30.59% | 66.67% | 0.5648 | 严重的概念偏移 | ### 2. 特征重要性排名 (LightGBM) 根据表现最佳的模型得出,按分裂重要性排名的顶级特征如下: 1. `dst_port`(重要性:7,734) 2. `total_length`(重要性:535) 3. `src_port`(重要性:286) 4. `proto`(重要性:202) 5. `syn`(重要性:201) ## ⏱️ 实时检测适配器 **`realtime_adapter.py`** 脚本充当网络流量与训练好的 LightGBM 模型之间的桥梁。 ``` [ Real Network Traffic ] | v [ Packet Sniffer / Capture Layer ] --(Scapy sniff() / Simulation Engine) | v [ Feature Extractor Module ] -------(Extracts proto, length, TCP flags, ports) | v [ Data Transform Layer ] -----------(Aligns to 10-feature model schema) | v [ Inference Engine ] ---------------(LightGBM Best Binary Model) | +------------------------+ v v [ Prediction: 0 ] [ Prediction: 1 ] | | v v [ Normal Traffic ] [ DDOS ATTACK ALERT! ] (Console Logging) (Red Alert Printed) ``` 它支持两种抓包模式: 1. **实时抓包模式(选择 1)**:利用 Scapy 在您的网卡上嗅探原始数据包,动态提取特征并输出警报。 2. **模拟模式(选择 2)**:如果缺少 Npcap 或管理员权限,则回退到模拟真实的后台 TCP/UDP 流量以及突发的 SYN/UDP/ICMP 泛洪攻击。 ## 🚀 执行与复现指南 要按顺序运行整个 pipeline,请依次执行以下命令: ``` # 预处理原始训练数据集 (Dataset_A3.csv) python preprocessing/preprocess_dataset.py # 提取特征并对齐 schema python feature_engineering/feature_engineering.py # 创建 temporal splits (70/10/20) python training/split_dataset.py # 使用 SMOTE 平衡训练类别 python training/apply_smote.py # 训练 classifiers python training/train_random_forest.py python training/train_xgboost.py python training/train_lightgbm.py python training/train_catboost.py # 将 LightGBM 保存为最佳模型 python models/save_best_model.py # 映射未见 Dataset B3 并评估泛化性能 python dataset/feature_mapping.py python dataset_B/evaluate_datasetB.py # 运行消融研究和泛化报告 python evaluation/run_ablation_and_generalization.py # 提取特征重要性 python feature_selection/feature_importance.py # 启动 Real-Time Detection Adapter python realtime_adapter.py ```
标签:Apex, DDoS检测, Python, SDN网络, 无后门, 机器学习, 逆向工具, 集成学习