MAGIKA: AI-Powered Content-Type Detection

Yanick Fratantonio, Luca Invernizzi, Loua Farah, Kurt Thomas, Marina Zhang, Ange Albertini, Francois Galilee, Giancarlo Metitieri, Julien Cretin, Alex Petit-Bianco, David Tao, Elie Bursztein

TL;DR

Google researchers have introduced Magika, an AI-powered content-type detection tool that replaces fragile, manually crafted file signatures with a highly optimized deep learning model. Operating with a tiny 1MB memory footprint, Magika runs efficiently on a single CPU and achieves a stellar 99% average F1 score across 113 content types, significantly outperforming industry standards like libmagic.

What Problem Does This Paper Address?

Determining what kind of data is encoded in a file (content-type detection) is a foundational task for operating systems, code editors, and security pipelines. For decades, the gold standard has been the UNIX file command, which relies on libmagic and a massive database of hardcoded regular expressions and byte "signatures."

However, signature-based approaches are notoriously brittle. A minor, non-functional change—such as stripping or adding whitespace around an assignment operator in a script—can completely throw off a regex scanner, causing a JavaScript file to be mislabeled as generic ASCII text.

Example of signature-based frailty on JavaScript snippets.

While some machine learning models have been proposed to fix this, they have historically been too slow, too memory-intensive, or limited to niche domains (like only identifying programming languages).

Methods

Magika treats content-type detection as a multi-class classification problem but optimizes the network to ensure it can run at massive scale without requiring a GPU.

Instead of reading an entire file—which would be computationally prohibitive for large files—Magika extracts three 512-byte blocks from the beginning, middle, and end of the file (after stripping leading and trailing whitespace). This fixed 1.5 KB input guarantees constant-time inference regardless of file size.

Architecture of the Magika deep learning model.

The architecture consists of:

  1. Input Embedding: The 3x512 bytes are one-hot encoded and passed through a Dense layer to create a 128-float vector representation.
  2. Trunk Network: The vector is reshaped into 4-byte blocks (384x512 dimensions) so the model can process local byte structures efficiently. It then passes through two 256-dimensional Dense layers with GELU activations, followed by global max pooling to downsample back to a 1D vector.
  3. Output Layer: A final Dense layer with a softmax activation outputs a probability distribution over the 113 supported content types.

To make it production-ready, the authors exported the model to ONNX Runtime, which speeds up model loading by 15x compared to TensorFlow/Keras, making it highly suitable for command-line utilities.

Datasets

The authors curated a massive, novel dataset of 26.5 million files spanning 113 canonical content types (70 binary formats, 43 text formats).

  • Sources: Files were randomly sampled from GitHub (primarily text/code) and VirusTotal (primarily binaries and documents).
  • Validation: To ensure ground-truth accuracy without relying on the very tools they were trying to beat, the authors used strict validation heuristics: minimum file sizes, mandatory magic byte prefixes for specific formats (like verifying Windows executables start with MZ), and character encoding checks for text.
  • Data Augmentation: They synthetically generated UNKNOWN (random byte sequences) and TXT (random text strings) samples to train the model to gracefully handle out-of-distribution files.

The final evaluation benchmark used a clean, holdout split of 1.2 million samples (10k per content type).

Key Insights and Ideas

  • Sub-file Visibility is Sufficient: One of the most surprising takeaways is that a model doesn't need to see a whole file to accurately identify it. Just 1.5 KB split across the header, midsection, and footer provides enough structural context to hit 99% accuracy.
  • No Unpacking Required: Traditional tools often have to unpack container formats (like Zip files for .docx or .apk) before running signature checks. Magika bypasses this entirely, learning to recognize the distinct statistical patterns of compressed formats directly from the raw bytes.
  • Per-Content-Type Thresholds: Instead of using a global confidence threshold, Magika tunes thresholds individually for each file type. For instance, a 0.80 probability might be robust enough to declare a file is HTML, but the same threshold might yield poor accuracy for a PDF.

Key Results

  • Accuracy: Magika achieves an average 99% F1 score overall. It delivers a 4% F1 gain over file-mime on binary types and a massive 22% F1 gain over guesslang on text types.
  • Speed: For bulk processing, Magika takes just 5.77ms per file on a single CPU. When scaled across 8 CPUs, it drops to 1.39ms per file.
  • Real-World Impact: The tool is highly practical and already deployed at scale. Google uses Magika in production to scan hundreds of billions of files weekly within Gmail and Google Drive.

Limitations and Future Work

  • Polyglot Files: Magika currently only outputs a single, most-probable content type. It struggles with "polyglot" files—files intentionally crafted to be simultaneously valid under multiple formats (e.g., a file that can run as both a Python script and a Bash script).
  • Adversarial Vulnerability: Because Magika is open-source, attackers can theoretically use adversarial machine learning techniques to inject subtle byte perturbations that trick the model into misclassifying malicious files without breaking the file's actual execution.
  • Scope of Content Types: The version evaluated in the paper supports 113 types. While the authors note that newer iterations support over 200, it still requires ongoing training data collection to keep pace with emerging file formats.

Summary updated

BibTeX
@inproceedings{fratantonio2025magika,
  author = {Yanick Fratantonio and Luca Invernizzi and Loua Farah and Kurt Thomas and Marina Zhang and Ange Albertini and Francois Galilee and Giancarlo Metitieri and Julien Cretin and Alex Petit-Bianco and David Tao and Elie Bursztein},
  title = {{MAGIKA: AI-Powered Content-Type Detection}},
  booktitle = {ICSE},
  year = {2025},
  doi = {10.1109/icse55347.2025.00158},
}
Figure