DVWA——Brute Force

文章目录

  • Brute Force(暴力(破解))
    • (1)Low等级
    • (2)Medium等级
    • (3)High等级
    • (4)Impossible

Brute Force(暴力(破解))

目的:通过枚举逐个猜测匹配某个预定值

Brute Force主要表现形式:通过设置(如表单) 预配值发送给服务器分析响应

攻击形式:传统暴力破解、字典暴力破解。

DVWA——Brute Force_第1张图片

(1)Low等级

漏洞原理:直接使用原始的sql语句进行查询,没做代码审计。

代码审计:检查源代码的安全性

Brute Force Source
vulnerabilities/brute/source/low.php
<?php

if( isset( $_GET[ 'Login' ] ) ) {
    
    $user = $_GET[ 'username' ];	// Get username
    $pass = $_GET[ 'password' ];	// Get password
    $pass = md5( $pass );	//对password做16字节的摘要算法

    // Check the database
    $query  = "SELECT * FROM `users` WHERE user = '$user' AND password = '$pass';";
    $result = mysqli_query($GLOBALS["___mysqli_ston"],  $query ) or die( '
' . 						((is_object($GLOBALS["___mysqli_ston"])) ? 												mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = 							mysqli_connect_error()) ? $___mysqli_res : false)) . '
'
); if( $result && mysqli_num_rows( $result ) == 1 ) { // Get users details $row = mysqli_fetch_assoc( $result ); $avatar = $row["avatar"]; // Login successful echo "

Welcome to the password protected area {$user}

"
; echo "{$avatar}\" />"; } else { // Login failed echo "

Username and/or password incorrect.
"
; } ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res); } ?>

漏洞利用:

1)传统破解(利用sql注入漏洞):

在username输入

admin' or '1' = '1

2)字典破解(burp工具利用):

抓包 -> ctrl+I将包复制到intruder模块,对需要破解的参数值(如密码)两边设置单$ -> 在Payloads载入字典 -> 后按 Start attack按钮进行破解 -> 得到的响应结果length与其他不一致的,极有可能是该用户密码。

(2)Medium等级

利用了 mysqli_real_escape_string()函数对user中的特殊符号(x00,n,r,,’,”,x1a)进行转义,用md5()加密后,杜绝了password的sql注入可能;



if( isset( $_GET[ 'Login' ] ) ) {
    // Sanitise username input
    $user = $_GET[ 'username' ];
    $user = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $user ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));    
    //利用了 mysqli_real_escape_string()函数:转移特殊字符

    // Sanitise password input
    $pass = $_GET[ 'password' ];
    $pass = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $pass ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));
    $pass = md5( $pass );   //对password加密

    // Check the database
    $query  = "SELECT * FROM `users` WHERE user = '$user' AND password = '$pass';";
    $result = mysqli_query($GLOBALS["___mysqli_ston"],  $query ) or die( '
' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '
'
); if( $result && mysqli_num_rows( $result ) == 1 ) { // Get users details $row = mysqli_fetch_assoc( $result ); $avatar = $row["avatar"]; // Login successful echo "

Welcome to the password protected area {$user}

"
; echo "{$avatar}\" />"
; } else { // Login failed sleep( 2 ); echo "

Username and/or password incorrect.
"
; } ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res); } ?>

漏洞利用:(既然对 手动输入的user进行字符转义和password的加密,普通注入难度较大)但仍可通过抓包对password进行枚举爆破

(3)High等级

加了令牌验证身份,对user和password都进行了转义,对pass加密

Brute Force Source
vulnerabilities/brute/source/high.php
<?php

if( isset( $_GET[ 'Login' ] ) ) {
    // Check Anti-CSRF token  //Token,可以抵御CSRF攻击,同时也增加了爆破的难度
    checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );

    // Sanitise username input
    $user = $_GET[ 'username' ];
    $user = stripslashes( $user );  //去除字符串中的反斜线字符,如果有两个连续的反斜线,则只去掉一个
    $user = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $user ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));

    // Sanitise password input
    $pass = $_GET[ 'password' ];
    $pass = stripslashes( $pass );
    $pass = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $pass ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));    //mysql_real_escape_string()函数进行转义
    $pass = md5( $pass );

    // Check database
    $query  = "SELECT * FROM `users` WHERE user = '$user' AND password = '$pass';";
    $result = mysqli_query($GLOBALS["___mysqli_ston"],  $query ) or die( '
' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '
'
); if( $result && mysqli_num_rows( $result ) == 1 ) { // Get users details $row = mysqli_fetch_assoc( $result ); $avatar = $row["avatar"]; // Login successful echo "

Welcome to the password protected area {$user}

"
; echo "{$avatar}\" />"; } else { // Login failed sleep( rand( 0, 3 ) ); echo "

Username and/or password incorrect.
"
; } ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res); } // Generate Anti-CSRF token //抵御CSRF的攻击,参考下面的token generateSessionToken(); ?>

每次请求行参数 携带user_token,一般token生成会加入时间,所以每次访问的令牌是不同的。(token不仅可以低于CSRF攻击,还可防止爆破)

漏洞利用: 既然每次的token不一样,那就需要知道每次的token

burp利用,

在攻击模块:选择攻击方式为pitchfork,为password和user_token设置负载标志;
设置第一个password负载集导入字段,第二个token设置recursive grep负载类型;

将拦截的请求报文token值复制到,Payload setting[recursive grep]的初始请求初始化负载中

找到Grep-Extract模块,add, fetch-response(提取响应报文)按钮搜索  token,分别填写搜索到的token的值的 前后标签和符号如(value=')('/>)点击OK

找到Redirections模块,选择always重定向

开始,点击不一样的,然后就可以在响应看到渲染render显示

(4)Impossible

防御原理:token, 转义,加密,限制登录次数/时间

PDO(PHP Data Object)防御sql注入,同时 采用,登录次数过多,会锁定用户:

Brute Force Source
vulnerabilities/brute/source/impossible.php
<?php

if( isset( $_POST[ 'Login' ] ) && isset ($_POST['username']) && isset ($_POST['password']) ) {
    // Check Anti-CSRF token
    checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );

    // Sanitise username input
    $user = $_POST[ 'username' ];
    $user = stripslashes( $user );
    $user = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $user ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));     //mysqli_real_escape_string()函数

    // Sanitise password input
    $pass = $_POST[ 'password' ];
    $pass = stripslashes( $pass );  // 去掉反斜线字符
    $pass = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $pass ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));    //mysqli_real_escape_string()函数
    $pass = md5( $pass );

    // Default values
    $total_failed_login = 3;
    $lockout_time       = 15;
    $account_locked     = false;

    // Check the database (Check user information)
    $data = $db->prepare( 'SELECT failed_login, last_login FROM users WHERE user = (:user) LIMIT 1;' );
    $data->bindParam( ':user', $user, PDO::PARAM_STR );
    $data->execute();
    $row = $data->fetch();

    // Check to see if the user has been locked out.
    if( ( $data->rowCount() == 1 ) && ( $row[ 'failed_login' ] >= $total_failed_login ) )  {  //失败3次,
        // User locked out.  Note, using this method would allow for user enumeration!
        //echo "

This account has been locked due to too many incorrect logins.
";
// Calculate when the user would be allowed to login again 计算再登录时间 $last_login = strtotime( $row[ 'last_login' ] ); $timeout = $last_login + ($lockout_time * 60); $timenow = time(); /* print "The last login was: " . date ("h:i:s", $last_login) . "
"; print "The timenow is: " . date ("h:i:s", $timenow) . "
"; print "The timeout is: " . date ("h:i:s", $timeout) . "
"; */
// Check to see if enough time has passed, if it hasn't locked the account if( $timenow < $timeout ) { $account_locked = true; // print "The account is locked
";
} } // Check the database (if username matches the password) $data = $db->prepare( 'SELECT * FROM users WHERE user = (:user) AND password = (:password) LIMIT 1;' ); $data->bindParam( ':user', $user, PDO::PARAM_STR); $data->bindParam( ':password', $pass, PDO::PARAM_STR ); $data->execute(); $row = $data->fetch(); // If its a valid login... if( ( $data->rowCount() == 1 ) && ( $account_locked == false ) ) { // Get users details $avatar = $row[ 'avatar' ]; $failed_login = $row[ 'failed_login' ]; $last_login = $row[ 'last_login' ]; // Login successful echo "

Welcome to the password protected area {$user}

"
; echo "{$avatar}\" />"; // Had the account been locked out since last login? if( $failed_login >= $total_failed_login ) { echo "

Warning: Someone might of been brute forcing your account.

"
; echo "

Number of login attempts: {$failed_login}.
Last login attempt was at: {$last_login}.

"
; } // Reset bad login count $data = $db->prepare( 'UPDATE users SET failed_login = "0" WHERE user = (:user) LIMIT 1;' ); $data->bindParam( ':user', $user, PDO::PARAM_STR ); $data->execute(); } else { // Login failed sleep( rand( 2, 4 ) ); // Give the user some feedback echo "

Username and/or password incorrect.

Alternative, the account has been locked because of too many failed logins.
If this is the case, please try again in {$lockout_time} minutes.
"
; // Update bad login count $data = $db->prepare( 'UPDATE users SET failed_login = (failed_login + 1) WHERE user = (:user) LIMIT 1;' ); $data->bindParam( ':user', $user, PDO::PARAM_STR ); $data->execute(); } // Set the last login time $data = $db->prepare( 'UPDATE users SET last_login = now() WHERE user = (:user) LIMIT 1;' ); $data->bindParam( ':user', $user, PDO::PARAM_STR ); $data->execute(); } // Generate Anti-CSRF token generateSessionToken(); ?>

除了通过以上最高级别的代码防御,还可通过工具;

检验服务器是否处于被暴力破解的工具:

WFuzz, OWASP DirBuster and vulnerability scanners such as Nessus, Nikto, Acunetix

你可能感兴趣的:(DVWA,DVWA)