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