This repository demonstrates a real security issue in PyTorch's pickle-based model serialization.
PyTorch .pt model files can execute arbitrary Python code during deserialization when loaded unsafely.
A model file is not just “data”. It can become executable code.
- DO NOT use this against systems you do not own.
- DO NOT upload generated demonstration models to public model hubs.
- DO understand the security implications before loading untrusted
.ptfiles. - DO use this project to learn AI supply chain security and unsafe deserialization risks.
PyTorch uses Python's pickle module internally for model serialization.
pickle is powerful — but dangerous.
When torch.load() deserializes a malicious object, arbitrary code can run immediately during loading.
import torch
# 💀 Arbitrary code can execute here
model = torch.load("harmless_look.pt")This behavior turns a model file into a potential Remote Code Execution (RCE) vector.
PyTorch introduced safer loading behavior using:
weights_only=TrueExample:
import torch
# ✅ Safer loading
model = torch.load("harmless_look.pt", weights_only=True)This blocks dangerous Python object deserialization and only loads tensor weights.
AI supply chains are becoming a major attack surface.
Developers frequently download models from:
- Hugging Face
- GitHub repositories
- Discord/Telegram communities
- Random model mirrors
- AI marketplaces
Most people assume model files are harmless.
This project demonstrates why that assumption is dangerous.
============================================================
📚 PyTorch Pickle RCE Demo | Educational Purpose Only
👤 Made by Aryan Giri | giriaryan694-a11y
============================================================
📡 What command should the model execute?
Examples (safe, demonstrable):
1. touch hacked.txt
2. echo 'This model is dangerous' > warning.txt
3. ls -la > directory_listing.txt
💀 Enter your custom command: touch YOU_HAVE_BEEN_PWNED
📦 What should the model be named? harmless_look.pt
✅ Successfully generated: harmless_look.pt
git clone https://github.com/giriaryan694-a11y/pickle-ride
cd pickle-ridepip install -r requirements.txtIf PyTorch wheel installation fails:
apt install python-torch
pip install pyfiglet termcolorpython main.pyThe tool will:
- Generate a demonstration
.ptmodel - Ask for a custom command
- Show unsafe loading behavior
- Generate a safe loading example
- Explain why the exploit works
import torch
# DEMONSTRATION:
# This executes the embedded payload
torch.load("demo_model.pt", weights_only=False)
print("✅ Model loaded — demonstration payload executed")import torch
# Safer loading
# Dangerous objects are blocked
torch.load("demo_model.pt", weights_only=True)Expected behavior:
_pickle.UnpicklingError
because unsafe functions like exec and os.system are not allowlisted.
Under the hood, the project abuses Python's special pickle deserialization behavior.
Malicious classes can override:
__reduce__()During deserialization, pickle executes the callable returned by __reduce__().
Example concept:
class Evil:
def __reduce__(self):
return (os.system, ("touch YOU_HAVE_BEEN_PWNED",))When unpickled:
pickle.loads(...)Python executes:
os.system("touch YOU_HAVE_BEEN_PWNED")PyTorch inherits this risk because .pt files rely on pickle serialization.
This type of issue affects:
- ML engineers
- AI researchers
- Kaggle users
- Self-hosted LLM users
- Fine-tuning pipelines
- CI/CD model deployment systems
- AI startups
- GPU cloud workloads
A malicious model can:
- Execute shell commands
- Modify files
- Drop persistence
- Exfiltrate secrets
- Backdoor environments
- Attack CI runners
- Pivot inside internal infrastructure
To understand AI supply chain attacks deeper:
- TryHackMe — Understanding AI Supply Chains
- Pickle deserialization vulnerabilities
- Unsafe model loading attacks
- AI supply chain security research
- Hugging Face model trust risks
https://tryhackme.com/room/understanding-ai-supplychains
Safe educational demonstrations:
touch hacked.txtecho 'This model is dangerous' > warning.txtls -la > directory_listing.txtThis project exists to:
- Teach unsafe deserialization
- Demonstrate AI supply chain risks
- Show why model provenance matters
- Encourage safer model loading practices
- Help developers understand pickle internals
A .pt file can be:
- A model
- A payload
- Or both.
Always:
weights_only=Truewhen loading untrusted PyTorch models.
Loading untrusted AI models is equivalent to running untrusted code.
Treat model files like executables — not harmless assets.