更具安全性的addslashes_deep函数

更具安全性的addslashes_deep函数-by Sodoit

我们知道,ecshop的很多漏洞都是因为对变量的过滤不严格而导致mysql注入的,虽然在init.php文件里有对$_GET,$_GET,$_COOKIE,$_REQUEST用addslashes_deep进行处理,但是,分析addslashes_deep函数后我们就可以发现,该函数只处理数组的值,对数组的key是完全不作任何过滤的,这直接导致了漏洞。例如:
http://bbs.honkwin.com/read-htm-tid-6174.html
http://sebug.net/vulndb/20188/

为此,我们应该重写addslashes_deep函数,下面是我的代码:


  1. function addslashes_deep($value,$htmlspecialchars=false)
  2. {
  3.     if (empty($value))
  4.     {
  5.         return $value;
  6.     }
  7.     else
  8.     {
  9.         if(is_array($value))
  10.         {
  11.          foreach($value as $key => $v)
  12.          {
  13.           unset($value[$key]);
  14.          
  15.           if($htmlspecialchars==true)
  16.           {
  17.            $key=addslashes(htmlspecialchars($key));
  18.           }
  19.           else{
  20.            $key=addslashes($key);
  21.           }
  22.          
  23.           if(is_array($v))
  24.           {
  25.            $value[$key]=addslashes_deep($v);
  26.           }
  27.           else{
  28.            if($htmlspecialchars==true)
  29.            {
  30.             $value[$key]=addslashes(htmlspecialchars($v));
  31.            }
  32.            else{
  33.             $value[$key]=addslashes($v);
  34.            }
  35.           }
  36.          }
  37.         }
  38.         else{
  39.          if($htmlspecialchars==true)
  40.          {
  41.           $value=addslashes(htmlspecialchars($value));
  42.          }
  43.          else{
  44.           $value=addslashes($value);
  45.          }
  46.         }
  47.         
  48.         return $value;
  49.     }
  50. }
复制代码


该函数对数组的值和key都进行了addslashes处理,并且根据需要,还可以进行htmlspecialchars过滤。在init.php文件里,应该这样调用:
$_GET  = addslashes_deep($_GET,true);
$_POST = addslashes_deep($_POST,true);
$_COOKIE   = addslashes_deep($_COOKIE,true);
$_REQUEST  = addslashes_deep($_REQUEST,true);

posted on 2014-09-04 08:59  山冈龙  阅读(1332)  评论(0)    收藏  举报

导航