当前位置:网站首页>[security attack and Defense] how much do you know about serialization and deserialization?
[security attack and Defense] how much do you know about serialization and deserialization?
2022-07-04 04:30:00 【InfoQ】
1. Serialization and deserialization
serialize() object Array <?php
highlight_file(__FILE__);
$sites=array('I', 'Like', 'PHP');
echo'<br/>';
var_dump(serialize($sites)); // Serialize this object
echo'<br/>';
classman{
public$name="xiaocui";
public$sex="man";
private$age=26;
}
$M=newman();// Create an object
var_dump(serialize($M)); // Serialize this object
?>
string(47) "a:3:{i:0;s:1:"I";i:1;s:4:"Like";i:2;s:3:"PHP";}"
string(79) "O:3:"man":3:{s:4:"name";s:7:"xiaocui";s:3:"sex";s:3:"man";s:8:"manage";i:26;}"
Serialization of arrays :
a Represents an array
3 There are... In the representative array 3 Elements
i Subscripts representing arrays
0 representative I Subscript value of element
s Representative elements I The data type of is character type
1 Representative elements I The length of is 1
object serialization :
O Representation is an object
3 Represents the class name man The length of
3 Represents the number of fields in the class
s Representative attribute name The type of is character type
4 Representative attribute name The length of
// And so on , Serialize the field contents in the string with { Start ,;} end
unserialize() The original array or object <?php
highlight_file(__FILE__);
$sites=array('I', 'Like', 'PHP');
echo'<br/>';
echo$ser=serialize($sites).'<br/>'; // Serialize this object
var_dump(unserialize($ser)); // Deserialize the serialized string
echo'<br/>';
classman{
public$name="xiaocui";
public$sex="man";
private$age=26;
}
$M=newman();// Create an object
echo$ser=serialize($M).'<br/>'; // Serialize this object
var_dump(unserialize($ser)); // Deserialize the serialized string
?>
a:3:{i:0;s:1:"I";i:1;s:4:"Like";i:2;s:3:"PHP";}
array(3) { [0]=>string(1) "I"[1]=>string(4) "Like"[2]=>string(3) "PHP"}
O:3:"man":3:{s:4:"name";s:7:"xiaocui";s:3:"sex";s:3:"man";s:8:"manage";i:26;}
object(man)#2 (3) { ["name"]=> string(7) "xiaocui" ["sex"]=> string(3) "man" ["age":"man":private]=> int(26) }
2. Magic methods
____PHP Common magic methods in
__construct()
<?php
highlight_file(__FILE__);
classdemo{
public$name="xiaocui";
public$sex="man";
private$age=26;
publicfunction__construct()
{
echo"<br/>"." Call me when the class is instantiated !";
}
}
$D=newdemo(); // Instantiate objects
?>
Call me when the class is instantiated !
__destruct()
<?php
highlight_file(__FILE__);
class demo{
public $name="xiaocui";
public $sex="man";
private $age=26;
public function __construct()
{
echo "<br/>"." Call me when the class is instantiated !"."<br/>";
}
public function num($a,$b){
echo $c = $a + $b.'<br/>';
return $c;
}
public function __destruct(){
echo " Call me when all the methods in the class are destroyed !";
}
public function person($per){
echo "We are $per !!!".'<br/>';
}
}
$D=new demo(); // Instantiate objects
$D->num(5,6); // call num() Method
$D->person(man); // call person() Method
?>
Call me when the class is instantiated !
11
Weareman!!!
Call me when all the methods in the class are destroyed !
__construct()num(5,6)person(nanren)__destruct()__construct()num()person()__destruct()__wakeup()
unserialize()__wakeup()<?php
highlight_file(__FILE__);
classdemo{
public$name="xiaocui";
protected$sex="man";
private$age=26;
publicfunction__construct()
{
echo"<br/>"." Call me when the class is instantiated !"."<br/>";
}
publicfunction__destruct(){
echo"<br/>"." Call me when all the methods in the class are destroyed !"."<br/>";
}
publicfunction__wakeup()
{
echo"<br/>"." When the sequence is reversed, first call me !".'<br/>';
}
}
$D=newdemo(); // Instantiate objects
echo$ser=serialize($D); // Serializing objects $D
var_dump(unserialize($ser)); // Deserialize string $ser
?>
$age Call me when the class is instantiated !
O:4:"demo":3:{s:4:"name";s:7:"xiaocui";s:6:"*sex";s:3:"man";s:9:"demoage";i:26;}
Call me when the sequence is reversed !
object(demo)#2
(3) { ["name"]=> string(7) "xiaocui" ["sex":protected]=>
string(3) "man" ["age":"demo":private]=> int(26) }
Call me when all the methods in the class are destroyed !
Call me when all the methods in the class are destroyed !
__construct()serialize($D)__wakeup()unserialize($ser)__destruct()__destruct()__construct()serialize($D)__wakeup()unserialize($ser)__destruct()__destruct()__toString()
__toString()<?php
highlight_file(__FILE__);
class demo{
public $name="xiaocui";
protected $sex="man";
private $age=26;
public function __construct()
{
echo "<br/>"." Call me when the class is instantiated !"."<br/>";
}
public function __destruct(){
echo "<br/>"." Call me when all the methods in the class are destroyed !"."<br/>";
}
public function __wakeup()
{
echo "<br/>"." Call me when the sequence is reversed !".'<br/>';
}
public function __toString(){
return "<br/>"." Class is called when it is treated as a string !"."<br/>";
}
}
$D=new demo(); // Instantiate objects
echo $D; // Class is output as a string
?>
Call me when the class is instantiated !
Class is called when it is treated as a string !
Call me when all the methods in the class are destroyed !
echo__toString()__toString()Catchablefatalerror: ObjectofclassdemocouldnotbeconvertedtostringinD:\XXXX\phpstudy_pro\WWW\two\demo.phponline30
__sleep()
serialize()__sleep()__sleep()<?php
highlight_file(__FILE__);
class demo{
public $name="xiaocui";
protected $sex="man";
private $age=26;
public function __construct()
{
echo "<br/>"." Call me when the class is instantiated !"."<br/>";
}
public function __destruct(){
echo "<br/>"." Call me when all the methods in the class are destroyed !"."<br/>";
}
public function __wakeup()
{
echo "<br/>"." Call me when the sequence is reversed !".'<br/>';
}
public function __sleep(){
echo "<br/>"." Call me when the sequence !".'<br/>';
return array("name","sex","age"); // Here you have to return a number , The element inside represents the name of the returned attribute
}
}
$D=new demo(); // Instantiate objects
echo $ser = serialize($D); // Serializing objects
?>
Call me when the class is instantiated !
Call me when the sequence !
O:4:"demo":3:{s:4:"name";s:7:"xiaocui";s:6:"*sex";s:3:"man";s:9:"demoage";i:26;}
Call me when all the methods in the class are destroyed !
__sleep()__invoke()
__invoke<?php
highlight_file(__FILE__);
class demo{
public $name="xiaocui";
protected $sex="man";
private $age=26;
public function __construct()
{
echo "<br/>"." Call me when the class is instantiated !"."<br/>";
}
public function __destruct(){
echo "<br/>"." Call me when all the methods in the class are destroyed !"."<br/>";
}
public function __wakeup()
{
echo "<br/>"." Call me when deserializing !".'<br/>';
}
public function __sleep(){
echo "<br/>"." Call me when serializing !".'<br/>';
return array("name","sex","age");
}
public function __invoke()
{
echo "<br/>"." When you call an object in a functional way, you will call me !".'<br/>';
}
}
$D=new demo(); // Instantiate objects
$D(); // Call the object as a function
?>
Call me when the class is instantiated !
When you call an object in a functional way, you will call me !
Call me when all the methods in the class are destroyed !
__invoke()Fatalerror:
UncaughtError:
FunctionnamemustbeastringinD:\xxxxx\phpstudy_pro\WWW\two\demo.php:42Stacktrace:
#0 {main} thrown in D:\xxxxx\phpstudy_pro\WWW\two\demo.php on line 42
__call()
__call<?php
highlight_file(__FILE__);
class demo{
public $name="xiaocui";
protected $sex="man";
private $age=26;
public function __construct()
{
echo "<br/>"." Call me when the class is instantiated !"."<br/>";
}
public function num($a,$b){
echo "<br/>".$c = $a + $b.'<br/>';
return $c;
}
public function __destruct(){
echo "<br/>"." Call me when all the methods in the class are destroyed !"."<br/>";
}
public function person($per){
echo "<br/>"."We are $per !!!".'<br/>';
}
public function __wakeup()
{
echo "<br/>"." Call me when deserializing !".'<br/>';
}
public function __sleep(){
echo "<br/>"." Call me when the sequence !".'<br/>';
return array("name","sex","age");
}
public function __call($arg1,$arg2){
echo "<br/>"." Call me when an object calls a method that does not exist or is inaccessible !".'<br/>';
}
}
$D=new demo(); // Instantiate objects
$D->num1(1,2); // Call a method that doesn't exist
?>
Call me when the class is instantiated !
Call me when an object calls a method that does not exist or is inaccessible !
Call me when all the methods in the class are destroyed !
__set()
__set<?php
highlight_file(__FILE__);
class demo extends demo1{
public $name="xiaocui";
protected $sex="man";
private $age=26;
public function __construct()
{
echo "<br/>"." Call me when the class is instantiated !"."<br/>";
}
public function __destruct(){
echo "<br/>"." Call me when all the methods in the class are destroyed !"."<br/>";
}
public function person($per){
echo "<br/>"."We are $per !!!".'<br/>';
}
public function __set($arg1,$arg2){
echo "<br/>"." Call me when assigning a value to a nonexistent or inaccessible property !"."<br/>";
}
}
class demo1{
private $weight;
public $height;
public function people(){
echo $this->weight;
echo $this->height;
}
}
$D=new demo(); // Instantiate objects
$D->weight=74; // Assign values to inaccessible properties
?>
Call me when the class is instantiated !
Call me when assigning a value to a nonexistent or inaccessible property !
Call me when all the methods in the class are destroyed !
__isset()
isset()empty()__iset()__unset()
unset()__unset()__get()
__get<?php
highlight_file(__FILE__);
class demo extends demo1{
public $name="xiaocui";
protected $sex="man";
private $age=26;
public function __construct()
{
echo "<br/>"." Call me when the class is instantiated !"."<br/>";
}
public function __destruct(){
echo "<br/>"." Call me when all the methods in the class are destroyed !"."<br/>";
}
public function __get($arg1){
echo "<br/>"." Call me when reading non-existent or inaccessible properties !";
}
}
class demo1{
private $weight = 0;
public $height;
public function people(){
echo $this->weight;
echo $this->height;
}
}
$D=new demo(); // Instantiate objects
$D->weight; // Read inaccessible properties in the parent class
?>
Call me when the class is instantiated !
Call me when reading non-existent or inaccessible properties !
Call me when all the methods in the class are destroyed !
3. Deserialization vulnerability
3.1 Deserialization exploit condition
① unserialize() The parameters in the function are controllable
② There are classes available , And there are magic methods in the class
<?php
highlight_file(__FILE__);
class demo
{
public $arg1 = "0";
public function __destruct()
{
echo $this->arg1; // Output user passed arg1 value
}
}
$a=$_GET['arg']; // Receive the... Transmitted by the front end arg1 Variable
$unser = unserialize($a); // Deserialize passed arg1
?>
argunserialize()demo__destruct()__destruct()argargunserialize()__destruct()XSS

<?php
highlight_file(__FILE__);
class demo
{
public $arg1 = "0";
public function __destruct()
{
eval($this->arg1); //eval() To execute the user passed arg1 value
}
}
$a=$_GET['arg']; // Receive the... Transmitted by the front end arg1 Variable
$unser = unserialize($a); // Deserialize passed arg1
var_dump($unser);
?>
eval()RCE
3.2 __wakeup() Function bypasses
__wakeup()<?php
highlight_file(__FILE__);
class demo
{
public $arg1 = "0";
public function __destruct()
{
eval($this->arg1); //eval() To execute the user passed arg1 value
}
public function __wakeup(){
foreach(get_object_vars($this) as $k => $v) {
$this->$k = ''; // Traverse the passed parameters , All assignments are empty
}
}
}
$a=$_GET['arg']; // Receive the... Transmitted by the front end arg1 Variable
$unser = unserialize($a); // Deserialize passed arg1
var_dump($unser);
?>
__wakeup()
__wakeup()
边栏推荐
猜你喜欢

什么是上下文?

微信公众号无限回调授权系统源码

Leetcode skimming: binary tree 08 (maximum depth of n-ary tree)

十字路口通行优先权,十字路口通行规则图解

NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon

2021 RSC | Drug–target affinity prediction using graph neural network and contact maps

Graduation project

浅谈JVM的那些事

疫情远程办公经验分享| 社区征文

UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x98 in position 1093: illegal multibyte sequence
随机推荐
微信脑力比拼答题小程序_支持流量主带最新题库文件
C语言单向链表练习
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
架构实战营 - 第 6 期 模块九之毕业设计
微信公众号无限回调授权系统源码
【云原生】那些看起来很牛X,原理却很简单的一行代码
MySQL JDBC编程
[csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability
戳气球和布尔运算问题(巨难)
MIN_RTO 对话
2020 Bioinformatics | TransformerCPI
(pointeur) Écrivez - vous une fonction qui compare la taille de la chaîne et fonctionne comme strcmp.
仿《游戏鸟》源码 手游发号评测开服开测合集专区游戏下载网站模板
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
How to add custom API objects in kubernetes (1)
浅谈一篇优质的小红书文案需要具备什么
【微服务|openfeign】feign的两种降级方式|Fallback|FallbackFactory
Parameterization of controls in katalon
Katalon uses script to query list size
Leetcode skimming: binary tree 04 (sequence traversal of binary tree)