I'm carrying out a code review of a site and have found a possible SQL injection vulnerability. I should point out that this is for a CTF exercise so this might be a bit of a noob question. The code below queries the database for the username and password and if it finds a match on both it allows you to login.
$username = mysql_real_escape_string((string) $_POST["username"]);
$password = md5($_POST["password"]);
$query = @mysql_query("SELECT * FROM user WHERE username='$username' AND pwd='$password'");
if (@mysql_num_rows($query) !== 1) {
$html = "Your username or password is wrong<br>".ShowLoginForm();
return;
} else {
$html = "Logged in. <a href='index.php'><font color=\"#9CCFEC\">continue</font></a>";
}
I have found that it is possible to log in as any user if the following code is injected directly to the database:
SELECT * FROM user WHERE username='admin' OR 1=1 '' AND pwd='anyrandomtext'
The challenge here is breaking out of the mysql_real_escape_string() statement and to do so I would like to see exactly what is being passed to the server once the input has been modified by this function. Is there a way to do this? The site is running on Apache.