Fine-tuning GPT-2 for malware classification using raw assembly (.asm) files. This repository contains the full pipeline and experiments conducted during my Master's research at IME (Instituto Militar de Engenharia).
Malware classification and identification are critical tasks in cybersecurity. This work proposes a method based on fine-tuning GPT-2, a transformer-based language model, to classify malware directly from assembly code. The assembly files are preprocessed to extract only the pure code segment, tokenized, and fed into GPT-2 for sequence classification. Experiments on two datasets - Microsoft BIG 2015 (9-class classification) and ESET2022 (binary identification) - achieve results near the state of the art, with 98.96% accuracy on BIG 2015 and 96.18% accuracy on ESET2022.
- Master's Thesis: DissertaΓ§Γ£o.pdf (Portuguese)
- Published Paper: Malware Classification Using the GPT-2 Model - Paper.pdf
- LinkedIn Article: Malware Classification using the GPT-2 Model
- Institution: IME - Instituto Militar de Engenharia
- Full pipeline: preprocessing β tokenization/chunking β GPT-2 fine-tuning β evaluation
- Multi-dataset support: Microsoft BIG 2015 (9 classes) and ESET2022 (binary)
- 10-fold cross-validation for robust evaluation
- Binary and multi-class classification modes
- CSV-based dataset caching for fast reload across experiments
- WhatsApp notifications during long training runs (opt-in via env vars)
- TF-IDF baseline archived separately from the main pipeline
| Epochs | Accuracy (%) | F1 Macro (%) | Training Time |
|---|---|---|---|
| 1 | 98.62 Β± 0.50 | 96.87 Β± 0.76 | 135.29 h |
| 2 | 98.82 Β± 0.33 | 97.35 Β± 0.81 | 242.43 h |
| 3 | 98.96 Β± 0.27 | 97.79 Β± 1.04 | 457.09 h |
| Epochs | Accuracy (%) | F1 Macro (%) | Training Time |
|---|---|---|---|
| 1 | 95.63 Β± 0.56 | 95.40 Β± 0.59 | 7.19 h |
| 2 | 95.85 Β± 0.43 | 95.65 Β± 0.45 | 14.92 h |
| 3 | 96.01 Β± 0.46 | 95.82 Β± 0.48 | 23.41 h |
| 4 | 96.18 Β± 0.47 | 96.00 Β± 0.50 | 29.74 h |
Results reported as mean Β± standard deviation across 10 folds.
- Source: Microsoft Malware Classification Challenge (BIG 2015)
- Size: ~20,000
.asmfiles - Classes: 9 malware families (Ramnit, Lollipop, Kelihos_ver3, Vundo, Simda, Tracur, Kelihos_ver1, Obfuscator.ACY, Gatak)
- Task: Multi-class classification
- Source: Private dataset provided by ESET
- Size: ~1,000
.asmfiles - Classes: 2 (malware / benign)
- Task: Binary identification
git clone https://github.com/matheusvanzan/gpt-malware-class.git
cd gpt-malware-class
python -m venv venv
source venv/bin/activate # Linux
venv\Scripts\activate # Windows
pip install -r requirements.txtProcess the raw .asm files:
python main.py --processThis filters code segments, removes noise, and saves to proc-1/.
Tokenize and cache the dataset as CSV:
python main.py --cache --fold=1 --version=3# Binary mode (ESET2022)
python main.py --train --model=gpt2 --limit=all --fold=1 --epochs=4
# Multi-class mode (BIG 2015)
python main.py --train --multi --model=gpt2 --limit=all --fold=1 --epochs=3Run all 10 folds with the provided script:
# Linux
bash src/scripts/run.sh
# Windows
src/scripts/run.batpython main.py --test --multi --fold=1
python main.py --metrics --multiβββ main.py # CLI entry point (thin wrapper)
βββ src/ # Source code
β βββ cli.py # CLI argument parsing and orchestration
β βββ dataset.py # Tokenization, chunking, CSV caching
β βββ model.py # GPT-2 fine-tuning (train / test / metrics)
β βββ processor.py # Assembly preprocessing & cleaning
β βββ settings.py # Configuration (paths, hyperparameters)
β βββ metrics.py # Accuracy, F1, confusion matrix
β βββ utils/
β β βββ whats.py # WhatsApp notification helper
β βββ tests/
β β βββ test_settings.py
β βββ scripts/ # Experiment automation scripts
β β βββ run.bat
β β βββ run.sh
β β βββ dataset_cache.bat
β β βββ tensorboard.bat
β β βββ zip_partials.py
β β βββ dataset_stats.py
β βββ __init__.py
βββ papers/ # Academic publications
β βββ Paper.pdf # Published paper
β βββ DissertaΓ§Γ£o.pdf # Master's thesis
βββ assets/ # Images and diagrams
βββ requirements.txt
βββ pyproject.toml
βββ ruff.toml
βββ .gitignore
βββ LICENSE
Set the data path via environment variable:
export GPT_MALWARE_PATH=/path/to/gpt-malware # Linux
set GPT_MALWARE_PATH=D:\path\to\gpt-malware # WindowsOr edit src/settings.py to set the default paths.
WhatsApp notifications are disabled by default. To enable:
export WHATSAPP_PHONE=+5511999999999
export WHATSAPP_API_KEY=your_api_keyIf you use this work in your research, please cite:
@mastersthesis{vanzan2024,
author = {Matheus Vanzan},
title = {Malware Classification Using the GPT-2 Model},
school = {Instituto Militar de Engenharia (IME)},
year = {2024},
}This project is licensed under the MIT License - see LICENSE.
- IME (Instituto Militar de Engenharia) - Master's program support
- ESET - Dataset and collaboration
- HuggingFace - Transformers library
