BlockerHelper/backends/abuseipdb.pm

filter: Statementstmt 46.3% 121 / 261 Branchbran 45.0% 45 / 100 Conditioncond 47.6% 20 / 42 MC/DCmcdc 24.0% 6 / 25 Subroutinesub 70.8% 17 / 24 Podpod 100% 13 / 13 totaltotal 47.7% 222 / 465 CC 66 SCAR 36.6
CC
66
Cov
46%
_request
72.0
new
69.9
ban
39.6
CRAP 38.8
« BlockerHelper/backends/vyos.pm ↑ Summary BlockerHelper/backends/bgp_rtbh.pm »
1 package Net::Firewall::BlockerHelper::backends::abuseipdb;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use base 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Regexp::IPv4 qw($IPv4_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Regexp::IPv6 qw($IPv6_re);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::Firewall::BlockerHelper::backends::abuseipdb - Report banned IPs to AbuseIPDB.
13  
14 =head1 VERSION
15  
16 Version 0.1.0
17  
18 =cut
19  
20 1 our $VERSION = '0.1.0';
21  
22 =head1 SYNOPSIS
23  
24 use Net::Firewall::BlockerHelper;
25  
26 my $fw_helper = Net::Firewall::BlockerHelper->new(
27 backend => 'abuseipdb',
28 name => 'ssh',
29 options => {
30 key => $abuseipdb_api_key,
31 categories => [ '18', '22' ],
32 },
33 );
34  
35 $fw_helper->init_backend;
36 $fw_helper->ban( ban => '1.2.3.4' );
37  
38 =head1 DESCRIPTION
39  
40 Reports banned IPs to L<AbuseIPDB|https://www.abuseipdb.com/> via its v2 REST
41 API, equivalent to the fail2ban abuseipdb action. This backend blocks nothing
42 itself; it is meant to be used alongside a blocking backend so a ban both
43 blocks locally and reports upstream.
44  
45 Reports can not be withdrawn, so unban, flush, and teardown are purely
46 internal bookkeeping and touch the API not at all. re_init likewise does not
47 re-report anything, as there is no remote state to restore.
48  
49 Reporting the same IP again within AbuseIPDB's rate limit window (15 minutes
50 at the time of writing) is answered with a HTTP 429, which this backend
51 treats as already reported rather than as an error, so replaying saved ban
52 state through ban does not error out.
53  
54 Both IPv4 and IPv6 are supported. Reporting is per IP; ports and protocols are
55 not supported and specifying them is an error.
56  
57 The comment sent with each report is publicly visible on AbuseIPDB, so do not
58 put anything sensitive in the C<comment> option.
59  
60 L<LWP::UserAgent> is loaded at run time, so it is only required when this
61 backend is actually used. As the API is https only, L<LWP::Protocol::https>
62 must be present as well.
63  
64 =head1 METHODS
65  
66 =head2 new
67  
68 Initiates the backend object. No API calls are made until L</init>.
69  
70 - options :: Backend specific options. See below.
71 - prefix :: Prefix to use. Must match /^[a-zA-Z0-9]+$/. Default kur.
72 - name :: Name of this instance. Required.
73  
74 Ports and protocols are not supported and specifying either is an error.
75  
76 The options hash accepts the following.
77  
78 - key :: AbuseIPDB API key. Required.
79 - Default :: undef
80  
81 - categories :: Report categories, as either a array ref of category
82 numbers or a comma separated string of them. See
83 L<https://www.abuseipdb.com/categories> for the meanings; the
84 default, 18, is Brute-Force.
85 - Default :: 18
86  
87 - comment :: Comment attached to each report. '%%%BAN%%%' is replaced
88 with the IP being reported. Publicly visible on AbuseIPDB. If
89 blank, no comment is sent.
90 - Default :: banned by <prefix>_<name>
91  
92 - timeout :: HTTP timeout in seconds.
93 - Default :: 30
94  
95 All errors are considered fatal, meaning if new fails it will die.
96  
97 =cut
98  
99 sub new {
100 8 ▶ my ( $blank, %opts ) = @_;
Subroutine new: called CC=30 SCAR=42.5
Pod documented
101  
102 8 my $self = {
103 perror => undef,
104 error => undef,
105 errorLine => undef,
106 errorFilename => undef,
107 errorString => "",
108 errorExtra => {
109 all_errors_fatal => 1,
110 # all_fatal is what Error::Helper 2.1.0 actually checks; all_errors_fatal
111 # is kept for the name documented in its POD
112 all_fatal => 1,
113 flags => {
114 1 => 'notInited',
115 6 => 'invalidPrefixSpecified',
116 7 => 'invalidName',
117 8 => 'optionsNotHash',
118 9 => 'noBanItem',
119 10 => 'banItemNotIP',
120 12 => 'backendInitError',
121 13 => 'banFailed',
122 14 => 'unbanFailed',
123 15 => 'listFailed',
124 16 => 'reInitFailed',
125 17 => 'teardownFailed',
126 18 => 'alreadyInited',
127 23 => 'initFailed',
128 24 => 'checkFailed',
129 25 => 'flushFailed',
130 26 => 'portsNotSupported',
131 27 => 'protocolsNotSupported',
132 30 => 'keyNotDefined',
133 31 => 'categoriesInvalid',
134 32 => 'banCidrFailed',
135 33 => 'unbanCidrFailed',
136 34 => 'cidrItemNotCidr',
137 35 => 'cidrNotSupported',
138 36 => 'listCidrFailed',
139 },
140 fatal_flags => {},
141 perror_not_fatal => 0,
142 },
143 options => {},
144 ports => [],
145 protocols => [],
146 testing => undef,
147 test_data => undef,
148 prefix => 'kur',
149 name => undef,
150 frontend_obj => undef,
151 inited => 0,
152 banned => {},
153 cidr_supported => 0,
154 ua => undef,
155 };
156 8 bless $self;
157  
158 # reporting is per IP; ports and protocols have no meaning here
159 8 ▶ if ( defined( $opts{ports} ) && ref( $opts{ports} ) eq 'ARRAY' && defined( $opts{ports}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]) 1 7 8
Condition50%3/6
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]
!ll&&!rl&&r
071
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY"
!ll&&!rl&&r
008
MC/DC33%1/3
defined $opts{"ports"} and ref $opts{"ports"} eq "ARRAY" and defined $opts{"ports"}[0]
defined $opts{"ports"} ref $opts{"ports"} eq "ARRAY" defined $opts{"ports"}[0]
Truth table50%2/4
A and B and C
A= defined $opts{"ports"}B= ref $opts{"ports"} eq "ARRAY"C= defined $opts{"ports"}[0]
ABCresult
0XX0
10X0
1100
1111
160 1 $self->{perror} = 1;
161 1 $self->{error} = 26;
162 1 $self->{errorString} = 'the abuseipdb backend reports whole IPs and does not support ports';
163 1 $self->warn;
164 }
165 7 ▶ if ( defined( $opts{protocols} ) && ref( $opts{protocols} ) eq 'ARRAY' && defined( $opts{protocols}[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]) 0 7 7
Condition33%2/6
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]
!ll&&!rl&&r
070
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY"
!ll&&!rl&&r
007
MC/DC0%0/3
defined $opts{"protocols"} and ref $opts{"protocols"} eq "ARRAY" and defined $opts{"protocols"}[0]
defined $opts{"protocols"} ref $opts{"protocols"} eq "ARRAY" defined $opts{"protocols"}[0]
Truth table25%1/4
A and B and C
A= defined $opts{"protocols"}B= ref $opts{"protocols"} eq "ARRAY"C= defined $opts{"protocols"}[0]
ABCresult
0XX0
10X0
1100
1111
166 0 $self->{perror} = 1;
167 0 $self->{error} = 27;
168 0 $self->{errorString} = 'the abuseipdb backend reports whole IPs and does not support protocols';
169 0 $self->warn;
170 }
171  
172 # make sure prefix is sane if defined
173 7 ▶ if ( defined( $opts{prefix} ) && $opts{prefix} !~ /^[a-zA-Z0-9]+$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'prefix'} and not $opts{'prefix'} =~ /^[a-zA-Z0-9]+$/) { } 0 7 7
elsif (defined $opts{'prefix'}) { } 7 0 7
Condition33%1/3
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
!ll&&!rl&&r
070
MC/DC0%0/2
defined $opts{"prefix"} and not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
defined $opts{"prefix"} not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
Truth table33%1/3
A and not(B)
A= defined $opts{"prefix"}B= not $opts{"prefix"} =~ /^[a-zA-Z0-9]+$/
ABresult
0X0
100
111
174 0 $self->{perror} = 1;
175 0 $self->{error} = 6;
176 $self->{errorString}
177 0 = '"' . $opts{prefix} . '" is not a valid prefix as it does not match the regex /^[a-zA-Z0-9]+$/';
178 0 $self->warn;
179 } elsif ( defined( $opts{prefix} ) ) {
180 7 $self->{prefix} = $opts{prefix};
181 }
182  
183 # make sure we have a name and that it is valid
184 7 ▶ if ( !defined( $opts{name} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts{'name'}) { } 0 7 7
elsif (not $opts{'name'} =~ /^[a-zA-Z0-9\-]+$/) { } 0 7 7
185 0 $self->{perror} = 1;
186 0 $self->{error} = 7;
187 0 $self->{errorString} = 'name is undef';
188 0 $self->warn;
189 } elsif ( $opts{name} !~ /^[a-zA-Z0-9\-]+$/ ) {
190 0 $self->{perror} = 1;
191 0 $self->{error} = 7;
192 0 $self->{errorString} = 'name set to "' . $opts{name} . '" which does not match the regexp /^[a-zA-Z0-9\-]+$/';
193 0 $self->warn;
194 }
195 7 $self->{name} = $opts{name};
196  
197 7 ▶ if ( defined( $opts{testing} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"testing"}) 7 0 7
198 7 $self->{testing} = $opts{testing};
199 }
200 7 ▶ if ( defined( $opts{frontend_obj} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"frontend_obj"}) 7 0 7
201 7 $self->{frontend_obj} = $opts{frontend_obj};
202 }
203  
204 7 ▶ if ( defined( $opts{options} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"options"}) 7 0 7
205 7 ▶ if ( ref( $opts{options} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{"options"} ne "HASH") 0 7 7
206 0 $self->{perror} = 1;
207 0 $self->{error} = 8;
208 0 $self->{errorString} = 'ref for options is "' . ref( $opts{options} ) . '" and not HASH';
209 0 $self->warn;
210 }
211 7 $self->{options} = $opts{options};
212 }
213  
214 # required options
215 7 ▶ if ( !defined( $self->{options}{key} ) || $self->{options}{key} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"key"} or $self->{"options"}{"key"} eq "") 1 6 7
Condition66%2/3
not defined $self->{"options"}{"key"} or $self->{"options"}{"key"} eq ""
l!l&&r!l&&!r
106
MC/DC50%1/2
not defined $self->{"options"}{"key"} or $self->{"options"}{"key"} eq ""
not defined $self->{"options"}{"key"} $self->{"options"}{"key"} eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->{"options"}{"key"}B= $self->{"options"}{"key"} eq ""
ABresult
1X1
011
000
216 1 $self->{perror} = 1;
217 1 $self->{error} = 30;
218 1 $self->{errorString} = 'the option key is undef or blank';
219 1 $self->warn;
220 }
221  
222 # categories may be a array ref of category numbers or a comma separated
223 # string of them... normalized to the comma separated string form
224 6 ▶ if ( !defined( $self->{options}{categories} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $self->{'options'}{'categories'}) { } 1 5 6
elsif (ref $self->{'options'}{'categories'} eq 'ARRAY') { } 2 3 5
elsif (ref $self->{'options'}{'categories'} ne '' or not $self->{'options'}{'categories'} =~ /^[0-9]+(,[0-9]+)*$/) { } 2 1 3
225 1 $self->{options}{categories} = '18';
226 } elsif ( ref( $self->{options}{categories} ) eq 'ARRAY' ) {
227 2 foreach my $item ( @{ $self->{options}{categories} } ) {
228 4 ▶ if ( !defined($item) || ref($item) ne '' || $item !~ /^[0-9]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $item or ref $item ne "" or not $item =~ /^[0-9]+$/) 1 3 4
Condition50%3/6
not defined $item or ref $item ne "" or not $item =~ /^[0-9]+$/
l!l&&r!l&&!r
013
not defined $item or ref $item ne ""
l!l&&r!l&&!r
004
MC/DC33%1/3
not defined $item or ref $item ne "" or not $item =~ /^[0-9]+$/
not defined $item ref $item ne "" not $item =~ /^[0-9]+$/
Truth table50%2/4
not(A) or B or not(C)
A= not defined $itemB= ref $item ne ""C= not $item =~ /^[0-9]+$/
ABCresult
1XX1
01X1
0011
0000
229 1 $self->{perror} = 1;
230 1 $self->{error} = 31;
231 $self->{errorString}
232 1 ▶ = 'categories contains "'
Branch: 1 branch
BranchTrueFalseTotal
defined $item ? : 1 0 1
233 . ( defined($item) ? $item : 'undef' )
234 . '" which is not a positive int';
235 1 $self->warn;
236 }
237 }
238 1 $self->{options}{categories} = join( ',', @{ $self->{options}{categories} } );
239 ▶ } elsif ( ref( $self->{options}{categories} ) ne '' || $self->{options}{categories} !~ /^[0-9]+(,[0-9]+)*$/ ) {
Condition66%2/3
ref $self->{"options"}{"categories"} ne "" or not $self->{"options"}{"categories"} =~ /^[0-9]+(,[0-9]+)*$/
l!l&&r!l&&!r
021
MC/DC50%1/2
ref $self->{"options"}{"categories"} ne "" or not $self->{"options"}{"categories"} =~ /^[0-9]+(,[0-9]+)*$/
ref $self->{"options"}{"categories"} ne "" not $self->{"options"}{"categories"} =~ /^[0-9]+(,[0-9]+)*$/
Truth table66%2/3
A or not(B)
A= ref $self->{"options"}{"categories"} ne ""B= not $self->{"options"}{"categories"} =~ /^[0-9]+(,[0-9]+)*$/
ABresult
1X1
011
000
240 2 $self->{perror} = 1;
241 2 $self->{error} = 31;
242 $self->{errorString}
243 = 'categories is "'
244 . $self->{options}{categories}
245 2 . '" and not a array ref or comma separated string of positive ints';
246 2 $self->warn;
247 }
248  
249 # defaults
250 3 ▶ $self->{options}{timeout} = 30 if ( !defined( $self->{options}{timeout} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"timeout"} 3 0 3
251 3 ▶ if ( !defined( $self->{options}{comment} ) && defined( $self->{name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"options"}{"comment"} and defined $self->{"name"}) 1 2 3
Condition66%2/3
not defined $self->{"options"}{"comment"} and defined $self->{"name"}
!ll&&!rl&&r
201
MC/DC50%1/2
not defined $self->{"options"}{"comment"} and defined $self->{"name"}
not defined $self->{"options"}{"comment"} defined $self->{"name"}
Truth table66%2/3
not(A) and B
A= not defined $self->{"options"}{"comment"}B= defined $self->{"name"}
ABresult
0X0
100
111
252 1 $self->{options}{comment} = 'banned by ' . $self->{prefix} . '_' . $self->{name};
253 }
254  
255 3 return $self;
256 } ## end sub new
257  
258 # Internal helper. Returns the base URL of the AbuseIPDB v2 API.
259 sub _api_base {
260 12 ▶ my ($self) = @_;
Subroutine _api_base: called CC=1 SCAR=0.0
261  
262 12 return 'https://api.abuseipdb.com/api/v2';
263 }
264  
265 # Internal helper. Returns the URL used by init and check to verify the API
266 # key and reachability, a minimal check call.
267 sub _check_url {
268 8 ▶ my ($self) = @_;
Subroutine _check_url: called CC=1 SCAR=0.0
269  
270 8 return $self->_api_base . '/check?ipAddress=127.0.0.2&maxAgeInDays=1';
271 }
272  
273 # Internal helper. Minimal percent encoder so URI::Escape is not needed.
274 sub _uri_escape {
275 11 ▶ my ( $self, $string ) = @_;
Subroutine _uri_escape: called CC=1 SCAR=0.0
276  
277 11 $string =~ s/([^A-Za-z0-9\-._~])/sprintf('%%%02X', ord($1))/ge;
278  
279 11 return $string;
280 }
281  
282 # Internal helper. Returns the form encoded body for reporting the passed IP,
283 # with '%%%BAN%%%' in the comment replaced by the IP. A blank comment is left
284 # out entirely.
285 sub _report_body {
286 4 ▶ my ( $self, $ip ) = @_;
Subroutine _report_body: called CC=3 SCAR=11.0
287  
288 4 my $body = 'ip=' . $self->_uri_escape($ip) . '&categories=' . $self->_uri_escape( $self->{options}{categories} );
289  
290 4 ▶ if ( defined( $self->{options}{comment} ) && $self->{options}{comment} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"options"}{"comment"} and $self->{"options"}{"comment"} ne "") 3 1 4
Condition66%2/3
defined $self->{"options"}{"comment"} and $self->{"options"}{"comment"} ne ""
!ll&&!rl&&r
013
MC/DC50%1/2
defined $self->{"options"}{"comment"} and $self->{"options"}{"comment"} ne ""
defined $self->{"options"}{"comment"} $self->{"options"}{"comment"} ne ""
Truth table66%2/3
A and B
A= defined $self->{"options"}{"comment"}B= $self->{"options"}{"comment"} ne ""
ABresult
0X0
100
111
291 3 my $comment = $self->{options}{comment};
292 3 $comment =~ s/\%\%\%BAN\%\%\%/$ip/g;
293 3 $body .= '&comment=' . $self->_uri_escape($comment);
294 }
295  
296 4 return $body;
297 } ## end sub _report_body
298  
299 # Internal helper. Performs a HTTP request via LWP::UserAgent with the API key
300 # header, dying with an explanation on any HTTP level failure. Status codes
301 # listed in the passed tolerate hash ref are returned as undef rather than
302 # died on. Never called in testing mode.
303 sub _request {
304 0 ▶ my ( $self, $method, $url, $body, $tolerate ) = @_;
Subroutine _request: not called CC=8 SCAR=42.8
305  
306 0 ▶ if ( !defined( $self->{ua} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ua"}) 0 0 0
307 0 local $@;
308 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require LWP::UserAgent; $self->{"ua"} = "LWP::UserAgent"->new("agent", 'Net::Firewall::BlockerHelper/' . $Net::Firewall::BlockerHelper::backends::abuseipdb::VERSION, "timeout", $self->{"options"}{"timeout"}); 1 } } or die 'failed to load LWP::UserAgent, which the abuseipdb backend requires... ' . $@ 0 0 0
309 0 require LWP::UserAgent;
310 $self->{ua} = LWP::UserAgent->new(
311 agent => 'Net::Firewall::BlockerHelper/' . $VERSION,
312 timeout => $self->{options}{timeout},
313 0 );
314 0 1;
315 } or die( 'failed to load LWP::UserAgent, which the abuseipdb backend requires... ' . $@ );
316 }
317  
318 0 my @headers = ( 'Key' => $self->{options}{key}, 'Accept' => 'application/json' );
319 0 ▶ if ( defined($body) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) 0 0 0
320 0 push( @headers, 'Content-Type' => 'application/x-www-form-urlencoded' );
321 }
322  
323 0 require HTTP::Request;
324 0 my $request = HTTP::Request->new( $method, $url, \@headers, $body );
325  
326 0 my $response = $self->{ua}->request($request);
327  
328 0 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
329 0 ▶ if ( defined($tolerate) && $tolerate->{ $response->code } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tolerate and $$tolerate{$response->code}) 0 0 0
Condition0%0/3
defined $tolerate and $$tolerate{$response->code}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $tolerate and $$tolerate{$response->code}
defined $tolerate $$tolerate{$response->code}
Truth table0%0/3
A and B
A= defined $tolerateB= $$tolerate{$response->code}
ABresult
0X0
100
111
330 0 return undef;
331 }
332 0 my $detail = $response->decoded_content;
333 0 ▶ $detail = defined($detail) ? ( ' body... ' . $detail ) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $detail ? : 0 0 0
334 0 die( $method . ' ' . $url . ' failed... HTTP status... ' . $response->status_line . $detail );
335 }
336  
337 0 return $response->decoded_content;
338 } ## end sub _request
339  
340 =head2 init
341  
342 Initiates the backend. Verifies the API key and reachability via a minimal
343 GET to the v2 C</check> endpoint. Nothing is created remotely; there is no
344 remote state to set up.
345  
346 =cut
347  
348 sub init {
349 3 ▶ my ( $self, %opts ) = @_;
Subroutine init: called CC=4 SCAR=20.9
Pod documented
350  
351 3 $self->errorblank;
352  
353 3 ▶ if ( $self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inited"}) 0 3 3
354 0 $self->{error} = 18;
355 0 $self->{errorString} = 'backend has already been inited';
356 0 $self->warn;
357 }
358  
359 3 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 3 0 3
360 3 $self->{frontend_obj}->{test_data} = [ { method => 'GET', url => $self->_check_url } ];
361 } else {
362 0 local $@;
363 0 ▶ eval { $self->_request( 'GET', $self->_check_url ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("GET", $self->_check_url); 1 } } or do { $self->{"error"} = 23; $self->{"errorString"} = 'init failed. probing the check endpoint failed... ' . $@; $self->warn } 0 0 0
364 0 $self->{error} = 23;
365 0 $self->{errorString} = 'init failed. probing the check endpoint failed... ' . $@;
366 0 $self->warn;
367 };
368 }
369  
370 3 $self->{inited} = 1;
371 } ## end sub init
372  
373 =head2 ban
374  
375 Bans the IP by reporting it. The value of ban is validated as being a IPv4 or
376 IPv6 address and lowercased, then reported via a POST to the v2 C</report>
377 endpoint with the configured categories and comment. A HTTP 429, AbuseIPDB's
378 answer to reporting the same IP again inside its rate limit window, is
379 treated as already reported rather than as an error. Banning an already
380 banned IP is a noop.
381  
382 $backend->ban(ban => $ip);
383  
384 =cut
385  
386 sub ban {
387 4 ▶ my ( $self, %opts ) = @_;
Subroutine ban: called CC=10 SCAR=36.8
Pod documented
388  
389 4 $self->errorblank;
390  
391 4 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 4 4
392 0 $self->{error} = 1;
393 0 $self->{errorString} = 'backend has not been inited';
394 0 $self->warn;
395 0 return;
396 }
397  
398 4 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 4 4
elsif (ref $opts{'ban'} ne '') { } 0 4 4
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::abuseipdb::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::abuseipdb::IPv6_re\z/) { } 0 4 4
399 0 $self->{error} = 9;
400 0 $self->{errorString} = 'Nothing specified for the value ban';
401 0 $self->warn;
402 0 return;
403 } elsif ( ref( $opts{ban} ) ne '' ) {
404 0 $self->{error} = 10;
405 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
406 0 $self->warn;
407 0 return;
408 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition66%2/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::abuseipdb::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::abuseipdb::IPv6_re\z/
l!l&&r!l&&!r
310
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::abuseipdb::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::abuseipdb::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::abuseipdb::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::abuseipdb::IPv6_re\z/
Truth table66%2/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::abuseipdb::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::abuseipdb::IPv6_re\z/
ABresult
1X1
011
000
409 && $opts{ban} !~ /\A$IPv6_re\z/ )
410 {
411 0 $self->{error} = 10;
412 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
413 0 $self->warn;
414 0 return;
415 }
416  
417 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
418 4 $opts{ban} = lc( $opts{ban} );
419  
420 4 ▶ if ( $self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"banned"}{$opts{"ban"}}) 0 4 4
421 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
422 0 $self->{frontend_obj}->{test_data} = 'already banned';
423 }
424 0 return;
425 }
426  
427 4 my $url = $self->_api_base . '/report';
428 4 my $body = $self->_report_body( $opts{ban} );
429  
430 4 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'testing'}) { } 4 0 4
431 4 $self->{frontend_obj}->{test_data} = [ { method => 'POST', url => $url, content => $body } ];
432 } else {
433 0 local $@;
434 0 ▶ eval { $self->_request( 'POST', $url, $body, { 429 => 1 } ); 1; } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("POST", $url, $body, {429, 1}); 1 } } or do { $self->{"error"} = 13; $self->{"errorString"} = 'reporting "' . $opts{"ban"} . '" failed... ' . $@; $self->warn; return } 0 0 0
435 0 $self->{error} = 13;
436 0 $self->{errorString} = 'reporting "' . $opts{ban} . '" failed... ' . $@;
437 0 $self->warn;
438 0 return;
439 };
440 }
441  
442 4 $self->{banned}{ $opts{ban} } = 1;
443 } ## end sub ban
444  
445 =head2 unban
446  
447 Unbans the IP. The value of ban is validated as being a IPv4 or IPv6 address
448 and lowercased, then removed from the internal ban list. Reports can not be
449 withdrawn, so no API call is made. Unbanning an IP that is not banned is a
450 noop.
451  
452 $backend->unban(ban => $ip);
453  
454 =cut
455  
456 sub unban {
457 1 ▶ my ( $self, %opts ) = @_;
Subroutine unban: called CC=9 SCAR=34.2
Pod documented
458  
459 1 $self->errorblank;
460  
461 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
462 0 $self->{error} = 1;
463 0 $self->{errorString} = 'backend has not been inited';
464 0 $self->warn;
465 0 return;
466 }
467  
468 1 ▶ if ( !defined( $opts{ban} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $opts{'ban'}) { } 0 1 1
elsif (ref $opts{'ban'} ne '') { } 0 1 1
elsif (not $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::abuseipdb::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::abuseipdb::IPv6_re\z/) { } 0 1 1
469 0 $self->{error} = 9;
470 0 $self->{errorString} = 'Nothing specified for the value ban';
471 0 $self->warn;
472 0 return;
473 } elsif ( ref( $opts{ban} ) ne '' ) {
474 0 $self->{error} = 10;
475 0 $self->{errorString} = 'Bad ref type for ban... ref is "' . ref( $opts{ban} ) . '"';
476 0 $self->warn;
477 0 return;
478 ▶ } elsif ( $opts{ban} !~ /\A$IPv4_re\z/
Condition33%1/3
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::abuseipdb::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::abuseipdb::IPv6_re\z/
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::abuseipdb::IPv4_re\z/ || $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::abuseipdb::IPv6_re\z/
$opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::abuseipdb::IPv4_re\z/ $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::abuseipdb::IPv6_re\z/
Truth table33%1/3
A || B
A= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::abuseipdb::IPv4_re\z/B= $opts{'ban'} =~ /\A$Net::Firewall::BlockerHelper::backends::abuseipdb::IPv6_re\z/
ABresult
1X1
011
000
479 && $opts{ban} !~ /\A$IPv6_re\z/ )
480 {
481 0 $self->{error} = 10;
482 0 $self->{errorString} = 'ban item,"' . $opts{ban} . '", does not appear to be a IPv4 or IPv6 IP';
483 0 $self->warn;
484 0 return;
485 }
486  
487 # lowercase so the same IPv6 IP in differing cases can't result in duplicate entries
488 1 $opts{ban} = lc( $opts{ban} );
489  
490 1 ▶ if ( !$self->{banned}{ $opts{ban} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"banned"}{$opts{"ban"}}) 0 1 1
491 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
492 0 $self->{frontend_obj}->{test_data} = 'not banned';
493 }
494 0 return;
495 }
496  
497 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
498 1 $self->{frontend_obj}->{test_data} = 'unban is internal only';
499 }
500  
501 1 delete( $self->{banned}{ $opts{ban} } );
502 } ## end sub unban
503  
504 =head2 ban_cidr
505  
506 CIDR bans are not supported by this backend; this always sets the
507 cidrNotSupported error.
508  
509 $backend->ban_cidr(ban => '1.2.3.0/24');
510  
511 =cut
512  
513 sub ban_cidr {
514 0 ▶ my ( $self, %opts ) = @_;
Subroutine ban_cidr: not called CC=1 SCAR=6.9
Pod documented
515  
516 0 $self->errorblank;
517  
518 0 $self->{error} = 35;
519 0 $self->{errorString} = 'the ' . __PACKAGE__ . ' backend does not support CIDR bans';
520 0 $self->warn;
521  
522 0 return;
523 } ## end sub ban_cidr
524  
525 =head2 unban_cidr
526  
527 CIDR bans are not supported by this backend; this always sets the
528 cidrNotSupported error.
529  
530 $backend->unban_cidr(ban => '1.2.3.0/24');
531  
532 =cut
533  
534 sub unban_cidr {
535 0 ▶ my ( $self, %opts ) = @_;
Subroutine unban_cidr: not called CC=1 SCAR=6.9
Pod documented
536  
537 0 $self->errorblank;
538  
539 0 $self->{error} = 35;
540 0 $self->{errorString} = 'the ' . __PACKAGE__ . ' backend does not support CIDR bans';
541 0 $self->warn;
542  
543 0 return;
544 } ## end sub unban_cidr
545  
546 =head2 list_cidr
547  
548 CIDR bans are not supported by this backend, so this always returns an empty
549 list.
550  
551 my @banned_cidrs = $backend->list_cidr;
552  
553 =cut
554  
555 sub list_cidr {
556 0 ▶ my ( $self, %opts ) = @_;
Subroutine list_cidr: not called CC=1 SCAR=6.9
Pod documented
557  
558 0 $self->errorblank;
559  
560 0 return ();
561 }
562  
563 =head2 list
564  
565 List banned IPs. Returns an array of the IPs reported since init, from the
566 internal ban list; no API call is made.
567  
568 my @banned = $backend->list;
569  
570 =cut
571  
572 sub list {
573 2 ▶ my ( $self, %opts ) = @_;
Subroutine list: called CC=2 SCAR=7.0
Pod documented
574  
575 2 $self->errorblank;
576  
577 2 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 2 0 2
578 2 $self->{frontend_obj}->{test_data} = 'list';
579 }
580  
581 2 return keys( %{ $self->{banned} } );
582 }
583  
584 =head2 re_init
585  
586 Tears down and re-inits. Unlike the blocking backends nothing is re-added, as
587 reports are fire and forget and there is no remote state to restore.
588  
589 =cut
590  
591 sub re_init {
592 0 ▶ my ( $self, %opts ) = @_;
Subroutine re_init: not called CC=2 SCAR=17.9
Pod documented
593  
594 0 $self->errorblank;
595  
596 0 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 0 0
597 0 $self->{error} = 1;
598 0 $self->{errorString} = 'backend has not been inited';
599 0 $self->warn;
600 0 return;
601 }
602  
603 # teardown is best effort here for parity with the other backends, even
604 # though for this backend it can not fail
605 {
606 0 local $@;
607 0 eval { $self->teardown; };
608 }
609 0 $self->init;
610  
611 0 $self->{inited} = 1;
612 } ## end sub re_init
613  
614 =head2 teardown
615  
616 Tears down the backend. Reports can not be withdrawn, so no API calls are
617 made. The internal list of bans is kept.
618  
619 $backend->teardown;
620  
621 =cut
622  
623 sub teardown {
624 0 ▶ my ( $self, %opts ) = @_;
Subroutine teardown: not called CC=2 SCAR=17.9
Pod documented
625  
626 0 $self->errorblank;
627  
628 0 $self->{inited} = 0;
629  
630 0 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 0 0 0
631 0 $self->{frontend_obj}->{test_data} = 'teardown is internal only';
632 }
633 } ## end sub teardown
634  
635 =head2 stop
636  
637 Alias for L</teardown>, provided for parity with the fail2ban C<actionstop>
638 concept.
639  
640 $backend->stop;
641  
642 =cut
643  
644 sub stop {
645 0 ▶ my ( $self, %opts ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
646  
647 0 return $self->teardown(%opts);
648 }
649  
650 =head2 check
651  
652 Verifies the API key and reachability are still usable via a minimal GET to
653 the v2 C</check> endpoint. Returns a true value if so and a false value
654 otherwise. This is the
655 equivalent of fail2ban's C<actioncheck>.
656  
657 if ( !$backend->check ) {
658 $backend->re_init;
659 }
660  
661 =cut
662  
663 sub check {
664 5 ▶ my ( $self, %opts ) = @_;
Subroutine check: called CC=3 SCAR=15.4
Pod documented
665  
666 5 $self->errorblank;
667  
668 5 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 5 0 5
669 5 $self->{frontend_obj}->{test_data} = [ { method => 'GET', url => $self->_check_url } ];
670 5 return 1;
671 }
672  
673 0 local $@;
674 0 ▶ eval { $self->_request( 'GET', $self->_check_url ); 1; } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_request("GET", $self->_check_url); 1 } } or return 0 0 0 0
675  
676 0 return 1;
677 } ## end sub check
678  
679 =head2 flush
680  
681 Removes all currently banned IPs at once by forgetting them. Reports can not
682 be withdrawn, so no API calls are made. This is the equivalent of fail2ban's
683 C<actionflush>.
684  
685 $backend->flush;
686  
687 =cut
688  
689 sub flush {
690 1 ▶ my ( $self, %opts ) = @_;
Subroutine flush: called CC=3 SCAR=13.1
Pod documented
691  
692 1 $self->errorblank;
693  
694 1 ▶ if ( !$self->{inited} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"inited"}) 0 1 1
695 0 $self->{error} = 1;
696 0 $self->{errorString} = 'backend has not been inited';
697 0 $self->warn;
698 0 return;
699 }
700  
701 1 ▶ if ( $self->{testing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"testing"}) 1 0 1
702 1 $self->{frontend_obj}->{test_data} = 'flush is internal only';
703 }
704  
705 1 $self->{banned} = {};
706 } ## end sub flush
707  
708 =head1 ERROR CODES / FLAGS
709  
710 Error handling is provided by L<Error::Helper>. All
711 errors are considered fatal.
712  
713 =head2 1, notInited
714  
715 The backend has not been inited yet.
716  
717 =head2 6, invalidPrefixSpecified
718  
719 The specified prefix did not match /^[a-zA-Z0-9]+$/.
720  
721 =head2 7, invalidName
722  
723 The name is either undef or does not match /^[a-zA-Z0-9\-]+$/.
724  
725 =head2 8, optionsNotHash
726  
727 The item passed to new for options is not a hash.
728  
729 =head2 9, noBanItem
730  
731 No IP specified to ban or unban.
732  
733 =head2 10, banItemNotIP
734  
735 The item to ban is not an IP. Either wrong ref type or regexp
736 test using L<Regexp::IPv4> and L<Regexp::IPv6> failed.
737  
738 =head2 12, backendInitError
739  
740 Failed to init the backend.
741  
742 =head2 13, banFailed
743  
744 Failed to ban the item.
745  
746 =head2 14, unbanFailed
747  
748 Failed to unban the item.
749  
750 =head2 15, listFailed
751  
752 Failed to get a list of bans.
753  
754 =head2 16, reInitFailed
755  
756 Failed to re_init the backend.
757  
758 =head2 17, teardownFailed
759  
760 Failed to teardown the backend.
761  
762 =head2 18, alreadyInited
763  
764 init called, but the backend has already been inited.
765  
766 =head2 23, initFailed
767  
768 init failed. Probing the check endpoint failed.
769  
770 =head2 24, checkFailed
771  
772 The backend check raised an error.
773  
774 =head2 25, flushFailed
775  
776 Failed to flush the bans.
777  
778 =head2 26, portsNotSupported
779  
780 The abuseipdb backend reports whole IPs and does not support ports.
781  
782 =head2 27, protocolsNotSupported
783  
784 The abuseipdb backend reports whole IPs and does not support protocols.
785  
786 =head2 30, keyNotDefined
787  
788 The option key is undef or blank.
789  
790 =head2 31, categoriesInvalid
791  
792 The option categories is not an array ref of positive ints or a comma
793 separated string of positive ints.
794  
795 =head2 32, banCidrFailed
796  
797 Failed to ban the CIDR range.
798  
799 =head2 33, unbanCidrFailed
800  
801 Failed to unban the CIDR range.
802  
803 =head2 34, cidrItemNotCidr
804  
805 The item to ban is not a CIDR range. Either wrong ref type or it is not an
806 IPv4 or IPv6 address followed by a prefix length valid for its family.
807  
808 =head2 35, cidrNotSupported
809  
810 The backend does not support CIDR bans.
811  
812 =head2 36, listCidrFailed
813  
814 Failed to get a list of CIDR bans.
815  
816 =head1 AUTHOR
817  
818 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.ent> >>
819  
820 =head1 LICENSE AND COPYRIGHT
821  
822 This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
823  
824 This is free software, licensed under:
825  
826 The GNU Lesser General Public License, Version 2.1, February 1999
827  
828 =cut
829  
830 1 1; # End of Net::Firewall::BlockerHelper::backends::abuseipdb
« BlockerHelper/backends/vyos.pm ↑ Summary BlockerHelper/backends/bgp_rtbh.pm »