DVWA--全等级XSS存储型(Stored)

什么是存储型XSS:
攻击者事先将恶意代码上传或储存到漏洞服务器中,只要受害者浏览包含此恶意代码的页面就会执行恶意代码。这就意味着只要访问了这个页面的访客,都有可能会执行这段恶意脚本,因此储存型XSS的危害会更大。因为存储型XSS的代码存在于网页的代码中,可以说是永久型的。

存储型 XSS 一般出现在网站留言、评论、博客日志等交互处,恶意脚本存储到客户端或者服务端的数据库中。

接下来我们进入正题:

LOw

源码:

 1 <?php
 2 
 3 if( isset( $_POST[ 'btnSign' ] ) ) {
 4     // Get input
 5     $message = trim( $_POST[ 'mtxMessage' ] );
 6     $name    = trim( $_POST[ 'txtName' ] );
 7 
 8     // Sanitize message input
 9     $message = stripslashes( $message );
10     $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)) ? "" : ""));
11 
12     // Sanitize name input
13     $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)) ? "" : ""));
14 
15     // Update database
16     $query  = "INSERT INTO guestbook ( comment, name ) VALUES ( '$message', '$name' );";
17     $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>' );
18 
19     //mysql_close();
20 }
21 
22 ?>

相关函数介绍
trim(string,charlist)
函数移除字符串两侧的空白字符或其他预定义字符,预定义字符包括、 、 、x0B、 以及空格,可选参数charlist支持添加额外需要删除的字符。
mysql_real_escape_string(string,connection)
函数会对字符串中的特殊符号(x00, , ,,‘,“,x1a)进行转义。
stripslashes(string)
函数删除字符串中的反斜杠。

毕竟是简单的,对输入的东西并没有做XSS方面的过滤与检查,且存储在数据库中,存储型XSS漏洞很是明显。

在Message一栏中输入 <script>alert(li)</script>   Name随便

既然是存储型的,顾名思义,是可以存储记录的,每当我们再次回到此页面的时候会有弹框出现。

而且name框也是存在XSS漏洞的,但是他有字数限制,只能有十个空位,但是我们可以抓包来进行注入,没有字数限制了!

Medium

源码:

 1 ?php
 2 
 3 if( isset( $_POST[ 'btnSign' ] ) ) {
 4     // Get input
 5     $message = trim( $_POST[ 'mtxMessage' ] );
 6     $name    = trim( $_POST[ 'txtName' ] );
 7 
 8     // Sanitize message input
 9     $message = strip_tags( addslashes( $message ) );
10     $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)) ? "" : ""));
11     $message = htmlspecialchars( $message );
12 
13     // Sanitize name input
14     $name = str_replace( '<script>', '', $name );
15     $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)) ? "" : ""));
16 
17     // Update database
18     $query  = "INSERT INTO guestbook ( comment, name ) VALUES ( '$message', '$name' );";
19     $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>' );
20 
21     //mysql_close();
22 }
23 
24 ?>

相关函数说明:

strip_tags() 函数剥去字符串中的HTML、XML以及PHP的标签,但允许使用<b>标签。

addslashes() 函数返回在预定义字符(单引号、双引号、反斜杠、NULL)之前添加反斜杠的字符串。

可以看到,由于对message参数使用了htmlspecialchars函数进行编码,因此无法再通过message参数注入XSS代码,但是对于name参数,只是简单过滤了<script>字符串,仍然存在存储型的XSS。

方法还是双写饶过和大写饶过,但是这回我们要进行抓包:

双写饶过:<sc<script>ript>alert(/我的/)</script>,成功!

大写饶过:<Script>alert(/你的/)</script>

 

High

源码:

 1 ?php
 2 
 3 if( isset( $_POST[ 'btnSign' ] ) ) {
 4     // Get input
 5     $message = trim( $_POST[ 'mtxMessage' ] );
 6     $name    = trim( $_POST[ 'txtName' ] );
 7 
 8     // Sanitize message input
 9     $message = strip_tags( addslashes( $message ) );
10     $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)) ? "" : ""));
11     $message = htmlspecialchars( $message );
12 
13     // Sanitize name input
14     $name = preg_replace( '/<(.*)s(.*)c(.*)r(.*)i(.*)p(.*)t/i', '', $name );
15     $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)) ? "" : ""));
16 
17     // Update database
18     $query  = "INSERT INTO guestbook ( comment, name ) VALUES ( '$message', '$name' );";
19     $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>' );
20 
21     //mysql_close();
22 }
23 
24 ?>

看了这个代码,我们觉得是不是和反射型的差不多,message参数使用了htmlspecialchars函数进行编码,我们还是无法再通过message参数注入XSS代码,至于Name,这里过滤了<script>标签,所以我们就来使用img、和iframe标签进行注入。

Img标签

  <img src=1 onerror=alert(/hahah/)>

 

iframe标签

  <iframe onload=alert(haha)>

Impossible

源码:

 1 ?php
 2 
 3 if( isset( $_POST[ 'btnSign' ] ) ) {
 4     // Check Anti-CSRF token
 5     checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
 6 
 7     // Get input
 8     $message = trim( $_POST[ 'mtxMessage' ] );
 9     $name    = trim( $_POST[ 'txtName' ] );
10 
11     // Sanitize message input
12     $message = stripslashes( $message );
13     $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)) ? "" : ""));
14     $message = htmlspecialchars( $message );
15 
16     // Sanitize name input
17     $name = stripslashes( $name );
18     $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)) ? "" : ""));
19     $name = htmlspecialchars( $name );
20 
21     // Update database
22     $data = $db->prepare( 'INSERT INTO guestbook ( comment, name ) VALUES ( :message, :name );' );
23     $data->bindParam( ':message', $message, PDO::PARAM_STR );
24     $data->bindParam( ':name', $name, PDO::PARAM_STR );
25     $data->execute();
26 }
27 
28 // Generate Anti-CSRF token
29 generateSessionToken();
30 
31 ?>

依旧无懈可击!

原文地址:https://www.cnblogs.com/li2019/p/12629351.html