Bash pattern match bypass (unquoted ==)
Full command arsenal in the cheatsheetInside [[ ]], the operand on the right of == is not treated as a literal string but as a pattern, unless it is quoted. It is a bash quirk that does not exist in [ ] nor in most languages, and it turns a comparison into a match:
if [[ $DB_PASS == $USER_PASS ]]; thenThat line does not compare the password with what is typed: it matches the password against what is typed, treating it as a glob. And * matches anything, so the correct password is a single asterisk (wildcard abuse):
sudo /opt/scripts/mysql-backup.sh
# Enter MySQL password for root: *
# Password confirmed!The fix is one quote: [[ "$DB_PASS" == "$USER_PASS" ]] compares strings.
The bypass is not always the prize. Passing the if only gives you whatever the script does next, and that may be nothing useful: leaving dumps with permissions we cannot touch, for instance. What usually pays is what the script does with the secret once convinced we know it. A script like this almost always ends up calling another program, handing it the real password on the command line:
/usr/bin/mysql -u root -p"$DB_PASS" -e "SHOW DATABASES;"And a command line is public: it sits in /proc/<pid>/cmdline for as long as the process lives. With the if bypassed, the privileged script can be run as many times as needed while we watch it with pspy or by reading /proc (/proc/*/cmdline enumeration), and what shows up is the cleartext password. The full pattern is two chained flaws: the unquoted == grants the attempts and the -p on the command line hands over the secret.
The same glob also works as an oracle: a*, b*, c* test the first character, and the confirmation message says which one hits. It is the slow route, but it works when the script leaks nothing else.
Requirements#
A script runnable with privileges (a sudo rule, a cron job, a SUID binary) that compares our input against a secret using [[ ... == $var ]] with the right-hand operand unquoted. You need to be able to read the script, or to infer the comparison from its behaviour.
How to detect it. Read the script: shellcheck flags the unquoted comparison as SC2053 (Quote the right-hand side of == in [[ ]] to prevent glob matching.), so in code review it is an automatic finding. At runtime, /var/log/auth.log shows the same sudo ... COMMAND= repeated many times in a short window, which is the oracle's footprint, and auditd records the cleartext password in the execve of the mysql -p... if the script passes it that way.
Defence. Quote the right-hand operand, always: [[ "$a" == "$b" ]]. To compare secrets, not even that form: compare hashes, and in constant time. And do not pass passwords on the command line: mysql reads ~/.my.cnf or takes the password on stdin, which keeps it out of /proc and out of everybody's sight.
Machines where it's used
- CodifyHackTheBox