DVWA(二): Brute Force(全等级暴力破解)
tags:
DVWA Brute Force
Burp Suite
Firefox
windows2003
暴力破解基本利用密码字典使用穷举法对于所有的账号密码组合全排列猜解出正确的组合。
LEVEL: Low
1.配置firefox代理服务器:
我这里用的是本机ip,也可以用127.0.0.1代理。(这里不使用代理那部分我写上了本机IP,设置127.0.0.1的时候那里也会自动出现localhost和127.0.0.1,如果这个框里是空的就登陆不上DVWA,如果不是空的用burp suite抓包的时候抓不到网页上的包。很奇怪)
2.登录DVWA 设置level等级为low,点击submit。
3.设置burpsuite,进入options选项卡,选中那一栏IP点击edit,调整需要拦截的代理服务器IP和端口。点击OK
4.进入intercept选项卡点击intercept is on打开拦截
5.返回DVWA界面的Brute Force模块,随便输入一个账号和密码然后返回burp suite界面查看是否已经拦截到代理服务器的数据包。(如果没抓到包尝试把不使用代理的IP去掉)
6.成功抓到包后,右键空白部分Send to Intruder发送到intruder暴力破解模块(或者ctrl+i)
7.进入Intruder选项卡下的Positions模块先clear掉所有变量,再把username和password后的变量add进去。(注意选中的顺序,比如我先选的username后的值,第二个选的password后的值,软件会自己标记你选择的变量的顺序)
关于attack type中有4个选项,(Sniper为对变量依次进行破解、Battering Ram为对变量同时进行破解、Pitch fork为每个变量将会对应一个字典,Cluster bomb为每个变量将会对应一个字典,并且交集破解,尝试每一个组合)。我选择的是第四项 ,图里没有截到
8.进入Payloads选项卡,如下图payload set①代表第1个变量也就是我们上面选择的username变量,点击load...加载用户名文件,之后在payload set模块选择2变量,同理load...加载密码字典
9.点击start attack进行爆破
找到length中最长的值,可以看到因为代码中返回了welcome to the password protected area admin这句话,而别的组合只是返回了 username and/or password incorrect 所以造成了响应时间length不一样。
9.返回DVWA验证
下面附上low等级的Brute Force的php代码:
<?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 ) == 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 echo "<pre><br />Username and/or password incorrect.</pre>"; } ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res); } ?>
其中 isset函数是检测变量是否设置。这里存在明显的sql注入漏洞,可以使用SQL注入绕过的方法进行破解。(这里先空着,不定期更新)
所以用户名可以用 admin'# 或者 admin'or'1'='1 密码随意也可登录
(二)LEVEL Medium Brute Force
附上Medium的php代码
<?php 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)) ? "" : "")); // 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 ); // 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 sleep( 2 ); echo "<pre><br />Username and/or password incorrect.</pre>"; } ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res); } ?>
''相比Low级别的代码,Medium级别的代码主要增加了mysql_real_escape_string函数,这个函数会对字符串中的特殊符号(x00,n,r,,’,”,x1a)进行转义,基本上能够抵御sql注入攻击,说基本上是因为查到说 MySQL5.5.37以下版本如果设置编码为GBK,能够构造编码绕过mysql_real_escape_string 对单引号的转义(因实验环境的MySQL版本较新,所以并未做相应验证);同时,$pass做了MD5校验,杜绝了通过参数password进行sql注入的可能性。但是,依然没有加入有效的防爆破机制(sleep(2)实在算不上)。''
但是依然可以用暴力破解的方式,步骤与上面完全一样,所以不再赘述。
(三)LEVEL High Brute Force
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)) ? "" : ""));
// 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>欢迎使用密码保护区 <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>警告</em>: 有人可能暴力破解你的帐户.</p>";
echo "<p>登录尝试次数: <em>{$failed_login}</em>.<br />上次登录尝试时间: <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 />用户名或密码不正确.<br /><br/>或者,由于登录失败太多,帐户已被锁定.<br />如果是这样的话, <em>请在 {$lockout_time} 分钟后尝试</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();
?>
代码解析:下面给个传送门,大家可以一起学习!
(关于High等级的代码解释不定期更新)
通过抓包可以看到登录时需要4个参数:username password login user_token
将DVWA调至high级别,发现用之前的暴力破解就不好使了,因为其使用了随机token机制来防止CSRF,从而在一定程度上防止了重放攻击,增加了爆破难度。但是依然可以使用burpsuite来爆破。
这里我们还是先使用burp suite:
设置 password user_token为参数,使用Pitchfork模式。
设置参数,在option选项卡中将攻击线程thread设置为1,因为Recursive_Grep模式不支持多线程攻击,然后选择Grep-Extract,意思是用于提取响应消息中的有用信息,点击Add,如下图进行设置,最后将Redirections设置为Always
设置payloads,第一个参数(password)不再赘述,主要讲一下第二个,
start attack