For the complete documentation index, see llms.txt. This page is also available as Markdown.

Pentesting PostgreSQL

Default Port: 5432

PostgreSQL (psql) is an open-source relational database system. It's commonly found in web applications, especially those using Python/Django, Ruby on Rails, and Java/Spring Boot frameworks.


Enumeration

Nmap Scripts

nmap -sV -p 5432 --script="pgsql-*" $ip

PostgreSQL may be exposed on nonstandard ports. Scan all TCP ports and include the discovered port in later tooling:

5437/tcp open  postgresql  PostgreSQL DB 11.3 - 11.9

Afrog may flag weak PostgreSQL credentials:

postgresql-weak-login HIGH TARGET:5437 [username="postgres",password="postgres",db="postgres"]
nc -nv $ip 5432

Connecting to PostgreSQL

Using psql Client

Common Default Credentials

Username
Password

postgres

postgres

postgres

(empty)

admin

admin


Essential psql Commands

Command
Description

\l or \list

List all databases

\c <database>

Connect to a database

\dt

List tables in current database

\dt+

List tables with size and description

\d <table>

Describe table structure (columns, types)

\d+ <table>

Describe table with extra info

\du

List users/roles

\dn

List schemas

\df

List functions

\x

Toggle expanded display (vertical output)

\q

Quit psql

Example Workflow


Extracting Data

Dumping Users and Passwords

Searching for Sensitive Data


File Operations (Requires Superuser)

Reading Files

Writing Files


Command Execution

Using COPY FROM PROGRAM (PostgreSQL 9.3+)

If curl, wget, or other transfer tools are missing inside a database container, COPY FROM PROGRAM still works for command output. Use built-ins first (id, ls, cat, find, base64) and pivot or port-forward to the database from a container that can reach it.

The public postgresqlversions_9.3-11.7-RCE.py helper automates authenticated command execution against vulnerable PostgreSQL 9.3 through 11.7 instances:

Successful output:

When shell metacharacters are painful through the command wrapper, base64-encode the reverse shell and decode it on the target:

Successful shell context:

PostgreSQL labels COPY FROM PROGRAM as an intended administrative feature rather than a vulnerability, even though it is commonly tracked as CVE-2019-9193.

Using Extensions


Cracking PostgreSQL Hashes

PostgreSQL password hashes are typically bcrypt ($2a$, $2b$, $2y$).


Privilege Escalation

Check Current User Privileges

PostgreSQL to System Shell

If PostgreSQL is running as root or has SUID, check for privilege escalation:


PostgreSQL NSS Privilege Escalation (Name Service Switch)

PostgreSQL can be used with NSS (Name Service Switch) to store Linux user credentials. If you can write to the passwd_table, you can add SSH users or escalate privileges.

Discovery

NSS Tables Structure

Connect and Enumerate

Add User with Root Privileges

SSH as New Root User

Alternative: Add User to Sudo Group

If direct UID 0 doesn't work, try adding user to sudoers group (GID 27):

Note: Different NSS PostgreSQL users may have different table permissions. Test multiple credentials from nss-pgsql.conf and nss-pgsql-root.conf.


Useful psql Settings


Useful Resources

  • https://book.hacktricks.wiki/en/network-services-pentesting/pentesting-postgresql.html

  • https://www.postgresql.org/docs/current/app-psql.html

Last updated