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-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 (Postgres to eventual RCE); unparameterized param confirmed as postgres superuser, then catalog enumeration to PII (superuser compromise); PostgreSQL pentesting primitives across clauses (OnSecurity). Managed-Postgres hardening extensions (Supabase supautils, Azure, Aiven) have 76 reported bypasses that re-grant superuser and reach the OS via COPY ... PROGRAM (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 (khunt in Oracle, 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 (Rapid7 analysis). A public PoC chains the WebSocket + SQLi path using lo_export to 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.

Sources