Courses

UDF Reverse Shells
OSWE · Chapter 17

UDF Reverse Shells

A UDF reverse shell is the practical exploitation outcome of the PostgreSQL User-Defined Function technique covered earlier: once an attacker can register a native C UDF, they can implement a function that spawns a shell connected back to an attacker-controlled listener.

Key concepts

  • This requires the same precondition as any C UDF abuse: sufficient privilege (superuser) to run `CREATE FUNCTION ... LANGUAGE C`, and the ability to place a compiled shared object where PostgreSQL can load it.
  • The compiled function typically calls a system-level exec/fork routine from within the C UDF body — this is why C-language UDFs are so much more dangerous than UDFs in a sandboxed procedural language (PL/pgSQL).
  • This is a well-documented post-exploitation technique in tools like Metasploit's PostgreSQL modules, used once database-level admin access is already achieved — not an initial-access technique.
  • Defense: never grant `CREATEROLE`/superuser to application accounts, and monitor for unexpected `CREATE FUNCTION ... LANGUAGE C` statements in database audit logs.

Try it yourself

Read the Metasploit PostgreSQL pentesting guide and identify which specific privilege level the documented modules require before a UDF-based code execution technique becomes possible.

@ Daniel Burlacu