BruteforceAttempts.php 2.3 KB

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