Build Your Own Antivirus β€” MalConv PE Malware Classifier

⚠️ Safety notice: the dataset for this project contains live Windows malware. Only build it on Linux (Google Colab, the SE-122 lab, or a Linux VM). Never run, open, or double-click a sample. This repository contains code, a manifest of SHA-256 hashes, and model weights only β€” no binaries.

A MalConv neural network that reads the raw bytes of a Windows program (PE file) and predicts whether it is malware or benign. It is trained on a cleaned subset of DikeDataset and wrapped in a small Gradio app.

Project layout

README.md            setup, how to run everything, results
requirements.txt     pinned dependencies (Python 3.13, the Colab runtime used)
notebooks/           part1_data_exploration.ipynb, part2_model.ipynb (with outputs)
src/                 data_utils.py: file inventory, format detection, PE checks, cleaning funnel
dataset/             manifest.csv, build_dataset.py, revision.txt, baseline.npz, README.md (no binaries)
model/               model.pt (trained weights), config.json (max_len, threshold)
app/                 app.py (Gradio app)

Setup

  • Python 3.13
  • pip install -r requirements.txt
  • Run both notebooks in a hosted Google Colab runtime (Part 2 needs a T4 GPU). Each notebook starts with setup cells that check it is on Colab's Linux VM.

Rebuild the dataset

Linux / Colab only, because it downloads live malware:

python dataset/build_dataset.py

The script clones DikeDataset at the exact commit in dataset/revision.txt into /content/DikeDataset, checks every file's SHA-256 and size against dataset/manifest.csv, and copies it to data/<split>/<benign|malware>/<sha256>. The Part 2 notebook does this automatically after you upload the dataset package.

Split Benign Malware Total Malware %
train 570 5,361 5,931 90.39
val 194 2,005 2,199 91.18
test 198 1,603 1,801 89.01

Run the app

From the project root (the folder that contains app/ and model/):

python app/app.py            # serves the page at http://127.0.0.1:7860
python app/app.py --share    # also prints a temporary public link (useful in Colab)

Drop a .exe or .dll on the page to get a verdict and the model's confidence. The app only reads the file as bytes: it never runs or saves it. Files that are not Windows PE programs (empty files, text, old DOS-only programs) are reported as "Not scanned" instead of getting a verdict. Files larger than 1 MB are scored on their first 1 MB, as in training. On a personal computer, only test with harmless files.

Data (Part 1)

  • DikeDataset has 11,923 files (1,082 benign, 10,841 malware). Checking the first bytes of every file showed 9,932 real PE files. The rest were dropped: 1,774 OLE (Office) files, 8 DOS-only programs and 209 other files.
  • No exact duplicates, and every file name matched its SHA-256. 349 near-duplicate groups (same size and same first 4 KB) hold 3,038 malware files and no benign files, so the split keeps each group together (StratifiedGroupKFold, seed 42, about 60/20/20).
  • pefile parsed every PE file; 1 truncated malware file was removed, leaving 9,931 files (962 benign, 8,969 malware).
  • Labels look clean: all benign files have malice ≀ 0.2 and all malware files > 0.6.
  • Shortcut risk: 54.1% of benign files are 64-bit, but 0.0% of malware is.
  • MAX_LEN = 1 MB (1,048,576 bytes): 4.3% of benign and 0.2% of malware files are longer.
  • Baseline: logistic regression on a 256-bin byte histogram plus log file size.

Model (Part 2)

  • MalConv: embedding (257 tokens β†’ 8 values; byte + 1, with 0 as padding), two parallel 1-D convolutions over 500-byte windows (128 channels, one used as a sigmoid gate), global max pooling, then 128 β†’ 128 (ReLU) β†’ 1 logit. 1,042,953 trainable parameters.
  • Input: the first 1,048,576 bytes of the file, zero-padded.
  • Training: weighted BCE loss (malware weighted by the benign-to-malware ratio of the training split), batch size 4, learning rates 0.001 and 0.0003 for 4 epochs each on a T4 GPU. The best validation ROC AUC was 0.9993 (learning rate 0.0003, epoch 4).
  • Threshold: 0.2584, chosen on the validation set to minimize 2 Γ— false-negative rate + false-positive rate, so a missed malware file costs twice a false alarm. It is fixed in model/config.json and used by the app.

Test-set results

Both models were evaluated on the same 1,801 held-out files (198 benign, 1,603 malware). The model and threshold were fixed before the test set was used.

Model Threshold Accuracy Balanced accuracy ROC AUC Malware precision Malware recall Malware F1
Logistic regression baseline 0.5000 0.9900 0.9745 0.9768 0.9944 0.9944 0.9944
MalConv 0.2584 0.9878 0.9732 0.9979 0.9944 0.9919 0.9931
  • MalConv missed 13 of 1,603 malware files and flagged 9 of 198 benign files.
  • MalConv ranks files better than the baseline (ROC AUC 0.9979 vs 0.9768). At the chosen thresholds, both models make a similar number of mistakes.
  • App check in Colab: 300 random test malware files and all 198 test benign files run through the app's own scan function: 297 of 300 malware caught, 189 of 198 benign cleared. A sample sent through the running Gradio server gave identical results.

Limitations

  • 64-bit shortcut: every malware file is 32-bit, so the model is more suspicious of 32-bit programs. In the app check, 7 of 84 benign 32-bit files were flagged, against 2 of 114 benign 64-bit files.
  • 1 MB cut-off: content after the first 1 MB is never seen. 4 of the 9 benign files flagged in the app check are larger than 1 MB.
  • Small, older benign set: under 1,000 benign PE files, and DikeDataset is not recent malware, so results may not carry over to new threats.
  • Near-duplicate grouping uses size and the first 4 KB, so it cannot guarantee that every member of a malware family stays in one split.

AI-assistant use

  • Claude (Anthropic) set up the project scaffolding (folder structure, .gitignore, the README outline, and the environment-setup cells in the Colab notebooks). It also wrote src/data_utils.py, the Part 1 Section 2–3 analysis cells and their "Results" notes, and app/app.py. It tested the app, uploaded the project to Hugging Face, and wrote this README from the notebook results. The keep/drop decisions, MAX_LEN, and how to treat near-duplicates and 64-bit files were my own decisions after discussing options with Claude.
  • ChatGPT was used to plan the VS Code + Colab workflow.
Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Dataset used to train GRBartho/antivirus-project