Vampirk/graph-malware-analysis
GitHub: Vampirk/graph-malware-analysis
该项目提出可解释性驱动的特征工程方法,在 Android 函数调用图上筛选出高效的 7 维节点结构特征,配合 JK-GraphSAGE 实现轻量且可解释的恶意软件分类。
Stars: 0 | Forks: 0
# 以表示为中心的 Android 恶意软件分类
在 **函数调用图 (FCGs)** 上进行可解释性驱动的特征工程,结合图神经网络进行
Android 恶意软件分类。
## 概述
大多数基于 GNN 的 Android 恶意软件检测工作都集中在 **架构** 上;本项目转而
采用 **以表示为中心** 的视角,探讨哪些 *节点特征表示* 真正
有效。我们构建了一个 **双层结构特征框架**,然后使用可解释 AI 方法
对其进行剪枝:
1. **13维初始特征集**
- **5个局部** — 局部度数特征:`ldp_degree`, `ldp_min_neighbor`,
`ldp_max_neighbor`, `ldp_mean_neighbor`, `ldp_std_neighbor`
- **8个全局** — 社区结构、执行流与连通性:`khop_density`,
`scc_size`, `call_depth`, `is_entry_point`, `clustering_asymmetry`, `fan_out_ratio`,
`reachability`, `reciprocity`
2. **可解释性驱动的特征选择** — Integrated Gradients (IG)、GradCAM 和 SHAP 一致
选出了 **7维优化子集**,将维度减少了 46%。
3. **分类器** — Jumping-Knowledge GraphSAGE (**JK-GraphSAGE**,6层,最大聚合,
全局最大池化),并基于 GCN、GIN 和普通 GraphSAGE 基线进行了验证。
**核心结果**:优化后的 7维特征集在
MalNet-Tiny 上达到了 **94.47 ± 0.25%** 的准确率 — 相比
仅使用 LDP 的基线提升了 **+0.32 pp**,且 **特征减少了 46%** —
并且所选特征在所有四种 GNN 架构中均保持重要性(模型无关)。
### 特征配置
| 配置 | 维度 | 描述 |
|-----------------|-----|------------------------------------------------------|
| `LDP_5D` | 5 | 基线 — 仅 LDP 特征 |
| `Full_13D` | 13 | 所有局部 + 全局结构特征 |
| `Optimized_7D` | 7 | IG + GradCAM + SHAP 选出的子集 (**最佳**) |
### 结果 — `Optimized_7D`,5个随机种子 (准确率 %)
| 模型 | 准确率 |
|----------------|------------------|
| **JK-GraphSAGE** | **94.47 ± 0.22** |
| GraphSAGE | 94.07 ± 0.30 |
| GIN | 93.25 ± 0.37 |
| GCN | 92.61 ± 0.49 |
完整的每类 F1 和每个种子的数值详见 [`results/`](results/)。
## 仓库结构
```
.
├── preprocessing/ # [1-2] graph loading + feature extraction
│ ├── malnet_data_loader.py # MalNet-Tiny → PyG graphs + LDP (directed/undirected × norm)
│ ├── malware_community_features.py # + community-level structural features → 13-D
│ └── malware_graph_features.py # + graph-level global features (experimental)
├── experiments/ # [3] training + ablation studies (3 configs × 5 seeds)
│ ├── minimal_paper_exact.py # faithful JK-GraphSAGE reference training
│ ├── jk_graphsage_ablation.py # main model
│ ├── graphsage_ablation.py # baseline
│ ├── gcn_ablation.py # baseline
│ └── gin_ablation.py # baseline
├── analysis/ # [4] interpretability, timing, plots
│ ├── feature_importance.py
│ ├── feature_importance_3methods.py # IG / GradCAM / SHAP
│ ├── feature_extraction_timing.py
│ ├── inference_speed_comparison.py
│ └── plot_confusion_matrix.py
├── scripts/
│ └── evaluate_models.py # evaluate saved checkpoints (rebuilds the scaler)
├── results/ # metrics (CSV/JSON) + figures (PNG) — checkpoints are NOT here
├── docs/ # published paper + Korean feature-importance note (PDF)
├── data/ # (git-ignored) preprocessed .pt tensors — see below
├── pyproject.toml # uv project + CUDA/PyG index configuration
└── .python-version # 3.10
```
## 环境配置 (uv)
本项目使用 [**uv**](https://docs.astral.sh/uv/)。Python **3.10** 和 CUDA **12.1**
PyTorch wheels 已锁定在 `pyproject.toml` 中(已在 RTX 4080 / CUDA 12.4 驱动程序上验证 —
cu121 wheels 在 12.4 runtime 上运行良好)。
```
# 如有需要,安装 uv: curl -LsSf https://astral.sh/uv/install.sh | sh
uv sync # creates .venv, resolves torch(cu121) + PyG extensions, writes uv.lock
```
`uv sync` 会从三个已预先配置的源进行拉取:
- **PyPI** — 大多数包
- **`download.pytorch.org/whl/cu121`** — `torch` / `torchvision` / `torchaudio`
- **`data.pyg.io/whl/torch-2.3.0+cu121.html`** — `torch-sparse` / `torch-cluster` /
`torch-spline-conv` (为 torch 2.3.0 + cu121 预编译的 C++ 扩展)
使用 `uv run` 运行任何内容:
```
uv run python experiments/jk_graphsage_ablation.py --data ./data/malnet_tiny_malware_community_optimized.pt
```
## 数据与模型权重
为了保持仓库轻量化,**数据集和训练后的权重未纳入 git 版本控制**
(见 `.gitignore`) — 它们是分开分发的。
**数据集 — MalNet-Tiny。** 一个公开的 Android 函数调用图基准 (5 个类别 ×
1,000 个图:`addisplay`, `adware`, `benign`, `downloader`, `trojan`)。从
**MalNet** 项目下载原始图 — — 并将它们放在 `malnet-graphs-tiny/` 目录下。
**预处理后的张量** (`data/*.pt`, ~1.5 GB) 和 **训练好的检查点**
(`results/**/*.pt`, `*_scaler.pkl`, ~150 MB) 体积较大,托管在外部存储
(Google Drive) 中。如果您只有原始图,请使用下方的流水线重新生成张量。
## 复现流水线
```
# [1] 原始 MalNet-Tiny graphs → PyG tensors + LDP (5-D)
uv run python preprocessing/malnet_data_loader.py
# [2] 添加 community-level structural features → 13-D (community_optimized.pt)
uv run python preprocessing/malware_community_features.py
# [3] 训练 / ablation — 3 feature configs × 5 seeds
uv run python experiments/jk_graphsage_ablation.py # main model
uv run python experiments/graphsage_ablation.py # baselines ...
uv run python experiments/gcn_ablation.py
uv run python experiments/gin_ablation.py
# [4] 分析与图表
uv run python analysis/feature_importance_3methods.py # IG / GradCAM / SHAP
uv run python analysis/inference_speed_comparison.py
uv run python analysis/plot_confusion_matrix.py
```
## 引用
```
@article{kim2026representation,
title = {Representation-Centric Approach for Android Malware Classification:
Interpretability-Driven Feature Engineering on Function Call Graphs},
author = {Kim, Gyumin and Yoon, Dongmin and Kwak, NaeJoung and Lee, ByoungYup},
journal = {Applied Sciences},
volume = {16},
number = {6},
pages = {2670},
year = {2026},
publisher = {MDPI},
doi = {10.3390/app16062670}
}
```
## 许可协议
代码在 [MIT 许可证](LICENSE) 下发布。MalNet-Tiny 数据集受其
MalNet 项目自身的许可/条款约束。
仅 CPU 安装
将 PyTorch 索引指向 CPU 频道,并移除 PyG 的 C++ 扩展: ``` # pyproject.toml [[tool.uv.index]] name = "pytorch-cu121" url = "https://download.pytorch.org/whl/cpu" # was .../cu121 explicit = true ``` 然后从 `dependencies` 中移除 `torch-sparse` / `torch-cluster` / `torch-spline-conv` (现代版的 `torch-geometric` 即使没有它们也能运行此处使用的层,只是速度较慢)。标签:Apex, 凭据扫描, 可解释性AI, 图神经网络, 安卓恶意软件检测, 机器学习, 特征工程, 逆向工具