“mysqli_real_escape_string”足以避免SQL注入或其他SQL攻击吗?
问题描述:
这是我的代码:“mysqli_real_escape_string”足以避免SQL注入或其他SQL攻击吗?
$email= mysqli_real_escape_string($db_con,$_POST['email']);
$psw= mysqli_real_escape_string($db_con,$_POST['psw']);
$query = "INSERT INTO `users` (`email`,`psw`) VALUES ('".$email."','".$psw."')";
有人能告诉我,如果它是安全的,或者如果它很容易受到SQL注入攻击或其他SQL攻击?
答
有人能告诉我它是安全的还是容易受到SQL注入攻击或其他SQL攻击?
正如uri2x所述,请参阅SQL injection that gets around mysql_real_escape_string()
。
The best way to prevent SQL injection is to use prepared statements.它们将数据(您的参数)与指令(SQL查询字符串)分开,并且不会留下任何数据空间来污染查询结构。编制的报表解决了fundamental problems of application security之一。
对于不能使用预处理语句的情况(例如LIMIT
),对每个特定用途使用非常严格的白名单是保证安全性的唯一方法。
// This is a string literal whitelist
switch ($sortby) {
case 'column_b':
case 'col_c':
// If it literally matches here, it's safe to use
break;
default:
$sortby = 'rowid';
}
// Only numeric characters will pass through this part of the code thanks to type casting
$start = (int) $start;
$howmany = (int) $howmany;
if ($start < 0) {
$start = 0;
}
if ($howmany < 1) {
$howmany = 1;
}
// The actual query execution
$stmt = $db->prepare(
"SELECT * FROM table WHERE col = ? ORDER BY {$sortby} ASC LIMIT {$start}, {$howmany}"
);
$stmt->execute(['value']);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
我认为上述代码对SQL注入是免疫的,即使在不明显的边缘情况下也是如此。如果你使用的是MySQL,确保你关闭了模拟准备。
$db->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
的
可能重复的[SQL注入该得到周围的MySQL \ _REAL \ _escape \ _string()](http://*.com/questions/5741187/sql-injection-that-gets-around-mysql-实时逃生字符串) – uri2x