« blib/lib/Lemonldap/NG/Portal/Lib/LDAP.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Auth/Facebook.pm »
| 1 | package Lemonldap::NG::Portal::Plugins::CrowdSecAgent; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Mouse; | ||
| 5 | use Lemonldap::NG::Portal::Main::Constants qw( | ||
| 6 | PE_BADCERTIFICATE | ||
| 7 | PE_BADCREDENTIALS | ||
| 8 | PE_BADOTP | ||
| 9 | PE_CAPTCHAERROR | ||
| 10 | PE_MALFORMEDUSER | ||
| 11 | PE_OK | ||
| 12 | PE_OPENID_BADID | ||
| 13 | PE_SAML_SIGNATURE_ERROR | ||
| 14 | PE_SENDRESPONSE | ||
| 15 | PE_USERNOTFOUND | ||
| 16 | PE_WAIT | ||
| 17 | ); | ||
| 18 | |||
| 19 | our $VERSION = '2.23.0'; | ||
| 20 | |||
| 21 | our @ALERTS = ( | ||
| 22 | PE_USERNOTFOUND, PE_BADCREDENTIALS, | ||
| 23 | PE_BADCERTIFICATE, PE_MALFORMEDUSER, | ||
| 24 | PE_SAML_SIGNATURE_ERROR, PE_OPENID_BADID, | ||
| 25 | PE_CAPTCHAERROR, PE_BADOTP, | ||
| 26 | PE_WAIT, | ||
| 27 | ); | ||
| 28 | |||
| 29 | extends 'Lemonldap::NG::Portal::Main::Plugin'; | ||
| 30 | with 'Lemonldap::NG::Portal::Lib::CrowdSec'; | ||
| 31 | |||
| 32 | has rule => ( is => 'rw', default => sub { 0 } ); | ||
| 33 | |||
| 34 | has filters => ( is => 'rw', default => sub { {} } ); | ||
| 35 | |||
| 36 | has scenarios => ( is => 'rw', default => sub { {} } ); | ||
| 37 | |||
| 38 | has scenarioMaxFailures => ( is => 'rw', default => sub { {} } ); | ||
| 39 | |||
| 40 | has scenarioBanDuration => ( is => 'rw', default => sub { {} } ); | ||
| 41 | |||
| 42 | has scenarioTimeWindow => ( is => 'rw', default => sub { {} } ); | ||
| 43 | |||
| 44 | # Entrypoint | ||
| 45 | use constant aroundSub => { | ||
| 46 | |||
| 47 | # Filter function (if crowdsecFilters is set) | ||
| 48 | controlUrl => 'controlUrl', | ||
| 49 | |||
| 50 | # Generate alerts for bad credentials | ||
| 51 | getUser => 'sendIpAlerts', | ||
| 52 | authenticate => 'sendIpAlerts' | ||
| 53 | }; | ||
| 54 | |||
| 55 | sub init { | ||
| 56 | my ($self) = @_; | ||
| 57 | $self->_init or return 0; | ||
| 58 | unless ($self->conf->{crowdsecMachineId} | ||
| 59 | and $self->conf->{crowdsecPassword} ) | ||
| 60 | { | ||
| 61 | $self->logger->error( | ||
| 62 | 'Crowdsec report enabled without machine_id and password'); | ||
| 63 | return 0; | ||
| 64 | } | ||
| 65 | $self->rule( | ||
| 66 | $self->p->buildRule( $self->conf->{crowdsecAgent}, 'crowdsecAgent' ) ); | ||
| 67 | |||
| 68 | if ( $self->conf->{crowdsecFilters} ) { | ||
| 69 | if ( -d $self->conf->{crowdsecFilters} ) { | ||
| 70 | with 'Lemonldap::NG::Portal::Lib::CrowdSecFilter'; | ||
| 71 | $self->initializeFilters; | ||
| 72 | } | ||
| 73 | else { | ||
| 74 | $self->logger->error( | ||
| 75 | 'Crowdsec filter directory not found, ignoring'); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | $self->conf->{crowdSecAgentResponseCode} ||= 404; | ||
| 79 | |||
| 80 | return 1; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub sendIpAlerts { | ||
| 84 | my ( $self, $sub, $req ) = @_; | ||
| 85 | my $ret = $sub->($req); | ||
| 86 | |||
| 87 | if ( !$self->rule->( $req, $req->sessionInfo ) ) { | ||
| 88 | $self->logger->debug('Crowdsec-agent disabled for this env'); | ||
| 89 | return $ret; | ||
| 90 | } | ||
| 91 | |||
| 92 | # Nothing to do if config doesn't allow alerts or if auth is OK | ||
| 93 | unless ( grep { $_ == $ret } @ALERTS ) { | ||
| 94 | $self->logger->debug( | ||
| 95 | "Crowdsec-Agent: not an authentication error: code $ret"); | ||
| 96 | return $ret; | ||
| 97 | } | ||
| 98 | my $msg = 'Authentication failed: ' | ||
| 99 | . &Lemonldap::NG::Portal::Main::Constants::portalConsts->{$ret}; | ||
| 100 | my $login = $req->user // ''; | ||
| 101 | $self->alert( $req->address, $msg, | ||
| 102 | { scenario => 'llng/badcredentials', reason => $msg, login => $login } ) | ||
| 103 | ? $self->auditLog( | ||
| 104 | $req, | ||
| 105 | code => 200, | ||
| 106 | message => "Alert sent to Crowdsec: $msg", | ||
| 107 | ip => $req->address | ||
| 108 | ) | ||
| 109 | : $self->logger->error( | ||
| 110 | "Unable to send alert to Crowdsec (was '$msg' for " . $req->address ); | ||
| 111 | return $ret; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub controlUrl { | ||
| 115 | my ( $self, $sub, $req ) = @_; | ||
| 116 | my $ret = $sub->($req); | ||
| 117 | return $ret if $ret; | ||
| 118 | |||
| 119 | if ( !$self->rule->( $req, $req->sessionInfo ) ) { | ||
| 120 | $self->logger->debug('Crowdsec-agent disabled for this env'); | ||
| 121 | return $ret; | ||
| 122 | } | ||
| 123 | |||
| 124 | return $ret unless %{ $self->filters }; | ||
| 125 | |||
| 126 | # Use matchScenario to find matching scenario (with whitelist check) | ||
| 127 | # Use the routed URI: an encoded URL would avoid the scenarios below | ||
| 128 | my $uri = $req->access_control_uri; | ||
| 129 | my ( $scenario, $cat, $maxFailures, $banDuration, $timeWindow ) = | ||
| 130 | $self->matchScenario($uri); | ||
| 131 | |||
| 132 | # No match or whitelisted | ||
| 133 | return $ret unless $scenario; | ||
| 134 | |||
| 135 | my $msg = "Bad URI detected: $uri"; | ||
| 136 | my $data = { scenario => $scenario, reason => $msg, uri => $uri }; | ||
| 137 | $data->{maxFailures} = $maxFailures if defined $maxFailures; | ||
| 138 | $data->{banDuration} = $banDuration if defined $banDuration; | ||
| 139 | $data->{timeWindow} = $timeWindow if defined $timeWindow; | ||
| 140 | $self->alert( $req->address, $msg, $data ) | ||
| 141 | ? $self->auditLog( | ||
| 142 | $req, | ||
| 143 | code => 200, | ||
| 144 | message => "Alert sent to Crowdsec: $msg", | ||
| 145 | ip => $req->address, | ||
| 146 | uri => $uri, | ||
| 147 | scenario => $scenario, | ||
| 148 | ) | ||
| 149 | : $self->logger->error( | ||
| 150 | "Unable to send alert to Crowdsec (was '$msg' for " . $req->address ); | ||
| 151 | |||
| 152 | $req->response( | ||
| 153 | |||
| 154 | # Case "redirection", crowdSecAgentResponseValue is the "location" value | ||
| 155 | $self->conf->{crowdSecAgentResponseCode} =~ /^3/ | ||
| 156 | ? $self->p->sendRawHtml( | ||
| 157 | $req, | ||
| 158 | $self->conf->{crowdSecAgentResponseValue}, | ||
| 159 | { | ||
| 160 | code => $self->conf->{crowdSecAgentResponseCode}, | ||
| 161 | headers => [ | ||
| 162 | Location => ( | ||
| 163 | $self->conf->{crowdSecAgentResponseValue} | ||
| 164 | || 'https://somewhere.else' | ||
| 165 | ) | ||
| 166 | ], | ||
| 167 | } | ||
| 168 | ) | ||
| 169 | |||
| 170 | # Case "custom response | ||
| 171 | : $self->conf->{crowdSecAgentResponseValue} ? $self->p->sendRawHtml( | ||
| 172 | $req, | ||
| 173 | $self->conf->{crowdSecAgentResponseValue}, | ||
| 174 | { code => $self->conf->{crowdSecAgentResponseCode} } | ||
| 175 | ) | ||
| 176 | |||
| 177 | # Case "default Lemon response | ||
| 178 | : $self->sendError( | ||
| 179 | $req, '', $self->conf->{crowdSecAgentResponseCode} | ||
| 180 | ) | ||
| 181 | ); | ||
| 182 | return PE_SENDRESPONSE; | ||
| 183 | } | ||
| 184 | |||
| 185 | 1; |