Log/Set/Record.pm

filter: Statementstmt 94.5% 87 / 92 Branchbran 62.5% 15 / 24 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 88.7% 126 / 142 CC 15 SCAR 17.6
CC
15
Cov
88%
parse
11.9
_process_value
3.0
create
2.0
CRAP 5.8
↑ Summary Log/Set.pm »
1 #!/usr/bin/perl
2  
3 #=======================================================================
4 # Record.pm / IPTables::Log::Set::Record
5 # $Id: Record.pm 21 2010-12-17 21:07:37Z andys $
6 # $HeadURL: https://daedalus.dmz.dn7.org.uk/svn/IPTables-Log/trunk/IPTables-Log/lib/IPTables/Log/Set/Record.pm $
7 # (c)2009 Andy Smith <andy.smith@netprojects.org.uk>
8 #-----------------------------------------------------------------------
9 #:Description
10 # This class holds a single IPTables/Netfilter record.
11 #-----------------------------------------------------------------------
12 #:Synopsis
13 # NOTE: This class isn't designed to be created directly.
14 #
15 # use IPTables::Log;
16 # my $l = IPTables::Log->new;
17 # my $s = $l->create_set;
18 # my $r = $s->create_record({text => '...IN=eth0 OUT=eth1 MAC=00:...'});
19 # $r->parse;
20 #=======================================================================
21  
22 # The pod (Perl Documentation) for this module is provided inline. For a
23 # better-formatted version, please run:-
24 # $ perldoc Record.pm
25  
26 =head1 NAME
27  
28 IPTables::Log::Set::Record - Holds a single IPTables/Netfilter log entry.
29  
30 =head1 SYNOPSIS
31  
32 Note that this class isn't designed to be created directly. You can create these objects via a C<IPTables::Log::Set> object.
33  
34 use IPTables::Log;
35 my $l = IPTables::Log->new;
36 my $s = $l->create_set;
37 my $r = $s->create_record({text => '...IN=eth0 OUT=eth1 MAC=00:...'});
38 $r->parse;
39  
40 =head1 DEPENDENCIES
41  
42 =over 4
43  
44 =item * Class::Accessor - for accessor methods
45  
46 =item * Data::GUID - for GUID generation
47  
48 =item * NetAddr::IP - for the C<src> and C<dst> methods
49  
50 =back
51  
52 =cut
53  
54 # Set the package name
55 package IPTables::Log::Set::Record;
56  
57 3 ▶use 5.010000;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 # Use Data::GUID for generating GUIDs
62 3 ▶use Data::GUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 # Use Data::Dumper for debugging. Can be removed for releases.
64 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 # Use NetAddr::IP for IP addresses
66 3 ▶use NetAddr::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 3 ▶use NetAddr::IP::Util qw(inet_aton);
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 # Inherit from Class::Accessor, which saves us quite a bit of time.
70 3 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
71 # Follow best practice
72 3 __PACKAGE__->follow_best_practice;
73 # Make 'text' a read/write accessor method
74 3 __PACKAGE__->mk_accessors( qw(text parsed) );
75 # Make the rest read-only
76 3 __PACKAGE__->mk_ro_accessors( qw(log guid date time hostname prefix in out mac src dst proto _spt _dpt spt dpt id len ttl df window syn type code) );
77  
78 # Set version information
79 3 our $VERSION = '0.0005';
80  
81 =head1 CONSTRUCTORS
82  
83 =head2 Record->create(I<{text => '...IN=eth0 OUT=eth1 MAC=00:...'}>)
84  
85 Creates a new C<IPTables::Log::Set::Record> object. You shouldn't call this directly - see the synopsis for an example.
86  
87 =cut
88  
89 # Call create instead of new, and the GUID will be generated automatically
90 sub create
91 9 ▶{
Subroutine create: called CC=2 SCAR=7.0
Pod documented
92 9 my ($class, $args) = @_;
93  
94 9 my $self = __PACKAGE__->new($args);
95 # Generate a GUID for the ID
96 9 my $g = Data::GUID->new;
97 9 $self->{guid} = $g->as_string;
98 9 ▶ $self->{no_header} = $args->{'no_header'} ? $args->{'no_header'} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'no_header'} ? : 0 9 9
99 9 $self->{parsed} = 0;
100  
101 9 return $self;
102 }
103  
104 # Private function for checking the content of fields
105 # Not documented in pod format because this is a private function.
106 sub _process_value
107 139 ▶{
Subroutine _process_value: called CC=3 SCAR=11.0
108 139 my ($self, $value, $name) = @_;
109  
110 # If $value isn't set, set it to "NONE". A blank string will break IPTables::Log::Set->get_by().
111 139 ▶ if((!$value) || ($value eq ""))
Branch: 1 branch
BranchTrueFalseTotal
if (not $value or $value eq "") 23 116 139
Condition66%2/3
not $value or $value eq ""
l!l&&r!l&&!r
230116
MC/DC50%1/2
not $value or $value eq ""
not $value $value eq ""
Truth table66%2/3
not(A) or B
A= not $valueB= $value eq ""
ABresult
1X1
011
000
112 {
113 23 $value = "NONE";
114 }
115  
116 139 $self->{$name} = $value;
117 139 return 1;
118 }
119  
120 # As for _process_value, but if true replaces the value with a 1, otherwise replaces it with a 0
121 # Not documented in pod format because this is a private function.
122 sub _process_present
123 13 ▶{
Subroutine _process_present: called CC=2 SCAR=6.9
124 13 my ($self, $value, $name) = @_;
125  
126 13 ▶ if($value)
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 2 11 13
127 {
128 2 $self->{$name} = 1;
129 }
130 else
131 {
132 11 $self->{$name} = 0;
133 }
134 13 return 1;
135 }
136  
137 =head1 METHODS
138  
139 =head2 $record->parse
140  
141 Parses the log message text passed either to the constructor, or via C<set_text>.
142  
143 =cut
144  
145 # Parses the log text
146 sub parse
147 9 ▶{
Subroutine parse: called CC=11 SCAR=24.8
Pod documented
148 9 my ($self, $text) = @_;
149  
150 9 ▶ if(!$self->get_text)
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->get_text) { } 0 9 9
151 {
152 0 ▶ if($text)
Branch: 1 branch
BranchTrueFalseTotal
if ($text) { } 0 0 0
153 {
154 # Set the text attribute to the original log message
155 0 $self->set_text($text);
156 }
157 else
158 {
159 #$self->get_log->error("No log text found?");
160 0 return;
161 }
162 }
163 else
164 {
165 9 $text = $self->get_text;
166 }
167  
168 #$self->get_log->debug_value("Original log message is", 'yellow', $text);
169  
170 # First, we pull parts out common to all protocols
171 9 my ($date, $time, $hostname, $prefix, $in, $out, $mac, $src, $dst, $len, $ttl, $id, $df, $proto);
172 9 ▶ if($self->{'no_header'} eq 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'no_header'} eq 1) { } 0 9 9
173 {
174 0 (undef, $prefix, $in, $out, undef, $mac, $src, $dst, $len, $ttl, $id, $df, $proto)
175 = $text =~ /kernel:(\s\[\d+\.\d+\])?\s(\S*)\sIN=(\S*)\sOUT=(\S*)\s(MAC=)?(\S+)?\s*SRC=(\d+\.\d+\.\d+\.\d+|\S+)\sDST=(\d+\.\d+\.\d+\.\d+|\S+)\sLEN=(\d+).+TTL=(\d+).+ID=(\d+)\s(DF)*\s*PROTO=(\S+)/;
176 }
177 else
178 {
179 9 ($date, $time, $hostname, undef, $prefix, $in, $out, undef, $mac, $src, $dst, $len, $ttl, $id, $df, $proto)
180 = $text =~ /(\w{3}\s\d{1,2})\s{1,2}(\d{2}:\d{2}:\d{2})\s(.+)\skernel:(\s\[\d+\.\d+\])?\s(\S*)\sIN=(\S*)\sOUT=(\S*)\s(MAC=)?(\S+)?\s*SRC=(\d+\.\d+\.\d+\.\d+|\S+)\sDST=(\d+\.\d+\.\d+\.\d+|\S+)\sLEN=(\d+).+TTL=(\d+).+ID=(\d+)\s(DF)*\s*PROTO=(\S+)/;
181 }
182  
183 # Get the protocol first. Based on this, we know what regex we need next.
184 9 $self->_process_value($proto, 'proto');
185 9 ▶ if(!$proto)
Branch: 1 branch
BranchTrueFalseTotal
unless ($proto) 0 9 9
186 {
187 #$self->get_log->error("Cannot determine the protocol for this log message!");
188 #$self->get_log->error("The log text is ".$self->get_log->fcolour('yellow', $text));
189 0 return;
190 }
191  
192 # Process values
193 # Date
194 9 $self->_process_value($date, 'date');
195 # Time
196 9 $self->_process_value($time, 'time');
197 # Hostname
198 9 $self->_process_value($hostname, 'hostname');
199 # IPTable logging prefix (as specified by '-j LOG --log-prefix="<prefix>"'
200 9 $self->_process_value($prefix, 'prefix');
201 # Ingress interface
202 9 $self->_process_value($in, 'in');
203 # Egress interface
204 9 $self->_process_value($out, 'out');
205 # MAC address, if applicable
206 9 $self->_process_value($mac, 'mac');
207 # Source IP
208 9 $self->_process_value($src, '_src');
209 9 ▶ if($self->{_src})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_src"}) 9 0 9
210 {
211 9 $self->{_src} = new_from_aton NetAddr::IP::Lite (inet_aton($self->{_src}));
212 9 $self->{src} = $self->{_src}->addr();
213 }
214 # Destination IP
215 9 $self->_process_value($dst, '_dst');
216 9 ▶ if($self->{_dst})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_dst"}) 9 0 9
217 {
218 9 $self->{_dst} = new_from_aton NetAddr::IP::Lite (inet_aton($self->{_dst}));
219 9 $self->{dst} = $self->{_dst}->addr();
220 }
221 # Packet length
222 9 $self->_process_value($len, 'len');
223 # TTL
224 9 $self->_process_value($ttl, 'ttl');
225 # Packet ID
226 9 $self->_process_value($id, 'id');
227 # Don't fragment
228 9 $self->_process_present($df, 'df');
229  
230 9 ▶ if(($proto eq "TCP") || ($proto eq "UDP"))
Branch: 2 branches
BranchTrueFalseTotal
if ($proto eq 'TCP' or $proto eq 'UDP') { } 7 2 9
elsif ($proto eq 'ICMP') { } 2 0 2
Condition100%3/3
$proto eq "TCP" or $proto eq "UDP"
l!l&&r!l&&!r
432
MC/DC100%2/2
$proto eq "TCP" or $proto eq "UDP"
$proto eq "TCP" $proto eq "UDP"
Truth table100%3/3
A or B
A= $proto eq "TCP"B= $proto eq "UDP"
ABresult
1X1
011
000
231 {
232 # TCP or UDP packet
233 7 my ($spt, $dpt) = $text =~ /PROTO=$proto\sSPT=(\d+)\sDPT=(\d+)/;
234  
235 # Source port
236 7 $self->_process_value($spt, 'spt');
237 # Destination port
238 7 $self->_process_value($dpt, 'dpt');
239  
240 7 ▶ if($proto eq "TCP")
Branch: 1 branch
BranchTrueFalseTotal
if ($proto eq "TCP") 4 3 7
241 {
242 # TCP specifics
243 4 my ($window, $syn) = $text =~ /WINDOW=(\d+).*(SYN)/;
244  
245 # TCP window size
246 4 $self->_process_value($window, 'window');
247 # SYN present?
248 4 $self->_process_present($syn, 'syn');
249 }
250 }
251 elsif($proto eq "ICMP")
252 {
253 2 my ($type) = $text =~ /ICMP TYPE=(\d+)\sCODE=(\d+)/;
254  
255 # ICMP Type
256 2 $self->_process_value($type, 'type');
257 2 $self->_process_value($type, 'code');
258 }
259  
260 # Return true if we've gotten this far.
261 9 $self->set_parsed(1);
262 9 return 1;
263 }
264  
265 =head2 $record->set_text("...IN=eth0 OUT=eth1 MAC=00:...")
266  
267 Sets the log message text. Either this must be set, or the text must have been passed to C<create>, otherwise C<parse> will error.
268  
269 =head1 ACCESSOR METHODS
270  
271 =head2 get(I<field>)
272  
273 Returns the value of I<field>. Field can be one of I<guid>, I<date>, I<time>, I<hostname>, I<prefix>, I<in>, I<out>, I<mac>, I<src>, I<dst>, I<proto>, I<spt>, I<dpt>, I<id>, I<len>, I<ttl>, I<df>, I<window>, I<syn>.
274  
275 =cut
276  
277 # Get accessor that takes the variable to return as an argument
278 sub get
279 507 ▶{
Subroutine get: called CC=1 SCAR=0.0
Pod documented
280 507 my ($self, $value) = @_;
281  
282 507 return $self->{$value};
283 }
284  
285 =head2 get_guid
286  
287 Returns the GUID for the packet.
288  
289 =head2 get_date
290  
291 Returns the date portion of the log message.
292  
293 =head2 get_time
294  
295 Returns the time portion of the log message.
296  
297 =head2 get_hostname
298  
299 rETURns the hostname portion of the log message.
300  
301 =head2 get_prefix
302  
303 Returns the iptables/netfilter log prefix for the log message, i.e. the part specified by C<-j LOG --log-prefix='I<LOG PREFIX> '>.
304  
305 =head2 get_in
306  
307 Returns the ingress interface, if specified.
308  
309 =head2 get_out
310  
311 Returns the egress interface, if specified.
312  
313 =head2 get_mac
314  
315 Returns the MAC address, if specified.
316  
317 =head2 get_src
318  
319 Returns the source IP address.
320  
321 =head2 get_dst
322  
323 Returns the destination IP address.
324  
325 =head2 get_proto
326  
327 Returns the protocol.
328  
329 =head2 get_spt - TCP and UDP packets only.
330  
331 Returns the source port, if applicable.
332  
333 =head2 get_dpt - TCP and UDP packets only.
334  
335 Returns the destination port, if applicable
336  
337 =head2 get_id
338  
339 Returns the packet ID.
340  
341 =head2 get_len
342  
343 Returns the packet length.
344  
345 =head2 get_ttl
346  
347 Returns the packet's TTL (Time To Live).
348  
349 =head2 get_df
350  
351 Returns the packet's DF (Don't Fragment) value.
352  
353 =head2 get_window - TCP and UDP packets only.
354  
355 Returns the packet's window size.
356  
357 =head2 get_sync
358  
359 Returns 1 if the packet is a SYN, otherwise returns 0.
360  
361 =head2 get_parsed
362  
363 Returns 1 if the packet has been successfully parsed, otherwise returns 0.
364  
365 =head1 CAVEATS
366  
367 It parses log entries. It doesn't do much else, yet.
368  
369 =head1 BUGS
370  
371 None that I'm aware of ;-)
372  
373 =head1 AUTHOR
374  
375 This module was written by B<Andy Smith> <andy.smith@netprojects.org.uk>.
376  
377 =head1 COPYRIGHT
378  
379 $Id: Record.pm 21 2010-12-17 21:07:37Z andys $
380  
381 (c)2009 Andy Smith (L<http://andys.org.uk/>)
382  
383 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
384  
385 =cut
386  
387 3 1
↑ Summary Log/Set.pm »