2020-03-17 12:06:52 -04:00
|
|
|
<?php
|
2023-08-15 02:27:01 -04:00
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2020-03-17 12:06:52 -04:00
|
|
|
/**
|
2024-05-24 13:43:47 -04:00
|
|
|
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2020-03-17 12:06:52 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Core\Command\Security;
|
|
|
|
|
|
|
|
|
|
use OC\Core\Command\Base;
|
2023-08-15 02:27:01 -04:00
|
|
|
use OCP\Security\Bruteforce\IThrottler;
|
2020-03-17 12:06:52 -04:00
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
|
2023-08-15 02:27:01 -04:00
|
|
|
class BruteforceAttempts extends Base {
|
2023-06-13 08:36:16 -04:00
|
|
|
public function __construct(
|
2023-08-16 11:40:38 -04:00
|
|
|
protected IThrottler $throttler,
|
2023-06-13 08:36:16 -04:00
|
|
|
) {
|
2020-03-17 12:06:52 -04:00
|
|
|
parent::__construct();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 02:27:01 -04:00
|
|
|
protected function configure(): void {
|
|
|
|
|
parent::configure();
|
2020-03-17 12:06:52 -04:00
|
|
|
$this
|
2023-08-15 02:27:01 -04:00
|
|
|
->setName('security:bruteforce:attempts')
|
2024-02-19 08:09:58 -05:00
|
|
|
->setDescription('Show bruteforce attempts status for a given IP address')
|
2020-03-17 12:06:52 -04:00
|
|
|
->addArgument(
|
|
|
|
|
'ipaddress',
|
|
|
|
|
InputArgument::REQUIRED,
|
2024-02-19 08:09:58 -05:00
|
|
|
'IP address for which the attempts status is to be shown',
|
2023-08-15 02:27:01 -04:00
|
|
|
)
|
|
|
|
|
->addArgument(
|
|
|
|
|
'action',
|
|
|
|
|
InputArgument::OPTIONAL,
|
|
|
|
|
'Only count attempts for the given action',
|
|
|
|
|
)
|
|
|
|
|
;
|
2020-03-17 12:06:52 -04:00
|
|
|
}
|
|
|
|
|
|
2020-06-26 08:54:51 -04:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int {
|
2020-03-17 12:06:52 -04:00
|
|
|
$ip = $input->getArgument('ipaddress');
|
|
|
|
|
|
|
|
|
|
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
|
|
|
|
|
$output->writeln('<error>"' . $ip . '" is not a valid IP address</error>');
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-15 02:27:01 -04:00
|
|
|
$data = [
|
2023-08-16 11:40:38 -04:00
|
|
|
'bypass-listed' => $this->throttler->isBypassListed($ip),
|
2023-08-15 02:27:01 -04:00
|
|
|
'attempts' => $this->throttler->getAttempts(
|
|
|
|
|
$ip,
|
|
|
|
|
(string)$input->getArgument('action'),
|
|
|
|
|
),
|
|
|
|
|
'delay' => $this->throttler->getDelay(
|
|
|
|
|
$ip,
|
|
|
|
|
(string)$input->getArgument('action'),
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$this->writeArrayInOutputFormat($input, $output, $data);
|
|
|
|
|
|
2020-06-26 08:54:51 -04:00
|
|
|
return 0;
|
2020-03-17 12:06:52 -04:00
|
|
|
}
|
|
|
|
|
}
|