Tharaa-Oueslati/MalScan-ML
GitHub: Tharaa-Oueslati/MalScan-ML
基于机器学习的 Windows PE 恶意软件静态分析与分类平台,通过提取 77 个特征实现恶意与良性文件的自动判别。
Stars: 0 | Forks: 0
# 🛡️ MalScan-ML — 静态恶意软件检测器
[](https://python.org)
[](https://scikit-learn.org)
[](https://xgboost.ai)
[](tests/)
[](LICENSE)
## 📊 结果
| 模型 | 准确率 | F1 分数 | AUC-ROC | 精确率 | 召回率 | 交叉验证 AUC (5折) |
|---|---|---|---|---|---|---|
| Random Forest | 95.5% | 0.955 | 0.953 | 0.958 | 0.952 | 0.964 ± 0.002 |
| **XGBoost** ✓ | **95.5%** | **0.955** | **0.955** | **0.958** | **0.952** | **0.965 ± 0.001** |
*在 1,000 个样本的留出集上进行评估。添加了 4% 的标签噪声以模拟规避型/错误标记的样本。*
## ⚡ 快速开始
```
git clone https://github.com/Tharaa-Ouelsati/MalScan-ML.git
cd MalScan-ML
pip install -r requirements.txt
# 训练 models
python src/train.py --data data/processed/features.csv
# 扫描文件
python scan.py suspicious.exe
```
### CLI 输出
```
╔══════════════════════════════════════════╗
║ Static Malware Detector v1.0 ║
╚══════════════════════════════════════════╝
[*] Analyzing: suspicious.exe...
┌────────────────────────────────────────────┐
│ File: suspicious.exe │
│ ⚠️ VERDICT: MALWARE Confidence: 94.7% │
│ [████████████████████░░░░] │
│ Model: xgboost │
├────────────────────────────────────────────┤
│ Risk Indicators: │
│ • High file entropy (7.8) — likely packed │
│ • CreateRemoteThread — process injection │
│ • IsDebuggerPresent — anti-debug technique│
│ • Network library imports (WinINet) │
│ • No debug info — stripped binary │
└────────────────────────────────────────────┘
⏱ Scan time: 47ms
```
## 🔬 特征工程(77 个特征)
我们从 PE(Portable Executable)二进制格式中提取跨 5 个类别的 **77 个特征**:
| 类别 | 数量 | 关键信号 |
|---|---|---|
| **Header** | 22 | `compile_timestamp`(恶意软件中为 0)、`checksum`、`entry_point_entropy` |
| **Sections** | 14 | Shannon 熵、`virtual/raw ratio`、W^X 节、加壳器名称 |
| **Imports** | 21 | `CreateRemoteThread`、`WriteProcessMemory`、`IsDebuggerPresent`、网络 DLL |
| **Strings** | 10 | URL、IP 地址、base64 块、注册表项、`powershell`/`cmd.exe` |
| **Metadata** | 7 | `file_entropy`、`has_debug_info`、加壳启发式分析 |
### 为什么熵是关键信号
Shannon 熵衡量字节序列的随机性。恶意软件作者会对他们的 payload 进行加密或压缩以规避特征检测——这会使熵值飙升至 7.0 以上。合法的可执行文件通常介于 4.0–6.5 之间。

## 📈 评估
### ROC 曲线

### 混淆矩阵

### 特征重要性

### 交叉验证(5折)

### 预测置信度与误分类分析

## 🗂️ 项目结构
```
MalScan-ML/
├── src/
│ ├── feature_extractor.py # PE parsing + 77-feature extraction engine
│ ├── train.py # Training pipeline with CV and plot generation
│ ├── evaluate.py # ROC, confusion matrix, importance plots
│ ├── predict.py # Inference + human-readable risk indicators
│ └── utils.py # Dataset utilities + synthetic data generator
├── notebooks/
│ └── MalScan_Analysis.ipynb # Full EDA + model comparison (11 sections)
├── data/
│ ├── processed/ # Feature CSVs (generated by feature_extractor.py)
│ └── samples/ # Test PE files (benign only in repo)
├── models/ # Saved model .pkl files
├── reports/figures/ # Generated evaluation plots (10 plots)
├── tests/
│ └── test_feature_extractor.py # 28 unit + integration tests
├── .github/workflows/ci.yml # GitHub Actions CI (Python 3.10 + 3.11)
├── scan.py ← CLI entry point
└── requirements.txt
```
## 🧪 测试
```
pytest tests/ -v
# 28 passed in 2.36s
```
测试覆盖率包括熵的边缘情况、特征提取验证、数据集完整性检查,以及完整的 RF + XGBoost pipeline 集成测试。
## 📦 使用真实数据
本项目兼容 **[EMBER 数据集](https://github.com/elastic/ember)**(Elastic Malware Benchmark for Empowering Researchers — 包含 110 万个 PE 文件)。
要从你自己的 PE 样本中提取特征:
```
# 从 malware 目录中提取 features
python src/feature_extractor.py -d /path/to/malware/ -l 1 -o malware_features.csv
# 从 benign 样本中提取 features
python src/feature_extractor.py -d /path/to/benign/ -l 0 -o benign_features.csv
# 组合并训练
cat malware_features.csv benign_features.csv > combined.csv
python src/train.py --data combined.csv
```
## 🔧 技术栈
`pefile` · `scikit-learn` · `xgboost` · `numpy` · `pandas` · `matplotlib` · `seaborn` · `click` · `joblib` · `pytest`
## 📄 许可证
MIT — 详见 [LICENSE](LICENSE)
标签:Apex, DNS 反向解析, Windows PE文件, XGBoost, 云安全监控, 安全规则引擎, 机器学习, 逆向工具, 静态分析