I've a website that hacked today. Server logs returned something like this as hacker's tries:
www.site.com/notifications.php?PID=7&id=999999.9%20union%20all%20select%20%28select%20distinct%20concat%280x7e%2C0x27%2Cunhex%28Hex%28cast%28schema_name%20as%20char%29%29%29%2C0x27%2C0x7e%29%20from%20%60information_schema%60.schemata%20limit%201%2C1%29%2C0x31303235343830303536%2C0x31303235343830303536%2C0x31303235343830303536--
But I've used mysql_real_escape_string() in my code:
if (isset($_GET['id']) && $_GET['id'] != '') {
$id = mysql_real_escape_string($_GET['id']);
} else {
$id = '';
}
if ($id == '') {
$stmt = "SELECT * FROM tbln13 ORDER BY id DESC";
} else {
$stmt = "SELECT * FROM tbln13 WHERE id = $id";
}
$NewsResult = mysql_query($stmt) or die (mysql_error());
Why my website could not prevent this attack?
WHERE id = $id";.... not quoted, so you're not treating it as a string; yet you're still escaping it as though it was a stringmysql_queryinterface. Throw that code in the garbage before it burns you again and write it properly using PDO and placeholders.mysql_*functions? THAT, is what I don't get. It was only a matter of time till this happened. This, being one of those questions --- Now, you really need to read this post carefully => stackoverflow.com/questions/60174/… and this one => owasp.org/index.php/Top_10_2013-Top_10 and STOP usingmysql_*functions, once and for all.