[实验]xss dvwa

来源:互联网 发布:华硕超频软件 编辑:程序博客网 时间:2024/04/30 18:21

  • 反射型XSS
    • Low
      • 源代码
      • 漏洞利用
    • Medium
        • 源代码
      • 漏洞利用
    • High
      • 源代码
      • 漏洞利用
    • Impossible
      • 源代码
      • 漏洞利用
  • 存储型XSS
    • Low
      • 源代码
      • 漏洞利用
    • Medium
      • 源代码
      • 漏洞利用
    • High
      • 源代码
      • 漏洞利用
    • Impossible
      • 源代码
      • 漏洞利用
  • 参考list
  • tips


反射型XSS

Low

源代码

<?php// Is there any input?if( array_key_exists( "name", $_GET ) && $_GET[ 'name' ] != NULL ) {    // Feedback for end user    $html .= '<pre>Hello ' . $_GET[ 'name' ] . '</pre>';}?>

漏洞利用

输入,成功弹框:


Medium

源代码

<?php// 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    $html .= "<pre>Hello ${name}</pre>";}?>

可以看到,这里对输入进行了过滤,基于黑名单的思想,使用str_replace函数将输入中的<script>删除,这种防护机制是可以被轻松绕过的。

漏洞利用

1.双写绕过
输入<sc<script>ript>alert(/xss/)</script>,成功弹框:

2.大小写混淆绕过

输入<ScRipt>alert(/xss/)</script>,成功弹框:


High

源代码

<?php// 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' ] );//用空格替换所有的出现的script的字母    // Feedback for end user    $html .= "<pre>Hello ${name}</pre>";}?>

可以看到,High级别的代码同样使用黑名单过滤输入,preg_replace() 函数用于正则表达式的搜索和替换,这使得双写绕过、大小写混淆绕过(正则表达式中i表示不区分大小写)不再有效。

漏洞利用

虽然无法使用<script>标签注入XSS代码,但是可以通过img、body等标签的事件或者iframe等标签的src注入恶意的js代码。

输入<img src=1 onerror=alert(/xss/)>,成功弹框:


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    $html .= "<pre>Hello ${name}</pre>";}// Generate Anti-CSRF tokengenerateSessionToken();?>
  • PHP htmlspecialchars() 函数
    把预定义的字符 “<” (小于)和 “>” (大于)转换为 HTML 实体:

可以看到,Impossible级别的代码使用htmlspecialchars函数把预定义的字符&、”、’、<、>转换为 HTML 实体,防止浏览器将其作为HTML元素。

  • HTMLSpecialChars重要性

如果不用HTMLSpecialChars,就会导致读取时,要把" <script>"之类的HTML标签“原本”的输出,而这一输出就有漏洞了,万一那个插入数据库的人是黑客,插入的不是一般的字符串,而是 “ <script> <b> <body>”等等之类的东西,读取后,就是一个HTML文档之类的东西,这样,他可以利用这个漏洞欺骗其他人,或者借这个漏洞攻击别人等等,搞个框架跳转到某一网站等等操作。

简言之,用户输入的如何东西都不当标签。

漏洞利用

None


存储型XSS

Low

源代码

<?phpif( isset( $_POST[ 'btnSign' ] ) ) {    // Get input    $message = trim( $_POST[ 'mtxMessage' ] );    $name    = trim( $_POST[ 'txtName' ] );    //trim(string,charlist)    //函数移除字符串两侧的空白字符或其他预定义字符,预定义字符包括、\t、\n、\x0B、\r以及空格    //可选参数charlist支持添加额外需要删除的字符。    // 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)) ? "" : ""));//mysqli_real_escape_string()转义特殊字符,比如转义单引号,防止影响$sql语句的闭合    // 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();}?>

漏洞利用

可以看到,对输入并没有做XSS方面的过滤与检查,且存储在数据库中,因此这里存在明显的存储型XSS漏洞。

message一栏输入,成功弹框:

name一栏前端有字数限制,抓包改为:


Medium

源代码

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

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

漏洞利用

1.双写绕过

抓包改name参数为<sc<script>ript>alert(/xss/)</script>
弹窗

2.大小写混淆

抓包改name参数为<ScRipt>alert(/xss/)</script>
弹窗

存储在页面中

在数据库中


High

源代码

<?phpif( 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不存在注入点,name有注入,但是过滤了<script>标签,也不可以双写绕过或者大小写混淆。

要用其他危险标签
<img src=1 onerror=alert(1)>

漏洞利用

抓包改name参数为 <img src=1 onerror=alert(1)>

弹窗

在页面中,

在数据库中,


Impossible

源代码

<?phpif( 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 tokengenerateSessionToken();?>

可以看到,通过使用htmlspecialchars函数,解决了XSS,但是要注意的是,如果htmlspecialchars函数使用不当,攻击者就可以通过编码的方式绕过函数进行XSS注入,尤其是DOM型的XSS。

漏洞利用

None


参考list:

新手指南:DVWA-1.9全级别教程(完结篇,附实例)之XSS


tips:

  • 基本上是跟着教程做一遍,有源码,能看到数据库真实存储的内容的实验才能知道原因。

  • 在作存储型xss的时候,由于之前做的代码已经注入到数据库中了,所以在作下一次的时候,之前的代码会弹窗影响实验,所以建议每做一次就清空一次数据表。(delete from guestbook where 1=1)

0 0
原创粉丝点击