0x01 前言
任意文件读取漏洞,从代码审计的角度讲一讲。
0x02 什么是任意文件下载漏洞
一般的网站都提供读取文件功能,常规的思路是使用一个动态页面(php、jsp、aspx、asp等)将待下载文件作为参数一般参数名称为filename,如.php?filename/.jsp?filename等。一般实现过程是,在根据参数filename的值,获得该文件在网站上的绝对路径,读取文件。大部分情况下,与任意文件下载的危害性相同(都获取到了源代码里的包含的信息)。
0x03 任意文件下载漏洞的危害
任意文件读取漏洞,是web安全里高危的漏洞,它可以泄露源码、数据库配置文件等等,导致网站处于极度不安全状态。
0x04 实战的案例
我们来看看zzcms的任意文件读取漏洞,这是个能拿出来当教材的CMS(有兴趣的朋友可以下载看看)
index.php
参数$siteskin是通过$_request['skin']获取的,然后通过fread读取,最后echo出来。
- $file=dirname(__FILE__)."/template/".$siteskin."/index.htm";
- if (file_exists($file)==false){
- WriteErrMsg($file.'模板文件不存在');
- exit;
- }
- $fso = fopen($file,'r');
- $strout = fread($fso,filesize($file));
- 。。。。。。
- echo $strout;
复制代码
http://127.0.0.1/index.php?skin=../inc/config.php%00(00截断)
第二处:
/area/show.php
代码是一样的
- $fp="../template/".$siteskin."/area_show.htm";
- $f = fopen($fp,'r');
- $strout = fread($f,filesize($fp));
- fclose($f);
- $strout=str_replace("{#siteskin}",$siteskin,$strout) ;
- $strout=str_replace("{#sitename}",sitename,$strout) ;
- $strout=str_replace("{#siteurl}",siteurl,$strout) ;
- $strout=str_replace("{#pagetitle}",$province.sitetitle,$strout);
- $strout=str_replace("{#pagekeywords}",$province.sitekeyword,$strout);
- $strout=str_replace("{#pagedescription}",sitedescription,$strout);
- $strout=str_replace("{#province}",$province,$strout) ;
- $strout=str_replace("{#sitebottom}",sitebottom(),$strout);
- $strout=str_replace("{#sitetop}",sitetop(),$strout);
- $strout=showlabel($strout);
- echo $strout;
复制代码
第三处:
/company/company.php
- $fp="../template/".$siteskin."/company.htm";
- $f= fopen($fp,'r');
- $strout = fread($f,filesize($fp));
- fclose($f);
- 。。。。。
- echo $strout;
复制代码
第四处:
/company/index.php
- $file="../template/".$siteskin."/company_index.htm";
- if (file_exists($file)==false){
- WriteErrMsg($file.'模板文件不存在');
- exit;
- }
- $fso = fopen($file,'r');
- $strout = fread($fso,filesize($file));
- $strout=str_replace("{#siteskin}",$siteskin,$strout) ;
- $strout=str_replace("{#sitename}",sitename,$strout) ;
- $strout=str_replace("{#pagetitle}",companylisttitle,$strout);
- $strout=str_replace("{#pagekeywords}",companylistkeyword,$strout);
- $strout=str_replace("{#pagedescription}",companylistdescription,$strout);
- $strout=str_replace("{#sitebottom}",sitebottom(),$strout);
- $strout=str_replace("{#sitetop}",sitetop(),$strout);
- $strout=showlabel($strout);
- echo $strout;
复制代码
第五处:
/company/search.php
- $file="../template/".$siteskin."/company_search.htm";
- $fp = fopen($file,'r');
- $strout = fread($fp,filesize($file));
- fclose($fp);
-
- 。。。。。
- echo $strout;
复制代码
第六处:
/dl/dl.php
- $fp="../template/".$siteskin."/dl.htm";
- $f = fopen($fp,'r');
- $strout = fread($f,filesize($fp));
- fclose($f);
- 。。。。。
- echo $strout;
复制代码
第七处:
/dl/index.php
- $fp="../template/".$siteskin."/dl_index.htm";
- if (file_exists($fp)==false){
- WriteErrMsg($fp.'模板文件不存在');
- exit;
- }
- $f = fopen($fp,'r');
- $strout = fread($f,filesize($fp));
- fclose($f);
- $strout=str_replace("{#siteskin}",$siteskin,$strout) ;
- $strout=str_replace("{#sitename}",sitename,$strout) ;
- $strout=str_replace("{#pagetitle}",dllisttitle,$strout);
- $strout=str_replace("{#pagekeywords}",dllistkeyword,$strout);
- $strout=str_replace("{#pagedescription}",dllistdescription,$strout);
- $strout=str_replace("{#sitebottom}",sitebottom(),$strout);
- $strout=str_replace("{#sitetop}",sitetop(),$strout);
- $strout=showlabel($strout);
- mysql_close($conn);
- echo $strout;
复制代码
第八处:
/dl/search.php
- $fp="../template/".$siteskin."/dl_search.htm";
- $f = fopen($fp,'r');
- $strout = fread($f,filesize($fp));
- fclose($f);
- 。。。。
- echo $strout;
复制代码
第九处:
/inc/bottom.php
- $file=zzcmsroot."/template/".$siteskin."/bottom.htm";
- $fso = fopen($file,'r');
- $strout = fread($fso,filesize($file));
-
- $strout=str_replace("{#sitename}",sitename,$strout) ;
- $strout=str_replace("{#siteurl}",siteurl,$strout) ;
- $strout=str_replace("{#zzcmsver}",zzcmsver,$strout) ;
- $strout=str_replace("{#kftel}",kftel,$strout) ;
- $strout=str_replace("{#kfmobile}",kfmobile,$strout);
- $strout=str_replace("{#kfqq}",kfqq,$strout);
- $strout=str_replace("{#icp}",icp,$strout);
- $strout=str_replace("{#sitecount}",sitecount,$strout);
- return $strout;
复制代码
第十处:
/one/getpassword.php
- $file="../template/".$siteskin."/getpassword.htm";
- if (file_exists($file)==false){
- WriteErrMsg($file.'模板文件不存在');
- exit;
- }
- 。。。。。
- echo $strout;
复制代码
第十一处:
/one/help.php
- $fp="../template/".$siteskin."/help.htm";
- if (file_exists($fp)==false){
- WriteErrMsg($fp.'模板文件不存在');
- exit;
- }
- $f = fopen($fp,'r');
- $strout = fread($f,filesize($fp));
- fclose($f);
- $strout=str_replace("{#siteskin}",$siteskin,$strout) ;
- $strout=str_replace("{#sitename}",sitename,$strout) ;
- $strout=str_replace("{#siteurl}",siteurl,$strout) ;
- $strout=str_replace("{#logourl}",logourl,$strout) ;
- $strout=str_replace("{#sitebottom}",sitebottom(),$strout);
- $strout=str_replace("{#sitetop}",sitetop(),$strout);
- $strout=showlabel($strout);
- mysql_close($conn);
- echo $strout;
复制代码
第十二处:
/one/link.php
- $fp="../template/".$siteskin."/link.htm";
- if (file_exists($fp)==false){
- WriteErrMsg($fp.'模板文件不存在');
- exit;
- }
- $f = fopen($fp,'r');
- $strout = fread($f,filesize($fp));
- fclose($f);
- $strout=str_replace("{#siteskin}",$siteskin,$strout) ;
- $strout=str_replace("{#sitename}",sitename,$strout) ;
- $strout=str_replace("{#siteurl}",siteurl,$strout) ;
- $strout=str_replace("{#logourl}",logourl,$strout) ;
- $strout=str_replace("{#sitebottom}",sitebottom(),$strout);
- $strout=str_replace("{#sitetop}",sitetop(),$strout);
- $strout=showlabel($strout);
- mysql_close($conn);
- echo $strout;
复制代码
第十三处:
/one/siteinfo.php
- $fp="../template/".$siteskin."/siteinfo.htm";
- if (file_exists($fp)==false){
- WriteErrMsg($fp.'模板文件不存在');
- exit;
- }
- $f = fopen($fp,'r');
- $strout = fread($f,filesize($fp));
- fclose($f);
- $strout=str_replace("{#siteskin}",$siteskin,$strout) ;
- $strout=str_replace("{#sitename}",sitename,$strout) ;
- $strout=str_replace("{#siteurl}",siteurl,$strout) ;
- $strout=str_replace("{#title}",$title,$strout) ;
- $strout=str_replace("{#content}",$content,$strout) ;
- $strout=str_replace("{#logourl}",logourl,$strout) ;
- $strout=str_replace("{#sitebottom}",sitebottom(),$strout);
- $strout=str_replace("{#sitetop}",sitetop(),$strout);
- //$strout=showlabel($strout);
- echo $strout;
复制代码
第十四处:
/one/sitemap.php
- $file="../template/".$siteskin."/sitemap.htm";
- if (file_exists($file)==false){
- WriteErrMsg($file.'模板文件不存在');
- exit;
- }
- 。。。。。
- echo $strout;
复制代码
第十五处:
/pp/index.php
- $fp="../template/".$siteskin."/pp_index.htm";
- if (file_exists($fp)==false){
- WriteErrMsg($fp.'模板文件不存在');
- exit;
- }
- $f = fopen($fp,'r');
- $strout = fread($f,filesize($fp));
- fclose($f);
- $strout=str_replace("{#siteskin}",$siteskin,$strout) ;
- $strout=str_replace("{#sitename}",sitename,$strout) ;
- $strout=str_replace("{#pagetitle}",pplisttitle,$strout);
- $strout=str_replace("{#pagekeywords}",pplistkeyword,$strout);
- $strout=str_replace("{#pagedescription}",pplistdescription,$strout);
- $strout=str_replace("{#sitebottom}",sitebottom(),$strout);
- $strout=str_replace("{#sitetop}",sitetop(),$strout);
-
- $strout=str_replace("{#ppclass}",bigclass(2),$strout);
-
- $strout=showlabel($strout);
- echo $strout;
复制代码
第十六处:
/pp/pp.php
- $fp="../template/".$siteskin."/pp.htm";
- $f = fopen($fp,'r');
- $strout = fread($f,filesize($fp));
- fclose($f);
- 。。。。。。
- echo $strout;
复制代码
第十七处:
/pp/search.php
- $fp="../template/".$siteskin."/pp_search.htm";
- $f = fopen($fp,'r');
- $strout = fread($f,filesize($fp));
- fclose($f);
- 。。。。。
- echo $strout;
复制代码
第十八处:
/pp/show.php
- $fp="../template/".$siteskin."/ppshow.htm";
- $f = fopen($fp,'r');
- $strout = fread($f,filesize($fp));
- fclose($f);
- 。。。。。。
- echo $strout;
复制代码
0x04 挖掘任意文件读取漏洞的技巧、
一:注意观察网站的URL,如果读取一个文件的时候文件后缀为.com/1.php?lujing=/test/test.php
你可以关注一下这儿,有些网站(主要是针对系统)并不会在URL里显示给你看,这时候你需要抓包,从数据包里修改路径,达到任意文件读取。
原文链接: