题目平台:buuctf
题目名称:[安洵杯 2019]easy_serialize_php
本题涉及知识点
-
extract() 变量覆盖 -
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;
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();');
}else if($function == 'show_image'){
$userinfo = unserialize($serialize_info);
echo file_get_contents(base64_decode($userinfo['img']));
}
?>
一、审计代码
先分析与变量$function 有关的代码
$function = @$_GET['f'];
if(!$function){
echo '<a href="index.php?f=highlight_file">source_code</a>';
}
f($function == 'highlight_file'){
highlight_file('index.php');
}else if($function == 'phpinfo'){
eval('phpinfo();');
}else if($function == 'show_image'){
$userinfo = unserialize($serialize_info);
echo file_get_contents(base64_decode($userinfo['img']));
}
(1) 令/index.php?f=phpinfo,查看phpinfo中的隐藏信息
(2) 发现d0g3_f1ag.php文件
(3)最终肯定是需要/index.php?f=show_image利用反序列化漏洞来获取flag
分析有关$_SESSION 部分的代码
if($_SESSION){
unset($_SESSION);
}
$_SESSION["user"] = 'guest';
$_SESSION['function'] = $function;
extract($_POST);
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));
function filter($img){
$filter_arr = array('php','flag','php5','php4','fl1g');
$filter = '/'.implode('|',$filter_arr).'/i';
return preg_replace($filter,'',$img);
}
$userinfo = unserialize($serialize_info);
echo file_get_contents(base64_decode($userinfo['img']))
- 首先,粗略的过一遍代码,file_get_contents函数读取文件的内容肯定是
d0g3_f1ag.php ,因此base64_decode($userinfo['img']) 的结果也得是d0g3_f1ag.php 。不会有sha1() 这个加密过程,也就是说$_GET['img_path'] 变量为空,但这样,$_SESSION['img'] 似乎是固定值base64_encode('guest_img.png');
二、漏洞利用
- 发现flag文件在
/d0g3_fllllllag 中
/d0g3_fllllllag base64后为L2QwZzNfZmxsbGxsbGFn - 得到flag{61011a5f-08cc-4884-9666-6b639382f93b}
三、反思总结
题目有两个坑的点
|