2018-09-23 11:09:04 +02:00
|
|
|
package Lemonldap::NG::Portal::Plugins::BruteForceProtection;
|
2018-09-22 19:24:16 +02:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Mouse;
|
2020-10-09 22:26:00 +02:00
|
|
|
use Lemonldap::NG::Portal::Main::Constants qw(
|
|
|
|
PE_OK
|
|
|
|
PE_WAIT
|
|
|
|
);
|
2018-09-22 19:24:16 +02:00
|
|
|
|
2020-10-09 22:26:00 +02:00
|
|
|
our $VERSION = '2.0.10';
|
2018-09-22 19:24:16 +02:00
|
|
|
|
2018-09-22 22:25:57 +02:00
|
|
|
extends 'Lemonldap::NG::Portal::Main::Plugin';
|
2018-09-22 19:24:16 +02:00
|
|
|
|
|
|
|
# INITIALIZATION
|
2020-02-26 22:31:22 +01:00
|
|
|
use constant afterSub => { storeHistory => 'run' };
|
2018-09-22 19:24:16 +02:00
|
|
|
|
2020-02-24 21:27:50 +01:00
|
|
|
has lockTimes => (
|
|
|
|
is => 'rw',
|
|
|
|
isa => 'ArrayRef',
|
|
|
|
default => sub { [] }
|
|
|
|
);
|
|
|
|
|
|
|
|
has maxAge => (
|
|
|
|
is => 'rw',
|
|
|
|
isa => 'Int'
|
|
|
|
);
|
|
|
|
|
2018-10-09 22:54:04 +02:00
|
|
|
sub init {
|
|
|
|
my ($self) = @_;
|
2020-01-14 19:01:37 +01:00
|
|
|
if ( $self->conf->{disablePersistentStorage} ) {
|
|
|
|
$self->logger->error(
|
|
|
|
'"BruteForceProtection" plugin enabled WITHOUT persistent session storage"'
|
|
|
|
);
|
|
|
|
return 0;
|
|
|
|
}
|
2020-10-09 22:26:00 +02:00
|
|
|
|
2018-10-09 22:54:04 +02:00
|
|
|
unless ( $self->conf->{loginHistoryEnabled} ) {
|
2018-10-10 23:12:38 +02:00
|
|
|
$self->logger->error(
|
2020-01-14 19:01:37 +01:00
|
|
|
'"BruteForceProtection" plugin enabled WITHOUT "History" plugin');
|
2018-10-10 23:12:38 +02:00
|
|
|
return 0;
|
2018-10-09 22:54:04 +02:00
|
|
|
}
|
2020-10-09 22:26:00 +02:00
|
|
|
|
2020-01-13 14:10:14 +01:00
|
|
|
unless ( $self->conf->{failedLoginNumber} >
|
|
|
|
$self->conf->{bruteForceProtectionMaxFailed} )
|
|
|
|
{
|
2020-08-27 14:38:11 +02:00
|
|
|
$self->logger->error( 'Number of failed logins history ('
|
2020-01-13 14:10:14 +01:00
|
|
|
. $self->conf->{failedLoginNumber}
|
2020-08-27 14:38:11 +02:00
|
|
|
. ') must be higher than allowed failed logins attempt ('
|
2020-01-13 14:10:14 +01:00
|
|
|
. $self->conf->{bruteForceProtectionMaxFailed}
|
|
|
|
. ')' );
|
|
|
|
return 0;
|
|
|
|
}
|
2020-10-09 22:26:00 +02:00
|
|
|
|
2020-02-24 21:27:50 +01:00
|
|
|
if ( $self->conf->{bruteForceProtectionIncrementalTempo} ) {
|
|
|
|
my $lockTimes = @{ $self->lockTimes } =
|
|
|
|
sort { $a <=> $b }
|
2020-08-28 10:41:40 +02:00
|
|
|
map {
|
|
|
|
$_ =~ s/\D//;
|
|
|
|
$_ < $self->conf->{bruteForceProtectionMaxLockTime} ? $_ : ()
|
|
|
|
}
|
2020-02-24 21:27:50 +01:00
|
|
|
grep { /\d+/ }
|
2020-08-25 22:58:47 +02:00
|
|
|
split /\s*,\s*/, $self->conf->{bruteForceProtectionLockTimes};
|
2020-02-24 21:27:50 +01:00
|
|
|
|
2020-02-26 22:31:22 +01:00
|
|
|
unless ($lockTimes) {
|
|
|
|
@{ $self->lockTimes } = ( 5, 15, 60, 300, 600 );
|
|
|
|
$lockTimes = 5;
|
|
|
|
}
|
2020-05-24 00:04:33 +02:00
|
|
|
|
2020-08-27 14:38:11 +02:00
|
|
|
for (
|
|
|
|
my $i = 1 ;
|
|
|
|
$i <= $self->conf->{bruteForceProtectionMaxFailed} ;
|
|
|
|
$i++
|
|
|
|
)
|
|
|
|
{
|
|
|
|
unshift @{ $self->lockTimes }, 0;
|
|
|
|
$lockTimes++;
|
|
|
|
}
|
2020-08-25 22:58:47 +02:00
|
|
|
|
2020-08-28 10:41:40 +02:00
|
|
|
unless ( $lockTimes < $self->conf->{failedLoginNumber} ) {
|
2020-08-28 14:44:03 +02:00
|
|
|
$self->logger->warn( 'Number failed logins history ('
|
2020-02-26 22:31:22 +01:00
|
|
|
. $self->conf->{failedLoginNumber}
|
2020-08-28 14:44:03 +02:00
|
|
|
. ') must be higher than incremental lock time values plus allowed failed logins attempt ('
|
|
|
|
. "$lockTimes)" );
|
2020-02-26 22:31:22 +01:00
|
|
|
splice @{ $self->lockTimes }, $self->conf->{failedLoginNumber};
|
|
|
|
$lockTimes = $self->conf->{failedLoginNumber};
|
|
|
|
}
|
2020-02-24 21:27:50 +01:00
|
|
|
|
2020-05-24 00:04:33 +02:00
|
|
|
my $sum = $self->conf->{bruteForceProtectionMaxAge} *
|
|
|
|
( 1 + $self->conf->{failedLoginNumber} - $lockTimes );
|
2020-02-24 21:27:50 +01:00
|
|
|
$sum += $_ foreach @{ $self->lockTimes };
|
|
|
|
$self->maxAge($sum);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->maxAge( $self->conf->{bruteForceProtectionMaxAge} );
|
|
|
|
}
|
2020-10-09 22:26:00 +02:00
|
|
|
|
2018-10-09 22:54:04 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2018-09-22 19:24:16 +02:00
|
|
|
|
|
|
|
# RUNNING METHOD
|
|
|
|
sub run {
|
|
|
|
my ( $self, $req ) = @_;
|
2020-02-24 21:27:50 +01:00
|
|
|
my $now = time;
|
|
|
|
my $countFailed = my @failedLogins =
|
|
|
|
map { ( $now - $_->{_utime} ) < $self->maxAge ? $_ : () }
|
|
|
|
@{ $req->sessionInfo->{_loginHistory}->{failedLogin} };
|
|
|
|
$self->logger->debug( ' Failed login maxAge = ' . $self->maxAge );
|
2020-02-23 23:19:32 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
" Number of failed login(s) to take into account = $countFailed");
|
|
|
|
|
|
|
|
if ( $self->conf->{bruteForceProtectionIncrementalTempo} ) {
|
|
|
|
my $lastFailedLoginEpoch = $failedLogins[0]->{_utime} || undef;
|
|
|
|
|
|
|
|
return PE_OK unless $lastFailedLoginEpoch;
|
|
|
|
|
|
|
|
my $delta = $now - $lastFailedLoginEpoch;
|
|
|
|
$self->logger->debug(" -> Delta = $delta");
|
2020-02-24 21:27:50 +01:00
|
|
|
my $waitingTime = $self->lockTimes->[ $countFailed - 1 ]
|
2020-08-25 22:58:47 +02:00
|
|
|
// $self->conf->{bruteForceProtectionMaxLockTime};
|
2020-02-23 23:19:32 +01:00
|
|
|
$self->logger->debug(" -> Waiting time = $waitingTime");
|
2020-08-25 22:58:47 +02:00
|
|
|
if ( $waitingTime && $delta <= $waitingTime ) {
|
2020-02-23 23:19:32 +01:00
|
|
|
$self->logger->debug("BruteForceProtection enabled");
|
2020-02-24 21:27:50 +01:00
|
|
|
$req->lockTime($waitingTime);
|
2020-02-23 23:19:32 +01:00
|
|
|
return PE_WAIT;
|
|
|
|
}
|
|
|
|
return PE_OK;
|
|
|
|
}
|
2019-07-04 22:50:46 +02:00
|
|
|
|
2018-12-12 23:51:33 +01:00
|
|
|
return PE_OK
|
2019-02-05 23:12:17 +01:00
|
|
|
if ( $countFailed <= $self->conf->{bruteForceProtectionMaxFailed} );
|
2018-09-22 19:24:16 +02:00
|
|
|
|
2020-02-23 23:19:32 +01:00
|
|
|
my @lastFailedLoginEpoch = ();
|
2020-02-24 21:27:50 +01:00
|
|
|
my $MaxAge = $self->maxAge + 1;
|
2020-02-23 23:19:32 +01:00
|
|
|
|
|
|
|
# Auth_N-2 failed login epoch
|
2018-12-12 23:51:33 +01:00
|
|
|
foreach ( 0 .. $self->conf->{bruteForceProtectionMaxFailed} - 1 ) {
|
2019-07-04 22:50:46 +02:00
|
|
|
push @lastFailedLoginEpoch,
|
|
|
|
$req->sessionInfo->{_loginHistory}->{failedLogin}->[$_]->{_utime}
|
|
|
|
if ( $req->sessionInfo->{_loginHistory}->{failedLogin}->[$_] );
|
2018-09-28 00:03:46 +02:00
|
|
|
}
|
2019-07-04 22:50:46 +02:00
|
|
|
|
2018-12-12 23:51:33 +01:00
|
|
|
# If Auth_N-MaxFailed older than MaxAge -> another try allowed
|
2019-02-05 23:12:17 +01:00
|
|
|
$MaxAge =
|
|
|
|
$lastFailedLoginEpoch[0] -
|
|
|
|
$lastFailedLoginEpoch[ $self->conf->{bruteForceProtectionMaxFailed} - 1 ]
|
|
|
|
if $self->conf->{bruteForceProtectionMaxFailed};
|
2018-09-28 20:08:54 +02:00
|
|
|
$self->logger->debug(" -> MaxAge = $MaxAge");
|
2019-07-04 22:50:46 +02:00
|
|
|
|
2018-09-28 00:03:46 +02:00
|
|
|
return PE_OK
|
2020-02-24 21:27:50 +01:00
|
|
|
if ( $MaxAge > $self->maxAge );
|
2018-09-22 19:24:16 +02:00
|
|
|
|
2018-09-28 00:03:46 +02:00
|
|
|
# Delta between the two last failed logins -> Auth_N - Auth_N-1
|
2020-02-23 23:19:32 +01:00
|
|
|
my $delta =
|
|
|
|
defined $lastFailedLoginEpoch[1] ? $now - $lastFailedLoginEpoch[1] : 0;
|
2018-09-28 20:08:54 +02:00
|
|
|
$self->logger->debug(" -> Delta = $delta");
|
2018-09-22 19:24:16 +02:00
|
|
|
|
2018-12-12 23:51:33 +01:00
|
|
|
# Delta between the two last failed logins < Tempo => wait
|
2018-09-28 19:50:01 +02:00
|
|
|
return PE_OK
|
2019-02-05 23:12:17 +01:00
|
|
|
unless ( $delta <= $self->conf->{bruteForceProtectionTempo} );
|
2018-09-28 19:50:01 +02:00
|
|
|
|
2018-09-28 00:03:46 +02:00
|
|
|
# Account locked
|
2020-02-23 23:19:32 +01:00
|
|
|
$self->logger->debug("BruteForceProtection enabled");
|
2020-02-24 21:27:50 +01:00
|
|
|
$req->lockTime( $self->conf->{bruteForceProtectionTempo} );
|
2018-09-28 00:03:46 +02:00
|
|
|
return PE_WAIT;
|
2018-09-22 19:24:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|