[SQLi] So you thought your login system was secure? 02-22-2014, 07:29 PM
#1
Often developers things that as long as they don't check for a valid password in the query, but does it in the code instead, it cannot be bypassed with sql injection. This together with poor hashing like md5 is super simple to bypass if you can injection union.. In this post I will explain why..
First let's look at how many processes the username. We do some simple sanitation by stripping all spaces. This should be secure enough right? Meh, not really
This is easily bypassed using alternative white spaces or comments
Next we look at the password check.
So this is pretty secure right? Because you cannot inject and the code is actually checking this content for you, so the sql injection alone is not validating.. Or is it? Let's look at what happens
1) You enter your username and password in the form
2) The query locates the record containing the decired username
3) The query returns the content of the columns in the record
4) The code checks to see if the password that was stored matches the one that the the user entered
So this should be rock solid.. But you should think again.. Let's see what we can do to bypass this
(The example is using 3 columns. 1) id 2) username 3) password)
Username: ' UNION SELECT null,'admin',MD5('bypassed')-- -
Password: bypassed
So what happens here?
This result will return 1 row only, and the reason for this is because there's no user without a username. So instead it will be using the crafted credentials that we added in the union select payload. And we then added a unhashed version of the password "bypassed" in the password field.
This means that the query will return the username "admin" and the password will be a hashed version of the word "bypassed". Now when the code validates the password it hashes it with md5 and validates as an existing account..
This was a quick run down on how to bypass these types of scenarios
First let's look at how many processes the username. We do some simple sanitation by stripping all spaces. This should be secure enough right? Meh, not really
Code:
$username = str_replace(' ', '', $_POST['username']);
$query = "SELECT * FROM users WHERE username = '{$username}'";
/* Execute the query here and fetch data to $row */This is easily bypassed using alternative white spaces or comments
Next we look at the password check.
Code:
if ($row['password'] !== md5($_POST['password'])) {
// Login failed! Report this to the user
} else {
// Login was successful! Proceed to user account
}So this is pretty secure right? Because you cannot inject and the code is actually checking this content for you, so the sql injection alone is not validating.. Or is it? Let's look at what happens
1) You enter your username and password in the form
2) The query locates the record containing the decired username
3) The query returns the content of the columns in the record
4) The code checks to see if the password that was stored matches the one that the the user entered
So this should be rock solid.. But you should think again.. Let's see what we can do to bypass this
(The example is using 3 columns. 1) id 2) username 3) password)
Username: ' UNION SELECT null,'admin',MD5('bypassed')-- -
Password: bypassed
So what happens here?
This result will return 1 row only, and the reason for this is because there's no user without a username. So instead it will be using the crafted credentials that we added in the union select payload. And we then added a unhashed version of the password "bypassed" in the password field.
This means that the query will return the username "admin" and the password will be a hashed version of the word "bypassed". Now when the code validates the password it hashes it with md5 and validates as an existing account..
This was a quick run down on how to bypass these types of scenarios




![[+]](https://sinister.li/images/modern/collapse_collapsed.png)
![[Image: wvBFmA5.png]](http://i.imgur.com/wvBFmA5.png)
I guess I could get some more sleep, but ain't got no time for that 
![[Image: T4OUWZ1.png]](http://i.imgur.com/T4OUWZ1.png)