在 CodeIgniter PHP 框架,有一个函数可以在每个请求上自动运行,除其他外,filters the GET/POST/COOKIE array keys ,并在遇到它认为不安全的字符时终止应用程序。
To prevent malicious users from trying to exploit keys we make sure that keys are only named with alpha-numeric text and a few other items.
类似:
// foreach GET/POST/COOKIE keys as $str...
if ( ! preg_match("/^[a-z0-9:_\/-]+$/i", $str))
{
exit('Disallowed Key Characters.');
}
例如,如果您不小心发布了 <input name="TE$T">
之类的内容,就会触发此事件。或者有一个像 ?name|first=1
这样的查询字符串.
我认为这是强制执行常识 key 名称或在开发应用程序时发现错误的好方法,但我不明白:恶意用户如何可能“利用 $_POST
中的 key ”例如数据?特别是因为(我会假设)输入 values 一样可被利用,这实际上阻止了什么?
最佳答案
你经常在菜鸟代码中看到这些垃圾:
$_SESSION = $_POST;
一个鲜为人知的 secret 是 $_SESSION
is "special" and can't handle the pipe character , |
,作为顶级数组键。 php 在关闭/session_write_close 期间无法保存 session 变量,而是删除整个数组。
session_start();
if (!isset($_SESSION['cnt'])) {
$_SESSION['cnt'] = 0;
}
$_SESSION['cnt']++;
/* prior to php 5.4, it will never increment, because it never successfuly saves
unless you comment line below
*/
$_SESSION['a|b'] = 1;
print_r($_SESSION);
我并不是说这就是 CodeIgniter 删除键的原因,但这是许多“输入键可利用的方式”之一。
也许更可能的原因是因为人们肯定会做这样的事情:
if ($formPostDidntValidate) {
echo "Please fix the form submission errors and try again\n";
foreach ($_POST as $key => $value) {
echo "<p>$key<br>
<input name='$key' value='$value'></p>";
}
}
输出请求变量而不进行适当的特定于上下文的转义,例如转义 html 上下文、html 属性上下文,甚至 sql 上下文:
$sql = "select * from myTable where 1=1";
foreach ($_POST as $key => $value) {
$sql .= " and $key = '$value'";
}
我见过很多人在构建 sql 和/或 html 时转义值,而不是键。
如果你不逃避一切,你很容易被黑。清理值不如转义,但总比没有好得多,考虑到有多少开发人员还不够成熟,无法理解何时以及如何转义,我可以看到添加自动请求变量清理的吸引力。
关于php - 恶意用户如何利用输入键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10559482/