background image

PHP 程序漏洞产生的原因和防范方法   

   

   
文章录入:7747.Net    责任编辑:7747.Net  140   

  【字体:小  大】 

   

   
滥用 include  

 
    1.漏洞原因:   

 
    Include 是编写 PHP 网站中最常用的函数,并且支持相对路径。有很多 PHP 脚本直接把
某输入变量作为 Include 的参数,造成任意引用脚 

 
本、绝对路径泄露等漏洞。看以下代码:   

 

...  

$includepage=$_GET["includepage"];  

include($includepage);  

...   

 
    很明显,我们只需要提交不同的 Includepage 变量就可以获得想要的页面。如果提交一
个不存在的页面,就可以使 PHP 脚本发生错误而 

 
泄露实际绝对路径(这个问题的解决办法在下面的文章有说明)。   

 
    2.漏洞解决:   

 
    这个漏洞的解决很简单,就是先判断页面是否存在再进行 Include。或者更严格地,使
用数组对可 Include 的文件作出规定。看以下代 

 
码:   

 

$pagelist=array("test1.php","test2.php","test3.php"); //这里规定可进行 include 的文件   

if(isset($_GET["includepage"])) //判断是否有$includepage  

{  
  $includepage=$_GET["includepage"];  
  foreach($pagelist as $prepage)   
  {  
    if($includepage==$prepage) //检查文件是否在允许列表中   
    {  
      include($prepage);  
      $checkfind=true;  
      break;