rrd/rbruteforce

此包已被废弃且不再维护。作者建议使用 middlewares/honeypot 包。

CakePHP 3 防止暴力破解攻击插件

安装次数: 40 180

依赖项: 0

建议者: 0

安全性: 0

星标: 6

关注者: 3

分支: 14

开放问题: 2

类型:cakephp-plugin

0.0.3 2021-07-10 08:09 UTC

README

CakePHP 3 防止暴力破解攻击插件

CakePHP rBruteForce 插件

使用 rBruteForce 可以保护您的 CakePHP 应用程序免受暴力破解攻击。

要求

  • CakePHP 3.0.0 或更高版本。
  • PHP 5.4.16 或更高版本。

安装

1. 创建数据库表。

模式可以在 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 中找到。

通过 composer 安装。

composer require rrd/rbruteforce

将插件添加到项目的 composer.json - 例如:

{
  "require": {
    "rrd/rbruteforce": "*"
  }
}

加载插件

Plugin::load('RBruteForce', ['bootstrap' => false, 'routes' => true]);

.gitignore

因为此插件在其自身的 composer.json 中设置了类型 cakephp-plugin,所以 composer 知道将其安装到您的 /Plugin 目录中,而不是通常的 vendors 文件中。建议您将 /Plugin/RBruteForce 添加到 .gitignore 文件中。

报告问题

如果您有关于 rBruteForce 的问题,请在此 报告。

文档

rBruteForce 会禁止在登录失败或其他任何方法上提交的 IP 地址。

用法

由于此插件是一个组件,您应该将其添加到 Controller$components 数组中。

class UsersController extends AppController {
	
	public $components = ['RBruteForce.RBruteForce'];

让我们看看 UsersControllerlogin 方法使用 rBruteForce 的示例:

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')) {
		$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());
			}
			$this->RBruteForce->check($this->_options); //unsuccessful logins will be checked
			$this->Flash->error(__('Invalid username or password, try again'));
		} else {
			$this->Flash->error(__("Please, wait {$this->_options['expire']} to try login again!'));	
		}
	} else {
		if ($this->RBruteForce->isIpBanned($this->_options)) {
			$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

有两个有效值; allbeforeBan

如果您选择 all,则所有尝试都将记录到插件数据库中。如果您选择 beforeBan,则只有被禁止之前的尝试会被记录。

checkUrl

插件是否应将URL包含在暴力破解检查中。

如果设置为 false,并且有人尝试在 /users/login/admin/users/login 登录,则插件会认为它们是相同的URL。如果设置为 true,则插件会认为上述两个是不同的尝试。

cleanupAttempts

在遭受暴力破解攻击时,您可能在几分钟内就有成千上万条记录在数据库中。如果您想限制存储的数据量,可以使用此选项。通常,您应该不必担心这个问题,直到您有不到一百万条记录。

它是如何工作的?

当用户(或自动攻击)向登录(或任何其他)功能发送数据时,CakePHP会调用您控制器中的相应方法。在这个方法中,您应该有

$this->RBruteForce->check();

此方法调用插件,并将记录每次尝试。它检查插件数据库中客户端的IP地址。如果在给定过期时间内有更多条目,则插件会禁止请求,记录尝试并重定向用户到失败的登录页面。自动攻击将将其视为成功的登录。

每次失败尝试后,插件将页面渲染延迟1秒。所以经过3次尝试后,渲染将延迟3秒。这会减慢自动攻击,同时只会给真实用户带来一点不便。

如果IP地址被禁止,并且您在用户身份验证之前 check,则插件不会让用户即使有有效的用户名和密码也能进入。

要提前移除禁止,您应该浏览到 /r_brute_force/rbruteforces 并手动删除禁止。或者,您只需等待禁止到期。

提交的数据条目可在 /r_brute_force/rbruteforcelogs 查找。

警告

这不仅仅是一个防火墙!如果您使用此插件,您仍然容易受到暴力破解攻击。涉及代理的慢速攻击很难检测。如果您想保护自己免受它们的影响,您应该编写自己的保护方法,如尝试几次后限制用户账户,或要求额外的登录数据,如安全问题,或允许管理员登录的IP地址白名单,或其他想法。同时,您可以在服务器防火墙上禁止顶级尝试源。此信息可在 /r_brute_force/rbruteforces 查找。小心不要禁止合法用户使用的代理。