当前位置:网站首页>[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()
边栏推荐
- RHCSA 06 - suid, sgid, sticky bit(待补充)
- Why use node
- ROS2中CMake编译选项的设置
- UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x98 in position 1093: illegal multibyte sequence
- Redis:有序集合zset类型数据操作命令
- RHCSA 03 - 文件的基础权限
- C language bidirectional linked list first edition
- JS realizes the effect of text scrolling marquee
- Redis:哈希hash类型数据操作命令
- 5张图告诉你:同样是职场人,差距怎么这么大?
猜你喜欢

【微信小程序】好看的轮播图组件

96% of the collected traffic is prevented by bubble mart of cloud hosting

戳气球和布尔运算问题(巨难)

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

PPt 教程,如何在 PowerPoint 中将演示文稿另存为 PDF 文件?

DP83848+网线热拔插

Wechat official account infinite callback authorization system source code

Graduation project: design seckill e-commerce system

Emlog user registration plug-in is worth 80 yuan

十字路口通行优先权,十字路口通行规则图解
随机推荐
UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x98 in position 1093: illegal multibyte sequence
程序员远程办公喜忧参半| 社区征文
统计遗传学:第三章,群体遗传
Apple CMS imitation watermelon video atmospheric response video template source code
One click compilation and deployment of MySQL
西部数据绿盘、蓝盘、黑盘、红盘和紫盘有什么区别
Wechat official account infinite callback authorization system source code
User defined path and file name of Baidu editor in laravel admin
ROS2中CMake编译选项的设置
批处理初识
Unity Resource path
Emlog user registration plug-in is worth 80 yuan
RHCSA 07 - 用户与群组管理
dried food! Generation of rare samples based on GaN
leetcode刷题:二叉树06(对称二叉树)
Leetcode 121 best time to buy and sell stock (simple)
Redis:有序集合zset类型数据操作命令
一位毕业生的自我分享
Understand the principle of bytecode enhancement technology through the jvm-sandbox source code
2021 RSC | Drug–target affinity prediction using graph neural network and contact maps