文件包含漏洞原理
服务器执行PHP文件时,可以通过文件包含函数加载另一个文件中的PHP代码,并且当PHP来执行。在通过PHP的函数引入文件时,由于传入的文件名没有经过过滤,导致执行了非预期的代码。
<?php
$a = $_GET['a'];
include($a);
?>
1.本地文件包含漏洞。网站服务器本身存在恶意文件,然后利用本地文件包含使用
2.远程文件包含漏洞。调用其他网站的恶意文件进行打开
需要php.ini
allow_url_fopen = On(是否允许打开远程文件)
allow_url_include = On(是否允许include/require远程文件)
PHP中文件包含函数 :
require():找不到包含文件只警告
include():找不到包含文件则停止
require_once():只运行一次
include_once()
https://www.freebuf.com/articles/web/182280.html
DVWA - File Inclusion
low
<?php
// The page we wish to display
$file = $_GET[ 'page' ];
?>
对输入的page没有过滤
能直接进本地的文档
可以连接远程的phpinfo
应该也可以连菜刀,但是不知道怎么在蚁剑里改DVWA难度(迷之错误。
medium
<?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://", "https://"
防止远程包含漏洞的产生
过滤"../", "..\"
防止进行目录切换的包含
可以使用双写绕过
high
<?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;
}
?>
if( !fnmatch( "file*", $file ) && $file != "include.php" )
限制file必须为 file* 或 include.php
可以利用file协议进行包含本地的文件
用file:///+文件的地址
impossible
<?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;
}
?>
if( $file != "include.php" && $file != "file1.php" && $file != "file2.php" && $file != "file3.php" )
确定file必须为include.php、file1.php、file2.php、file3.php之一(白名单过滤)。