Analysis of Security Problems Caused by PHP Magic Quotes

  • 2021-07-09 07:28:03
  • OfStack

The "\" character produced by PHP by extracting magic quotation marks will cause a certain security problem, such as the following code snippet:


// 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 \

The above code would have expected an array variable $xigr ['hi'] secured by daddslashes (), However, there is no strict type specification for the variable $xigr. When we submit a string variable $xigr= 'ryat, it becomes\' ryat after the above processing, and finally $xigr ['hi'] will output\. If this variable is introduced into the SQL statement, it will cause serious security problems. Let's look at the following code snippet:


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

Using the ideas mentioned above, by submitting foo. php? xigr [] = ' & xigr [] [uid] = evilcode can easily break through GPC or similar security processing and form SQL injection vulnerability! We should pay enough attention to this!


Related articles: