# SQL Injection to RCE
> Escalating a SQL injection from data access to operating-system command execution, using database-native features that read/write files, run programs, load code, or feed a deserialization sink.
## How it works
Whether SQLi reaches RCE depends mostly on the **DB role the app connects as** and the **engine's native capabilities**. A superuser/sysadmin connection is almost always game over; a least-privileged role usually caps impact at data. The escalation primitives differ per engine, and several chains run *through* the database into the host or into a second vulnerability (deserialization, file write) ([[raw/youtube.com/how-to-turn-sql-injection-into-an-rce-or-a-file-read-case-study-of-128-487|case study of 128 reports]]).
## Techniques / payloads (by engine)
- **PostgreSQL** — superuser gives `COPY ... TO/FROM PROGRAM` (direct command exec), large-object `lo_export`/`lo_import` for file write/read, and `query_to_xml`/error channels for exfil. Real chains: `ORDER BY` injection in a Flask app to RCE as the DB user ([[raw/pulsesecurity.co.nz/sql-injection-and-postgres-an-adventure-to-eventual-rce-4791|Postgres to eventual RCE]]); unparameterized param confirmed as `postgres` superuser, then catalog enumeration to PII ([[raw/infosecwriteups.com/from-sql-injection-to-infrastructure-level-rce-a-postgresql-superuser-10550|superuser compromise]]); PostgreSQL pentesting primitives across clauses ([[raw/onsecurity.io/pentesting-postgresql-with-sql-injections-4792|OnSecurity]]). Managed-Postgres hardening extensions (Supabase `supautils`, Azure, Aiven) have 76 reported bypasses that re-grant superuser and reach the OS via `COPY ... PROGRAM` ([[raw/mehmetince.net/breaking-the-superuser-guardrails-of-managed-postgresql-providers-12080|superuser guardrail bypasses]]).
- **MSSQL** — `xp_cmdshell` for direct OS commands; extended stored procedures and UNC-path tricks for file ops and OOB egress.
- **Oracle** — `CREATE JAVA SOURCE` compiles and runs Java *inside* the DB ("oraexec"): the **khunt** toolkit was smuggled in via SQLi into a Java app, giving SYSTEM-level command exec, credential/registry theft, and EDR evasion ([[raw/csoonline.com/attackers-hid-malware-inside-oracle-database-after-sql-injection-breac-11103|khunt in Oracle]], [[raw/thehackernews.com/attackers-compile-khunt-inside-oracle-to-turn-sql-injection-into-windo-11141|THN]]).
- **MySQL/MariaDB** — `INTO OUTFILE`/`INTO DUMPFILE` for webshell drop when `FILE` priv and a writable webroot exist; UDF-based command exec in older/misconfigured installs.
- **Stacked queries** needed for most write/DDL/RCE paths — check driver/API support (PHP/Python MySQL APIs sometimes allow it; PostgreSQL/MSSQL generally do).
## Notable cases
- **CVE-2025-1094 → BeyondTrust CVE-2024-12356** — PostgreSQL quoting/`psql` injection was the prerequisite that turned a BeyondTrust argument-injection bug into unauth RCE; exploited as a zero-day ([[raw/rapid7.com/rapid7-analysis-cve-2024-12356-9362|Rapid7 analysis]]). A public PoC chains the WebSocket + SQLi path using `lo_export` to a reverse shell ([[raw/github.com/cve-2025-1094-websocket-and-sql-injection-exploit-script-4633|PoC]]).
- **WordPress wp2shell** — unauth SQLi in WP core (CVE-2026-63030/60137) chained to pre-auth RCE ([[raw/aikido.dev/unauthenticated-rce-in-wordpress-core-wp2shell-via-sql-injection-10493|wp2shell]]).
- **Philips Hue Bridge Pro (ZDI-26-891)** — auth bypass + SQLi in the HTTP service yields code execution as **root** ([[raw/zerodayinitiative.com/zdi-26-891-sql-injection-advisory-13404|ZDI]]).
- **LangGraph checkpointer** — SQLi (CVE-2025-67644/CVE-2026-27022) chains into unsafe msgpack deserialization (CVE-2026-28277) for RCE when `get_state_history()` is exposed with a user-controlled filter ([[raw/research.checkpoint.com/from-sqli-to-rce-exploiting-langgraphs-checkpointer-8942|Check Point]]) — link [[Insecure Deserialization]].
- **HTB DutyBound** — PostgreSQL stacked-query SQLi to RCE, a clean teaching chain ([[raw/ibrahimisiaqbolaji.com/dutybound-htb-postgresql-stacked-query-sqli-to-rce-13405|DutyBound]]).
## Defenses
Least privilege is the primary control: never connect apps as superuser/sysadmin; strip `FILE`, `xp_cmdshell`, `CREATE JAVA`, and `COPY ... PROGRAM` where not needed. Note that even minimal roles leave native engine attack surface (query optimizer, wire parser) reachable — memory-corruption chains in Oracle/PostgreSQL/MySQL achieve OS code exec from low-privilege accounts, so patching the engine matters too ([[raw/depthfirst.com/narrow-grants-wide-engines-native-database-attack-surface-from-minimal-15649|narrow grants, wide engines]]). Monitor Oracle for unexpected Java source/compiled class objects; segment the DB host's outbound network. And fix the SQLi — these are all downstream of an injectable sink.
## Related
- [[SQL Injection]]
- [[Union-Based SQL Injection]]
- [[Second-Order SQL Injection]]
- [[Insecure Deserialization]]
- [[Command Injection]]
## Sources
- [[raw/pulsesecurity.co.nz/sql-injection-and-postgres-an-adventure-to-eventual-rce-4791|SQL Injection and Postgres: An Adventure to Eventual RCE]]
- [[raw/infosecwriteups.com/from-sql-injection-to-infrastructure-level-rce-a-postgresql-superuser-10550|From SQLi to Infrastructure-Level RCE: PostgreSQL Superuser]]
- [[raw/onsecurity.io/pentesting-postgresql-with-sql-injections-4792|Pentesting PostgreSQL with SQL Injections]]
- [[raw/mehmetince.net/breaking-the-superuser-guardrails-of-managed-postgresql-providers-12080|Breaking the Superuser Guardrails of managed-PostgreSQL Providers]]
- [[raw/csoonline.com/attackers-hid-malware-inside-oracle-database-after-sql-injection-breac-11103|Attackers hid malware inside Oracle Database after SQLi]]
- [[raw/thehackernews.com/attackers-compile-khunt-inside-oracle-to-turn-sql-injection-into-windo-11141|Attackers Compile khunt Inside Oracle]]
- [[raw/rapid7.com/rapid7-analysis-cve-2024-12356-9362|Rapid7 Analysis: CVE-2024-12356]]
- [[raw/research.checkpoint.com/from-sqli-to-rce-exploiting-langgraphs-checkpointer-8942|From SQLi to RCE: Exploiting LangGraph's Checkpointer]]
- [[raw/zerodayinitiative.com/zdi-26-891-sql-injection-advisory-13404|ZDI-26-891 Philips Hue Bridge Pro SQLi RCE]]
- [[raw/depthfirst.com/narrow-grants-wide-engines-native-database-attack-surface-from-minimal-15649|Narrow Grants, Wide Engines]]