Dev9269/malware-detector
GitHub: Dev9269/malware-detector
基于 AI 与静态分析的 PE 恶意软件检测系统,融合 YARA 规则与 Random Forest 机器学习模型实现自动化判定与风险评分。
Stars: 0 | Forks: 0
# AI 恶意软件检测器
基于 AI 的 PE (Portable Executable) 恶意软件检测系统,使用静态分析、YARA 规则和 Random Forest 分类器。
## 功能
- **PE 分析** — 解析 PE 头、节区、导入表、导出表、资源、TLS 回调
- **静态分析** — 熵分析、可疑 API 检测、节区异常检测
- **ML 分类** — 基于 19 个结构特征训练的 Random Forest 模型 + EMBER 2381 特征模型(真实恶意软件数据)
- **YARA 集成** — 内置规则集,用于基于特征签名的检测
- **FastAPI 后端** — 提供 `/scan` 和 `/scan/batch` 端点的 REST API
- **Web UI** — 支持拖拽上传文件并实时显示结果
- **风险评分** — 结合多种启发式算法的 0–100 静态风险评分
## 环境要求
- **Python 3.10+**
- **网络访问权限**:需能访问 huggingface.co(用于在训练期间下载 EMBER 恶意软件数据集)
- **lief** — 通过 `requirements.txt` 自动安装(用于 EMBER 特征提取)
- **在 Linux/Mac 上**:如果未设置 `BENIGN_DIRS`,训练时可能找不到任何良性 PE 文件。详见下文。
## 快速开始
```
pip install -r requirements.txt
python model/train.py # train the ML model
python main.py # start the API on port 8000
```
打开 http://localhost:8000/frontend/index.html
### 在 Linux / macOS 上训练
训练流水线通过扫描默认目录,从您的系统中收集良性 PE 文件。
在 Windows 上,会自动使用 `C:\Windows\System32`。在其他平台上,请将
`BENIGN_DIRS` 环境变量设置为包含 PE (`.exe`, `.dll`) 文件的目录列表(以逗号分隔):
```
# 指向 Wine 的 System32 或已挂载的 PE 语料库
export BENIGN_DIRS="/path/to/pe/files,/another/dir"
python model/train.py
```
如果未设置 `BENIGN_DIRS`,训练将退出并提示明确的错误信息。
### Docker
```
docker build -t malware-detector .
docker run -p 8000:8000 malware-detector
```
## API
| 端点 | 方法 | 描述 |
|---|---|---|
| `/health` | GET | 服务状态 |
| `/scan` | POST | 上传并扫描单个文件 |
| `/scan/batch` | POST | 上传并扫描多个文件 |
| `/scan/ember` | POST | 使用预提取的 EMBER 2381 特征向量进行扫描 |
| `/history` | GET | 分页查看扫描历史 |
| `/report/{id}` | GET | 按 ID 获取详细的扫描报告 |
### 示例:扫描文件
```
curl -X POST http://localhost:8000/scan \
-F "file=@/path/to/sample.exe"
```
响应:
```
{
"verdict": "malicious",
"confidence": 0.97,
"risk_score": 78,
"is_pe": true,
"ml_prediction": {
"prediction": "malicious",
"confidence": 0.97,
"malicious_probability": 0.97
},
"yara_matches": ["SuspiciousAPIImports"],
"pe_analysis": {
"hashes": {"sha256": "abc...", "md5": "def..."},
"section_count": 5,
"import_count": 42
}
}
```
### 示例:检查健康状态
```
curl http://localhost:8000/health
```
## 运行测试
```
pip install -r requirements.txt
python -m pytest tests/ -v
```
## 架构
```
malware-detector/
├── scanner/ # PE analysis, static analysis, feature extraction, YARA
│ ├── pe_analyzer.py
│ ├── static_analysis.py
│ ├── feature_extractor.py
│ └── yara_integration.py
├── model/ # Random Forest classifier + training pipeline
│ ├── predictor.py
│ ├── train.py
│ ├── dataset.py
│ └── ember_compat.py
├── api/ # FastAPI REST endpoints
│ ├── main.py
│ └── database.py
├── frontend/ # HTML/CSS/JS web UI
├── yara_rules/ # YARA signature rules
└── tests/ # pytest unit + integration tests
├── test_scanner.py
├── test_api.py
├── test_model.py
└── test_database.py
```
## 贡献
1. Fork 本仓库
2. 创建功能分支
3. 为任何新功能添加测试
4. 确保所有测试通过:`python -m pytest tests/ -v`
5. 提交 pull request
## 许可证
MIT
标签:Apex, AV绕过, FastAPI, PE文件分析, YARA, 云安全监控, 云资产可视化, 无服务器架构, 机器学习, 网络测绘, 请求拦截, 逆向工具, 静态分析