Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@
from them (and the table is limited to 100000 rows, oldest first). This
maintenance, like the limit of the username statistics, runs once a day
whatever the purge age is set to
- Security: the cached results of the reverse-DNS risk signal are deleted
by the daily maintenance once they have expired, whatever the purge age
is set to (before: only by the purge by age, i.e. never with a purge age
of 0), and limited to 10000 rows, oldest first: the cache has a row per
address, and an IPv6 client can fail every login from another one
- Unblock tokens which have expired are deleted by the daily maintenance too
(before: only when somebody visited the unblock page, or by the purge by
age if one is configured). The status code of the block page is set with
Expand Down Expand Up @@ -177,7 +182,22 @@
lifted from the file (a timed block used to stay there for ever, and the
file grew with every blocked address); at most 2000 lines are kept; the
file is replaced atomically instead of being rewritten in place; the lock
file lives in Joomla's tmp directory instead of the system's
file lives in Joomla's tmp directory instead of the system's. A block
lifted in the component's block list or with an unblock link is taken out
of the file right away, not when the plugin next blocks an address or
does its daily maintenance (until then, the web server kept rejecting it)
- Security: in "Login Only" block mode, a blocked address could still try
passwords at the backend's login by leaving "option=com_login" out of
the request, or naming any other component: Joomla's backend runs
com_login for every request of a guest. Such requests are recognised as
the logins they are now; the exemptions for password recovery and the
unblock page don't let a blocked address through to the backend either
- Component: unblocking several entries of the block list at once no
longer stops at the first one which was unblocked already (the others
stayed blocked), and a database error while unblocking is reported as a
failure; the allow list can be sorted by its columns again; blocks of a
week, 30 days or a duration of the progressive block duration show their
duration instead of a language key
- Security: failed logins are purged after 4 weeks by default (set the
purge age to 0 for the old behaviour) and limited to 200000 rows
- Security: a response is delayed by 60 seconds at most, however the delay
Expand Down
68 changes: 44 additions & 24 deletions src/Extension/Bfstop.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,30 +298,12 @@ private function getHtaccessPath()

/**
* With blocking through .htaccess the web server holds back a blocked
* address; it doesn't know that a block has run out or was lifted, so the
* entry has to be taken out of the file here, or the block would be
* permanent (and the file would grow with every address ever blocked).
* Entries without a block in the database - the ones an administrator
* added to the file by hand - are left alone.
* address; it doesn't know that a block has run out or was lifted, see
* HtaccessHelper::removeLiftedBlocks().
*/
private function removeLiftedHtaccessBlocks()
{
$addresses = $this->mydb->getAddressesWithoutActiveBlock();
if (count($addresses) === 0)
{
return;
}
$htaccess = new HtaccessHelper($this->getHtaccessPath(), $this->logger);
$present = $htaccess->getDeniedIPs();
foreach ($addresses as $address)
{
if (in_array($address, $present, true))
{
$this->logger->log('Block of '.$address.' is over, removing it from '.
$htaccess->getFileName(), Log::INFO);
$htaccess->undenyIP($address);
}
}
(new HtaccessHelper($this->getHtaccessPath(), $this->logger))->removeLiftedBlocks($this->mydb);
}

private function getRealDurationFromDBDuration($duration)
Expand Down Expand Up @@ -657,7 +639,10 @@ public function onUserLogin($event)
*/
private function isUnblockRequest($blockIds)
{
if (strcmp($this->requestString('option'), 'com_bfstop') != 0 ||
// tokenunblock is a view of the site; in the backend, the request of
// a guest would go to com_login (see requestedOption())
if (!$this->getApplication()->isClient('site') ||
strcmp($this->requestString('option'), 'com_bfstop') != 0 ||
strcmp($this->requestString('view'), 'tokenunblock') != 0 ||
$this->requestString('task') !== '')
{
Expand Down Expand Up @@ -705,6 +690,38 @@ private function requestedTask()
return array($controller === '' ? 'display' : $controller, $task);
}

// the components the backend runs for a guest (or a user who may not log in
// there) as requested; every other request goes to com_login, see
// AdministratorApplication::$allowedUnprivilegedOptions
private const UnprivilegedBackendOptions = array('com_login', 'com_ajax');

/**
* The component Joomla runs for this request, in lower case. Usually the
* "option" parameter, but not in the backend: there,
* AdministratorApplication::findOption() - called when the request is
* dispatched, after onAfterRoute - runs com_login for a guest, whatever
* else the request names, and com_cpanel if it names nothing. Checks of
* the component have to go by that, or a login posted to the backend
* with "option" left out (or set to any other component) would not be
* recognised as the login it is.
*/
private function requestedOption()
{
$option = strtolower($this->requestString('option'));
$app = $this->getApplication();
if (!$app->isClient('administrator'))
{
return $option;
}
$user = $app->getIdentity();
if (($user === null || $user->guest || !$user->authorise('core.login.admin')) &&
!in_array($option, self::UnprivilegedBackendOptions, true))
{
return 'com_login';
}
return $option === '' ? 'com_cpanel' : $option;
}

// the tasks of com_users (controller.method) with which a user who forgot
// their password or username gets a new one: the forms of the reset and
// remind views post to them (index.php?task=reset.request, ...)
Expand Down Expand Up @@ -732,7 +749,7 @@ private function requestedTask()
*/
private function isPasswordRecoveryRequest()
{
if (strcasecmp($this->requestString('option'), 'com_users') != 0)
if ($this->requestedOption() !== 'com_users')
{
return false;
}
Expand Down Expand Up @@ -770,10 +787,12 @@ private function isPasswordRecoveryRequest()
* - "task=user.LOGIN", "task=user.login.x" and
* "controller=user&task=login" are all logins. Matching more than
* Joomla would run only blocks a request which wouldn't do anything.
* The same goes for the component (see requestedOption()): a backend
* login without "option=com_login" is a login all the same.
*/
private function isLoginAttemptRequest()
{
$option = strtolower($this->requestString('option'));
$option = $this->requestedOption();
list(, $method) = $this->requestedTask();
$result = ($option === 'com_users' || $option === 'com_login') && $method === 'login';
if ($result)
Expand Down Expand Up @@ -815,6 +834,7 @@ public function onAfterInitialise($event)
$this->mydb->trimFailedLogins();
$this->mydb->trimUsernameStats();
$this->mydb->pruneKnownIps();
$this->mydb->pruneDnsCache();
$this->mydb->purgeExpiredUnblockTokens();
if ($this->getStringParam('blockMode', 'full') === 'htaccess')
{
Expand Down
59 changes: 51 additions & 8 deletions src/Helper/DatabaseHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class DatabaseHelper
// IP per TTL window, see issue #103
public static $DNS_CACHE_TTL_DAYS = 7;

// upper bound for the rows of the reverse-DNS cache: it has a row per
// address, and a client with an IPv6 network can fail every login from
// another one
public static $DNS_CACHE_MAX_ROWS = 10000;

public function getClientString($id)
{
return ($id == 0) ? 'Frontend' : 'Backend';
Expand Down Expand Up @@ -883,14 +888,7 @@ public function purgeOldEntries($purgeAgeWeeks)

$this->db->setQuery('DELETE FROM #__bfstop_unblock_token WHERE crdate < '.$deleteDate);
$this->db->execute();

// dnscache entries are already treated as expired by
// getCachedHostname() past DNS_CACHE_TTL_DAYS regardless of the
// admin-configured purge age above - this just reclaims the
// storage for rows nothing will ever read as valid again.
$this->db->setQuery('DELETE FROM #__bfstop_dnscache WHERE checked_at < '.
$this->db->quote(self::minutesBefore($now, self::$DNS_CACHE_TTL_DAYS * 24 * 60)));
$this->db->execute();
// the reverse-DNS cache has an age of its own, see pruneDnsCache()
}
catch (\Exception $e)
{
Expand Down Expand Up @@ -999,6 +997,51 @@ public function purgeExpiredUnblockTokens()
}
}

/**
* Deletes the cached reverse-DNS results which have expired - nothing
* reads them any more (see getCachedHostname()) - and, oldest first, as
* many more as needed to keep at most $maxRows. Run by the daily
* maintenance whatever the purge age is set to: with the reverse-DNS risk
* signal on, a visitor adds a row for every address they fail a login
* from.
*/
public function pruneDnsCache($maxRows = null)
{
$maxRows = $maxRows ?? self::$DNS_CACHE_MAX_ROWS;
try
{
$this->db->setQuery('DELETE FROM #__bfstop_dnscache WHERE checked_at < '.
$this->db->quote(self::minutesBefore(date("Y-m-d H:i:s"), self::$DNS_CACHE_TTL_DAYS * 24 * 60)));
$this->db->execute();
for ($round = 0; $round < 1000; ++$round)
{
$this->db->setQuery('SELECT COUNT(*) FROM #__bfstop_dnscache');
$excess = ((int) $this->db->loadResult()) - $maxRows;
if ($excess <= 0)
{
return;
}
$query = $this->db->getQuery(true)
->select($this->db->quoteName('ipaddress'))
->from($this->db->quoteName('#__bfstop_dnscache'))
->order($this->db->quoteName('checked_at').' ASC');
$this->db->setQuery($query, 0, min($excess, 1000));
$addresses = $this->db->loadColumn();
if (count($addresses) === 0)
{
return;
}
$this->db->setQuery('DELETE FROM #__bfstop_dnscache WHERE ipaddress IN ('.
implode(',', array_map(array($this->db, 'quote'), $addresses)).')');
$this->db->execute();
}
}
catch (\Exception $e)
{
$this->logger->log("Database exception occured: ".$e->getMessage(), Log::ERROR);
}
}

/**
* Forgets the addresses users logged in from longer ago than $maxAgeDays,
* and, oldest first, as many more as needed to keep at most $maxRows.
Expand Down
38 changes: 38 additions & 0 deletions src/Helper/HtaccessHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,44 @@ public function undenyIP($IP)
});
}

/**
* Takes the addresses whose blocks have run out or were lifted out of the
* file. The web server knows nothing about durations or unblocks, so the
* entry has to be removed, or the block would be permanent (and the file
* would grow with every address ever blocked). Entries without a block in
* the database - the ones an administrator added to the file by hand -
* are left alone. Called by the plugin whenever it blocks and once a day,
* and by com_bfstop right after it unblocked something.
*
* @return int the number of entries removed
*/
public function removeLiftedBlocks(DatabaseHelper $db)
{
$addresses = $db->getAddressesWithoutActiveBlock();
if (count($addresses) === 0)
{
return 0;
}
$present = $this->getDeniedIPs();
$removed = 0;
foreach ($addresses as $address)
{
if (in_array($address, $present, true))
{
if (!is_null($this->logger))
{
$this->logger->log('Block of '.$address.' is over, removing it from '.
$this->getFileName(), Log::INFO);
}
if ($this->undenyIP($address))
{
++$removed;
}
}
}
return $removed;
}

/**
* Edit ErrorDocument 403 line in .htaccess.
*
Expand Down
60 changes: 54 additions & 6 deletions tests/Integration/BlockedRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,33 @@ private function block($ip = self::Ip, $minutesAgo = 0, $duration = 60)
'crdate' => self::minutesAgo($minutesAgo), 'duration' => $duration), 'id');
}

private function request($query = '', $ip = self::Ip, $routed = true)
/**
* @param string|null $backendUser null for a request to the site, else one
* to the backend: '' by a guest, or by the
* user with this username
*/
private function request($query = '', $ip = self::Ip, $routed = true, $backendUser = null)
{
$command = escapeshellarg(PHP_BINARY).' '.escapeshellarg(__DIR__.'/fixtures/request.php').' '.
escapeshellarg($ip).' '.escapeshellarg($query).($routed ? '' : ' --no-route').' 2>&1';
escapeshellarg($ip).' '.escapeshellarg($query).($routed ? '' : ' --no-route').
($backendUser === null ? '' : ' '.escapeshellarg('--admin'.($backendUser === '' ? '' : '='.$backendUser))).
' 2>&1';
exec($command, $output, $exitCode);
$output = implode("\n", $output);
$this->assertSame(0, $exitCode, $output);
return $output;
}

private function assertBlocked($query = '', $ip = self::Ip)
private function assertBlocked($query = '', $ip = self::Ip, $backendUser = null)
{
$output = $this->request($query, $ip);
$output = $this->request($query, $ip, true, $backendUser);
$this->assertStringContainsString(self::BlockedMessage, $output, "request '$query' should be blocked");
$this->assertStringNotContainsString('NOT BLOCKED', $output);
}

private function assertNotBlocked($query = '', $ip = self::Ip)
private function assertNotBlocked($query = '', $ip = self::Ip, $backendUser = null)
{
$output = $this->request($query, $ip);
$output = $this->request($query, $ip, true, $backendUser);
$this->assertStringContainsString('NOT BLOCKED', $output, "request '$query' should not be blocked");
$this->assertStringNotContainsString(self::BlockedMessage, $output);
}
Expand Down Expand Up @@ -154,8 +161,14 @@ public function testDailyMaintenancePrunesAndRemembersWhenItRan()
'crdate' => self::minutesAgo(4 * 24 * 60)));
$this->insert('#__bfstop_unblock_token', array('token' => str_repeat('cd', 20), 'block_id' => 2,
'crdate' => self::minutesAgo(60)));
// reverse-DNS results which have expired
$this->insert('#__bfstop_dnscache', array('ipaddress' => '203.0.113.77', 'hostname' => null,
'checked_at' => self::minutesAgo(8 * 24 * 60)));
$this->insert('#__bfstop_dnscache', array('ipaddress' => '203.0.113.78', 'hostname' => null,
'checked_at' => self::minutesAgo(60)));
$this->assertNotBlocked();
$this->assertSame(array('203.0.113.2'), $this->knownIps());
$this->assertSame(array('203.0.113.78'), $this->db->setQuery('SELECT ipaddress FROM #__bfstop_dnscache')->loadColumn());
$this->assertSame(array(DatabaseHelper::hashToken(str_repeat('cd', 20))), $this->db->setQuery('SELECT token FROM #__bfstop_unblock_token')->loadColumn());
$saved = json_decode($this->getPluginParams(), true);
$this->assertGreaterThan(time() - 60, $saved['lastPurge']);
Expand Down Expand Up @@ -280,6 +293,41 @@ public function testLoginOnlyModeRecognisesEveryWayToLogIn()
$this->assertNotBlocked('option=com_content&task=user.login');
}

public function testLoginOnlyModeRecognisesBackendLoginsWithoutComLogin()
{
// the backend runs com_login for every request of a guest, whatever
// component it names (AdministratorApplication::findOption(), called
// after onAfterRoute): each of these is a login
$this->configure(array('blockMode' => 'loginonly'));
$this->block();
$this->assertBlocked('option=com_login&task=login', self::Ip, '');
$this->assertBlocked('task=login', self::Ip, '');
$this->assertBlocked('option=com_cpanel&task=login', self::Ip, '');
$this->assertBlocked('option=com_users&task=login', self::Ip, '');
$this->assertBlocked('option=com_content&task=display.login', self::Ip, '');
// the login form itself stays reachable, as do the components the
// backend lets a guest reach
$this->assertNotBlocked('', self::Ip, '');
$this->assertNotBlocked('option=com_cpanel', self::Ip, '');
$this->assertNotBlocked('option=com_ajax&task=login', self::Ip, '');
// a logged-in user's request goes where it says
$this->assertNotBlocked('option=com_content&task=display.login', self::Ip, 'Admin');
$this->assertBlocked('option=com_login&task=login', self::Ip, 'Admin');
}

public function testUnblockTokenAndPasswordRecoveryAreNoPassToTheBackend()
{
// in the backend, these requests of a guest would go to com_login
$this->configure();
$id = $this->block();
$token = (new DatabaseHelper($this->logger))->getNewUnblockToken($id, str_repeat('ef', 20));
$this->assertNotBlocked('option=com_bfstop&view=tokenunblock&token='.$token);
$this->assertBlocked('option=com_bfstop&view=tokenunblock&token='.$token, self::Ip, '');
$this->assertNotBlocked('option=com_users&view=reset');
$this->assertBlocked('option=com_users&view=reset', self::Ip, '');
$this->assertBlocked('option=com_users&task=reset.request', self::Ip, '');
}

public function testPasswordRecoveryStaysReachable()
{
// a blocked legitimate user must still be able to reset their password
Expand Down
Loading
Loading