Java反序列化Java序列化与反序列化依赖于两个函数writeObject()与readObject(),只要一个类实现了Serializable接口就可以使用这两个函数进行序列化与反序列化。Java反序列化漏洞原理类似PHP反序列化,PHP反序列化时会触发wakeup()函数,而Java中没有这种通用的函数,要实现类似wakeup()函数在反序列化时被触发的功能需要重写readObject()。Java反序列化漏洞需要触发一个被重写的readObject(),这个readObject()调用了一个其他类也有同名的函数,导致非预期的函数被执行。举个例子:原本的程序预期:从接口接到A类对象序列化的字节流调用A类的readObject()进行反序列化A类的readObject()中调用反序列化出来的A类对象中的一个B类对象的F方法完成反序列化传入恶意序列化字节流后:在A类对象中放置B类对象的位置嵌入一个恶意的C类对象,其中B与C有一个共同的F方法从接口接到A类对象序列化的字节流调用A类的readObject()进行反序列化A类的readObject()中调用反序列化出来的A类对象中的C类
一、代码审计include("flag.php"); highlight_file(__FILE__); class FileHandler { protected $op; protected $filename; protected $content; function __construct() { $op = "1"; $filename = "/tmp/tmpfile"; $content = "Hello World!"; $this->process(); } public function process() { if($this->op == "1") { $this->write(); } else if($this->op == "2") {
定义PHP反序列化可以将一个对象转换为一个字符串保存起来,这将导致类中的成员可被用户控制,同时PHP在反序列化时会触发一些魔术函数,若这些魔术函数中含有危险函数且危险函数的参数是类的成员变量,即可导致越权访问或者命令执行。主要涉及到两个函数:serialize($o):将对象序列化为字符串unserialize($str):将字符串反序列化为对象序列化字符串解析PHP序列化对象只会序列化对象的成员变量,对三个类型的成员变量有不同的标志方法。例:class Obj { public $int = 2; protected $str = "2"; private $a = "a"; } $o = new Obj(); echo serialize($o);O:3:"Obj":3:{s:3:"int";i:2;s:6:"%00*%00str";s:1:"2";s:6:"%00Obj%00a";s:1:"a"
Equinox
一个乐于分享的网安人