egcservices/rbruteforce2

CakePHP 4+ 防止暴力破解攻击插件

安装: 189

依赖项: 0

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 14

开放问题: 0

类型:cakephp-plugin

3.2 2022-04-14 21:38 UTC

This package is auto-updated.

Last update: 2024-09-15 03:13:51 UTC


README

CakePHP 4+ 防止暴力破解攻击插件

CakePHP rBruteForce 插件

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

要求

  • CakePHP 4.8 或更高版本。
  • PHP 7.2 或更高版本。

安装

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 中找到。

//CreateRBruteForces Migration
public function change()
{
	$table = $this->table('rbruteforces', ['id' => false, 'primary_key' => ['expire']]);
	$table
		->addColumn('ip', 'string', ['length' => 255])
		->addColumn('url', 'string', ['length' => 255])
		->addColumn('expire', 'timestamp', ['default' => null])
		->addIndex('ip');
	$table->create();
}
  
//CreateRBruteForceLogs Migration
public function change()
{
	$table = $this->table('rbruteforcelogs');
	$table->addColumn('data', 'text', ['null' => true]);
	$table->create();
	$table->changeColumn('id', 'integer', ['signed' => false, 'identity' => true]);
	$table->update();
}

通过 composer 安装。

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

{
  "require": {
    "egcservices/rbruteforce2": "3.0"
  }
}

加载插件

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

.gitignore

由于此插件在其自己的 composer.json 中设置了类型 cakephp-plugin,composer 会知道在您的 /Plugin 目录中安装它,而不是在常规的 vendors 文件中。建议您将 /Plugin/RBruteForce 添加到 .gitignore 文件中。

报告问题

如果您对 rBruteForce 有问题,请在此处报告 here

文档

rBruteForce 在登录失败或任何其他方法上都会封禁 IP 地址。

用法

由于此插件是一个组件,您应该将其添加到您的 Controller's $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地址被禁止,并且在用户认证之前进行检查,即使用户名和密码有效,插件也不会让用户进入。

要提前取消禁止,您应该浏览到/r_brute_force/rbruteforces并手动删除禁止。或者,您可以等待禁止到期。

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

警告

这不是防火墙!如果您使用此插件,您仍然可能受到暴力攻击。涉及代理的慢速攻击很难检测。如果您想再次保护自己,您应该编写自己的保护方法,例如在尝试几次之后限制用户账户,或者请求额外的登录数据,如安全问题,或白名单管理员登录的IP地址,或其他想法。同时,您可以在服务器防火墙上禁止顶级尝试来源。此信息可在/r_brute_force/rbruteforces找到。请注意,不要禁止合法用户使用的代理。