Possible Duplicate:
What does mysql_real_escape_string() do that addslashes() doesn't?
I have been reviewing articles on how/why PHP's addslashes function is vulnerable to sql injection. Everything I have read says there are problems with specific mysql encoding types (default-character-set=GBK), or there are problems if magic_quotes are enabled. However, I have been unable break out of the addslashes() function in this scenario and do something malicious - such as login as an administrator.
$user = addslashes($_POST['user']);
$pass = sha1($_POST['pass']);
$sql = "SELECT * FROM admins WHERE user = '".$user."' AND `pass` = '".$pass."'";
$nums = mysql_num_rows(mysql_query($sql));
if($nums==1){
$_SESSION['admin_user'] = $user;
$_SESSION['admin_pass'] = $pass;
This is a (minor) security audit for a client and I will recommend that they utilize PDO, but I need to display their current vulnerability.
References:
I will recommend that they utilize PDO
that's a fair recommendation, but in this specific piece of code, it will suffice to use the right sanitation function instead of addslashes:mysql_real_escape_string()
. – Cordagemysql_query()
with no sanitation – CordageAND pass
part, and that is sanitized because of thesha1()
call. But usingaddslashes()
is still wrong and dangerous practice and they should change it. – Cordageaddslashes()
in queries, go check whether there are any uses ofint
values without surrounding quotes:SELECT * FROM customers WHERE id = $value
. Those are easy to mess with: Just add999 OR id = {insert some other customer's ID here}
-addslashes()
will add no protection here – Cordage