blib/lib/Lemonldap/NG/Portal/Lib/CrowdSec.pm untested

filter: Statementstmt 0.0% 0 / 233 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 97 MC/DCmcdc 0.0% 0 / 56 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 496 CC 44 SCAR 45.9
CC
44
Cov
0%
_banPayload
272.0
_extractAlertContext
72.0
bouncer
42.0
CRAP 98.2
1 package Lemonldap::NG::Portal::Lib::CrowdSec;
2  
3 use strict;
4 use Date::Parse;
5 use Digest::SHA qw(sha256_hex);
6 use JSON qw(from_json to_json);
7 use Mouse::Role;
8 use POSIX qw(strftime);
9 use Lemonldap::NG::Common::UserAgent;
10 use Lemonldap::NG::Portal::Main::Constants qw(
11 PE_OK
12 PE_ERROR
13 PE_SESSIONNOTGRANTED
14 );
15  
16 our $VERSION = '2.23.0';
17  
18 our $defaultBanValues = {
19 events_count => 1,
20 scenario => 'llng',
21 scenario_version => $VERSION,
22 leakspeed => '1s',
23 remediation => JSON::true,
24 simulated => JSON::false,
25 capacity => 1,
26 };
27  
28 has ua => (
29 is => 'rw',
30 lazy => 1,
31 builder => sub {
32 my $ua = Lemonldap::NG::Common::UserAgent->new( $_[0]->{conf} );
33 $ua->agent( 'LLNG-CrowdsecAgent/' . $VERSION );
34 $ua->default_headers->header( 'Accept' => 'application/json' );
35 $ua->default_headers->header( 'Content-Type' => 'application/json' );
36 return $ua;
37 }
38 );
39  
40 has token => ( is => 'rw' );
41 has tokenExp => ( is => 'rw', default => 0 );
42  
43 has crowdsecUrl => ( is => 'rw' );
44  
45 sub _init {
46 my ($self) = @_;
47 if ( $self->conf->{crowdsecUrl} ) {
48 my $tmp = $self->conf->{crowdsecUrl};
49 $tmp =~ s#/+$##;
50 $self->crowdsecUrl($tmp);
51 }
52 else {
53 $self->logger->warn(
54 "crowdsecUrl isn't set, fallback to http://localhost:8080");
55 $self->crowdsecUrl('http://localhost:8080');
56 }
57 return 1;
58 }
59  
60 sub bouncer {
61 my ( $self, $ip ) = @_;
62 my $resp = $self->ua->get(
63 $self->crowdsecUrl . "/v1/decisions?ip=$ip",
64 'Accept' => 'application/json',
65 'X-Api-Key' => $self->conf->{crowdsecKey},
66 );
67 if ( $resp->is_error ) {
68 $self->logger->error( 'Bad CrowdSec response: ' . $resp->message );
69 $self->logger->debug( $resp->content );
70 return ( 1, PE_ERROR );
71 }
72 my $content = $resp->decoded_content;
73 if ( !$content or $content eq 'null' ) {
74 $self->userLogger->debug("$ip isn't known by CrowdSec");
75 return ( 1, PE_OK );
76 }
77 my $json_hash;
78 eval { $json_hash = from_json( $content, { allow_nonref => 1 } ); };
79 if ($@) {
80 $self->logger->error("Unable to decode CrowdSec response: $content");
81 $self->logger->debug($@);
82 return ( 1, PE_ERROR );
83 }
84 $self->logger->debug("CrowdSec response: $content");
85  
86 # Response is "null" when IP is unknown
87 if ($json_hash) {
88  
89 # CrowdSec may return more than one decision
90 foreach my $decision (@$json_hash) {
91 if ( $decision->{type} and $decision->{type} eq 'ban' ) {
92 return ( 0,
93 "$ip banned by CrowdSec ('"
94 . $decision->{scenario}
95 . "' for $decision->{duration})" );
96 }
97 }
98 $self->userLogger->info("$ip not banned by CrowdSec");
99 return ( 1, PE_OK );
100 }
101 }
102  
103 sub ban {
104 my $self = shift;
105 my ( $ip, $msg, $data ) = @_;
106 if ( my $token = $self->getToken ) {
107 my $request = HTTP::Request->new(
108 POST => $self->crowdsecUrl . '/v1/alerts',
109 [
110 'Content-Type' => 'application/json',
111 'Authorization' => "Bearer " . $self->token,
112 ],
113 $self->_banPayload(@_)
114 );
115 my $resp = $self->ua->request($request);
116 unless ( $resp->is_success ) {
117 $self->logger->error(
118 join ' ', 'Unable to push alert',
119 $resp->status_line, $resp->decoded_content
120 );
121 return;
122 }
123 $self->logger->notice("Push new Crowdsec alert for $ip: $msg");
124 return 1;
125 }
126 else {
127 $self->logger->error('Unable to push Crowdsec decision');
128 return;
129 }
130 }
131  
132 sub alert {
133 my $self = shift;
134 my ( $ip, $msg, $data ) = @_;
135  
136 # No new ban decision for already banned IPs
137 my ($notBanned) = $self->bouncer($ip);
138  
139 $data ||= {};
140  
141 # Use per-scenario maxFailures if provided, otherwise use global config
142 my $max = $data->{maxFailures} // $self->conf->{crowdsecMaxFailures};
143  
144 # Get both count and previous alerts for enrichment
145 my @prevAlerts =
146 $self->getAlertsByIp( $ip, $data->{scenario}, $data->{timeWindow} );
147  
148 $data->{remediation} =
149 ( $max and @prevAlerts >= ( $max - 1 ) and $notBanned )
150 ? JSON::true
151 : JSON::false;
152 $self->logger->debug(
153 "Crowdsec alerts count is " . scalar(@prevAlerts) . " for $ip" );
154 $data->{reason} ||= 'Reported by LLNG';
155 $msg ||= 'Reported by LLNG';
156  
157 # Pass previous alerts for enrichment when ban decision is made
158 # Only include alerts that contributed to the decision (max - 1), capped at 30
159 if ( $data->{remediation} and @prevAlerts ) {
160 my $limit = ( $max && $max > 1 ) ? $max - 1 : 30;
161 $limit = 30 if $limit > 30;
162 if ( @prevAlerts > $limit ) {
163  
164 # Take most recent alerts (sorted by start_at desc)
165 my @sorted =
166 sort { ( $b->{start_at} || '' ) cmp( $a->{start_at} || '' ) }
167 @prevAlerts;
168 $data->{_prevAlerts} = [ @sorted[ 0 .. $limit - 1 ] ];
169 }
170 else {
171 $data->{_prevAlerts} = \@prevAlerts;
172 }
173 }
174  
175 return $self->ban( $ip, $msg, $data );
176 }
177  
178 # Extract context from previous alerts for enrichment
179 sub _extractAlertContext {
180 my ( $self, $prevAlerts ) = @_;
181 return unless $prevAlerts and @$prevAlerts;
182  
183 my ( @logins, @uris, @timestamps );
184 my ( %seen_logins, %seen_uris );
185  
186 foreach my $alert (@$prevAlerts) {
187  
188 # Extract timestamp
189 push @timestamps, $alert->{start_at} if $alert->{start_at};
190  
191 # Extract login and uri from events meta
192 if ( $alert->{events} and @{ $alert->{events} } ) {
193 foreach my $event ( @{ $alert->{events} } ) {
194 if ( $event->{meta} and @{ $event->{meta} } ) {
195 foreach my $m ( @{ $event->{meta} } ) {
196 if ( $m->{key} eq 'login'
197 and $m->{value}
198 and !$seen_logins{ $m->{value} }++ )
199 {
200 push @logins, $m->{value};
201 }
202 if ( $m->{key} eq 'uri'
203 and $m->{value}
204 and !$seen_uris{ $m->{value} }++ )
205 {
206 push @uris, $m->{value};
207 }
208 }
209 }
210 }
211 }
212 }
213  
214 # Sort timestamps chronologically
215 @timestamps = sort @timestamps if @timestamps;
216  
217 return {
218 logins => \@logins,
219 uris => \@uris,
220 first_alert => $timestamps[0],
221 last_alert => $timestamps[-1],
222 count => scalar @$prevAlerts,
223 };
224 }
225  
226 sub getAlertsByIp {
227 my ( $self, $ip, $scenario, $timeWindow ) = @_;
228 $scenario ||= $defaultBanValues->{scenario};
229 if ( my $token = $self->getToken ) {
230  
231 # Use API filters to avoid default 50 alerts limit
232 my $url =
233 $self->crowdsecUrl
234 . '/v1/alerts?scope=ip&value='
235 . $ip
236 . '&scenario='
237 . $scenario;
238 my $request = HTTP::Request->new(
239 GET => $url,
240 [
241 'Authorization' => "Bearer " . $self->token,
242 ],
243 );
244 my $resp = $self->ua->request($request);
245 unless ( $resp->is_success ) {
246 $self->logger->error(
247 join ' ', 'Unable to get alerts',
248 $resp->status_line, $resp->decoded_content
249 );
250 return ();
251 }
252 my $content = $resp->decoded_content;
253  
254 # CrowdSec returns "null" when there are no alerts
255 if ( !$content or $content eq 'null' ) {
256 $self->logger->debug("No alerts found for $ip");
257 return ();
258 }
259 my $alerts = eval { from_json($content) };
260 if ($@) {
261 $self->logger->error( join ' ', 'Unable to read Crowdsec response:',
262 $@, ", $content" );
263 return ();
264 }
265  
266 # Use per-scenario timeWindow if provided, otherwise use global config
267 # Default: 900 seconds (15 minutes)
268 my $delay = $timeWindow // $self->conf->{crowdsecBlockDelay} // 900;
269 my $timeLimit = time - $delay;
270  
271 # API already filters by IP and scenario, just filter by time
272 my @ipAlerts =
273 grep { str2time( $_->{start_at} ) > $timeLimit } @$alerts;
274  
275 # In list context, return both count and alerts for enrichment
276 return @ipAlerts;
277 }
278 else {
279 $self->logger->error('Unable to query Crowdsec');
280 return ();
281 }
282 }
283  
284 sub _banPayload {
285 my ( $self, $ip, $msg, $data ) = @_;
286 $msg ||= 'Banned by LLNG Crowdsec plugin';
287 $data ||= {};
288  
289 # Extract and remove internal data
290 my $prevAlerts = delete $data->{_prevAlerts};
291  
292 foreach my $k ( keys %$defaultBanValues ) {
293 $data->{$k} //= $defaultBanValues->{$k};
294 }
295 my $timestamp = strftime( "%Y-%m-%dT%H:%M:%SZ", gmtime );
296 $data->{start_at} ||= $timestamp;
297 $data->{stop_at} ||= $timestamp;
298 $data->{source} ||= { scope => 'ip', value => $ip };
299 my $reason = delete( $data->{reason} ) || 'Banned by LLNG';
300 my $login = delete( $data->{login} );
301 my $uri = delete( $data->{uri} );
302  
303 # Include login in message for CAPI transmission
304 # (events[].meta is NOT forwarded by LAPI to CAPI)
305 $msg .= " (login: $login)" if $login;
306  
307 $data->{scenario_hash} = sha256_hex( $data->{scenario} );
308 my @meta = (
309 { key => 'log_type', value => 'llng-auth' },
310 { key => 'reason', value => $reason }
311 );
312 push @meta, { key => 'login', value => $login } if $login;
313 push @meta, { key => 'uri', value => $uri } if $uri;
314  
315 # Enrich with historical context when ban decision is made
316 if ( $data->{remediation} and $prevAlerts ) {
317 my $context = $self->_extractAlertContext($prevAlerts);
318 if ($context) {
319 my @msg_parts;
320  
321 # Add historical metadata
322 if ( @{ $context->{logins} } ) {
323 my $all_logins = join ', ', @{ $context->{logins} };
324 push @meta, { key => 'previous_logins', value => $all_logins };
325  
326 # Truncate for message display
327 my $login_summary =
328 @{ $context->{logins} } <= 3
329 ? $all_logins
330 : join( ', ', @{ $context->{logins} }[ 0 .. 2 ] ) . '...';
331 push @msg_parts, "attempted logins: $login_summary";
332 }
333  
334 if ( @{ $context->{uris} } ) {
335 my $all_uris = join ', ', @{ $context->{uris} };
336 push @meta, { key => 'previous_uris', value => $all_uris };
337  
338 # Truncate for message display
339 my $uri_summary =
340 @{ $context->{uris} } <= 3
341 ? $all_uris
342 : join( ', ', @{ $context->{uris} }[ 0 .. 2 ] ) . '...';
343 push @msg_parts, "scanned URIs: $uri_summary";
344 }
345  
346 if ( $context->{count} ) {
347 push @meta,
348 {
349 key => 'previous_alert_count',
350 value => "" . $context->{count}
351 };
352 }
353  
354 if ( $context->{first_alert} ) {
355 push @meta,
356 { key => 'first_alert', value => $context->{first_alert} };
357 }
358  
359 if ( $context->{last_alert} ) {
360 push @meta,
361 { key => 'last_alert', value => $context->{last_alert} };
362 }
363  
364 # Enrich human-readable message
365 if ( $context->{count} ) {
366 $msg .= sprintf "; after %d previous alert(s)",
367 $context->{count};
368 }
369 if (@msg_parts) {
370 $msg .= "; " . join( "; ", @msg_parts );
371 }
372 }
373 }
374  
375 my $alert = {
376 %$data,
377 message => $msg,
378 events => [ {
379 timestamp => $data->{start_at},
380 meta => \@meta,
381 source => $data->{source},
382 }
383 ],
384 };
385  
386 # Add decisions array when remediation is requested
387 # This ensures the IP is immediately banned by CrowdSec
388 if ( $data->{remediation} ) {
389 my $duration =
390 $data->{banDuration} || $self->conf->{crowdsecBanDuration} || '4h';
391 $alert->{decisions} = [ {
392 duration => $duration,
393 type => 'ban',
394 scope => 'ip',
395 value => $ip,
396 origin => 'llng',
397 scenario => $data->{scenario},
398 }
399 ];
400 }
401  
402 return to_json( [$alert] );
403 }
404  
405 sub getToken {
406 my ($self) = @_;
407  
408 # Use token if available
409 return $self->token if $self->token and $self->tokenExp > time + 10;
410  
411 # Get new token
412 my ( $user, $pwd ) =
413 map { $self->conf->{$_} } qw(crowdsecMachineId crowdsecPassword);
414 unless ( $user and $pwd ) {
415 $self->logger->error('Missing crowdsec credentials, aborting');
416 return;
417 }
418 my $request = HTTP::Request->new(
419 POST => $self->crowdsecUrl . '/v1/watchers/login',
420 [ 'Content-Type' => 'application/json' ],
421 to_json( {
422 machine_id => $user,
423 password => $pwd,
424 }
425 )
426 );
427 my $resp = $self->ua->request($request);
428 unless ( $resp->is_success ) {
429 $self->logger->error( join ' ', 'Unable to connect to Crowdsec:',
430 $resp->status_line, $resp->decoded_content );
431 return;
432 }
433 my $json = eval { from_json( $resp->decoded_content ) };
434 if ( $@ or !$json ) {
435 $self->logger->error( join ' ', 'Unable to read Crowdsec response:',
436 $@, $resp->decoded_content );
437 return;
438 }
439 $self->tokenExp( str2time( $json->{expire} ) );
440 $self->logger->debug("Get Crowdsec token");
441 return $self->token( $json->{token} );
442 }
443  
444 1;