Skip to content

Latest commit

Β 

History

46 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ‡§πŸ‡· PortuguΓͺs

GPT-2 Fine-Tuning for Malware Classification

Python PyTorch HuggingFace License Thesis Paper

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).


Abstract

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.

Academic Reference

Key Features

  • 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

Architecture

Proposed method

Results

Microsoft BIG 2015 - Classification (9 classes)

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

ESET2022 - Binary Identification

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.

Datasets

Microsoft BIG 2015

ESET2022

  • Source: Private dataset provided by ESET
  • Size: ~1,000 .asm files
  • Classes: 2 (malware / benign)
  • Task: Binary identification

Quick Start

1. Setup

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.txt

2. Preprocessing

Process the raw .asm files:

python main.py --process

This filters code segments, removes noise, and saves to proc-1/.

3. Dataset Cache

Tokenize and cache the dataset as CSV:

python main.py --cache --fold=1 --version=3

4. Training

# 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=3

Run all 10 folds with the provided script:

# Linux
bash src/scripts/run.sh

# Windows
src/scripts/run.bat

5. Evaluation

python main.py --test --multi --fold=1
python main.py --metrics --multi

Project Structure

β”œβ”€β”€ 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

Configuration

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    # Windows

Or 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_key

Citation

If 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},
}

License

This project is licensed under the MIT License - see LICENSE.

Acknowledgments

  • IME (Instituto Militar de Engenharia) - Master's program support
  • ESET - Dataset and collaboration
  • HuggingFace - Transformers library

About

GPT-2 Fine-Tuning for Malware Classification

Resources

Stars

0 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages