3 namespace Drupal\security_review\Checks;
5 use Drupal\Core\Logger\RfcLogLevel;
6 use Drupal\security_review\Check;
7 use Drupal\security_review\CheckResult;
10 * Checks for abundant failed logins.
12 class FailedLogins extends Check {
17 public function getNamespace() {
18 return 'Security Review';
24 public function getTitle() {
25 return 'Failed logins';
31 public function run() {
32 // If dblog is not enabled return with hidden INFO.
33 if (!$this->moduleHandler()->moduleExists('dblog')) {
34 return $this->createResult(CheckResult::INFO, [], FALSE);
37 $result = CheckResult::SUCCESS;
39 $last_result = $this->lastResult();
43 $query = $this->database()->select('watchdog', 'w');
52 $query->condition('type', 'user')
53 ->condition('severity', RfcLogLevel::NOTICE)
54 ->condition('message', 'Login attempt failed from %ip.');
55 if ($last_result instanceof CheckResult) {
56 // Only check entries that got recorded since the last run of the check.
57 $query->condition('timestamp', $last_result->time(), '>=');
61 $db_result = $query->execute();
63 // Count the number of failed logins per IP.
65 foreach ($db_result as $row) {
66 $ip = unserialize($row->variables)['%ip'];
67 $entry_for_ip = &$entries[$ip];
69 if (!isset($entry_for_ip)) {
75 // Filter the IPs with more than 10 failed logins.
76 if (!empty($entries)) {
77 foreach ($entries as $ip => $count) {
84 if (!empty($findings)) {
85 $result = CheckResult::FAIL;
89 return $this->createResult($result, $findings, $visible);
95 public function help() {
97 $paragraphs[] = $this->t('Failed login attempts from the same IP may be an artifact of a malicious user attempting to brute-force their way onto your site as an authenticated user to carry out nefarious deeds.');
100 '#theme' => 'check_help',
101 '#title' => $this->t('Abundant failed logins from the same IP'),
102 '#paragraphs' => $paragraphs,
109 public function evaluate(CheckResult $result) {
110 $findings = $result->findings();
111 if (empty($findings)) {
116 $paragraphs[] = $this->t('The following IPs were observed with an abundance of failed login attempts.');
119 '#theme' => 'check_evaluation',
120 '#paragraphs' => $paragraphs,
121 '#items' => $result->findings(),
128 public function evaluatePlain(CheckResult $result) {
129 $findings = $result->findings();
130 if (empty($findings)) {
134 $output = $this->t('Suspicious IP addresses:') . ":\n";
135 foreach ($findings as $ip) {
136 $output .= "\t" . $ip . "\n";
145 public function getMessage($result_const) {
146 switch ($result_const) {
147 case CheckResult::FAIL:
148 return $this->t('Failed login attempts from the same IP. These may be a brute-force attack to gain access to your site.');
151 return $this->t('Unexpected result.');