LIKE, omg!

If you read this blog, you might have seen my earlier post regarding my configurable SQL injection testbed, SQLol. It comes with challenges which I've seen some buzz about. In the latest version, there is a challenge involving use of the "LIKE" keyword and I'm just too jazzed about the attack vector that I had to share.

The LIKE keyword operates similarly to the equality operator "=". However, it also supports wildcards. The "%" wildcard matches zero or more characters, and the "_" keyword matches only one character. LIKE is commonly used in search pages like so:

select * from used_cars where make LIKE '%Toyota%'

This will return any rows from "used_cars" where "Toyota" appears anywhere in the "make" column. If we have control over what goes into the operand for the "LIKE" clause we can enumerate all the "make" values from the database through brute force if they are not shown to us directly. Imagine that this is used (erroneously) for a login prompt, using a query like this:

select * from users where username LIKE 'input_username' and password LIKE 'input_password'

If a user never uses wildcards, this operates as a normal login prompt. However, if we were to provide a username and password of "%", this will return all users from the database. Authentication bypass! Even worse, we can use this to enumerate all the usernames and passwords from the database, even if single quotes are filtered out, preventing us from breaking out of the string value and performing traditional SQL injection attacks.

First, we would enumerate all usernames. We start with character set reduction, by providing a password of "%" and a username of "%A%". This will tell us if there are any usernames which contain an upper-case A in the database. We can repeat this process with all other possible characters and know the character set used in all the usernames in the database. Next, we'll try each of these characters as the first character in the username and wildcard the rest.

Let's say we have a character set of "abfor".

We'll try:






Let's say that on the requests using "f%" and "b%" we are logged in. This indicates that all usernames begin with either "f" or "b". We'll continue onward like this, brute forcing each character to determine the possible character set at each position until no requests are successful:






Eventually, we learn all the usernames in this way. Next, we would enumerate all passwords and then attempt to log into each account with each password. Note that the process of enumerating all passwords would require us to make multiple login attempts with the username "%". Beware! If account lockout is enabled, we might also see LIKE in the query to set lockout status:

update users set locked_out=1 where username LIKE '%'

Imagine that phone call.

What's most interesting about this is that using parameterized queries, stored procedures, whitelisting, WAF etc. is not likely enough to prevent this! It's a plausible scenario that usernames could contain underscores, so even if the percent sign was not allowed, one could still perform the same attack by first enumerating lengths of items in the database using varying numbers of underscores and then performing a similar attack to the one described above. It just goes to show that you still need someone testing the application and you still need to be security conscious when writing code.

Trustwave reserves the right to review all comments in the discussion below. Please note that for security and other reasons, we may not approve comments containing links.