1. 程式人生 > >DVWA 黑客攻防實戰(四)檔案包含 File Inclusion

DVWA 黑客攻防實戰(四)檔案包含 File Inclusion

檔案包含(file Inclusion)是一種很常見的攻擊方式,主要是通過修改請求中變數從而訪問了使用者不應該訪問的檔案。還可以通過這個漏洞載入不屬於本網站的檔案,比如一個 webshell 從而實現網站控制。下面一起來看看 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' ];
?>

覺得主要問題還是對檔案路徑沒有驗證吧 看了中級的程式碼後,才發現還可以這樣用,

。。。看了中級程式碼才知道 低階的還可以這樣注入。

中級

<?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.31.166:5678/vulnerabilities/fi/?page=HTTP://www.baidu.com 比如輸入的是全路徑 http://192.168.31.166: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.31.166:5678/vulnerabilities/fi/?page=/etc/apache2/apache2.conf 之類的也打不開了。 此時 Hacker 輸入了 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;
}
?>

感覺有點滑稽。哈哈哈哈 這裡的檔案列表應該從讀檔案或者讀資料庫的方式獲取比較好吧。這樣不夠優雅。