今天我们的服务器上出现了一个奇怪的问题。我们在mysql查询中得到了来自%的DDOS和_符号,它们通过GET请求成功传递。例如
domain.com/search/%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25v%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25a
看起来像是结块,难道不过滤它们吗?在mysql官方指南中,他们对这个问题写了很多文章。这就是他们解决这个问题的方法:
addcslashes(mysql_real_escape_string(“%something_”), “%_”);
在cakepp框架中,函数escape()
在模型中随处可见。看看它包含什么:
/**
* Returns a quoted and escaped string of $data for use in an SQL statement.
*
* @param string $data String to be prepared for use in an SQL statement
* @param string $column The column into which this data will be inserted
* @param boolean $safe Whether or not numeric data should be handled automagically if no column data is provided
* @return string Quoted and escaped data
*/
function value($data, $column = null, $safe = false) {
$parent = parent::value($data, $column, $safe);
if ($parent != null) {
return $parent;
}
if ($data === null || (is_array($data) && empty($data))) {
return 'NULL';
}
if ($data === '' && $column !== 'integer' && $column !== 'float' && $column !== 'boolean') {
return "''";
}
if (empty($column)) {
$column = $this->introspectType($data);
}
switch ($column) {
case 'boolean':
return $this->boolean((bool)$data);
break;
case 'integer' :
case 'float' :
case null :
if ($data === '') {
return 'NULL';
}
if (is_float($data)) {
return str_replace(',', '.', strval($data));
}
if ((is_int($data) || is_float($data) || $data === '0') || (
is_numeric($data) && strpos($data, ',') === false &&
$data[0] != '0' && strpos($data, 'e') === false)) {
return $data;
}
default:
$data = "'" . mysqli_real_escape_string($this->connection, $data) . "'";
break;
}
return $data;
}
只是对一些变量类型和类似的东西的基本保护。。转义mysql特殊字符怎么样??大约一年前,我读到如何在mysqlquery中借助百分号来转义引号=)当时这是盲目注入的大肆宣传,这种技巧几乎在任何地方都有效,因为每个人都使用mysqli_real_escape_string。
我必须在这里陈述一个问题:如何安全地逃离cakepp中的变量?
更新:IRC中的一些人表示REQUEST字符串必须转义,而不是自己查询。他们可能是对的,那么我如何在不使用自定义函数的情况下转义GET请求字符串中的%和_字符。。有消毒方法吗?
这并不意味着Cake容易受到SQL注入的影响,因为它在底层使用了准备好的语句,这确实意味着你在CakePHP中使用了LIKE
搜索查询,但它允许使用通配符。
我不认为这是理想的行为,因为我在开发时也发现了这一点,我现在只看到上面这行使用LIKE
的发现。
$term = str_replace('%', ' ', $term);
你不需要把逃跑掌握在自己手中,框架会为你处理这件事。
您显示的代码示例是安全的。此行:
$data = "'" . mysqli_real_escape_string($this->connection, $data) . "'";
转义特殊字符。该方法检查变量的类型——如果变量不是字符串,则不需要对其进行转义。
很高兴看到mysql_real_escape_string()
所谓"漏洞"的来源。