【DVWA(九)】文件包含漏洞


文件包含漏洞

前言:

由于开发人员编写源码,将可重复使用的代码插入到单个的文件中,并在需要的时候将它们包含在特殊的功能代码文件中,然后包含文件中的代码会被解释执行。由于并没有针对代码中存在文件包含的函数入口做过滤,导致客户端可以提交恶意构造语句提交,并交由服务器端解释执行。文件包含攻击中WEB服务器源码里可能存在inlcude()此类文件包含操作函数,通过客户端构造提交文件路径,是该漏洞攻击成功的最主要原因。

如果攻击者为动态包含指令指定一个有效文件,那么该文件的内容会被传递给 PHP 解析器,可直接在远程服务器上执行任意PHP文件。
如果攻击者能够指定一条路径来指向被自己控制的远程站点,那么动态 include 指令就会执行由攻击者提供的任意恶意代码,也就是所谓的“远程文件包含”。

在开始实验之前,出现了小问题在最上面出现红色的 The PHP function allow_url_include is not enabled. 提示,我是用PHP study搭建的学习环境,在软件界面选择:其他选项菜单->打开配置文件->php-ini


low:

1.观察:
分别点击三个文件,可以通过观察看到,url分别是:
http://127.0.0.1/dvwa/vulnerabilities/fi/?page=file1.php
http://127.0.0.1/dvwa/vulnerabilities/fi/?page=file2.php
http://127.0.0.1/dvwa/vulnerabilities/fi/?page=file3.php
只有page后面有变化
2.测试:
http://127.0.0.1/dvwa/vulnerabilities/fi/?page=http://127.0.0.1/index.php
可以发现打开了www文件夹下的index.php文件
也就是说通过page=“”的形式,可以查看你想要受害者查看的文件,配合文件上传漏洞,就可以轻易的形成破坏。

medium:

1.测试:
继续用http://127.0.0.1/dvwa/vulnerabilities/fi/?page=http://127.0.0.1/index.php进行测试,发现报错
failed to open stream: No such file or directory
说明不能这样直接在page后打开
2.尝试其他方法:
但是第二行大的报错给了信息了:Failed opening '127.0.0.1/index.php' for inclusion
所以先尝试page=后大小写混合http://127.0.0.1/dvwa/vulnerabilities/fi/?page=Http://127.0.0.1/index.php
完美,可以的!

high:

1.尝试:
http://127.0.0.1/dvwa/vulnerabilities/fi/?page=http://127.0.0.1/index.php
http://127.0.0.1/dvwa/vulnerabilities/fi/?page=Http://127.0.0.1/index.php
http://127.0.0.1/dvwa/vulnerabilities/fi/?page=../../index.php
均失败,而且错误一致!

2.查看:
怀疑是对?page=后内容进行了判断,查看源代码
其中有  if( !fnmatch( "file*", $file ) && $file != "include.php" )  ,果然,进行了"file"匹配,所以把page后的内容改为file开头的
http://127.0.0.1/dvwa/vulnerabilities/fi/?page=file://C:/phpStudy/PHPTutorial/WWW/index.php
执行成功!其中C:/phpStudy/PHPTutorial/WWW/是服务器中的绝对路径

impossible:

impossible模式目前看来无懈可击;

配合所有等级的源代码分析:
low:

<?php
// The page we wish to display
$file = $_GET[ 'page' ];
?> 

没有任何防护,可以任意破坏,现实中肯定没有这样的。
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;
}
?> 

白名单模式,但是这里白名单算是留了后路,因为只是同意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;
}
?>

绝对白名单,只有四个文件可以,这样一来就很安全了,除非把服务器里这个四个文件给改了。


后记:

漏洞攻击的前提是:基本摸清了想要攻击的网站结构,敏感文件的路径可以得到(绝对路径),对攻击页面可控

原文地址:https://www.cnblogs.com/wayne-tao/p/11116758.html