如何限制PHP中的用户登录尝试

Jas*_*vis 54 security throttling honeypot

我刚刚阅读这篇文章关于防止快速登录尝试的基于表单的网站身份验证的权威指南.

最佳实践#1:短暂的时间延迟随着尝试失败的次数而增加,例如:

1次尝试失败=无延迟
2次尝试失败= 2秒延迟
3次尝试失败= 4秒延迟
4次尝试失败= 8秒延迟
5次尝试失败= 16秒延迟
等

DoS攻击这个方案将是非常不切实际的,但另一方面,可能具有破坏性,因为延迟会呈指数级增长.

我很好奇我如何在PHP中为我的登录系统实现这样的东西?

Cor*_*lou 76

您不能通过将限制链接到单个IP或用户名来简单地防止DoS攻击.好吧,你甚至不能真正阻止使用这种方法的快速登录尝试.

为什么? 因为攻击可以跨越多个IP和用户帐户,以绕过您的限制尝试.

我见过发布在其他地方,理想情况下,您应该跟踪网站上所有失败的登录尝试并将它们与时间戳相关联,或许:

CREATE TABLE failed_logins (
    id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(16) NOT NULL,
    ip_address INT(11) UNSIGNED NOT NULL,
    attempted DATETIME NOT NULL,
    INDEX `attempted_idx` (`attempted`)
) engine=InnoDB charset=UTF8;
Run Code Online (Sandbox Code Playgroud)

关于ip_address字段的快速说明:您可以使用INET_ATON()和INET_NTOA()分别存储数据和检索数据,这基本上等同于将ip地址转换为无符号整数.

# example of insertion
INSERT INTO failed_logins SET username = 'example', ip_address = INET_ATON('192.168.0.1'), attempted = CURRENT_TIMESTAMP;
# example of selection
SELECT id, username, INET_NTOA(ip_address) AS ip_address, attempted;
Run Code Online (Sandbox Code Playgroud)

根据给定时间内的失败登录总数(本例中为15分钟)确定某些延迟阈值.您应该根据从failed_logins表中提取的统计数据,因为它将根据用户数量以及有多少人可以调用(和键入)他们的密码而随时间变化.


> 10 failed attempts = 1 second
> 20 failed attempts = 2 seconds
> 30 failed attempts = reCaptcha
Run Code Online (Sandbox Code Playgroud)

在每次失败的登录尝试时查询表,以查找给定时间段(例如15分钟)的失败登录次数:


SELECT COUNT(1) AS failed FROM failed_logins WHERE attempted > DATE_SUB(NOW(), INTERVAL 15 minute);
Run Code Online (Sandbox Code Playgroud)

如果超过给定时间段的尝试次数超出限制,则强制限制或强制所有用户使用验证码(即reCaptcha),直到在给定时间段内失败尝试次数小于阈值.

// array of throttling
$throttle = array(10 => 1, 20 => 2, 30 => 'recaptcha');

// retrieve the latest failed login attempts
$sql = 'SELECT MAX(attempted) AS attempted FROM failed_logins';
$result = mysql_query($sql);
if (mysql_affected_rows($result) > 0) {
    $row = mysql_fetch_assoc($result);

    $latest_attempt = (int) date('U', strtotime($row['attempted']));

    // get the number of failed attempts
    $sql = 'SELECT COUNT(1) AS failed FROM failed_logins WHERE attempted > DATE_SUB(NOW(), INTERVAL 15 minute)';
    $result = mysql_query($sql);
    if (mysql_affected_rows($result) > 0) {
        // get the returned row
        $row = mysql_fetch_assoc($result);
        $failed_attempts = (int) $row['failed'];

        // assume the number of failed attempts was stored in $failed_attempts
        krsort($throttle);
        foreach ($throttle as $attempts => $delay) {
            if ($failed_attempts > $attempts) {
                // we need to throttle based on delay
                if (is_numeric($delay)) {
                    $remaining_delay = time() - $latest_attempt - $delay;
                    // output remaining delay
                    echo 'You must wait ' . $remaining_delay . ' seconds before your next login attempt';
                } else {
                    // code to display recaptcha on login form goes here
                }
                break;
            }
        }        
    }
}
Run Code Online (Sandbox Code Playgroud)

在特定阈值下使用reCaptcha将确保停止来自多个前端的攻击,并且正常站点用户不会遇到合法失败登录尝试的显着延迟.

  • 我根据你在这个评论中提出的概念创建了一个类,并为想要使用它的人提供了一些名为BruteForceBlocker的改进https://github.com/ejfrancis/BruteForceBlocker (2认同)

Mar*_*iot 5

您有三种基本方法:存储会话信息,存储cookie信息或存储IP信息.

如果您使用会话信息,最终用户(攻击者)可以强制调用新会话,绕过您的策略,然后再次登录,不会有任何延迟.会话实现起来非常简单,只需将用户的最后已知登录时间存储在会话变量中,将其与当前时间匹配,并确保延迟足够长.

如果您使用cookie,攻击者可以简单地拒绝cookie,总而言之,这确实不可行.

如果您跟踪IP地址,则需要以某种方式存储来自IP地址的登录尝试,最好是在数据库中.当用户尝试登录时,只需更新记录的IP列表即可.您应该以合理的间隔清除此表,转储在一段时间内未处于活动状态的IP地址.陷阱(总是存在缺陷)是,某些用户可能最终共享IP地址,并且在边界条件下,您的延迟可能会无意中影响用户.由于您正在跟踪登录失败,并且只登录失败,因此不会造成太大的痛苦.

  • @symcbean我已经解决了多个解决方案,其中任何组合都会阻止一些攻击者,没有神奇的解决方案.正如我在答案中所讨论的那样,知识产权地址的共享不是一个问题.有人可能会使用TOR改变它似乎不如强迫新会话的人.我错过了第四种选择吗? (2认同)
  • @symcbean 如果使用 TOR,通常会通过涉及多个层的开销进行隐式节流。根据其设计,TOR 必然会使暴力攻击的情况变得复杂。 (2认同)