2016-07-20 19:36:15 +03:00
|
|
|
<?php
|
2020-03-19 15:30:15 +03:00
|
|
|
declare(strict_types=1);
|
2016-07-20 19:36:15 +03:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Mark Berezovsky <xpnf@yandex.ru>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-07-21 18:07:57 +03:00
|
|
|
*
|
2016-07-20 19:36:15 +03:00
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-07-20 19:36:15 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Security\Bruteforce;
|
|
|
|
|
2017-04-12 21:32:48 +03:00
|
|
|
use OC\Security\Normalizer\IpAddress;
|
2016-07-20 19:36:15 +03:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IDBConnection;
|
|
|
|
use OCP\ILogger;
|
2020-03-19 14:09:57 +03:00
|
|
|
use OCP\Security\Bruteforce\MaxDelayReached;
|
2016-07-20 19:36:15 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Throttler implements the bruteforce protection for security actions in
|
|
|
|
* Nextcloud.
|
|
|
|
*
|
|
|
|
* It is working by logging invalid login attempts to the database and slowing
|
|
|
|
* down all login attempts from the same subnet. The max delay is 30 seconds and
|
|
|
|
* the starting delay are 200 milliseconds. (after the first failed login)
|
|
|
|
*
|
|
|
|
* This is based on Paragonie's AirBrake for Airship CMS. You can find the original
|
|
|
|
* code at https://github.com/paragonie/airship/blob/7e5bad7e3c0fbbf324c11f963fd1f80e59762606/src/Engine/Security/AirBrake.php
|
|
|
|
*
|
|
|
|
* @package OC\Security\Bruteforce
|
|
|
|
*/
|
|
|
|
class Throttler {
|
2020-04-10 17:54:27 +03:00
|
|
|
public const LOGIN_ACTION = 'login';
|
2020-03-19 14:09:57 +03:00
|
|
|
public const MAX_DELAY = 25;
|
2020-07-09 13:16:52 +03:00
|
|
|
public const MAX_DELAY_MS = 25000; // in milliseconds
|
2020-03-19 15:31:07 +03:00
|
|
|
public const MAX_ATTEMPTS = 10;
|
2016-07-20 19:36:15 +03:00
|
|
|
|
|
|
|
/** @var IDBConnection */
|
|
|
|
private $db;
|
|
|
|
/** @var ITimeFactory */
|
|
|
|
private $timeFactory;
|
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IDBConnection $db
|
|
|
|
* @param ITimeFactory $timeFactory
|
|
|
|
* @param ILogger $logger
|
|
|
|
* @param IConfig $config
|
|
|
|
*/
|
|
|
|
public function __construct(IDBConnection $db,
|
|
|
|
ITimeFactory $timeFactory,
|
|
|
|
ILogger $logger,
|
|
|
|
IConfig $config) {
|
|
|
|
$this->db = $db;
|
|
|
|
$this->timeFactory = $timeFactory;
|
|
|
|
$this->logger = $logger;
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a number of seconds into the appropriate DateInterval
|
|
|
|
*
|
|
|
|
* @param int $expire
|
|
|
|
* @return \DateInterval
|
|
|
|
*/
|
2020-03-19 15:30:15 +03:00
|
|
|
private function getCutoff(int $expire): \DateInterval {
|
2016-07-20 19:36:15 +03:00
|
|
|
$d1 = new \DateTime();
|
|
|
|
$d2 = clone $d1;
|
|
|
|
$d2->sub(new \DateInterval('PT' . $expire . 'S'));
|
|
|
|
return $d2->diff($d1);
|
|
|
|
}
|
|
|
|
|
2020-03-17 19:06:52 +03:00
|
|
|
/**
|
|
|
|
* Calculate the cut off timestamp
|
|
|
|
*
|
2020-03-19 15:42:31 +03:00
|
|
|
* @param float $maxAgeHours
|
2020-03-17 19:06:52 +03:00
|
|
|
* @return int
|
|
|
|
*/
|
2020-06-03 09:33:05 +03:00
|
|
|
private function getCutoffTimestamp(float $maxAgeHours = 12.0): int {
|
2020-03-17 19:06:52 +03:00
|
|
|
return (new \DateTime())
|
2020-03-19 15:42:31 +03:00
|
|
|
->sub($this->getCutoff((int) ($maxAgeHours * 3600)))
|
2020-03-17 19:06:52 +03:00
|
|
|
->getTimestamp();
|
|
|
|
}
|
|
|
|
|
2016-07-20 19:36:15 +03:00
|
|
|
/**
|
|
|
|
* Register a failed attempt to bruteforce a security control
|
|
|
|
*
|
|
|
|
* @param string $action
|
|
|
|
* @param string $ip
|
|
|
|
* @param array $metadata Optional metadata logged to the database
|
2017-07-20 23:48:13 +03:00
|
|
|
* @suppress SqlInjectionChecker
|
2016-07-20 19:36:15 +03:00
|
|
|
*/
|
2020-03-19 15:30:15 +03:00
|
|
|
public function registerAttempt(string $action,
|
|
|
|
string $ip,
|
|
|
|
array $metadata = []): void {
|
2016-07-20 19:36:15 +03:00
|
|
|
// No need to log if the bruteforce protection is disabled
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($this->config->getSystemValue('auth.bruteforce.protection.enabled', true) === false) {
|
2016-07-20 19:36:15 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-12 21:32:48 +03:00
|
|
|
$ipAddress = new IpAddress($ip);
|
2016-07-20 19:36:15 +03:00
|
|
|
$values = [
|
|
|
|
'action' => $action,
|
|
|
|
'occurred' => $this->timeFactory->getTime(),
|
2017-04-12 21:32:48 +03:00
|
|
|
'ip' => (string)$ipAddress,
|
|
|
|
'subnet' => $ipAddress->getSubnet(),
|
2016-07-20 23:47:33 +03:00
|
|
|
'metadata' => json_encode($metadata),
|
2016-07-20 19:36:15 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
$this->logger->notice(
|
|
|
|
sprintf(
|
|
|
|
'Bruteforce attempt from "%s" detected for action "%s".',
|
|
|
|
$ip,
|
|
|
|
$action
|
|
|
|
),
|
|
|
|
[
|
|
|
|
'app' => 'core',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
$qb->insert('bruteforce_attempts');
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($values as $column => $value) {
|
2016-07-20 19:36:15 +03:00
|
|
|
$qb->setValue($column, $qb->createNamedParameter($value));
|
|
|
|
}
|
|
|
|
$qb->execute();
|
|
|
|
}
|
|
|
|
|
2016-11-14 16:05:01 +03:00
|
|
|
/**
|
|
|
|
* Check if the IP is whitelisted
|
|
|
|
*
|
|
|
|
* @param string $ip
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-03-19 15:30:15 +03:00
|
|
|
private function isIPWhitelisted(string $ip): bool {
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($this->config->getSystemValue('auth.bruteforce.protection.enabled', true) === false) {
|
2017-05-01 19:31:45 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-14 16:05:01 +03:00
|
|
|
$keys = $this->config->getAppKeys('bruteForce');
|
2020-04-09 14:53:40 +03:00
|
|
|
$keys = array_filter($keys, function ($key) {
|
2020-03-19 16:14:37 +03:00
|
|
|
return 0 === strpos($key, 'whitelist_');
|
2016-11-14 16:05:01 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
|
|
|
|
$type = 4;
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
|
2016-11-14 16:05:01 +03:00
|
|
|
$type = 6;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$ip = inet_pton($ip);
|
|
|
|
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
$cidr = $this->config->getAppValue('bruteForce', $key, null);
|
|
|
|
|
|
|
|
$cx = explode('/', $cidr);
|
|
|
|
$addr = $cx[0];
|
|
|
|
$mask = (int)$cx[1];
|
|
|
|
|
|
|
|
// Do not compare ipv4 to ipv6
|
|
|
|
if (($type === 4 && !filter_var($addr, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) ||
|
|
|
|
($type === 6 && !filter_var($addr, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$addr = inet_pton($addr);
|
|
|
|
|
|
|
|
$valid = true;
|
2020-04-10 15:19:56 +03:00
|
|
|
for ($i = 0; $i < $mask; $i++) {
|
2016-11-14 16:05:01 +03:00
|
|
|
$part = ord($addr[(int)($i/8)]);
|
|
|
|
$orig = ord($ip[(int)($i/8)]);
|
|
|
|
|
2019-02-12 01:22:20 +03:00
|
|
|
$bitmask = 1 << (7 - ($i % 8));
|
|
|
|
|
|
|
|
$part = $part & $bitmask;
|
|
|
|
$orig = $orig & $bitmask;
|
2016-11-14 16:05:01 +03:00
|
|
|
|
|
|
|
if ($part !== $orig) {
|
|
|
|
$valid = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($valid === true) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-20 19:36:15 +03:00
|
|
|
/**
|
|
|
|
* Get the throttling delay (in milliseconds)
|
|
|
|
*
|
|
|
|
* @param string $ip
|
2017-01-17 13:51:10 +03:00
|
|
|
* @param string $action optionally filter by action
|
2020-03-19 15:42:31 +03:00
|
|
|
* @param float $maxAgeHours
|
2016-07-20 19:36:15 +03:00
|
|
|
* @return int
|
|
|
|
*/
|
2020-03-19 15:42:31 +03:00
|
|
|
public function getAttempts(string $ip, string $action = '', float $maxAgeHours = 12): int {
|
2017-04-12 21:32:48 +03:00
|
|
|
$ipAddress = new IpAddress($ip);
|
|
|
|
if ($this->isIPWhitelisted((string)$ipAddress)) {
|
2016-11-14 16:05:01 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-19 15:26:24 +03:00
|
|
|
$cutoffTime = $this->getCutoffTimestamp($maxAgeHours);
|
2016-07-20 19:36:15 +03:00
|
|
|
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
2020-03-19 15:01:34 +03:00
|
|
|
$qb->select($qb->func()->count('*', 'attempts'))
|
2016-07-20 19:36:15 +03:00
|
|
|
->from('bruteforce_attempts')
|
|
|
|
->where($qb->expr()->gt('occurred', $qb->createNamedParameter($cutoffTime)))
|
2017-04-12 21:32:48 +03:00
|
|
|
->andWhere($qb->expr()->eq('subnet', $qb->createNamedParameter($ipAddress->getSubnet())));
|
2017-01-17 13:51:10 +03:00
|
|
|
|
|
|
|
if ($action !== '') {
|
|
|
|
$qb->andWhere($qb->expr()->eq('action', $qb->createNamedParameter($action)));
|
|
|
|
}
|
|
|
|
|
2020-03-19 15:01:34 +03:00
|
|
|
$result = $qb->execute();
|
|
|
|
$row = $result->fetch();
|
|
|
|
$result->closeCursor();
|
|
|
|
|
2020-03-19 15:26:24 +03:00
|
|
|
return (int) $row['attempts'];
|
|
|
|
}
|
2016-07-20 19:36:15 +03:00
|
|
|
|
2020-03-19 15:26:24 +03:00
|
|
|
/**
|
|
|
|
* Get the throttling delay (in milliseconds)
|
|
|
|
*
|
|
|
|
* @param string $ip
|
|
|
|
* @param string $action optionally filter by action
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getDelay(string $ip, string $action = ''): int {
|
|
|
|
$attempts = $this->getAttempts($ip, $action);
|
2016-07-20 19:36:15 +03:00
|
|
|
if ($attempts === 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$firstDelay = 0.1;
|
2020-03-19 15:31:07 +03:00
|
|
|
if ($attempts > self::MAX_ATTEMPTS) {
|
2016-07-20 19:36:15 +03:00
|
|
|
// Don't ever overflow. Just assume the maxDelay time:s
|
2020-07-09 13:16:52 +03:00
|
|
|
return self::MAX_DELAY_MS;
|
2020-03-19 15:31:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$delay = $firstDelay * 2**$attempts;
|
|
|
|
if ($delay > self::MAX_DELAY) {
|
2020-07-09 13:16:52 +03:00
|
|
|
return self::MAX_DELAY_MS;
|
2016-07-20 19:36:15 +03:00
|
|
|
}
|
2020-03-19 15:31:07 +03:00
|
|
|
return (int) \ceil($delay * 1000);
|
2016-07-20 19:36:15 +03:00
|
|
|
}
|
|
|
|
|
2017-11-23 15:37:50 +03:00
|
|
|
/**
|
|
|
|
* Reset the throttling delay for an IP address, action and metadata
|
|
|
|
*
|
|
|
|
* @param string $ip
|
|
|
|
* @param string $action
|
2020-03-19 16:13:52 +03:00
|
|
|
* @param array $metadata
|
2017-11-23 15:37:50 +03:00
|
|
|
*/
|
2020-03-19 16:13:52 +03:00
|
|
|
public function resetDelay(string $ip, string $action, array $metadata): void {
|
2017-11-23 15:37:50 +03:00
|
|
|
$ipAddress = new IpAddress($ip);
|
|
|
|
if ($this->isIPWhitelisted((string)$ipAddress)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-17 19:06:52 +03:00
|
|
|
$cutoffTime = $this->getCutoffTimestamp();
|
2017-11-23 15:37:50 +03:00
|
|
|
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
$qb->delete('bruteforce_attempts')
|
|
|
|
->where($qb->expr()->gt('occurred', $qb->createNamedParameter($cutoffTime)))
|
|
|
|
->andWhere($qb->expr()->eq('subnet', $qb->createNamedParameter($ipAddress->getSubnet())))
|
|
|
|
->andWhere($qb->expr()->eq('action', $qb->createNamedParameter($action)))
|
|
|
|
->andWhere($qb->expr()->eq('metadata', $qb->createNamedParameter(json_encode($metadata))));
|
|
|
|
|
|
|
|
$qb->execute();
|
|
|
|
}
|
|
|
|
|
2020-03-17 19:06:52 +03:00
|
|
|
/**
|
|
|
|
* Reset the throttling delay for an IP address
|
|
|
|
*
|
|
|
|
* @param string $ip
|
|
|
|
*/
|
2020-05-25 15:03:21 +03:00
|
|
|
public function resetDelayForIP($ip) {
|
2020-03-17 19:06:52 +03:00
|
|
|
$cutoffTime = $this->getCutoffTimestamp();
|
|
|
|
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
$qb->delete('bruteforce_attempts')
|
|
|
|
->where($qb->expr()->gt('occurred', $qb->createNamedParameter($cutoffTime)))
|
|
|
|
->andWhere($qb->expr()->eq('ip', $qb->createNamedParameter($ip)));
|
|
|
|
|
|
|
|
$qb->execute();
|
|
|
|
}
|
|
|
|
|
2016-07-20 19:36:15 +03:00
|
|
|
/**
|
|
|
|
* Will sleep for the defined amount of time
|
|
|
|
*
|
|
|
|
* @param string $ip
|
2017-01-17 13:51:10 +03:00
|
|
|
* @param string $action optionally filter by action
|
2016-08-26 16:10:03 +03:00
|
|
|
* @return int the time spent sleeping
|
2016-07-20 19:36:15 +03:00
|
|
|
*/
|
2020-03-19 15:30:15 +03:00
|
|
|
public function sleepDelay(string $ip, string $action = ''): int {
|
2017-01-17 13:51:10 +03:00
|
|
|
$delay = $this->getDelay($ip, $action);
|
2016-08-26 16:10:03 +03:00
|
|
|
usleep($delay * 1000);
|
|
|
|
return $delay;
|
2016-07-20 19:36:15 +03:00
|
|
|
}
|
2020-03-19 14:09:57 +03:00
|
|
|
|
|
|
|
/**
|
2020-03-19 15:42:31 +03:00
|
|
|
* Will sleep for the defined amount of time unless maximum was reached in the last 30 minutes
|
|
|
|
* In this case a "429 Too Many Request" exception is thrown
|
2020-03-19 14:09:57 +03:00
|
|
|
*
|
|
|
|
* @param string $ip
|
|
|
|
* @param string $action optionally filter by action
|
|
|
|
* @return int the time spent sleeping
|
|
|
|
* @throws MaxDelayReached when reached the maximum
|
|
|
|
*/
|
2020-03-19 15:30:15 +03:00
|
|
|
public function sleepDelayOrThrowOnMax(string $ip, string $action = ''): int {
|
2020-03-19 14:09:57 +03:00
|
|
|
$delay = $this->getDelay($ip, $action);
|
2020-07-09 13:16:52 +03:00
|
|
|
if (($delay === self::MAX_DELAY_MS) && $this->getAttempts($ip, $action, 0.5) > self::MAX_ATTEMPTS) {
|
2020-03-19 15:42:31 +03:00
|
|
|
// If the ip made too many attempts within the last 30 mins we don't execute anymore
|
2020-03-19 15:30:15 +03:00
|
|
|
throw new MaxDelayReached('Reached maximum delay');
|
2020-03-19 14:09:57 +03:00
|
|
|
}
|
|
|
|
usleep($delay * 1000);
|
|
|
|
return $delay;
|
|
|
|
}
|
2016-07-20 19:36:15 +03:00
|
|
|
}
|