Courses

RCE via Database Functions
OSWE · Chapter 23

RCE via Database Functions

RCE via database functions is the exploitation pattern that ties together several earlier OSWE chapters (PostgreSQL UDFs, deserialization) — using a database's own extensibility features as the mechanism to execute OS-level commands from what starts as a data-layer vulnerability.

Key concepts

  • Different database engines expose different native mechanisms: PostgreSQL C-language UDFs, MSSQL `xp_cmdshell`, MySQL user-defined functions (UDFs) loaded from a plugin directory.
  • This technique typically requires elevated database privileges — it is most often a post-exploitation escalation from "I have SQL injection" to "I have a shell on the DB host", not the initial foothold.
  • Because it runs inside the database server process, successful exploitation compromises the entire host, not just the data the application account could normally reach.
  • Hardening: disable dangerous built-in procedures (`xp_cmdshell` in MSSQL is disabled by default in modern installs), and apply least-privilege database roles so the web application account can never reach admin-only functions.

Example

-- MSSQL: classic (and heavily monitored) command execution via xp_cmdshell
EXEC xp_cmdshell 'whoami';

Try it yourself

Research how `xp_cmdshell` is disabled by default in modern MSSQL installations, and explain what specific privilege an attacker would need to re-enable it via `sp_configure`.

@ Daniel Burlacu