Skip to content

Commit 0c7bc29

Browse files
committed
feat(OCC): Add a command to get the bruteforce state of an IP
Signed-off-by: Joas Schilling <[email protected]>
1 parent 815bbeb commit 0c7bc29

File tree

5 files changed

+100
-8
lines changed

5 files changed

+100
-8
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2023 Joas Schilling <[email protected]>
6+
*
7+
* @author Joas Schilling <[email protected]>
8+
*
9+
* @license GNU AGPL version 3 or any later version
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License as
13+
* published by the Free Software Foundation, either version 3 of the
14+
* License, or (at your option) any later version.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*
24+
*/
25+
namespace OC\Core\Command\Security;
26+
27+
use OC\Core\Command\Base;
28+
use OC\Security\Bruteforce\Throttler;
29+
use OCP\Security\Bruteforce\IThrottler;
30+
use Symfony\Component\Console\Input\InputArgument;
31+
use Symfony\Component\Console\Input\InputInterface;
32+
use Symfony\Component\Console\Output\OutputInterface;
33+
34+
class BruteforceAttempts extends Base {
35+
/** @var Throttler */
36+
protected IThrottler $throttler;
37+
38+
public function __construct(
39+
IThrottler $throttler,
40+
) {
41+
parent::__construct();
42+
$this->throttler = $throttler;
43+
}
44+
45+
protected function configure(): void {
46+
parent::configure();
47+
$this
48+
->setName('security:bruteforce:attempts')
49+
->setDescription('resets bruteforce attempts for given IP address')
50+
->addArgument(
51+
'ipaddress',
52+
InputArgument::REQUIRED,
53+
'IP address for which the attempts are to be reset',
54+
)
55+
->addArgument(
56+
'action',
57+
InputArgument::OPTIONAL,
58+
'Only count attempts for the given action',
59+
)
60+
;
61+
}
62+
63+
protected function execute(InputInterface $input, OutputInterface $output): int {
64+
$ip = $input->getArgument('ipaddress');
65+
66+
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
67+
$output->writeln('<error>"' . $ip . '" is not a valid IP address</error>');
68+
return 1;
69+
}
70+
71+
$data = [
72+
'allow-listed' => $this->throttler->isIPWhitelisted($ip),
73+
'attempts' => $this->throttler->getAttempts(
74+
$ip,
75+
(string) $input->getArgument('action'),
76+
),
77+
'delay' => $this->throttler->getDelay(
78+
$ip,
79+
(string) $input->getArgument('action'),
80+
),
81+
];
82+
83+
$this->writeArrayInOutputFormat($input, $output, $data);
84+
85+
return 0;
86+
}
87+
}

core/Command/Security/ResetBruteforceAttempts.php renamed to core/Command/Security/BruteforceResetAttempts.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
3+
declare(strict_types=1);
24
/**
35
* @copyright Copyright (c) 2020, Johannes Riedel ([email protected])
46
*
@@ -24,22 +26,22 @@
2426
namespace OC\Core\Command\Security;
2527

2628
use OC\Core\Command\Base;
27-
use OC\Security\Bruteforce\Throttler;
29+
use OCP\Security\Bruteforce\IThrottler;
2830
use Symfony\Component\Console\Input\InputArgument;
2931
use Symfony\Component\Console\Input\InputInterface;
3032
use Symfony\Component\Console\Output\OutputInterface;
3133

32-
class ResetBruteforceAttempts extends Base {
34+
class BruteforceResetAttempts extends Base {
3335
public function __construct(
34-
protected Throttler $throttler,
36+
protected IThrottler $throttler,
3537
) {
3638
parent::__construct();
3739
}
3840

39-
protected function configure() {
41+
protected function configure(): void {
4042
$this
4143
->setName('security:bruteforce:reset')
42-
->setDescription('resets bruteforce attemps for given IP address')
44+
->setDescription('resets bruteforce attempts for given IP address')
4345
->addArgument(
4446
'ipaddress',
4547
InputArgument::REQUIRED,

core/register_command.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@
209209
$application->add(new OC\Core\Command\Security\ListCertificates(\OC::$server->getCertificateManager(), \OC::$server->getL10N('core')));
210210
$application->add(new OC\Core\Command\Security\ImportCertificate(\OC::$server->getCertificateManager()));
211211
$application->add(new OC\Core\Command\Security\RemoveCertificate(\OC::$server->getCertificateManager()));
212-
$application->add(new OC\Core\Command\Security\ResetBruteforceAttempts(\OC::$server->getBruteForceThrottler()));
212+
$application->add(\OC::$server->get(\OC\Core\Command\Security\BruteforceAttempts::class));
213+
$application->add(\OC::$server->get(\OC\Core\Command\Security\BruteforceResetAttempts::class));
213214
} else {
214215
$application->add(\OC::$server->get(\OC\Core\Command\Maintenance\Install::class));
215216
}

lib/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1019,10 +1019,11 @@
10191019
'OC\\Core\\Command\\Preview\\Generate' => $baseDir . '/core/Command/Preview/Generate.php',
10201020
'OC\\Core\\Command\\Preview\\Repair' => $baseDir . '/core/Command/Preview/Repair.php',
10211021
'OC\\Core\\Command\\Preview\\ResetRenderedTexts' => $baseDir . '/core/Command/Preview/ResetRenderedTexts.php',
1022+
'OC\\Core\\Command\\Security\\BruteforceAttempts' => $baseDir . '/core/Command/Security/BruteforceAttempts.php',
1023+
'OC\\Core\\Command\\Security\\BruteforceResetAttempts' => $baseDir . '/core/Command/Security/BruteforceResetAttempts.php',
10221024
'OC\\Core\\Command\\Security\\ImportCertificate' => $baseDir . '/core/Command/Security/ImportCertificate.php',
10231025
'OC\\Core\\Command\\Security\\ListCertificates' => $baseDir . '/core/Command/Security/ListCertificates.php',
10241026
'OC\\Core\\Command\\Security\\RemoveCertificate' => $baseDir . '/core/Command/Security/RemoveCertificate.php',
1025-
'OC\\Core\\Command\\Security\\ResetBruteforceAttempts' => $baseDir . '/core/Command/Security/ResetBruteforceAttempts.php',
10261027
'OC\\Core\\Command\\Status' => $baseDir . '/core/Command/Status.php',
10271028
'OC\\Core\\Command\\SystemTag\\Add' => $baseDir . '/core/Command/SystemTag/Add.php',
10281029
'OC\\Core\\Command\\SystemTag\\Delete' => $baseDir . '/core/Command/SystemTag/Delete.php',

lib/composer/composer/autoload_static.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1052,10 +1052,11 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
10521052
'OC\\Core\\Command\\Preview\\Generate' => __DIR__ . '/../../..' . '/core/Command/Preview/Generate.php',
10531053
'OC\\Core\\Command\\Preview\\Repair' => __DIR__ . '/../../..' . '/core/Command/Preview/Repair.php',
10541054
'OC\\Core\\Command\\Preview\\ResetRenderedTexts' => __DIR__ . '/../../..' . '/core/Command/Preview/ResetRenderedTexts.php',
1055+
'OC\\Core\\Command\\Security\\BruteforceAttempts' => __DIR__ . '/../../..' . '/core/Command/Security/BruteforceAttempts.php',
1056+
'OC\\Core\\Command\\Security\\BruteforceResetAttempts' => __DIR__ . '/../../..' . '/core/Command/Security/BruteforceResetAttempts.php',
10551057
'OC\\Core\\Command\\Security\\ImportCertificate' => __DIR__ . '/../../..' . '/core/Command/Security/ImportCertificate.php',
10561058
'OC\\Core\\Command\\Security\\ListCertificates' => __DIR__ . '/../../..' . '/core/Command/Security/ListCertificates.php',
10571059
'OC\\Core\\Command\\Security\\RemoveCertificate' => __DIR__ . '/../../..' . '/core/Command/Security/RemoveCertificate.php',
1058-
'OC\\Core\\Command\\Security\\ResetBruteforceAttempts' => __DIR__ . '/../../..' . '/core/Command/Security/ResetBruteforceAttempts.php',
10591060
'OC\\Core\\Command\\Status' => __DIR__ . '/../../..' . '/core/Command/Status.php',
10601061
'OC\\Core\\Command\\SystemTag\\Add' => __DIR__ . '/../../..' . '/core/Command/SystemTag/Add.php',
10611062
'OC\\Core\\Command\\SystemTag\\Delete' => __DIR__ . '/../../..' . '/core/Command/SystemTag/Delete.php',

0 commit comments

Comments
 (0)