1. 程式人生 > 其它 >DVWA靶場通關----(1) Brute Force教程

DVWA靶場通關----(1) Brute Force教程

Brute Force(暴力破解)

Brute Force(暴力破解),就是指黑客利用黑客字典,通過窮舉法猜測使用者口令。

Brute Force主題:

Low

原始碼解析

<?php
//檢查變數是否設定(先看有沒有Login引數)
if( isset( $_GET[ 'Login' ] ) ) {
//獲取使用者
    $user = $_GET[ 'username' ];
//獲取密碼
    $pass = $_GET[ 'password' ];
//將密碼使用md5加密
    $pass = md5( $pass );
//構建SQL語句,查詢結果儲存在query變數中
    $query  = "SELECT * FROM `users` WHERE user = '$user' AND password = '$pass';";
//資料庫查詢,將查詢結果儲存在result變數中,查到了,儲存使用者具體資訊;未查到,就在頁面上輸入錯誤結果,result為空
    $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 ) == 1 ) {
//查詢結果關聯資料row,row已經變成鍵值對
        $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);
}

?>

漏洞復現

(1)先隨便輸入一個使用者名稱和密碼,使用burp抓包

在這裡我們可以看到是使用get獲取引數,且是明文傳輸,沒有驗證碼類的驗證機制,所以可以使用爆破。

(2)抓到包後,右鍵點選Send to Intruder,將抓到的包轉到Intruder

將抓到的包傳送到Intruder時,會存在很多變數,在頁面右側可以將他們全部清理掉,然後在存在爆破點的地方再新增上我們想要他存在的變數。

選擇合適的攻擊型別,在這裡我選擇的是第四個:Cluster bomb

(關於四個攻擊型別的簡介,有想要了解的朋友可以看我另一篇帖子:https://www.cnblogs.com/cc-qy/p/15693286.html)

(3)成功

在上面的Cluster bomb中的截圖中我們可以看到,成功到找到了使用者的賬號密碼,像這種正確的賬戶密碼,我們可以通過後面的Length進行判斷,一般正確的都是唯一的

Medium

原始碼解析

<?php
//是否存在Login變數(標籤裡面的name),檢查是否存在Login按鈕
if( isset( $_GET[ 'Login' ] ) ) {
    // Sanitise username input
//獲取使用者名稱,存入user變數裡
    $user = $_GET[ 'username' ];
//user中x00,n,r,,’,”,x1a轉義,防SQL注入
    $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中x00,n,r,,’,”,x1a轉義,防SQL注入
    $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 );

    // 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 ) == 1 ) {
        // 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,失敗後會延時2s
        sleep( 2 );
        echo "<pre><br />Username and/or password incorrect.</pre>";
    }

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

?>

漏洞復現

  關於Medium難度,它的原始碼中對於登入這一方面並沒有做太多的改動,因此它的爆破過程與Low難度的過程基本一樣,想要嘗試的朋友可以參考Low難度中的爆破過程,區別是每一次登入失敗後會延時2s,別的就沒有什麼改動了,總體過程影響不大。

  另外在這個Medium難度的原始碼中,它對防止注入做了很多的工作,像比如說,它使用了mysql_real_escape_string 函式,這個函式可以對字串中的函式進行轉義,一定程度上可以防止sql注入,到這個地方會不會想到在Low難度中是否可以通過注入來繞過,可以去試一下。(我在這個地方用了萬能密碼發現是可以的,在Username處:admin' or '1'='1,在Password處隨便填點東西,像123這樣的都行,這樣就可以成功登陸了)

High

原始碼解析

<?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)) ? "" : ""));

    // 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)) ? "" : ""));
    $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 ) )  {
        // User locked out.  Note, using this method would alLow for user enumeration!
        //echo "<pre><br />This account has been locked due to too many incorrect logins.</pre>";

        // 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) . "<br />";
        print "The timenow is: " . date ("h:i:s", $timenow) . "<br />";
        print "The timeout is: " . date ("h:i:s", $timeout) . "<br />";
        */

        // 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<br />";
        }
    }

    // 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 "<p>Welcome to the password protected area <em>{$user}</em></p>";
        echo "<img src=\"{$avatar}\" />";

        // Had the account been locked out since last login?
        if( $failed_login >= $total_failed_login ) {
            echo "<p><em>Warning</em>: Someone might of been brute forcing your account.</p>";
            echo "<p>Number of login attempts: <em>{$failed_login}</em>.<br />Last login attempt was at: <em>${last_login}</em>.</p>";
        }

        // 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 "<pre><br />Username and/or password incorrect.<br /><br/>Alternative, the account has been locked because of too many failed logins.<br />If this is the case, <em>please try again in {$lockout_time} minutes</em>.</pre>";

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

?>

漏洞復現

  在這個難度中,首先從程式碼中可以發現是加了token驗證的。針對這種帶有token驗證的,我們也可以使用Burp去爆破,接下來是具體的一些步驟:

(1)經典抓包,在這個頁面中很容易看到了token值

(2)在Intruder中,攻擊型別選擇Pitchfork,後面的變數一個是password,另一個是token值

(3)關於第一個變數password的字典,就不在詳細說了,詳細說一下變數token值得處理

設定執行緒數為1

點了ok按鈕後會在那個ADD按鈕後面新增生成的那個正則表示式,不用管,新增上就行,沒新增上的多試幾次(在這裡有一個點是剛開始抓的包不要釋放,否則這個地方獲取不到程式碼)

在這個地方注意選擇Always

(3)看效果

Impossible

原始碼分析

// 檢驗 token
    checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
 
    // 過濾 username 和 password
    $user = $_POST[ 'username' ];
    $user = stripslashes( $user );
    $user = mysql_real_escape_string( $user );
    $pass = $_POST[ 'password' ];
    $pass = stripslashes( $pass );
    $pass = mysql_real_escape_string( $pass );
    $pass = md5( $pass );
 
    // 失敗登入次數 3 鎖定時間單位 15 賬戶鎖定
    $total_failed_login = 3;
    $lockout_time       = 15;
    $account_locked     = false;
 
    // 驗證使用者名稱和密碼
    $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();
 
    // 檢查使用者是否已被鎖定.
    if( ( $data->rowCount() == 1 ) && ( $row[ 'failed_login' ] >= $total_failed_login ) )  {
 
        // 登入失敗超過 3 次 15 分鐘再嘗試
        $last_login = $row[ 'last_login' ];
        $last_login = strtotime( $last_login );
        $timeout    = strtotime( "{$last_login} +{$lockout_time} minutes" );
        $timenow    = strtotime( "now" );
 
        // 檢查是否已經過了足夠的時間,是否沒有鎖定帳戶
        if( $timenow > $timeout )
            $account_locked = true;
    }
 
    // 檢驗使用者名稱和密碼
    $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( ( $data->rowCount() == 1 ) && ( $account_locked == false ) ) {
        // 獲取使用者頭像、登入測試、和最近登入
        $avatar       = $row[ 'avatar' ];
        $failed_login = $row[ 'failed_login' ];
        $last_login   = $row[ 'last_login' ];
 
        // 輸出登入成功資訊
        echo "<p>Welcome to the password protected area <em>{$user}</em></p>";
        echo "<img src=\"{$avatar}\" />";
 
        // 自上次登入後帳戶是否已被鎖定?
        if( $failed_login >= $total_failed_login ) {
            echo "<p><em>Warning</em>: Someone might of been brute forcing your account.</p>";
            echo "<p>Number of login attempts: <em>{$failed_login}</em>.<br />Last login attempt was at: <em>${last_login}</em>.</p>";
        }
 
        // 重置登入失敗次數
        $data = $db->prepare( 'UPDATE users SET failed_login = "0" WHERE user = (:user) LIMIT 1;' );
        $data->bindParam( ':user', $user, PDO::PARAM_STR );
        $data->execute();
    }
    else {
        // 登入失敗隨機延時並輸出返回資訊
        sleep( rand( 2, 4 ) );
        echo "<pre><br />Username and/or password incorrect.<br /><br/>Alternative, the account has been locked because of too many failed logins.<br />If this is the case, <em>please try again in {$lockout_time} minutes</em>.</pre>";
 
        // 更新登入失敗數
        $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();
    }
 
    // 設定最後的登入時間
    $data = $db->prepare( 'UPDATE users SET last_login = now() WHERE user = (:user) LIMIT 1;' );
    $data->bindParam( ':user', $user, PDO::PARAM_STR );
    $data->execute();

  這個難度是最高的難度,其中的改變都有GET提交方式改為POST提交方式,同樣加了token校驗機制,還有就是他限制的登入的次數,如果登入失敗3次,賬戶就會被鎖定,需要等待15,然後才能重新嘗試。

  以上就是這個靶場中,個人的一些筆記整理,歡迎各位大佬互相交流學習,有不對的地方勿噴。