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) (case study of 128 reports).
Techniques / payloads (by engine)
- PostgreSQL — superuser gives
COPY ... TO/FROM PROGRAM(direct command exec), large-objectlo_export/lo_importfor file write/read, andquery_to_xml/error channels for exfil. Real chains:ORDER BYinjection in a Flask app to RCE as the DB user (Postgres to eventual RCE); unparameterized param confirmed aspostgressuperuser, then catalog enumeration to PII (superuser compromise); PostgreSQL pentesting primitives across clauses (OnSecurity). Managed-Postgres hardening extensions (Supabasesupautils, Azure, Aiven) have 76 reported bypasses that re-grant superuser and reach the OS viaCOPY ... PROGRAM(superuser guardrail bypasses). - MSSQL —
xp_cmdshellfor direct OS commands; extended stored procedures and UNC-path tricks for file ops and OOB egress. - Oracle —
CREATE JAVA SOURCEcompiles 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 (khunt in Oracle, THN). - MySQL/MariaDB —
INTO OUTFILE/INTO DUMPFILEfor webshell drop whenFILEpriv 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/
psqlinjection was the prerequisite that turned a BeyondTrust argument-injection bug into unauth RCE; exploited as a zero-day (Rapid7 analysis). A public PoC chains the WebSocket + SQLi path usinglo_exportto a reverse shell (PoC). - WordPress wp2shell — unauth SQLi in WP core (CVE-2026-63030/60137) chained to pre-auth RCE (wp2shell).
- Philips Hue Bridge Pro (ZDI-26-891) — auth bypass + SQLi in the HTTP service yields code execution as root (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 (Check Point) — link Insecure Deserialization. - HTB DutyBound — PostgreSQL stacked-query SQLi to RCE, a clean teaching chain (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 (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
- SQL Injection and Postgres: An Adventure to Eventual RCE · original
- From SQLi to Infrastructure-Level RCE: PostgreSQL Superuser · original
- Pentesting PostgreSQL with SQL Injections · original
- Breaking the Superuser Guardrails of managed-PostgreSQL Providers · original
- Attackers hid malware inside Oracle Database after SQLi · original
- Attackers Compile khunt Inside Oracle · original
- Rapid7 Analysis: CVE-2024-12356 · original
- From SQLi to RCE: Exploiting LangGraph's Checkpointer · original
- ZDI-26-891 Philips Hue Bridge Pro SQLi RCE · original
- Narrow Grants, Wide Engines · original