slamkajs / rbruteforce3
CakePHP 3+ 防止暴力破解攻击插件
Requires
- cakephp/cakephp: 3.7.*
- cakephp/migrations: ^2.0.0
- cakephp/plugin-installer: ^1.0
Requires (Dev)
- phpunit/phpunit: ^5|^6
This package is auto-updated.
Last update: 2024-09-13 05:09:33 UTC
README
CakePHP 3+ 防止暴力破解攻击插件
CakePHP rBruteForce 插件
使用 rBruteForce 可以为您的 CakePHP 应用程序提供防止暴力破解攻击的保护。
需求
- CakePHP 3.8 或更高版本。
- PHP 5.6 或更高版本。
安装
1. 安装插件。
通过 composer 安装。
通过 CLI
composer require slamkajs/rbruteforce3
或将插件添加到项目的 composer.json
文件中 - 例如:
{ "require": { "slamkajs/rbruteforce3": "*" } }
2. 加载插件
bin/cake plugin load RBruteForce -r
或将以下内容添加到 src/Application.php
中。
Plugin::load('RBruteForce', ['bootstrap' => false, 'routes' => true]);
3. 建立数据库模式。
通过 CLI
bin/cake migrations migrate -p RBruteForce
模式文件位于 config/Schema/rBruteForce.sql
。
CREATE TABLE IF NOT EXISTS `rbruteforcelogs` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `data` text, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE IF NOT EXISTS `rbruteforces` ( `ip` varchar(255) NOT NULL, `url` varchar(255) NOT NULL, `expire` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (`expire`), KEY `ip` (`ip`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
迁移文件位于 config/Migrations
。
.gitignore
由于此插件在其自己的 composer.json 中设置了类型为 cakephp-plugin,因此 composer 会知道将其安装到您的 /Plugin 目录中,而不是常规的 vendors 文件中。建议您将 /Plugin/RBruteForce 添加到您的 .gitignore 文件中。
报告问题
如果您对 rBruteForce 有任何问题,请在此处报告 这里
文档
rBruteForce 在登录失败或任何其他方法上都会封禁 IP。
使用方法
由于此插件是一个组件,您应该将其添加到控制器 $components
数组中。
class UsersController extends AppController { public $components = ['RBruteForce.RBruteForce'];
让我们看看如何将 rBruteForce 添加到 UsersController
的 login
方法中。
public $_options; public $_ipsAllowed; public function initialize() { parent::initialize(); // TODO: Change the autogenerated stub $this->_options = [ 'maxAttempts' => 4, //max failed attempts before banning 'expire' => "10 minutes", //expiration time 'dataLog' => true, //log the user submitted data 'urlToRedirect' => '/users/reportBruteForce' //url to redirect if failed. ]; $this->_ipsAllowed = ['127.0.0.1', '172.68.26.185', '191.179.112.160']; } public function login() { if ($this->request->is('post')) { if ($this->RBruteForce->isIpBanned($this->_options)) { $this->Flash->error(__("Please, wait {$this->_options['expire']} to try login again!')); } $myIp = $_SERVER['REMOTE_ADDR']; if (!$this->RBruteForce->isIpBanned($this->_options) || in_array($myIp, $this->_ipsAllowed)) { $user = $this->Auth->identify(); if ($user) { $this->Auth->setUser($user); return $this->redirect($this->Auth->redirectUrl()); } $_is_banned = $this->RBruteForce->check($this->_options); //unsuccessful logins will be checked if(!$_is_banned) { $this->Flash->error(__('Invalid username or password, try again')); } } else { $this->Flash->error(__("Please, wait {$this->_options['expire']} to try login again!')); } } }
就这样了! :)
选项
您可以使用选项来更改默认行为。
$options = [ 'maxAttempts' => 4, //max failed attempts before banning 'expire' => '3 minutes', //expiration time 'dataLog' => false, //log the user submitted data 'attemptLog' => 'beforeBan', //all|beforeBan 'checkUrl' => true, //check url or not 'cleanupAttempts' => 1000, //delete all old entries from attempts database if there are more rows that this 'urlToRedirect' => '/r_brute_force/Rbruteforces/failed' //url to redirect if failed. ]; $this->RBruteForce->check($options);
如果您觉得默认值已经足够好,则无需包含选项。例如。
$this->RBruteForce->check( [ 'maxAttempts' => 3, 'attemptLog' => 'all' ] );
maxAttempts
用户在失败尝试达到此次数后将被封禁。通常 3-5 次应该足够。
expire
封禁将持续此时间。这应该是类似于以下内容:
- 20 秒
- 5 分钟
- 1 小时
- 2 天
- 3 周
- 1 个月
dataLog
如果此选项设置为 true
,则用户提交的数据将保存到插件数据库中。您可以在任何需要时分析这些数据。
attemptLog
有两个有效值;all
和 beforeBan
如果您选择 all
,则所有尝试都将记录到插件的数据库中。如果您选择 beforeBan
,则只有封禁之前的尝试将记录下来。
checkUrl
插件是否应将 URL 包含在暴力破解检查中。
如果设置为 false
,并且有人在 /users/login
和 /admin/users/login
上尝试登录,则插件会将其视为相同的 URL。如果设置为 true
,则插件会将这两个 URL 视为不同的尝试。
cleanupAttempts
当您遭受暴力破解攻击时,您可能会在几分钟内数据库中有数千条日志条目。如果您想限制存储的数据量,则可以使用此选项。通常,直到记录少于一百万条记录之前,您不需要担心此问题。
它是如何工作的?
当用户(或自动攻击)向登录(或任何其他)函数发送一些数据时,CakePHP 会调用您控制器中对应的方法。在这个方法中,您应该有
$this->RBruteForce->check();
此方法调用插件,并将记录每个尝试。它会检查插件数据库中客户端的 IP 地址。如果在给定过期时间内有多个条目,则插件会封禁请求、记录尝试并将用户重定向到失败登录页面。自动攻击会将其视为成功的登录。
在每次失败尝试中,插件都会额外延迟1秒钟的页面渲染。所以经过3次尝试后,渲染将被延迟3秒钟。这可以减缓自动化攻击,但也会给真实用户带来一点不便。
如果某个IP地址被禁止,且你在用户身份验证之前进行检查,即使用户名和密码有效,插件也不会允许用户登录。
要在禁令到期之前将其移除,您应该浏览到 /r_brute_force/rbruteforces
并手动删除禁令。或者,您只需等待禁令到期。
提交的数据条目可在 /r_brute_force/rbruteforcelogs
处找到。
警告
这不是防火墙!如果您使用此插件,您仍然可能受到暴力破解攻击。涉及代理的慢速攻击很难检测。如果您想防范这些攻击,您应该编写自己的保护方法,例如在尝试几次后限制用户账户,或者要求提供额外的登录数据,如安全提问,或者将管理员登录的IP地址列入白名单,或者有其他想法。同时,您可以在服务器防火墙上禁止最高尝试来源。此信息可在 /r_brute_force/rbruteforces
处找到。请注意,不要禁止合法用户使用的代理。