blib/lib/Data/Netflow.pm

filter: Statementstmt 94.3% 134 / 142 Branchbran 65.6% 21 / 32 Conditioncond 64.2% 18 / 28 MC/DCmcdc 33.3% 5 / 15 Subroutinesub 92.8% 13 / 14 Podpod 75.0% 3 / 4 totaltotal 82.5% 194 / 235 CC 41 SCAR 26.4
CC
41
Cov
86%
decode
23.5
encodeV5
12.5
encodeV9
10.1
CRAP 14.0
↑ Summary
1 package Data::Netflow;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Time::HiRes qw(gettimeofday);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Socket qw( inet_pton inet_ntop inet_ntoa AF_INET6 AF_INET);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 my %Ls = (
14 1 => "C",
15 2 => "n",
16 4 => "N",
17 );
18  
19 =head1 NAME
20  
21 Data::Netflow - Module to process binary netflow data (v5 and v9)
22  
23 =head1 VERSION
24  
25 Version 0.02
26  
27 =cut
28  
29 3 our $VERSION = '0.02';
30  
31 =head1 SYNOPSIS
32  
33 Module to create netflow binary data from text data
34  
35 use Data::Netflow;
36 use IO::Socket;
37  
38 my $sock_udp = IO::Socket::INET->new(
39 Proto => 'udp',
40 PeerPort => 9995,
41 PeerAddr => '127.0.0.1',
42 ) or die "Could not create UDP socket: $!\n";
43  
44 my $TemplateV9 = {
45 'FlowSetId' => 0,
46 'TemplateId' => 300,
47 'Fields' => [
48 { 'Length' => 4, 'Id' => 1 }, # octetDeltaCount
49 { 'Length' => 4, 'Id' => 2 }, # packetDeltaCount
50 { 'Length' => 1, 'Id' => 4 }, # protocolIdentifier
51 { 'Length' => 1, 'Id' => 6 }, # tcp flags
52 { 'Length' => 2, 'Id' => 7 }, # sourceTransportPort
53 { 'Length' => 4, 'Id' => 8 }, # sourceIPv4Address
54 { 'Length' => 2, 'Id' => 11 }, # destinationTransportPort
55 { 'Length' => 4, 'Id' => 12 }, # destinationIPv4Address
56 { 'Length' => 4, 'Id' => 21 }, # last switched
57 { 'Length' => 4, 'Id' => 22 }, # first switched
58 ],
59 }
60  
61 my $Header = {
62 Version => 9,
63 SysUptime => int ( uptime() *1000 ),
64 };
65  
66  
67 my @flow;
68 my @tmp = qw( 5 8126 17 0 22 10.2.1.1 5365 10.2.1.254 ) ;
69 my $uptime = int ( (uptime()- $back ) *1000 );
70 push @tmp , $uptime + 5;
71 push @tmp , $uptime;
72 push @flow , \@tmp;
73  
74 my $encoded = Data::Netflow::encodeV9($Header, $TemplateV9 ,\@flow);
75 $sock_udp->send( $encoded );
76  
77  
78  
79 =head1 EXPORT
80  
81 encodeV5
82 encodeV9
83  
84 =head1 SUBROUTINES/METHODS
85  
86  
87 =head2 decode
88  
89 decode netflow data
90  
91 =cut
92  
93 sub decode
94 2 ▶{
Subroutine decode: called CC=21 SCAR=31.6
Pod documented
95 2 my ( $data, $byname ) = @_;
96 2 my $version = unpack 'n', $data;
97 2 my @flows;
98 2 ▶ if ( $version == 5 )
Branch: 2 branches
BranchTrueFalseTotal
if ($version == 5) { } 1 1 2
elsif ($version == 9) { } 1 0 1
99 {
100 1 my %data_id = (
101 1 => 'SrcAddr',
102 2 => 'DstAddr',
103 3 => 'NextHop',
104 4 => 'InputInt',
105 5 => 'OutputInt',
106 6 => 'Packets',
107 7 => 'Octets',
108 8 => 'StartTime',
109 9 => 'EndTime',
110 10 => 'SrcPort',
111 11 => 'DstPort',
112 12 => 'Padding',
113 13 => 'TCP Flags',
114 14 => 'Protocol',
115 15 => 'IP ToS',
116 16 => 'SrcAS',
117 17 => 'DstAS',
118 18 => 'SrcMask',
119 19 => 'DstMask',
120 20 => 'Padding',
121 );
122 1 my %headers_id = (
123 1 => 'version',
124 2 => 'count',
125 3 => 'SysUptime',
126 4 => 'unix_secs',
127 5 => 'unix_nsecs',
128 6 => 'flow_sequence',
129 7 => 'engine_type',
130 8 => 'engine_id',
131 9 => 'sampling_interval'
132 );
133 1 my %headers_name = reverse %headers_id;
134 1 my $header = substr $data, 0, 24, '';
135 1 my @headers = unpack 'n2N4CCn', $header;
136 1 $headers_name{$headers_id{$_ + 1}} = $headers[$_] foreach ( 0 .. 8 );
137 1 for ( 1 .. $headers[1] )
138 {
139 2 my $flow_data = substr $data, 0, 48, '';
140 2 my @flow = unpack 'NNNnnNNNNnnCCCCnnCCn', $flow_data;
141 2 my %data_name;
142 2 ▶ if ( $byname )
Branch: 1 branch
BranchTrueFalseTotal
if ($byname) { } 0 2 2
143 {
144 0 %data_name = reverse %data_id;
145 }
146 else
147 {
148 2 %data_name = %data_id;
149 }
150 2 foreach my $idx ( 0 .. 19 )
151 {
152 40 ▶ if ( $idx <= 2 )
Branch: 1 branch
BranchTrueFalseTotal
if ($idx <= 2) { } 6 34 40
153 {
154 6 ▶ if ( $byname )
Branch: 1 branch
BranchTrueFalseTotal
if ($byname) { } 0 6 6
155 {
156 0 $data_name{$data_id{$idx + 1}} = inet_ntop( AF_INET, pack 'N', $flow[$idx] );
157 }
158 else
159 {
160 6 $data_name{$idx + 1} = inet_ntop( AF_INET, pack 'N', $flow[$idx] );
161 }
162 }
163 else
164 {
165 0 ▶ if ( $byname ) {$data_name{$data_id{$idx + 1}} = $flow[$idx];}
Branch: 1 branch
BranchTrueFalseTotal
if ($byname) { } 0 34 34
166 else
167 {
168 34 $data_name{$idx + 1} = $flow[$idx];
169 }
170 }
171 }
172 2 delete $data_name{Padding};
173 2 push @flows, \%data_name;
174 }
175 1 return \%headers_name, \@flows;
176 }
177 elsif ( $version == 9 )
178 {
179 1 my %headers_id = (
180 1 => 'version',
181 2 => 'count',
182 3 => 'SysUptime',
183 4 => 'unix_secs',
184 5 => 'flow_sequence',
185 6 => 'engine_id',
186 );
187 1 my %headers_name = reverse %headers_id;
188 1 my $header = substr $data, 0, 20, '';
189 1 my @headers = unpack 'n2N4', $header;
190 1 $headers_name{$headers_id{$_ + 1}} = $headers[$_] foreach ( 0 .. 5 );
191 1 my %templates;
192 my $flowser_id;
193 1 my $field_count;
194 1 my ( $flowset_id, $flowset_length ) = unpack 'n2', $data;
195  
196 1 ▶ if ( $flowset_id == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($flowset_id == 0) 1 0 1
197 {
198 1 my $record = substr $data, 0, $flowset_length, '';
199 1 substr $record, 0, 4, '';
200 ## we are in template records
201 1 my ( $flowser_id, $field_count ) = unpack 'n2', substr( $record, 0, 4, '' );
202 1 my @tmp;
203 1 for my $idx ( 1 .. $field_count )
204 {
205 10 my ( $id, $len ) = unpack 'n2', ( substr( $record, 0, 4, '' ) );
206 10 push @tmp, "$id,$len";
207 }
208 1 $templates{$flowser_id} = \@tmp;
209 1 ( $flowset_id, $flowset_length ) = unpack 'n2', ( substr( $data, 0, 4, '' ) );
210 }
211  
212 1 ▶ if ( $flowset_id != 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($flowset_id != 0) 1 0 1
213 {
214 ## we are in data records
215 1 for my $nbr ( 1 .. ( $headers_name{count} - 1 ) )
216 {
217 2 my %record;
218 2 ▶ if ( exists $templates{$flowset_id} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $templates{$flowset_id}) 2 0 2
219 {
220 2 foreach my $item ( @{$templates{$flowset_id}} )
221 {
222 20 my ( $i, $l ) = split ',', $item;
223 20 ▶ next if ( !defined $i || !defined $l );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i and defined $l 0 20 20
Condition33%1/3
defined $i and defined $l
!ll&&!rl&&r
0020
MC/DC0%0/2
defined $i and defined $l
defined $i defined $l
Truth table33%1/3
A and B
A= defined $iB= defined $l
ABresult
0X0
100
111
224 20 ▶ if ( any {$_ == $i} qw(8 12 15 18) )
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 2 branches
BranchTrueFalseTotal
if (&any(sub { $_ == $i; } , '8', '12', '15', '18')) { } 4 16 20
elsif (&any(sub { $_ == $i; } , '27', '28', '62')) { } 0 16 16
225 {
226 4 $record{$i} = inet_ntop( AF_INET, ( substr( $data, 0, $l, '' ) ) );
227 }
228 48 ▶ elsif ( any {$_ == $i} qw(27 28 62) )
Subroutine __ANON__: called CC=1 SCAR=0.0
229 {
230 0 $record{$i} = inet_ntop( AF_INET6, ( substr( $data, 0, $l, '' ) ) );
231 }
232 else
233 {
234 16 $record{$i} = unpack $Ls{$l}, ( substr( $data, 0, $l, '' ) );
235 }
236 }
237 2 push @flows, \%record;
238 }
239 }
240 }
241 1 return \%headers_name, \@flows;
242 }
243 else
244 {
245 0 croak "Version $version not supported";
246 }
247  
248 }
249  
250 =head2 encodeV9
251  
252 encode data for netflow version 9 with template
253  
254 =cut
255  
256 sub encodeV9
257 1 ▶{
Subroutine encodeV9: called CC=10 SCAR=23.1
Pod documented
258 1 my ( $header, $template, $flowArrayRef ) = @_;
259 1 my @flows = @$flowArrayRef;
260 1 my $Id = $template->{Fields};
261 1 my $out;
262 ### start header ###
263 1 ▶ $header->{SysUptime} //= int uptime() * 1000;
Condition50%1/2
$header->{"SysUptime"} //= int uptime() * 1000
l!l
10
MC/DC0%0/1
$header->{"SysUptime"} //= int uptime() * 1000
$header->{"SysUptime"}
Truth table50%1/2
A
A= $header->{"SysUptime"}
Aresult
11
00
264 1 ▶ $header->{TicksMS} //= time;
Condition50%1/2
$header->{"TicksMS"} //= time
l!l
01
MC/DC0%0/1
$header->{"TicksMS"} //= time
$header->{"TicksMS"}
Truth table50%1/2
A
A= $header->{"TicksMS"}
Aresult
11
00
265 1 ▶ $header->{PackageNum} = ( ( $header->{PackageNum} // 0 ) + 1 ) % 0xFFFFFFFF;
Condition50%1/2
$header->{"PackageNum"} // 0
l!l
01
MC/DC0%0/1
$header->{"PackageNum"} // 0
$header->{"PackageNum"}
Truth table50%1/2
A
A= $header->{"PackageNum"}
Aresult
11
00
266 1 $header->{Count} = 1 + scalar @flows;
267 1 $header->{SourceId} += 0;
268 1 $out = pack "n2N4", @{$header}{qw{Version Count SysUptime TicksMS PackageNum SourceId}};
269 ### end header ###
270  
271 ### start template ###
272 1 $template->{FieldsCount} = scalar @$Id;
273 1 $template->{Length} = 8 + ( $template->{FieldsCount} * 2 * 2 );
274 1 my @template_data = ( $template->{FlowSetId}, $template->{Length}, $template->{TemplateId}, $template->{FieldsCount} );
275 1 foreach my $f ( sort {$a->{Id} <=> $b->{Id}} @{$template->{Fields}} )
276 {
277 10 push @template_data, $f->{Id}, $f->{Length};
278 }
279 1 $out .= pack "n*", @template_data;
280 ### end template ###
281  
282 1 my $packedData;
283 1 foreach my $flow ( @flows )
284 {
285 2 foreach my $fieldsIdx ( 0 .. $#$flow )
286 {
287 20 my $L = $Ls{$Id->[$fieldsIdx]{Length}};
288 20 ▶ if ( $Id->[$fieldsIdx]{Id} == 8 || $Id->[$fieldsIdx]{Id} == 12 )
Branch: 1 branch
BranchTrueFalseTotal
if ($Id->[$fieldsIdx]{'Id'} == 8 or $Id->[$fieldsIdx]{'Id'} == 12) { } 4 16 20
Condition100%3/3
$Id->[$fieldsIdx]{"Id"} == 8 or $Id->[$fieldsIdx]{"Id"} == 12
l!l&&r!l&&!r
2216
MC/DC100%2/2
$Id->[$fieldsIdx]{"Id"} == 8 or $Id->[$fieldsIdx]{"Id"} == 12
$Id->[$fieldsIdx]{"Id"} == 8 $Id->[$fieldsIdx]{"Id"} == 12
Truth table100%3/3
A or B
A= $Id->[$fieldsIdx]{"Id"} == 8B= $Id->[$fieldsIdx]{"Id"} == 12
ABresult
1X1
011
000
289 {
290 4 ▶ $packedData .= $flow->[$fieldsIdx] =~ /:/ ? inet_pton( AF_INET6, $flow->[$fieldsIdx] ) : inet_pton( AF_INET, $flow->[$fieldsIdx] );
Branch: 1 branch
BranchTrueFalseTotal
$flow->[$fieldsIdx] =~ /:/ ? : 0 4 4
291 }
292 else
293 {
294 16 $packedData .= pack $L, $flow->[$fieldsIdx];
295 }
296 }
297 }
298 1 $out .= pack "n2", $template->{TemplateId}, ( length( $packedData ) + 4 );
299 1 $out .= $packedData;
300 1 return $out;
301 }
302  
303 =head2 encodeV5
304  
305 encode data for netflow version 5
306  
307 =cut
308  
309 sub encodeV5
310 1 ▶{
Subroutine encodeV5: called CC=12 SCAR=25.2
Pod documented
311 1 my ( $header, $template, $flowArrayRef ) = @_;
312 1 my @flows = @$flowArrayRef;
313 1 my $Id = $template->{Fields};
314 1 ▶ $header->{SysUptime} //= int uptime() * 1000;
Condition50%1/2
$header->{"SysUptime"} //= int uptime() * 1000
l!l
10
MC/DC0%0/1
$header->{"SysUptime"} //= int uptime() * 1000
$header->{"SysUptime"}
Truth table50%1/2
A
A= $header->{"SysUptime"}
Aresult
11
00
315 1 ( $header->{UnixSecs}, $header->{UnixNsecs} ) = ( gettimeofday );
316 1 ▶ $header->{FlowSeq} //= 0;
Condition50%1/2
$header->{"FlowSeq"} //= 0
l!l
01
MC/DC0%0/1
$header->{"FlowSeq"} //= 0
$header->{"FlowSeq"}
Truth table50%1/2
A
A= $header->{"FlowSeq"}
Aresult
11
00
317 1 ▶ $header->{EngineType} //= 0;
Condition50%1/2
$header->{"EngineType"} //= 0
l!l
01
MC/DC0%0/1
$header->{"EngineType"} //= 0
$header->{"EngineType"}
Truth table50%1/2
A
A= $header->{"EngineType"}
Aresult
11
00
318 1 ▶ $header->{EngineId} //= 0;
Condition50%1/2
$header->{"EngineId"} //= 0
l!l
01
MC/DC0%0/1
$header->{"EngineId"} //= 0
$header->{"EngineId"}
Truth table50%1/2
A
A= $header->{"EngineId"}
Aresult
11
00
319 1 ▶ $header->{SamplingInt} //= 0;
Condition50%1/2
$header->{"SamplingInt"} //= 0
l!l
01
MC/DC0%0/1
$header->{"SamplingInt"} //= 0
$header->{"SamplingInt"}
Truth table50%1/2
A
A= $header->{"SamplingInt"}
Aresult
11
00
320  
321 1 $header->{Count} = scalar @flows;
322 1 my $out = pack "n2N4CCn", @{$header}{qw{Version Count SysUptime UnixSecs UnixNsecs FlowSeq EngineType EngineId SamplingInt}};
323  
324 1 my $packedData;
325 1 foreach my $flow ( @flows )
326 {
327 2 foreach my $fieldsIdx ( 0 .. $#$flow )
328 {
329 40 my $L = $Ls{$Id->[$fieldsIdx]{Length}};
330 40 ▶ if ( $Id->[$fieldsIdx]{Id} == 1 || $Id->[$fieldsIdx]{Id} == 2 || $Id->[$fieldsIdx]{Id} == 3 )
Branch: 1 branch
BranchTrueFalseTotal
if ($Id->[$fieldsIdx]{'Id'} == 1 or $Id->[$fieldsIdx]{'Id'} == 2 or $Id->[$fieldsIdx]{'Id'} == 3) { } 6 34 40
Condition100%6/6
$Id->[$fieldsIdx]{"Id"} == 1 or $Id->[$fieldsIdx]{"Id"} == 2 or $Id->[$fieldsIdx]{"Id"} == 3
l!l&&r!l&&!r
4234
$Id->[$fieldsIdx]{"Id"} == 1 or $Id->[$fieldsIdx]{"Id"} == 2
l!l&&r!l&&!r
2236
MC/DC100%3/3
$Id->[$fieldsIdx]{"Id"} == 1 or $Id->[$fieldsIdx]{"Id"} == 2 or $Id->[$fieldsIdx]{"Id"} == 3
$Id->[$fieldsIdx]{"Id"} == 1 $Id->[$fieldsIdx]{"Id"} == 2 $Id->[$fieldsIdx]{"Id"} == 3
Truth table100%4/4
A or B or C
A= $Id->[$fieldsIdx]{"Id"} == 1B= $Id->[$fieldsIdx]{"Id"} == 2C= $Id->[$fieldsIdx]{"Id"} == 3
ABCresult
1XX1
01X1
0011
0000
331 {
332 6 ▶ $packedData .= $flow->[$fieldsIdx] =~ /:/ ? inet_pton( AF_INET6, $flow->[$fieldsIdx] ) : inet_pton( AF_INET, $flow->[$fieldsIdx] );
Branch: 1 branch
BranchTrueFalseTotal
$flow->[$fieldsIdx] =~ /:/ ? : 0 6 6
333 }
334 else
335 {
336 34 $packedData .= pack $L, $flow->[$fieldsIdx];
337 }
338 }
339 }
340 1 $out .= $packedData;
341 1 return $out;
342 }
343  
344 sub uptime
345 0 ▶{
Subroutine uptime: not called CC=1 SCAR=6.9
Pod undocumented
346 return (
347 split /\s/,
348 0 do {local ( @ARGV, $/ ) = '/proc/uptime'; <>}
349 )[0];
350 }
351  
352 =head1 AUTHOR
353  
354 DULAUNOY Fabrice, C<< <fabrice at dulaunoy.com> >>
355  
356 =head1 BUGS
357  
358 Please report any bugs or feature requests to C<bug-data-netflow at rt.cpan.org>, or through
359 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-Netflow>. I will be notified, and then you'll
360 automatically be notified of progress on your bug as I make changes.
361  
362  
363 =head1 TODO
364  
365 decode V9 for multiple data set (and multiple template )
366 decode V9 return by id or by name (if flag) like for V5
367 A single encode (detect version by the header)
368 IPFIX (maybe)
369  
370 =head1 SUPPORT
371  
372 You can find documentation for this module with the perldoc command.
373  
374 perldoc Data::Netflow
375  
376 For the netflow format:
377  
378 Version 5:
379  
380  
381 Version 9:
382  
383 https://www.ietf.org/rfc/rfc3954.txt
384  
385 You can also look for information at:
386  
387 =over 4
388  
389 =item * RT: CPAN's request tracker (report bugs here)
390  
391 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-Netflow>
392  
393 =item * AnnoCPAN: Annotated CPAN documentation
394  
395 L<http://annocpan.org/dist/Data-Netflow>
396  
397 =item * CPAN Ratings
398  
399 L<http://cpanratings.perl.org/d/Data-Netflow>
400  
401 =item * Search CPAN
402  
403 L<http://search.cpan.org/dist/Data-Netflow/>
404  
405 =back
406  
407  
408 =head1 ACKNOWLEDGEMENTS
409  
410  
411 =head1 LICENSE AND COPYRIGHT
412  
413 Copyright 2017 DULAUNOY Fabrice.
414  
415 This program is free software; you can redistribute it and/or modify it
416 under the terms of the the Artistic License (2.0). You may obtain a
417 copy of the full license at:
418  
419 L<http://www.perlfoundation.org/artistic_license_2_0>
420  
421 Any use, modification, and distribution of the Standard or Modified
422 Versions is governed by this Artistic License. By using, modifying or
423 distributing the Package, you accept this license. Do not use, modify,
424 or distribute the Package, if you do not accept this license.
425  
426 If your Modified Version has been derived from a Modified Version made
427 by someone other than you, you are nevertheless required to ensure that
428 your Modified Version complies with the requirements of this license.
429  
430 This license does not grant you the right to use any trademark, service
431 mark, tradename, or logo of the Copyright Holder.
432  
433 This license includes the non-exclusive, worldwide, free-of-charge
434 patent license to make, have made, use, offer to sell, sell, import and
435 otherwise transfer the Package with respect to any patent claims
436 licensable by the Copyright Holder that are necessarily infringed by the
437 Package. If you institute patent litigation (including a cross-claim or
438 counterclaim) against any party alleging that the Package constitutes
439 direct or contributory patent infringement, then this Artistic License
440 to you shall terminate on the date that such litigation is filed.
441  
442 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
443 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
444 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
445 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
446 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
447 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
448 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
449 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
450  
451  
452 =cut
453  
454 3 1; # End of Data::Netflow
↑ Summary