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/chadlas/p/15706226.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,然后才能重新尝试。
以上就是这个靶场中,个人的一些笔记整理,欢迎各位大佬互相交流学习,有不对的地方勿喷。