ashanet/rbruteforce

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

安装: 1

依赖关系: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 14

开放问题: 0

类型:cakephp-plugin

1.0.5 2019-02-28 09:44 UTC

This package is auto-updated.

Last update: 2024-09-08 17:41:20 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 ashanet/rbruteforce

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

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

加载插件

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

.gitignore

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

报告问题

如果您在使用 rBruteForce 时遇到问题,请在此处报告:here

文档

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')) {
		$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中查看。请小心,不要封禁合法用户使用的代理。