MalwarePT: A Binary-Level Foundation Model for Malware Analysis

Saastha Vasan, Yuzhou Nie, Kaie Chen, Yigitcan Kaya, Hojjat Aghakhani, Roman Vasilenko, Wenbo Guo, Christopher Kruegel, Giovanni VignaarXiv 2026

TL;DR

MalwarePT is a binary-level foundation model designed specifically for malware analysis, moving away from fragmented, task-specific architectures and naive byte-level tokenization. By combining a ModernBERT-style encoder with a Byte-Pair Encoding (BPE) tokenizer trained on raw malicious code bytes, it achieves strong transferability across token-, function-, and document-level malware analysis tasks.

What Problem Does This Paper Address?

Machine learning has become a staple in automated malware analysis, but the current landscape is highly fragmented. Most models are built for a single task (e.g., detecting maliciousness vs. classifying a malware family) and rely on handcrafted features or raw byte sequences that don't transfer well to other tasks.

While binary foundation models exist, they are typically pretrained on benign, compiler-optimized code, making them ill-suited for the messy, obfuscated distributions found in real-world malware. Furthermore, almost all existing models use simple byte-level tokenization (a fixed 256-token vocabulary). This forces models to process common multi-byte patterns—like function prologues or API stubs—as long chains of individual tokens, wasting precious context windows and limiting the model's ability to capture long-range dependencies.

Methods

The authors introduce MalwarePT, an encoder-only bidirectional transformer inspired by ModernBERT. Its pipeline consists of three main stages:

  1. BPE Tokenization on Raw Bytes: To move beyond byte-level limitations, MalwarePT maps bytes to unique Unicode characters and trains a Byte-Pair Encoding (BPE) tokenizer directly on raw malware code sections. This allows frequent multi-byte patterns to be merged into single tokens.
  2. ModernBERT-Style Architecture: The model utilizes Rotary Position Embeddings (RoPE), RMSNorm pre-normalization, and a hybrid attention mechanism. Instead of full quadratic self-attention, most layers apply a local sliding window (size 128), while every fourth layer uses global attention. Crucially, the start token (<s>) is globally connected across all layers to effectively aggregate sequence-level representations.
  3. Masked Language Modeling (MLM) Pretraining: The model is pretrained by masking 25% of non-special tokens from the code sections (.text) of Windows Portable Executable (PE) files.
Overview of MalwarePT pipeline

Datasets

The authors curated a dataset of 146,836 unpacked Windows PE files (115,826 malicious and 31,010 benign) filtered from an initial pool of over 560k samples.

  • Sources: Malicious samples were gathered from VirusTotal, Packware, and DeepCapa; benign samples came from Assemblage, DeepCapa, and PACE.
  • Temporal Splits: To simulate realistic deployment and evaluate robustness against concept drift, the malicious data was split chronologically into three cohorts: $D_1$ (2017–2018), $D_2$ (2018–2020), and $D_3$ (2020–2022).

Key Insights and Ideas

  • The 1,024-Token Sweet Spot: Compressing raw bytes into larger BPE tokens allows the model to capture local code patterns more effectively and extends its practical context window. However, bigger isn't always better: vocabularies larger than 1,024 tokens suffer from diminishing returns because the tokenizer creates highly specialized tokens for rare byte sequences, leading to data sparsity.
  • Global <s> Connection is Essential: By keeping the sequence-starting <s> token globally connected even in local attention layers, MalwarePT preserves its ability to build comprehensive function- and document-level embeddings without incurring the massive computational cost of full self-attention.
Attention patterns in binary encoders
  • Code-Centric vs. Structure-Centric Complementarity: Code-centric models (like MalwarePT) and structure-centric models (like EmberGBM, which relies on PE headers and metadata) capture fundamentally different signals. MalwarePT excels at understanding what the binary does at runtime, whereas structural models catch metadata artifacts that can be easily manipulated post-compilation without altering execution.

Key Results

The authors evaluated MalwarePT across three granularities of downstream tasks:

  • API-Call Prediction (Token-level): MalwarePT achieved a Weighted F1 score of 86.55% and a Top-1 accuracy of 87.83%, vastly outperforming a non-pretrained baseline (64.54% F1), proving that pretraining successfully captures local code context.
  • Functionality Classification (Function-level): When trained to predict expert-crafted CAPA behavioral labels, MalwarePT hit 91.80% Top-1 accuracy on normal test splits and demonstrated strong generalization on a challenging "rule-shift" split (unseen labeling rules).
  • Malware Detection (Document-level): Under a strict low False Positive Rate constraint (FPR $\approx$ 0.001), MalwarePT outperformed all other neural baselines (including Malconv2, MalGraph, and the benchmark-pretrained XDA foundation model).

Limitations and Future Work

  • Code Segment Evasion: Malware authors can undermine the model by creating dummy code segments or redirecting entry points, which can cause the preprocessing tools to fail to extract the actual malicious code segment.
  • Packed Code: Because MalwarePT relies on static analysis of code bytes, it struggles with heavily packed, polymorphic, or encrypted malware. The authors suggest integrating dynamic artifacts, such as post-unpacking memory snapshots, in future work.
  • Computational Cost: As a transformer-based model, MalwarePT incurs higher latency (~3.3 seconds per file on an L40S GPU) than lightweight static feature extractors (~50 ms), making it better suited for deep backend analysis pipelines than rapid endpoint filtering.

Summary updated

BibTeX
@misc{vasan2026malwarept,
  author = {Saastha Vasan and Yuzhou Nie and Kaie Chen and Yigitcan Kaya and Hojjat Aghakhani and Roman Vasilenko and Wenbo Guo and Christopher Kruegel and Giovanni Vigna},
  title = {{MalwarePT: A Binary-Level Foundation Model for Malware Analysis}},
  year = {2026},
  eprint = {2605.16455},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2605.16455},
}
Figure