概述
一道很经典的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等),就会出现实际字符数与声明长度不一致,从而出现字符串逃逸漏洞。
1100

被折叠的 条评论
为什么被折叠?



