BUUCTF 加固题 Ezsql WriteUp

x33g5p2x  于2022-02-12 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(369)

想直接要加固代码的点这里

题目

靶机地址解释:

第一行:目标机器 WEB 服务地址

第二行:目标机器 SSH 地址以及端口

第三行:Check 服务访问地址

修复方法:

  1. SSH 连接上目标机器,用户 ctf,密码 123456。
  2. 对目标机器上的服务进行加固。
  3. 访问 Check 服务的 /check进行 check。
  4. 若返回 True,则访问 /flag 可获得 /flag。
  5. 每次 check 后目标机器会重置。

一、查看

访问目标机器web服务地址,发现是一个登陆界面

利用万能密码登录成功

用户名:admin' or 1=1#

密码: 任意

登陆成功,本题就是让加固这个登陆界面,以防止sql注入即可获取flag。

二、进入目标机器加固

根据给的地址和端口ssh连接目标机器,进入/var/www/html目录

加固index.php,查看它的源码

修改前的文件:
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>让我访问</title>
  8. <link href="http://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  9. <link href="http://cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
  10. <link rel="stylesheet" type="text/css" href="css/htmleaf-demo.css">
  11. <style type="text/css">
  12. .form-bg {
  13. padding: 2em 0;
  14. }
  15. .form-horizontal {
  16. background: #ffffff;
  17. padding-bottom: 40px;
  18. border-radius: 15px;
  19. text-align: center;
  20. }
  21. .form-horizontal .heading {
  22. display: block;
  23. font-size: 35px;
  24. font-weight: 700;
  25. padding: 35px 0;
  26. border-bottom: 1px solid #f0f0f0;
  27. margin-bottom: 30px;
  28. }
  29. .form-horizontal .form-group {
  30. padding: 0 40px;
  31. margin: 0 0 25px 0;
  32. position: relative;
  33. }
  34. .form-horizontal .form-control {
  35. background: #f0f0f0;
  36. border: none;
  37. border-radius: 20px;
  38. box-shadow: none;
  39. padding: 0 20px 0 45px;
  40. height: 40px;
  41. transition: all 0.3s ease 0s;
  42. }
  43. .form-horizontal .form-control:focus {
  44. background: #e0e0e0;
  45. box-shadow: none;
  46. outline: 0 none;
  47. }
  48. .form-horizontal .form-group i {
  49. position: absolute;
  50. top: 12px;
  51. left: 60px;
  52. font-size: 17px;
  53. color: #c8c8c8;
  54. transition: all 0.5s ease 0s;
  55. }
  56. .form-horizontal .form-control:focus + i {
  57. color: #00b4ef;
  58. }
  59. .form-horizontal .fa-question-circle {
  60. display: inline-block;
  61. position: absolute;
  62. top: 12px;
  63. right: 60px;
  64. font-size: 20px;
  65. color: #808080;
  66. transition: all 0.5s ease 0s;
  67. }
  68. .form-horizontal .fa-question-circle:hover {
  69. color: #000;
  70. }
  71. .form-horizontal .main-checkbox {
  72. float: left;
  73. width: 20px;
  74. height: 20px;
  75. background: #11a3fc;
  76. border-radius: 50%;
  77. position: relative;
  78. margin: 5px 0 0 5px;
  79. border: 1px solid #11a3fc;
  80. }
  81. .form-horizontal .main-checkbox label {
  82. width: 20px;
  83. height: 20px;
  84. position: absolute;
  85. top: 0;
  86. left: 0;
  87. cursor: pointer;
  88. }
  89. .form-horizontal .main-checkbox label:after {
  90. content: "";
  91. width: 10px;
  92. height: 5px;
  93. position: absolute;
  94. top: 5px;
  95. left: 4px;
  96. border: 3px solid #fff;
  97. border-top: none;
  98. border-right: none;
  99. background: transparent;
  100. opacity: 0;
  101. -webkit-transform: rotate(-45deg);
  102. transform: rotate(-45deg);
  103. }
  104. .form-horizontal .main-checkbox input[type=checkbox] {
  105. visibility: hidden;
  106. }
  107. .form-horizontal .main-checkbox input[type=checkbox]:checked + label:after {
  108. opacity: 1;
  109. }
  110. .form-horizontal .text {
  111. float: left;
  112. margin-left: 7px;
  113. line-height: 20px;
  114. padding-top: 5px;
  115. text-transform: capitalize;
  116. }
  117. .form-horizontal .btn {
  118. float: right;
  119. font-size: 14px;
  120. color: #fff;
  121. background: #00b4ef;
  122. border-radius: 30px;
  123. padding: 10px 25px;
  124. border: none;
  125. text-transform: capitalize;
  126. transition: all 0.5s ease 0s;
  127. }
  128. @media only screen and (max-width: 479px) {
  129. .form-horizontal .form-group {
  130. padding: 0 25px;
  131. }
  132. .form-horizontal .form-group i {
  133. left: 45px;
  134. }
  135. .form-horizontal .btn {
  136. padding: 10px 20px;
  137. }
  138. }
  139. </style>
  140. </head>
  141. <body>
  142. <div class="htmleaf-container">
  143. <header class="htmleaf-header">
  144. <h1>我还可以教你,敦 dua 郎哦。</h1>
  145. <div class="htmleaf-links">
  146. </div>
  147. </header>
  148. <div class="demo form-bg">
  149. <div class="container">
  150. <div class="row">
  151. <div class="col-md-offset-3 col-md-6">
  152. <form class="form-horizontal" method="get" action="">
  153. <span class="heading">让我访问</span>
  154. <div class="form-group">
  155. <input type="text" class="form-control" id="inputEmail3" placeholder="用户名" name="username">
  156. </div>
  157. <div class="form-group help">
  158. <input type="password" class="form-control" id="inputPassword3" placeholder="密码"
  159. name="password">
  160. </div>
  161. <div class="form-group help">
  162. <input type="submit" class="form-control" id="inputSubmit">
  163. </div>
  164. </form>
  165. </div>
  166. </div>
  167. </div>
  168. </div>
  169. <div class="related">
  170. </div>
  171. </div>
  172. </body>
  173. </html>
  174. <h4 style="text-align: center; color: #000000">
  175. <?php
  176. error_reporting(0);
  177. include 'dbConnect.php';
  178. $username = $_GET['username'];
  179. $password = $_GET['password'];
  180. if (isset($_GET['username']) && isset($_GET['password'])) {
  181. $sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
  182. $result = $mysqli->query($sql);
  183. if (!$result)
  184. die(mysqli_error($mysqli));
  185. $data = $result->fetch_all(); // 从结果集中获取所有数据
  186. if (!empty($data)) {
  187. echo '登录成功!';
  188. } else {
  189. echo "用户名或密码错误";
  190. }
  191. }
  192. ?>
  193. </h4>
添加如下代码:
  1. $username = addslashes($username);
  2. $password = addslashes($password);

以下是W3school对addslashes() 函数的解释

addslashes() 函数返回在预定义字符之前添加反斜杠的字符串。
预定义字符是:

  • 单引号(’)
  • 双引号(")
  • 反斜杠(\)
  • NULL

该函数可用于为存储在数据库中的字符串以及数据库查询语句准备字符串。

修改后的文件
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>让我访问</title>
  8. <link href="http://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  9. <link href="http://cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
  10. <link rel="stylesheet" type="text/css" href="css/htmleaf-demo.css">
  11. <style type="text/css">
  12. .form-bg {
  13. padding: 2em 0;
  14. }
  15. .form-horizontal {
  16. background: #ffffff;
  17. padding-bottom: 40px;
  18. border-radius: 15px;
  19. text-align: center;
  20. }
  21. .form-horizontal .heading {
  22. display: block;
  23. font-size: 35px;
  24. font-weight: 700;
  25. padding: 35px 0;
  26. border-bottom: 1px solid #f0f0f0;
  27. margin-bottom: 30px;
  28. }
  29. .form-horizontal .form-group {
  30. padding: 0 40px;
  31. margin: 0 0 25px 0;
  32. position: relative;
  33. }
  34. .form-horizontal .form-control {
  35. background: #f0f0f0;
  36. border: none;
  37. border-radius: 20px;
  38. box-shadow: none;
  39. padding: 0 20px 0 45px;
  40. height: 40px;
  41. transition: all 0.3s ease 0s;
  42. }
  43. .form-horizontal .form-control:focus {
  44. background: #e0e0e0;
  45. box-shadow: none;
  46. outline: 0 none;
  47. }
  48. .form-horizontal .form-group i {
  49. position: absolute;
  50. top: 12px;
  51. left: 60px;
  52. font-size: 17px;
  53. color: #c8c8c8;
  54. transition: all 0.5s ease 0s;
  55. }
  56. .form-horizontal .form-control:focus + i {
  57. color: #00b4ef;
  58. }
  59. .form-horizontal .fa-question-circle {
  60. display: inline-block;
  61. position: absolute;
  62. top: 12px;
  63. right: 60px;
  64. font-size: 20px;
  65. color: #808080;
  66. transition: all 0.5s ease 0s;
  67. }
  68. .form-horizontal .fa-question-circle:hover {
  69. color: #000;
  70. }
  71. .form-horizontal .main-checkbox {
  72. float: left;
  73. width: 20px;
  74. height: 20px;
  75. background: #11a3fc;
  76. border-radius: 50%;
  77. position: relative;
  78. margin: 5px 0 0 5px;
  79. border: 1px solid #11a3fc;
  80. }
  81. .form-horizontal .main-checkbox label {
  82. width: 20px;
  83. height: 20px;
  84. position: absolute;
  85. top: 0;
  86. left: 0;
  87. cursor: pointer;
  88. }
  89. .form-horizontal .main-checkbox label:after {
  90. content: "";
  91. width: 10px;
  92. height: 5px;
  93. position: absolute;
  94. top: 5px;
  95. left: 4px;
  96. border: 3px solid #fff;
  97. border-top: none;
  98. border-right: none;
  99. background: transparent;
  100. opacity: 0;
  101. -webkit-transform: rotate(-45deg);
  102. transform: rotate(-45deg);
  103. }
  104. .form-horizontal .main-checkbox input[type=checkbox] {
  105. visibility: hidden;
  106. }
  107. .form-horizontal .main-checkbox input[type=checkbox]:checked + label:after {
  108. opacity: 1;
  109. }
  110. .form-horizontal .text {
  111. float: left;
  112. margin-left: 7px;
  113. line-height: 20px;
  114. padding-top: 5px;
  115. text-transform: capitalize;
  116. }
  117. .form-horizontal .btn {
  118. float: right;
  119. font-size: 14px;
  120. color: #fff;
  121. background: #00b4ef;
  122. border-radius: 30px;
  123. padding: 10px 25px;
  124. border: none;
  125. text-transform: capitalize;
  126. transition: all 0.5s ease 0s;
  127. }
  128. @media only screen and (max-width: 479px) {
  129. .form-horizontal .form-group {
  130. padding: 0 25px;
  131. }
  132. .form-horizontal .form-group i {
  133. left: 45px;
  134. }
  135. .form-horizontal .btn {
  136. padding: 10px 20px;
  137. }
  138. }
  139. </style>
  140. </head>
  141. <body>
  142. <div class="htmleaf-container">
  143. <header class="htmleaf-header">
  144. <h1>我还可以教你,敦 dua 郎哦。</h1>
  145. <div class="htmleaf-links">
  146. </div>
  147. </header>
  148. <div class="demo form-bg">
  149. <div class="container">
  150. <div class="row">
  151. <div class="col-md-offset-3 col-md-6">
  152. <form class="form-horizontal" method="get" action="">
  153. <span class="heading">让我访问</span>
  154. <div class="form-group">
  155. <input type="text" class="form-control" id="inputEmail3" placeholder="用户名" name="username">
  156. </div>
  157. <div class="form-group help">
  158. <input type="password" class="form-control" id="inputPassword3" placeholder="密码"
  159. name="password">
  160. </div>
  161. <div class="form-group help">
  162. <input type="submit" class="form-control" id="inputSubmit">
  163. </div>
  164. </form>
  165. </div>
  166. </div>
  167. </div>
  168. </div>
  169. <div class="related">
  170. </div>
  171. </div>
  172. </body>
  173. </html>
  174. <h4 style="text-align: center; color: #000000">
  175. <?php
  176. error_reporting(0);
  177. include 'dbConnect.php';
  178. $username = $_GET['username'];
  179. $password = $_GET['password'];
  180. $username = addslashes($username);
  181. $password = addslashes($password);
  182. if (isset($_GET['username']) && isset($_GET['password'])) {
  183. $sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
  184. $result = $mysqli->query($sql);
  185. if (!$result)
  186. die(mysqli_error($mysqli));
  187. $data = $result->fetch_all(); // 从结果集中获取所有数据
  188. if (!empty($data)) {
  189. echo '登录成功!';
  190. }
  191. else { echo "用户名或密码错误"; }
  192. }
  193. ?>
  194. </h4>

三、Check

再次进行万能密码登录发现已经不行了,然后就访问Check服务进行check

然后访问flag,发现flag已经出来了

相关文章