Models Are Codes: Towards Measuring Malicious Code Poisoning Attacks on Pre-trained Model Hubs

Jian Zhao, Shenao Wang, Yanjie Zhao, Xinyi Hou, Kailong Wang, Peiming Gao, Y. Zhang, Chen Wei, Haoyu Wang · DOI

Abstract

The proliferation of pre-trained models (PTMs) and datasets has led to the emergence of centralized model hubs like Hugging Face, which facilitate collaborative development and reuse. However, recent security reports have uncovered vulnerabilities and instances of malicious attacks within these platforms, highlighting growing security concerns. This paper presents the first systematic study of malicious code poisoning attacks on pre-trained model hubs, focusing on the Hugging Face platform. We conduct a comprehensive threat analysis, develop a taxonomy of model formats, and perform root cause analysis of vulnerable formats. While existing tools like Fickling and ModelScan offer some protection, they face limitations in semantic-level analysis and comprehensive threat detection. To address these challenges, we propose MalHug, an end-to-end pipeline tailored for Hugging Face that combines dataset loading script extraction, model deserialization, in-depth taint analysis, and heuristic pattern matching to detect and classify malicious code poisoning attacks in datasets and models. In collaboration with Ant Group, we have implemented and deployed MalHug on a mirrored Hugging Face instance within their infrastructure, where it has been operational for over three months. During this period, MalHug has monitored more than 705K models and 176K datasets, uncovering 91 malicious models and 9 malicious dataset loading scripts. These findings reveal a range of security threats, including reverse shell, browser credential theft, and system reconnaissance. This work not only bridges a critical gap in understanding the security of the PTM supply chain but also provides a practical, industry-tested solution for enhancing the security of pre-trained model hubs.

Introduction

The rise of centralized model hubs like Hugging Face has transformed AI development into a collaborative ecosystem. However, this convenience introduces a significant security oversight: the assumption that models and datasets are passive data. In reality, many popular model formats and dataset loading mechanisms are functionally equivalent to executable code. This paper presents a systematic study of "code poisoning" attacks, where malicious actors embed harmful logic within these artifacts to compromise user systems. We introduce the concept that "Models Are Codes," emphasizing that loading a model is often as risky as running an untrusted executable file.

The MalHug Detection Pipeline

To address the limitations of existing keyword-based scanners, we developed MalHug, an end-to-end detection pipeline. MalHug moves beyond simple pattern matching by performing semantic-level analysis to distinguish between legitimate code and malicious payloads.

The MalHug pipeline for detecting malicious code in datasets and models.

The pipeline operates in four distinct stages:

  1. Extraction: Identifying vulnerable model files (e.g., Pickle variants, Keras HDF5) and extracting Python loading scripts from datasets.
  2. Deserialization & Disassembly: Converting opaque model bytecodes into human-readable Abstract Syntax Trees (ASTs) or disassembled instructions to analyze their underlying logic before execution.
  3. In-depth Taint Analysis: Utilizing the Scalpel framework to track data flow from sensitive "sources" to dangerous "sinks." This is critical because our analysis found that 91.2% of potentially dangerous API calls (like getattr) in legitimate models are benign; taint analysis allows MalHug to distinguish these from actual malicious exfiltration.
  4. Heuristic Pattern Matching: Applying YARA rules to identify known malware signatures, specific shell command sequences, and common obfuscation techniques.

The Vulnerability of the Model Supply Chain

Our audit of Hugging Face revealed a staggering reliance on insecure serialization formats. Python’s pickle module, while flexible, is "insecure by design" because it allows arbitrary code execution during the deserialization process. This makes it the primary vector for model poisoning attacks.

Distribution of model formats on Hugging Face, highlighting the dominance of vulnerable Pickle-based formats.

As shown in the distribution analysis, over 55% of models on the platform—primarily those within the PyTorch ecosystem—rely on Pickle-based formats. This creates a massive attack surface where simply calling torch.load() on a popular but compromised model could lead to immediate system takeover.

Real-World Findings and Impact

In collaboration with Ant Group, MalHug was deployed to monitor over 705,000 models and 176,000 datasets, totaling approximately 179.4 Terabytes of data. This large-scale audit uncovered 100 malicious artifacts that had successfully bypassed existing platform protections:

  • 91 Malicious Models: Including 76 Pickle variants and 15 Keras models.
  • 9 Malicious Dataset Scripts: These scripts are often overlooked by users but were found to be frequently used for stealthy system reconnaissance.
  • Specific Threats Found: We identified "reverse shells" (e.g., in the star23/baller10 model) that allow remote control of a user's machine, and credential stealers (e.g., in the Besthpz/best repository) designed to exfiltrate Chrome browser passwords and cookies.

Conclusion and Future Work

The "Models Are Codes" paradigm necessitates a fundamental shift in how we secure the AI supply chain. While MalHug provides a robust, industry-tested defense, the long-term solution lies in migrating the community toward secure-by-design formats like Safetensors, which store weights without allowing code execution. Future work will focus on addressing advanced anti-analysis techniques and refining detection for complex frameworks like TensorFlow to further reduce manual review overhead.

Summary updated

BibTeX
@misc{zhao2024models,
  author = {Jian Zhao and Shenao Wang and Yanjie Zhao and Xinyi Hou and Kailong Wang and Peiming Gao and Y. Zhang and Chen Wei and Haoyu Wang},
  title = {{Models Are Codes: Towards Measuring Malicious Code Poisoning Attacks on Pre-trained Model Hubs}},
  year = {2024},
  eprint = {2409.09368},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2409.09368},
}
Figure