Dump/Online2.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Dump/Online.pm ↑ Summary Dump/Writer.pm »
1 #
2 # $Id: Online2.pm,v a93481b098ec 2020/05/01 08:55:56 gomor $
3 #
4 package Net::Frame::Dump::Online2;
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use base qw(Net::Frame::Dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 our @AS = qw(
10 dev
11 timeoutOnNext
12 timeout
13 maxRunTime
14 promisc
15 snaplen
16 file
17 overwrite
18 _startTime
19 _pid
20 _sel
21 _frames
22 _timeWithoutReceiving
23 _fcode
24 );
25 __PACKAGE__->cgBuildIndices;
26 __PACKAGE__->cgBuildAccessorsScalar(\@AS);
27  
28 BEGIN {
29 my $osname = {
30 cygwin => [ \&_checkWin32, ],
31 MSWin32 => [ \&_checkWin32, ],
32 };
33  
34 *_check = $osname->{$^O}->[0] || \&_checkOther;
35 }
36  
37 use Net::Frame::Dump qw(:consts);
38  
39 use IO::Select;
40 use Net::Pcap;
41 use Time::HiRes qw(gettimeofday);
42 use Net::Frame::Layer qw(:subs);
43  
44 sub _checkWin32 { return 1; }
45  
46 sub _checkOther {
47 if ($>) {
48 die("[-] Net::Frame::Dump::Online2: Must be EUID 0 (or equivalent) to ".
49 "open a device for live capture\n");
50 }
51 return 1;
52 }
53  
54 sub new {
55 my $self = shift->SUPER::new(
56 timeoutOnNext => 3,
57 timeout => 0,
58 promisc => 0,
59 snaplen => 1514,
60 file => '',
61 overwrite => 0,
62 maxRunTime => 0,
63 _startTime => 0,
64 _frames => [],
65 _timeWithoutReceiving => 0,
66 @_,
67 );
68  
69 if (!defined($self->dev)) {
70 print("[-] ".__PACKAGE__.": You MUST pass `dev' attribute\n");
71 return;
72 }
73  
74 return $self;
75 }
76  
77 sub start {
78 my $self = shift;
79  
80 _check() or return;
81  
82 $self->isRunning(1);
83  
84 if (length($self->file)) {
85 if (-f $self->file && ! $self->overwrite) {
86 print("[-] ".__PACKAGE__.": We will not overwrite a file by default. ".
87 "Use `overwrite' attribute to do it.\n");
88 return;
89 }
90 }
91  
92 my $err;
93 my $pd = Net::Pcap::open_live(
94 $self->dev,
95 $self->snaplen,
96 $self->promisc,
97 100, # 100 ms timeout
98 \$err,
99 );
100 unless ($pd) {
101 print("[-] ".__PACKAGE__.": open_live: $err\n");
102 return;
103 }
104  
105 my $net = 0;
106 my $mask = 0;
107 Net::Pcap::lookupnet($self->dev, \$net, \$mask, \$err);
108 if ($err) {
109 print("[!] ".__PACKAGE__.": lookupnet: $err\n");
110 }
111  
112 my $fcode;
113 if (Net::Pcap::compile($pd, \$fcode, $self->filter, $self->filterCodeOptimizer, $mask) < 0) {
114 print("[-] ".__PACKAGE__.": compile: ". Net::Pcap::geterr($pd). "\n");
115 return;
116 }
117 $self->_fcode($fcode); # Used to free it later on, usually on stop call.
118  
119 if (Net::Pcap::setfilter($pd, $fcode) < 0) {
120 print("[-] ".__PACKAGE__.": setfilter: ". Net::Pcap::geterr($pd). "\n");
121 return;
122 }
123  
124 if (! length($self->file)) {
125 #my $r = Net::Pcap::setnonblock($pd, 1, \$err);
126 #if ($r == -1) {
127 #print("[-] ".__PACKAGE__.": setnonblock: $err\n");
128 #return;
129 #}
130  
131 # Gather a file descriptor to use by select()
132 # Avoid eating 100% CPU
133 my $fd = Net::Pcap::get_selectable_fd($pd);
134 if ($fd < 0) {
135 print("[-] ".__PACKAGE__.": get_selectable_fd failed\n");
136 return;
137 }
138 my $sel = IO::Select->new;
139 $sel->add($fd);
140 $self->_sel($sel);
141 $self->_startTime(gettimeofday());
142 }
143  
144 $self->_pcapd($pd);
145 $self->getFirstLayer;
146  
147 if (length($self->file)) {
148 my $pid = fork();
149 if (! defined($pid)) {
150 die("[-] ".__PACKAGE__.": fork: $!\n");
151 }
152  
153 if ($pid) { # Parent
154 $self->_pid($pid);
155 $SIG{CHLD} = 'IGNORE';
156 return 1;
157 }
158 else { # Son
159 $self->_pid(0);
160 $self->_pcapd($pd);
161  
162 my $dumper = Net::Pcap::dump_open($pd, $self->file);
163 if (! defined($dumper)) {
164 print("[-] ".__PACKAGE__.": dump_open: ".Net::Pcap::geterr($pd).
165 "\n");
166 exit(1);
167 }
168 Net::Pcap::dump_flush($dumper);
169  
170 Net::Pcap::loop($pd, -1, \&_saveCallback, [ $dumper, $self ]);
171 exit(0);
172 }
173 }
174  
175 return 1;
176 }
177  
178 sub _isFather {
179 my $self = shift;
180 return $self->_pid;
181 }
182  
183 sub _isSon {
184 my $self = shift;
185 return ! $self->_pid;
186 }
187  
188 sub _saveCallback {
189 my ($data, $hdr, $pkt) = @_;
190 my $p = $data->[0];
191 my $self = $data->[1];
192  
193 Net::Pcap::dump($p, $hdr, $pkt);
194 Net::Pcap::dump_flush($p);
195 }
196  
197 sub _killTcpdump {
198 my $self = shift;
199 return if $self->_isSon;
200 kill('KILL', $self->_pid);
201 $self->_pid(undef);
202 }
203  
204 sub stop {
205 my $self = shift;
206  
207 # We are in capture mode
208 if (defined($self->file) && length($self->file) && $self->_isFather) {
209 $self->_killTcpdump;
210 }
211  
212 # Free the pcap filter
213 if (defined($self->_fcode)) {
214 Net::Pcap::pcap_freecode($self->_fcode);
215 $self->_fcode(undef);
216 }
217  
218 if (defined($self->_pcapd)) {
219 Net::Pcap::close($self->_pcapd);
220 $self->_pcapd(undef);
221 $self->isRunning(0);
222 }
223  
224 return 1;
225 }
226  
227 sub getStats {
228 my $self = shift;
229  
230 if (!defined($self->_pcapd)) {
231 print("[-] ".__PACKAGE__.": unable to get stats, no pcap descriptor ".
232 "opened.\n");
233 return;
234 }
235  
236 my %stats;
237 Net::Pcap::stats($self->_pcapd, \%stats);
238 return \%stats;
239 }
240  
241 sub _printStats {
242 my $self = shift;
243  
244 my $stats = $self->getStats;
245 Net::Pcap::close($self->_pcapd);
246  
247 $self->cgDebugPrint(1, 'Frames received : '.$stats->{ps_recv});
248 $self->cgDebugPrint(1, 'Frames dropped : '.$stats->{ps_drop});
249 $self->cgDebugPrint(1, 'Frames if dropped: '.$stats->{ps_ifdrop});
250  
251 return 1;
252 }
253  
254 sub timeoutReset {
255 my $self = shift;
256  
257 $self->_timeWithoutReceiving(0);
258 $self->timeout(0);
259  
260 return 1;
261 }
262  
263 sub _dispatch {
264 my ($user, $header, $packet) = @_;
265  
266 my $self = $user->{self};
267 my $frames = $user->{frames};
268  
269 my $ts = $self->keepTimestamp ? $self->_getTimestamp($header)
270 : $self->_setTimestamp;
271  
272 my $frame = {
273 firstLayer => 'ETH',
274 timestamp => $ts,
275 raw => $packet,
276 };
277  
278 push @$frames, $frame;
279  
280 $user->{frames} = $frames;
281 }
282  
283 sub next {
284 my $self = shift;
285  
286 if (length($self->file)) {
287 die("[-] ".__PACKAGE__.": next method not available while in ".
288 "capture mode.\n");
289 }
290  
291 # We look ar our internal ring buffer
292 my $frames = $self->_frames;
293 if (@$frames > 0) {
294 my $next = shift @$frames;
295 $self->_frames($frames);
296 return $next;
297 }
298  
299 # If we reach here, we don't have frames anymore in our buffer,
300 # we ask for more except if we have a timeout condition.
301 if ($self->timeout) {
302 return;
303 }
304  
305 # We fetch new awaiting frames, if no timeout occurred yet.
306 # can_read will return as soon as there is something to read,
307 # or will block if nothing to read for timeoutOnNext seconds.
308 my $sel = $self->_sel;
309 my $some_received = 0;
310 my $thisTime = gettimeofday();
311  
312 # Gather all available frames
313 if ($sel->can_read($self->timeoutOnNext)) {
314 my $h = { self => $self, frames => $frames };
315 Net::Pcap::pcap_dispatch($self->_pcapd, -1, \&_dispatch, $h);
316 $some_received = scalar(@{$h->{frames}});
317 }
318  
319 # Update the ring buffer, or we loose those frames
320 $self->_frames($frames);
321  
322 # Check if we didn't received frames during $timeoutOnNext seconds,
323 my $endTime = gettimeofday();
324 my $diff = $endTime - $thisTime;
325 if (! $some_received) {
326 $self->_timeWithoutReceiving($self->_timeWithoutReceiving + $diff);
327 }
328 else {
329 $self->_timeWithoutReceiving(0);
330 }
331  
332 #print STDERR "*** diff [$diff] received [$some_received] thisTime [$thisTime] endTime [$endTime]\n";
333  
334 if ($self->_timeWithoutReceiving > $self->timeoutOnNext) {
335 #print STDERR "*** Timeout occurred\n";
336 $self->timeout(1);
337 }
338  
339 # Check if maximum run time has been reached
340 my $maxRunTime = $self->maxRunTime;
341 my $startTime = $self->_startTime;
342 if ($maxRunTime && ($endTime - $startTime) > $startTime) {
343 #print STDERR "*** Max running time reached\n";
344 $self->timeout(1);
345 }
346  
347 return;
348 }
349  
350 1;
351  
352 __END__
« Dump/Online.pm ↑ Summary Dump/Writer.pm »