lib/Net/SinFP3/Output/Pcap.pm untested

filter: Statementstmt 0.0% 0 / 122 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 218 CC 29 SCAR 55.5
CC
29
Cov
0%
run
462.0
_ipv4
30.0
_ipv6
30.0
CRAP 256.9
1 #
2 # $Id: Pcap.pm,v 008243d3e89a 2018/07/21 14:54:07 gomor $
3 #
4 package Net::SinFP3::Output::Pcap;
5 use strict;
6 use warnings;
7  
8 use base qw(Net::SinFP3::Output);
9 our @AS = qw(
10 anonymize
11 append
12 );
13 __PACKAGE__->cgBuildIndices;
14 __PACKAGE__->cgBuildAccessorsScalar(\@AS);
15  
16 use Net::Frame::Layer::ETH qw(:consts);
17 use Net::Frame::Layer::IPv4 qw(:consts);
18 use Net::Frame::Layer::IPv6 qw(:consts);
19  
20 sub take {
21 return [
22 'Net::SinFP3::Result::Passive',
23 'Net::SinFP3::Result::Active',
24 'Net::SinFP3::Result::Frame',
25 'Net::SinFP3::Result::MultiFrame',
26 ];
27 }
28  
29 sub new {
30 my $self = shift->SUPER::new(
31 anonymize => 0,
32 append => 0,
33 @_,
34 );
35  
36 return $self;
37 }
38  
39 sub _ipv4 {
40 my $self = shift;
41 my ($frames) = @_;
42  
43 my @new = ();
44 my $src;
45 for my $f (@$frames) {
46 next unless defined($f);
47 if ($f->ref->{IPv4}) {
48 if ($self->anonymize) {
49 $src ||= $f->ref->{IPv4}->src; # Takes src IP from first frame
50 $f->ref->{TCP}->checksum(666);
51 $f->ref->{IPv4}->checksum(666);
52 if ($f->ref->{IPv4}->src eq $src) {
53 $f->ref->{IPv4}->src('127.0.0.1');
54 $f->ref->{IPv4}->dst('127.0.0.2');
55 }
56 else {
57 $f->ref->{IPv4}->src('127.0.0.2');
58 $f->ref->{IPv4}->dst('127.0.0.1');
59 }
60 }
61 my $new = Net::Frame::Simple->new(layers => [
62 Net::Frame::Layer::ETH->new,
63 $f->ref->{IPv4},
64 $f->ref->{TCP},
65 ]);
66 $new->pack;
67 push @new, $new;
68 }
69 }
70  
71 return \@new;
72 }
73  
74 sub _ipv6 {
75 my $self = shift;
76 my ($frames) = @_;
77  
78 my @new = ();
79 my $src;
80 for my $f (@$frames) {
81 next unless defined($f);
82 if ($f->ref->{IPv6}) {
83 if ($self->anonymize) {
84 $src ||= $f->ref->{IPv6}->src; # Takes src IP from first frame
85 $f->ref->{TCP}->checksum(666);
86 if ($f->ref->{IPv6}->src eq $src) {
87 $f->ref->{IPv6}->src('::1');
88 $f->ref->{IPv6}->dst('::2');
89 }
90 else {
91 $f->ref->{IPv6}->src('::2');
92 $f->ref->{IPv6}->dst('::1');
93 }
94 }
95 my $new = Net::Frame::Simple->new(layers => [
96 Net::Frame::Layer::ETH->new(
97 type => NF_ETH_TYPE_IPv6,
98 ),
99 $f->ref->{IPv6},
100 $f->ref->{TCP},
101 ]);
102 $new->pack;
103 push @new, $new;
104 }
105 }
106  
107 return \@new;
108 }
109  
110 sub run {
111 my $self = shift->SUPER::run(@_) or return;
112  
113 my $global = $self->global;
114 my $log = $global->log;
115 my $mode = $global->mode;
116 my $input = $global->input;
117 my $next = $global->next;
118 my @results = $global->result;
119  
120 # We do not output a pcap file if the port is in error
121 if ($results[0] =~ /Net::SinFP3::Result::PortError/) {
122 $log->info("Port is in error, skipping");
123 return 1;
124 }
125  
126 # We do not output a pcap file in Input::SynScan with fingerprint
127 if (ref($input) =~ /Net::SinFP3::Input::SynScan/ && $input->fingerprint) {
128 $log->info("No pcap saving while -synscan-fingerprint option in use");
129 return 1;
130 }
131  
132 # Gather frames for active mode
133 my $frames = [];
134 my $refMode = ref($mode);
135 my $refNext = ref($next);
136 if ($refMode =~ /^Net::SinFP3::Mode::Active$/) {
137 if ($mode->p1) {
138 push @$frames, $mode->p1;
139 push @$frames, $mode->p1->reply if $mode->p1->reply;
140 }
141 if ($mode->p2) {
142 push @$frames, $mode->p2;
143 push @$frames, $mode->p2->reply if $mode->p2->reply;
144 }
145 if ($mode->p3) {
146 push @$frames, $mode->p3;
147 push @$frames, $mode->p3->reply if $mode->p3->reply;
148 }
149 }
150 # Gather frame for passive mode
151 elsif ($refNext =~ /^Net::SinFP3::Next::Frame$/) {
152 push @$frames, $next->frame;
153 }
154 # Gather frames when multiple Next objects are used
155 elsif ($refNext =~ /^Net::SinFP3::Next::MultiFrame$/) {
156 for my $f ($next->frameList) {
157 push @$frames, $f;
158 }
159 }
160 else {
161 $log->warning("Don't know what to do with this object: [$refMode]");
162 return;
163 }
164  
165 # Rewrite frames to add an ETH layer
166 # And anonymize them if asked by user
167 $frames = $global->ipv6 ? $self->_ipv6($frames) : $self->_ipv4($frames);
168  
169 my $file;
170 if ($refMode =~ /^Net::SinFP3::Mode::Active$/) {
171 if ($global->ipv6) {
172 $file = $self->anonymize ? 'sinfp6-::1-'.$next->port.'.pcap'
173 : 'sinfp6-'.$next->ip.'-'.$next->port.'.pcap';
174 }
175 else {
176 $file = $self->anonymize ? 'sinfp4-127.0.0.1-'.$next->port.'.pcap'
177 : 'sinfp4-'.$next->ip.'-'.$next->port.'.pcap';
178 }
179 }
180 else {
181 $file = $global->ipv6 ? 'sinfp6-output.pcap' : 'sinfp4-output.pcap';
182 }
183  
184 my %args = (
185 append => $self->append,
186 );
187 if (!$self->append) {
188 $args{overwrite} = 1;
189 }
190 my $out = $global->getDumpWriter(
191 file => $file,
192 firstLayer => 'ETH',
193 %args,
194 ) or return;
195 $out->start or return;
196  
197 for my $f (@$frames) {
198 next unless defined($f);
199 $out->write({ timestamp => $f->timestamp, raw => $f->raw });
200 }
201  
202 $out->stop;
203  
204 if ($refMode =~ /^Net::SinFP3::Mode::Active$/) {
205 print "File [$file] generation done.\n";
206 print "Please send it to GomoR[at]metabrik.org if you think this is ".
207 "not the\n";
208 print "good identification, or if it is new signature.\n";
209 print "In this last case, please specify `uname -a' (or equivalent) ".
210 "from\n";
211 print "the target host.\n";
212 }
213  
214 return 1;
215 }
216  
217 1;
218  
219 __END__