ThreatNet/Filter/Network.pm

filter: Statementstmt 100% 43 / 43 Branchbran 66.6% 12 / 18 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 88.8% 72 / 81 CC 11 SCAR 13.4
CC
11
Cov
89%
new
7.4
keep
4.1
type
2.0
CRAP 3.8
« ThreatNet/Message/IPv4.pm ↑ Summary ThreatNet/Filter/Chain.pm »
1 package ThreatNet::Filter::Network;
2  
3 =pod
4  
5 =head1 NAME
6  
7 ThreatNet::Filter::Network - Filter events within a set of IP ranges
8  
9 =head1 SYNOPSIS
10  
11 # Filter for IPs in our broadband customers' range
12 my $Broadband = ThreatNet::Filter::Network->new( keep => '123.123.0.0/16' );
13
14 # Create a filter for "local" and other things we want to discard
15 # (including our own personal broadband IP in the above network)
16 my $NotLocal = ThreatNet::Filter::Network->new( discard => qw{
17 123.123.123.123
18 LOCAL
19 111.245.76.248/29
20 222.234.52.192/29
21 } );
22
23 sub boot_zombies {
24 my $Msg = shift;
25 if ( $Broadband->keep($Msg) and $NotLocal->keep($Msg) ) {
26 my $account = $RadiusServer->ip_to_account($Msg->ip);
27 $account->disable();
28 $account->disconnect();
29 $account->add_support_note("You are infected with a virus");
30 }
31 }
32  
33 =head1 DESCRIPTION
34  
35 C<ThreatNet::Filter::Network> is a filter class for creating network
36 filters.
37  
38 That is, for filtering event streams to just those events that did
39 (or did not) occur within a particular network.
40  
41 The objects only check in two modes.
42  
43 The C<keep> keyword as first argument indicates events should be kept if
44 they match B<any> of the networks.
45  
46 The C<discard> keyword as first argument indicates events should be kept
47 only if they do B<not> match any of networks.
48  
49 For more complex network masks, see the L<ThreatNet::Filter::Chain> class
50 for chaining groups of C<keep> and C<discard> filters together.
51  
52 =head2 Specifying the Networks
53  
54 The actual matching is done using the L<Net::IP::Match::XS> module. Any
55 values that can be used by it can also be used with it can thus also be
56 used with C<ThreatNet::Filter::Network>.
57  
58 =head2 Keyword Expansion
59  
60 In addition to the normal IP specification above,
61 C<ThreatNet::Filter::Network> also supports keyword expansion for a
62 number of standard sets of network masks.
63  
64 When specified by name, they will be expanded into a list of IP ranges.
65  
66 Thus you can do something like the following.
67  
68 my $Remove = ThreatNet::Filter::Network->new(
69 discard => 'RFC1918', '123.123.123.0/24'
70 );
71  
72 This will filter out the three standard "local" IP blocks specified by
73 RFC1918, plus the addition range 123.123.123.0 - 123.123.123.255.
74  
75 All keywords are case-insensitive.
76  
77 =head3 RFC1918
78  
79 The C<RFC1918> keyword is expanded to the three network blocks reserved
80 for local intranets. This specifically does NOT include the localhost
81 address space.
82  
83 =head3 RFC3330
84  
85 The C<RFC3330> keyword is expanded to a larger set of network blocks
86 restricted for various purposes as identifier in RFC3330. This includes
87 those from C<RFC1918>, the localhost block, and several additional
88 blocks reserved for benchmarking, IP 6to4 identifiers and various other
89 blocks that should not appear in threat messages.
90  
91 Where correctness is a factor, such as posting to a non-C<tolerant>
92 channel, this filter should be applied before issuing messages, as they
93 are highly likely to be fraudulent or technically nonsensical.
94  
95 =head3 LOCAL
96  
97 The C<LOCAL> keyword is expanded to represent the most common
98 interpretation of a "local" address, which is the RFC1918 addresses,
99 plus the C<127.0.0.0/8> localhost block.
100  
101 =head2 Message Compatibility
102  
103 Please note that because the module on which this filter is based only
104 supports IPv4 ranges, this filter class is B<only> capable of processing
105 L<ThreatNet::Message::IPv4> (or subclass) objects.
106  
107 Any other message types passed to C<keep> will be returns C<undef>, and
108 thus will act as a null filter in most configurations.
109  
110 =head1 METHODS
111  
112 =cut
113  
114 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 3 ▶use Params::Util '_INSTANCE',
Subroutine BEGIN: called CC=1 SCAR=0.0
116 3 '_IDENTIFIER';
117 3 ▶use base 'ThreatNet::Filter';
Subroutine BEGIN: called CC=1 SCAR=0.0
118 3 ▶use Net::IP::Match::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
119 3 ▶use ThreatNet::Message::IPv4 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
120  
121 3 ▶use vars qw{$VERSION %KEYWORD};
Subroutine BEGIN: called CC=1 SCAR=0.0
122 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
123 3 $VERSION = '0.20';
124  
125 # Expandable network keywords
126 3 %KEYWORD = ();
127 3 $KEYWORD{RFC1918} = [qw{
128 10.0.0.0/8
129 172.16.0.0/12
130 192.168.0.0/16
131 }];
132 3 $KEYWORD{RFC3330} = [qw{
133 0.0.0.0/8
134 10.0.0.0/8
135 127.0.0.0/8
136 169.254.0.0/16
137 172.16.0.0/12
138 192.0.2.0/24
139 192.168.0.0/16
140 192.88.99.0/24
141 198.18.0.0/15
142 224.0.0.0/4
143 240.0.0.0/4
144 }];
145 3 $KEYWORD{LOCAL} = [qw{
146 127.0.0.0/8
147 10.0.0.0/8
148 172.16.0.0/12
149 192.168.0.0/16
150 }];
151 }
152  
153  
154  
155  
156  
157 ####################################################################3
158 # Constructor and Accessors
159  
160 =pod
161  
162 =head2 new ('keep' | 'discard'), $network, ...
163  
164 The C<new> constructor takes a param of either C<keep> or C<discard>,
165 followed by a list of one or more values which are either an expandable
166 keyword or an ip ranges compatible with L<Net::IP::Match::XS>.
167  
168 A ThreatNet filter is created which limits a message stream to events
169 either inside or outside of the resulting network.
170  
171 Returns a new C<ThreatNet::Filter::Network> object, or C<undef> if given
172 invalid params.
173  
174 =cut
175  
176 2 ▶sub new {
Subroutine new: called CC=7 SCAR=20.1
Pod documented
177 2 ▶ my $class = ref $_[0] ? ref shift : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 2 2
178  
179 # Check we are either a 'keep' or 'discard' filter
180 2 ▶ my $type = lc _IDENTIFIER(shift) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $type = lc &_IDENTIFIER(shift()) or return undef 2 0 2
181 2 ▶ $type eq 'keep' or $type eq 'discard' or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$type eq "keep" or $type eq "discard" or return undef 2 0 2
Condition66%2/3
$type eq "keep" or $type eq "discard"
l!l&&r!l&&!r
110
MC/DC0%0/2
$type eq "keep" or $type eq "discard"
$type eq "keep" $type eq "discard"
Truth table66%2/3
A or B
A= $type eq "keep"B= $type eq "discard"
ABresult
1X1
011
000
182  
183 # Get the list of values, filter and keyword-expand
184 4 ▶ my @network = map { $KEYWORD{$_} ? @{$KEYWORD{$_}} : $_ }
Branch: 1 branch
BranchTrueFalseTotal
$ThreatNet::Filter::Network::KEYWORD{$_} ? : 2 2 4
185 2 map { uc $_ } grep { defined $_ } @_;
186  
187 # Initial version doesn't check the IP ranges.
188 # It just makes sure we have at least one.
189 2 ▶ @network or return undef;
Branch: 1 branch
BranchTrueFalseTotal
@network or return undef 2 0 2
190  
191 # Create the object
192 2 my $self = bless {
193 keep => $type eq 'keep',
194 network => [ @network ],
195 }, $class;
196  
197 2 $self;
198 }
199  
200 =pod
201  
202 =head2 type
203  
204 The C<type> accessor returns the type of the network filter.
205  
206 Returns either C<'keep'> or C<'discard'>.
207  
208 =cut
209  
210 2 ▶sub type {
Subroutine type: called CC=2 SCAR=6.9
Pod documented
211 2 ▶ $_[0]->{keep} ? 'keep' : 'discard';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'keep'} ? : 1 1 2
212 }
213  
214 =pod
215  
216 =head2 network
217  
218 The C<network> accessor returns the list of ip ranges as provided to
219 the constructor.
220  
221 =cut
222  
223 2 ▶sub network {
Subroutine network: called CC=1 SCAR=0.0
Pod documented
224 2 @{$_[0]->{network}};
225 }
226  
227  
228  
229  
230  
231 #####################################################################
232 # ThreatNet::Filter Methods
233  
234 =pod
235  
236 =head2 keep $Message
237  
238 The C<keep> method takes a C<ThreatNet::Message::IPv4> message as per the
239 L<ThreatNet::Filter> specification, and checks it against the network
240 specification and C<keep>|C<discard> type.
241  
242 Returns true if the message should be kept, false if not, or C<undef> on
243 error.
244  
245 =cut
246  
247 10 ▶sub keep {
Subroutine keep: called CC=4 SCAR=14.1
Pod documented
248 10 my $self = shift;
249 10 ▶ my $Message = _INSTANCE(shift, 'ThreatNet::Message::IPv4') or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $Message = &_INSTANCE(shift(), "ThreatNet::Message::IPv4") or return undef 10 0 10
250 10 my $rv = Net::IP::Match::XS::match_ip( $Message->ip, @{$self->{network}} );
251 10 ▶ defined $rv ? $self->{keep} ? !! $rv : ! $rv : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined $rv ? : 10 0 10
$self->{'keep'} ? : 5 5 10
252 }
253  
254 3 1;
255  
256 =pod
257  
258 =head1 SUPPORT
259  
260 All bugs should be filed via the bug tracker at
261  
262 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=ThreatNet-Filter>
263  
264 For other issues, or commercial enhancement and support, contact the author
265  
266 =head1 AUTHORS
267  
268 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
269  
270 =head1 SEE ALSO
271  
272 L<http://ali.as/devel/threatnetwork.html>, L<ThreatNet::Filter>,
273 L<ThreatNet::Message::IPv4>.
274  
275 =head1 COPYRIGHT
276  
277 Copyright (c) 2005 Adam Kennedy. All rights reserved.
278 This program is free software; you can redistribute
279 it and/or modify it under the same terms as Perl itself.
280  
281 The full text of the license can be found in the
282 LICENSE file included with this module.
283  
284 =cut
« ThreatNet/Message/IPv4.pm ↑ Summary ThreatNet/Filter/Chain.pm »