ali1/brute-force-shield

一款实现强大、易于使用的暴力破解防护工具。

1.0.3 2020-02-22 21:29 UTC

This package is auto-updated.

Last update: 2024-09-16 02:45:44 UTC


README

功能

  • 适用于基于IP地址的保护,但也可用于其他唯一标识符
  • 不计算具有相同挑战详情的重新尝试(例如,如果用户尝试相同的用户名/密码组合几次)
  • 设计用于与任何数据存储(文件、mysql、缓存库)和任何框架一起使用
  • 可以比正常限制更早地阻止同一用户名的多次尝试(以给用户一个机会输入正确的用户名,如果他们一直在尝试错误的用户名)

要求

  • Composer
  • PHP 7.2或更高版本
  • 任何数据存储,用于在请求之间持久化用户历史数据

安装

composer require ali1/brute-force-shield

配置

在应用保护时,可以提供一个 Ali1\BruteForceShield\Configuration 对象。

    $configuration = new Configuration();
    $configuration->setTimeWindow(600) 
        ->setTotalAttemptsLimit(10);

使用方法

由于您需要自己的架构来存储数据和记录被阻止的事件,建议创建一个方法、函数、组件或中间件来使用此库。

查看 此 CakePHP 组件 作为您可以使用的一个辅助方法的示例。

但是,如果您想直接使用它,以下是一个示例

    public function login()
    {
        // prior to actually verifying data
        $bruteConfig = new Configuration();
        $bruteConfig->setTotalAttemptsLimit(10)
            ->setStricterLimitOnKey('username', 7)
            ->addUnencryptedKey('username');

        $cacheKey = 'BruteforceData.login.' . str_replace(':', '.', $_SERVER['REMOTE_ADDR']);
        $userDataRaw = Cache::read($cacheKey); // replace with your way of retrieving stored user data

        $shield = new BruteForceShield();
        $userData = $userDataRaw ? json_decode($userDataRaw, true) : null;
        $userData = $shield->validate($userData, $_POST, $bruteConfig);

        Cache::write($cacheKey, json_encode($userData)); // replace with your way of storing user data

        if (!$shield->isValidated()) {
            Log::alert(
                "Bruteforce blocked\nIP: {$this->getController()->getRequest()->getEnv('REMOTE_ADDR')}\n",
                json_encode($userData)
            ); // replace with your own method of logging

            throw new TooManyAttemptsException(); // replace with your way of error handling and stopping execution
        }

        // now you can process the login attempt in the normal way
    }