dvwa学习笔记——存储型xss

二、存储型xss

存储型xss一般出现在网站留言板,评论处,个人资料处,等需要用户对网站写入数据的地方。因为存储型xss的脚本是存储在网页中,所以也可以说是“持久性xss”

过程:攻击者在界面插入xss代码,服务器将数据导入数据库,当用户访问到存在xss漏洞的页面时,服务器从数据库取出数据放在页面,导致xss代码执行,达到攻击效果。

 最常见的就是cookie窃取。比如攻击者将一段窃取cookie的恶意js脚本写入评论处,如果该处对用户输入过滤不严格,这段恶意脚本就会写入数据库,当其他用户浏览这个被写入脚本的页面时,网站从数据库中读取恶意脚本显示到页面中被浏览器执行(一般是弹框方式),导致用户cookie被窃取,攻击者就可以用这段cookie无密码登录该用户的账号。

在dvwa下练习

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();
}

?>

漏洞分析与利用:

trim(string,charlist)

函数移除字符串两侧的空白字符或其他预定义字符。预定义字符包括“\0”,“t”,“\n”,“\x0B”,“\r”,“ ”

可选参数charlist支持添加额外需要删除的字符。

 

stripslashes()

函数删除字符串中的反斜杠。

 

mysqli_real_escape_string(connection,escapestring)函数转义在 SQL 语句中使用的字符串中的特殊字符,使得字符串是一个合法的SQL语句。

connection:要连接的MySQL数据库

escapestring:要转义的字符串,编码的字符是 NUL(ASCII 0),\n,\r,\,'," 和 Control-Z

最终未对用户输入的数据进行xss检测编码,直接写入数据库,所以存在存储型xss。

构造脚本:

<script>alert(/存储型xss/)</script>

执行脚本:message文本框写入脚本

因为脚本被写入数据库,所以每次刷新进入该界面都会有弹框,除非clear清除掉。

name字段对输入的长度做了限制,网上有人用burpsuite抓包再嵌入脚本可实现。

medium源码:

<?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 = str_replace( '<script>', '', $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();
}

?>

漏洞分析与利用:

strip_tags() 函数,剥去字符串中的HTML、XML以及PHP的标签。

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

addslashes函数详解https://www.w3school.com.cn/php/func_string_addslashes.asp

htmlspecialchars函数将message中的预定义字符转换成html实体,不存在xss漏洞。

但是对name的输入只是用str_replace函数对<script>标签做了简单的替换,因此可以在此处做xss。

构造脚本:

非<script>标签

<img src=0 onerror=alert(/xss/)>

大小写绕过:

<ScrIpt>alert(/xss/)</scRipt>

双重<scrpit>标签:

<sc<script>ript>alert(/xss/)<script>

但由于对name值长度做了限制,因此用burpsuite抓包改包重发绕过。

给IE浏览器设置burpsuite代理

手动设置完,点击应用,之后必须重启浏览器即可进行代理。

设置完代理服务器后,如果burp抓不到dvwa本地包,更换访问域名,将localhost/1270.0.1改成本机的ip地址即可。

执行脚本:

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(); 
} 

?>    

漏洞分析与利用:

值message仍然使用htmlspecialchars()函数将预定义字符转换成html实体,因此这块不存在xss漏洞。

值name使用pre_replace函数执行一个正则表达式的搜索与替换。从<开始,匹配script字符以及穿插在script之间的各种字符组合(意思是只要有script这六个字符一起出现,可以不连续,都将匹配出来被替换成空字符),最后的i表示不区分大小写。

可以使用其他标签绕过,如<img>。

构造脚本:

<img scr=0 onerror=alert(/xss/)>

执行脚本:

’用bush改包替换绕过

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(); 

?>  

分析:

值name和message都使用了HTMLspecialchars方法,于是此处不存在xss漏洞。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值