« blib/lib/Lemonldap/NG/Portal/Auth/Facebook.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Lib/SMTP.pm »
| 1 | package Lemonldap::NG::Portal::Plugins::BruteForceProtection; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Mouse; | ||
| 5 | use Lemonldap::NG::Portal::Main::Constants qw( | ||
| 6 | PE_OK | ||
| 7 | PE_WAIT | ||
| 8 | ); | ||
| 9 | |||
| 10 | our $VERSION = '2.23.0'; | ||
| 11 | |||
| 12 | extends 'Lemonldap::NG::Portal::Main::Plugin'; | ||
| 13 | |||
| 14 | # INITIALIZATION | ||
| 15 | use constant afterSub => { setPersistentSessionInfo => 'run' }; | ||
| 16 | |||
| 17 | has lockTimes => ( | ||
| 18 | is => 'rw', | ||
| 19 | isa => 'ArrayRef', | ||
| 20 | default => sub { [] } | ||
| 21 | ); | ||
| 22 | has maxAge => ( | ||
| 23 | is => 'rw', | ||
| 24 | isa => 'Int' | ||
| 25 | ); | ||
| 26 | has maxFailed => ( | ||
| 27 | is => 'rw', | ||
| 28 | isa => 'Int' | ||
| 29 | ); | ||
| 30 | |||
| 31 | sub init { | ||
| 32 | my ($self) = @_; | ||
| 33 | $self->maxFailed( abs $self->conf->{bruteForceProtectionMaxFailed} ); | ||
| 34 | |||
| 35 | if ( $self->conf->{disablePersistentStorage} ) { | ||
| 36 | $self->logger->error( | ||
| 37 | '"BruteForceProtection" plugin enabled WITHOUT persistent session storage"' | ||
| 38 | ); | ||
| 39 | return 0; | ||
| 40 | } | ||
| 41 | |||
| 42 | unless ( $self->conf->{loginHistoryEnabled} ) { | ||
| 43 | $self->logger->error( | ||
| 44 | '"BruteForceProtection" plugin enabled WITHOUT "History" plugin'); | ||
| 45 | return 0; | ||
| 46 | } | ||
| 47 | |||
| 48 | unless ( $self->conf->{failedLoginNumber} >= $self->maxFailed ) { | ||
| 49 | $self->logger->error( 'Number of failed logins history (' | ||
| 50 | . $self->conf->{failedLoginNumber} | ||
| 51 | . ') must be higher than allowed failed logins attempt (' | ||
| 52 | . $self->maxFailed | ||
| 53 | . ')' ); | ||
| 54 | return 0; | ||
| 55 | } | ||
| 56 | |||
| 57 | my $maxAge = $self->conf->{bruteForceProtectionMaxAge} || 300; | ||
| 58 | if ( $self->conf->{bruteForceProtectionIncrementalTempo} ) { | ||
| 59 | my @parsedTimes = | ||
| 60 | sort { $a <=> $b } | ||
| 61 | map { abs(s/\D//r) } | ||
| 62 | grep { /\d+/ } | ||
| 63 | split /\s*,\s*/, $self->conf->{bruteForceProtectionLockTimes}; | ||
| 64 | |||
| 65 | my @filteredOut = | ||
| 66 | grep { $_ >= $self->conf->{bruteForceProtectionMaxLockTime} } | ||
| 67 | @parsedTimes; | ||
| 68 | if (@filteredOut) { | ||
| 69 | $self->logger->warn( | ||
| 70 | 'BruteForceProtection: ignoring lock times >= maxLockTime (' | ||
| 71 | . $self->conf->{bruteForceProtectionMaxLockTime} . '): ' | ||
| 72 | . join( ', ', @filteredOut ) ); | ||
| 73 | } | ||
| 74 | |||
| 75 | my $lockTimes = @{ $self->lockTimes } = | ||
| 76 | grep { $_ < $self->conf->{bruteForceProtectionMaxLockTime} } | ||
| 77 | @parsedTimes; | ||
| 78 | |||
| 79 | unless ($lockTimes) { | ||
| 80 | @{ $self->lockTimes } = ( 15, 30, 60, 300, 600 ); | ||
| 81 | $lockTimes = 5; | ||
| 82 | } | ||
| 83 | |||
| 84 | for ( my $i = 1 ; $i < $self->maxFailed ; $i++ ) { | ||
| 85 | unshift @{ $self->lockTimes }, 0; | ||
| 86 | $lockTimes++; | ||
| 87 | } | ||
| 88 | |||
| 89 | unless ( $lockTimes <= $self->conf->{failedLoginNumber} ) { | ||
| 90 | $self->logger->warn( 'Number failed logins history (' | ||
| 91 | . $self->conf->{failedLoginNumber} | ||
| 92 | . ') must be higher than incremental lock time values plus allowed failed logins attempt (' | ||
| 93 | . "$lockTimes)" ); | ||
| 94 | splice @{ $self->lockTimes }, $self->conf->{failedLoginNumber}; | ||
| 95 | $lockTimes = $self->conf->{failedLoginNumber}; | ||
| 96 | } | ||
| 97 | |||
| 98 | my $sum = | ||
| 99 | $maxAge * ( 1 + $self->conf->{failedLoginNumber} - $lockTimes ); | ||
| 100 | $sum += $_ foreach @{ $self->lockTimes }; | ||
| 101 | $self->maxAge($sum); | ||
| 102 | } | ||
| 103 | else { | ||
| 104 | $self->maxAge( $maxAge * ( 1 + $self->maxFailed ) ); | ||
| 105 | } | ||
| 106 | |||
| 107 | return 1; | ||
| 108 | } | ||
| 109 | |||
| 110 | # RUNNING METHOD | ||
| 111 | sub run { | ||
| 112 | my ( $self, $req ) = @_; | ||
| 113 | my $now = time; | ||
| 114 | my $countFailed = my @failedLogins = | ||
| 115 | map { ( $now - $_->{_utime} ) <= $self->maxAge ? $_ : () } | ||
| 116 | @{ $req->sessionInfo->{_loginHistory}->{failedLogin} }; | ||
| 117 | $self->logger->debug( ' -> Failed login maxAge = ' . $self->maxAge ); | ||
| 118 | $self->logger->debug( | ||
| 119 | "Number of failed login(s) to take into account = $countFailed"); | ||
| 120 | my $lastFailedLoginEpoch = $failedLogins[0]->{_utime} || undef; | ||
| 121 | |||
| 122 | if ( $self->conf->{bruteForceProtectionIncrementalTempo} ) { | ||
| 123 | return PE_OK unless $lastFailedLoginEpoch; | ||
| 124 | |||
| 125 | # Delta between current attempt and last failed login | ||
| 126 | my $delta = $now - $lastFailedLoginEpoch; | ||
| 127 | $self->logger->debug(" -> Delta = $delta"); | ||
| 128 | |||
| 129 | # Time to wait | ||
| 130 | # countFailed is from history and doesn't include the current | ||
| 131 | # attempt, so use countFailed (not countFailed-1) as index | ||
| 132 | my $waitingTime = $self->lockTimes->[$countFailed] | ||
| 133 | // $self->conf->{bruteForceProtectionMaxLockTime}; | ||
| 134 | |||
| 135 | # Reach last tempo. Stop to increase waiting time | ||
| 136 | if ( $countFailed >= scalar @{ $self->lockTimes } ) { | ||
| 137 | $self->userLogger->warn( | ||
| 138 | "BruteForceProtection: Last lock time has been reached"); | ||
| 139 | $self->logger->debug("Force waitingTime to last value"); | ||
| 140 | $waitingTime = | ||
| 141 | $self->lockTimes->[ scalar @{ $self->lockTimes } - 1 ]; | ||
| 142 | } | ||
| 143 | $self->logger->debug(" -> Waiting time = $waitingTime"); | ||
| 144 | |||
| 145 | # Delta < waitingTime => wait | ||
| 146 | if ( $waitingTime && $delta < $waitingTime ) { | ||
| 147 | $self->userLogger->warn("BruteForceProtection enabled"); | ||
| 148 | $req->authResult(PE_WAIT); | ||
| 149 | $req->lockTime( $waitingTime - $delta ); | ||
| 150 | return PE_WAIT; | ||
| 151 | } | ||
| 152 | return PE_OK; | ||
| 153 | } | ||
| 154 | |||
| 155 | # Include current failed attempt in the count | ||
| 156 | return PE_OK | ||
| 157 | if ( $countFailed + 1 < $self->maxFailed ); | ||
| 158 | |||
| 159 | # Delta between current attempt and last failed login | ||
| 160 | my $delta = $lastFailedLoginEpoch ? $now - $lastFailedLoginEpoch : 0; | ||
| 161 | $self->logger->debug(" -> Delta = $delta"); | ||
| 162 | |||
| 163 | # Delta < Tempo => wait | ||
| 164 | return PE_OK | ||
| 165 | unless ( $delta < $self->conf->{bruteForceProtectionTempo} | ||
| 166 | && $countFailed ); | ||
| 167 | |||
| 168 | # Account locked | ||
| 169 | $self->userLogger->warn("BruteForceProtection enabled"); | ||
| 170 | $self->logger->debug( | ||
| 171 | " -> Waiting time = $self->{conf}->{bruteForceProtectionTempo}"); | ||
| 172 | $req->lockTime( $self->conf->{bruteForceProtectionTempo} - $delta ); | ||
| 173 | return PE_WAIT; | ||
| 174 | } | ||
| 175 | |||
| 176 | 1; |