Home > php教程 > php手册 > body text

PHP魔术引号所带来的安全问题分析,魔术引号

WBOY
Release: 2016-06-13 09:29:21
Original
789 people have browsed it

PHP魔术引号所带来的安全问题分析,魔术引号

PHP通过提取魔术引号产生的“\”字符会带来一定的安全问题,例如下面这段代码片段:

// foo.php?xigr='ryat
function daddslashes($string, $force = 0) {
!defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
if(!MAGIC_QUOTES_GPC || $force) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = daddslashes($val, $force);
}
} else {
$string = addslashes($string);
}
}
return $string;
}
...
foreach(array('_COOKIE', '_POST', '_GET') as $_request) {
foreach($$_request as $_key => $_value) {
$_key{0} != '_' && $$_key = daddslashes($_value);
}
}
echo $xigr['hi'];
// echo \
Copy after login

上面的代码原本期望得到一个经过daddslashes()安全处理后的数组变量$xigr['hi'],但是没有对变量$xigr做严格的类型规定,当我们提交一个字符串变量$xigr='ryat,经过上面的处理变为\'ryat,到最后$xigr['hi']就会输出\,如果这个变量引入到SQL语句,那么就会引起严重的安全问题了,对此再来看下面的代码片段:

...
if($xigr) {
foreach($xigr as $k => $v) {
$uids[] = $v['uid'];
}
$query = $db->query("SELECT uid FROM users WHERE uid IN ('".implode("','", $uids)."')");

Copy after login

利用上面提到的思路,通过提交foo.php?xigr[]='&xigr[][uid]=evilcode这样的构造形式可以很容易的突破GPC或类似的安全处理,形成SQL注射漏洞!对此应给与足够的重视!

[php学习]教1个魔术引号修正函数

多谢,基本理解了,这个函数的作用应该是:如果魔术引号功能打开,就把它添加的反斜线去掉,再根据情况自己用addslashes()或mysql_real_escape_string()来处理。
 

thinkphp 对于php魔术引号 我只要上传 带连接的 或者是 图片 之类的 就给我自动加一个 “ / ” 解决

我想确定一下,你传过来的值加的是"/"吗,如果是"/",你可以尝试在Action中把"/"替换成空。
以前我传过来的值加的是"\",所以我当初是用的stripslashes($_POST['ck'])来搞定的。
 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!