| 1 | # | ||
| 2 | # $Id: Online.pm,v c07e2f6c705b 2018/05/17 11:57:52 gomor $ | ||
| 3 | # | ||
| 4 | package Net::Frame::Dump::Online; | ||
| 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 | promisc | ||
| 14 | snaplen | ||
| 15 | unlinkOnStop | ||
| 16 | onRecv | ||
| 17 | onRecvCount | ||
| 18 | onRecvData | ||
| 19 | _pid | ||
| 20 | _sName | ||
| 21 | _sDataAwaiting | ||
| 22 | _firstTime | ||
| 23 | _son | ||
| 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 Net::Pcap; | ||
| 40 | use Time::HiRes qw(gettimeofday); | ||
| 41 | use Storable qw(lock_store lock_retrieve); | ||
| 42 | use Net::Frame::Layer qw(:subs); | ||
| 43 | |||
| 44 | sub _checkWin32 { return 1; } | ||
| 45 | |||
| 46 | sub _checkOther { | ||
| 47 | if ($>) { | ||
| 48 | die("[-]: Net::Frame::Dump::Online: Must be EUID 0 (or equivalent) ". | ||
| 49 | "to 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 | unlinkOnStop => 1, | ||
| 61 | onRecvCount => -1, | ||
| 62 | _sDataAwaiting => 0, | ||
| 63 | @_, | ||
| 64 | ); | ||
| 65 | |||
| 66 | if (!defined($self->_sName)) { | ||
| 67 | my $int = getRandom32bitsInt(); | ||
| 68 | $self->_sName("netframe-tmp-$$.$int.storable"); | ||
| 69 | } | ||
| 70 | |||
| 71 | $SIG{INT} = sub { | ||
| 72 | return unless $self->isFather; | ||
| 73 | $self->_clean; | ||
| 74 | }; | ||
| 75 | $SIG{TERM} = sub { | ||
| 76 | return unless $self->isFather; | ||
| 77 | $self->_clean; | ||
| 78 | }; | ||
| 79 | |||
| 80 | if (!defined($self->dev)) { | ||
| 81 | print("[-] ".__PACKAGE__.": You MUST pass `dev' attribute\n"); | ||
| 82 | return; | ||
| 83 | } | ||
| 84 | |||
| 85 | return $self; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub _sStore { | ||
| 89 | lock_store(\$_[1], $_[0]->_sName) or do { | ||
| 90 | print("[-] ".__PACKAGE__.": lock_store: @{[$_[0]->_sName]}: $!\n"); | ||
| 91 | return; | ||
| 92 | }; | ||
| 93 | return 1; | ||
| 94 | } | ||
| 95 | sub _sRetrieve { | ||
| 96 | ${lock_retrieve(shift->_sName)}; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub _sWaitFile { | ||
| 100 | my $self = shift; | ||
| 101 | my $startTime = gettimeofday(); | ||
| 102 | my $thisTime = $startTime; | ||
| 103 | while (! -f $self->_sName) { | ||
| 104 | if ($thisTime - $startTime > 10) { | ||
| 105 | print("[-] ".__PACKAGE__.": too long for file creation: ". | ||
| 106 | $self->_sName."\n"); | ||
| 107 | return; | ||
| 108 | } | ||
| 109 | $thisTime = gettimeofday(); | ||
| 110 | } | ||
| 111 | return 1; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub _sWaitFileSize { | ||
| 115 | my $self = shift; | ||
| 116 | |||
| 117 | $self->_sWaitFile; | ||
| 118 | |||
| 119 | my $startTime = gettimeofday(); | ||
| 120 | my $thisTime = $startTime; | ||
| 121 | while (! ((stat($self->_sName))[7] > 0)) { | ||
| 122 | if ($thisTime - $startTime > 10) { | ||
| 123 | $self->_clean; | ||
| 124 | print("[-] ".__PACKAGE__.": too long for file creation2: ". | ||
| 125 | $self->_sName."\n"); | ||
| 126 | return; | ||
| 127 | } | ||
| 128 | $thisTime = gettimeofday(); | ||
| 129 | } | ||
| 130 | return 1; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub _startOnRecv { | ||
| 134 | my $self = shift; | ||
| 135 | |||
| 136 | my $err; | ||
| 137 | my $pd = Net::Pcap::open_live( | ||
| 138 | $self->dev, | ||
| 139 | $self->snaplen, | ||
| 140 | $self->promisc, | ||
| 141 | 1000, | ||
| 142 | \$err, | ||
| 143 | ); | ||
| 144 | unless ($pd) { | ||
| 145 | print("[-] ".__PACKAGE__.": open_live: $err\n"); | ||
| 146 | return; | ||
| 147 | } | ||
| 148 | $self->_pcapd($pd); | ||
| 149 | |||
| 150 | my $net = 0; | ||
| 151 | my $mask = 0; | ||
| 152 | Net::Pcap::lookupnet($self->dev, \$net, \$mask, \$err); | ||
| 153 | if ($err) { | ||
| 154 | print("[!] ".__PACKAGE__.": lookupnet: $err\n"); | ||
| 155 | } | ||
| 156 | |||
| 157 | my $fcode; | ||
| 158 | if (Net::Pcap::compile($pd, \$fcode, $self->filter, 0, $mask) < 0) { | ||
| 159 | print("[-] ".__PACKAGE__.": compile: ". Net::Pcap::geterr($pd). "\n"); | ||
| 160 | return; | ||
| 161 | } | ||
| 162 | |||
| 163 | if (Net::Pcap::setfilter($pd, $fcode) < 0) { | ||
| 164 | print("[-] ".__PACKAGE__.": setfilter: ". Net::Pcap::geterr($pd). "\n"); | ||
| 165 | return; | ||
| 166 | } | ||
| 167 | |||
| 168 | $self->getFirstLayer; | ||
| 169 | |||
| 170 | # Setup onRecv enforced code, to make it simpler for user | ||
| 171 | my $callback = sub { | ||
| 172 | my ($userData, $hdr, $pkt) = @_; | ||
| 173 | my $h = { | ||
| 174 | raw => $pkt, | ||
| 175 | timestamp => $hdr->{tv_sec}.'.'.sprintf("%06d", $hdr->{tv_usec}), | ||
| 176 | firstLayer => $self->firstLayer, | ||
| 177 | }; | ||
| 178 | &{$self->onRecv}($h, $userData); | ||
| 179 | }; | ||
| 180 | |||
| 181 | { | ||
| 182 | # We have to access onRecvData ARRAY indice to pass a ref | ||
| 183 | no strict 'vars'; | ||
| 184 | |||
| 185 | Net::Pcap::loop( | ||
| 186 | $pd, $self->onRecvCount, $callback, $self->[$__onRecvData], | ||
| 187 | ); | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | sub start { | ||
| 192 | my $self = shift; | ||
| 193 | |||
| 194 | _check() or return; | ||
| 195 | |||
| 196 | $self->isRunning(1); | ||
| 197 | |||
| 198 | if (-f $self->file && !$self->overwrite) { | ||
| 199 | print("[-] ".__PACKAGE__."We will not overwrite a file by default. ". | ||
| 200 | "Use `overwrite' attribute to do it.\n"); | ||
| 201 | return; | ||
| 202 | } | ||
| 203 | |||
| 204 | if ($self->onRecv) { | ||
| 205 | $self->_startOnRecv; | ||
| 206 | } | ||
| 207 | else { | ||
| 208 | $self->_sStore(0); | ||
| 209 | $self->_sWaitFileSize; | ||
| 210 | $self->_startTcpdump; | ||
| 211 | $self->_openFile; | ||
| 212 | } | ||
| 213 | |||
| 214 | return 1; | ||
| 215 | } | ||
| 216 | |||
| 217 | sub _clean { | ||
| 218 | my $self = shift; | ||
| 219 | if ($self->unlinkOnStop && $self->file && -f $self->file) { | ||
| 220 | unlink($self->file); | ||
| 221 | $self->cgDebugPrint(1, "@{[$self->file]} removed"); | ||
| 222 | } | ||
| 223 | if ($self->_sName && -f $self->_sName) { | ||
| 224 | unlink($self->_sName); | ||
| 225 | } | ||
| 226 | return 1; | ||
| 227 | } | ||
| 228 | |||
| 229 | sub stop { | ||
| 230 | my $self = shift; | ||
| 231 | |||
| 232 | if (!$self->isRunning || $self->isSon) { | ||
| 233 | return; | ||
| 234 | } | ||
| 235 | |||
| 236 | if ($self->onRecv && $self->_pcapd) { | ||
| 237 | Net::Pcap::breakloop($self->_pcapd); | ||
| 238 | Net::Pcap::close($self->_pcapd); | ||
| 239 | } | ||
| 240 | else { | ||
| 241 | $self->_killTcpdump; | ||
| 242 | Net::Pcap::breakloop($self->_pcapd); | ||
| 243 | Net::Pcap::close($self->_pcapd); | ||
| 244 | } | ||
| 245 | |||
| 246 | $self->isRunning(0); | ||
| 247 | $self->_pcapd(undef); | ||
| 248 | |||
| 249 | $self->_clean; | ||
| 250 | |||
| 251 | return 1; | ||
| 252 | } | ||
| 253 | |||
| 254 | sub getStats { | ||
| 255 | my $self = shift; | ||
| 256 | |||
| 257 | if (!defined($self->_pcapd)) { | ||
| 258 | print("[-] ".__PACKAGE__.": unable to get stats, no pcap descriptor ". | ||
| 259 | "opened.\n"); | ||
| 260 | return; | ||
| 261 | } | ||
| 262 | |||
| 263 | my %stats; | ||
| 264 | Net::Pcap::stats($self->_pcapd, \%stats); | ||
| 265 | return \%stats; | ||
| 266 | } | ||
| 267 | |||
| 268 | sub isFather { ! shift->_son } | ||
| 269 | sub isSon { shift->_son } | ||
| 270 | |||
| 271 | sub _sonPrintStats { | ||
| 272 | my $self = shift; | ||
| 273 | |||
| 274 | my $stats = $self->getStats; | ||
| 275 | $self->cgDebugPrint(1, 'Frames received : '.$stats->{ps_recv}); | ||
| 276 | $self->cgDebugPrint(1, 'Frames dropped : '.$stats->{ps_drop}); | ||
| 277 | $self->cgDebugPrint(1, 'Frames if dropped: '.$stats->{ps_ifdrop}); | ||
| 278 | return; | ||
| 279 | } | ||
| 280 | |||
| 281 | sub _startTcpdump { | ||
| 282 | my $self = shift; | ||
| 283 | |||
| 284 | my $err; | ||
| 285 | my $pd = Net::Pcap::open_live( | ||
| 286 | $self->dev, | ||
| 287 | $self->snaplen, | ||
| 288 | $self->promisc, | ||
| 289 | 1000, | ||
| 290 | \$err, | ||
| 291 | ); | ||
| 292 | unless ($pd) { | ||
| 293 | print("[-] ".__PACKAGE__.": open_live: $err\n"); | ||
| 294 | return; | ||
| 295 | } | ||
| 296 | |||
| 297 | my $net = 0; | ||
| 298 | my $mask = 0; | ||
| 299 | Net::Pcap::lookupnet($self->dev, \$net, \$mask, \$err); | ||
| 300 | if ($err) { | ||
| 301 | print("[!] ".__PACKAGE__.": lookupnet: $err\n"); | ||
| 302 | } | ||
| 303 | |||
| 304 | my $fcode; | ||
| 305 | if (Net::Pcap::compile($pd, \$fcode, $self->filter, 0, $mask) < 0) { | ||
| 306 | print("[-] ".__PACKAGE__.": compile: ". Net::Pcap::geterr($pd). "\n"); | ||
| 307 | return; | ||
| 308 | } | ||
| 309 | |||
| 310 | if (Net::Pcap::setfilter($pd, $fcode) < 0) { | ||
| 311 | print("[-] ".__PACKAGE__.": setfilter: ". Net::Pcap::geterr($pd). "\n"); | ||
| 312 | return; | ||
| 313 | } | ||
| 314 | |||
| 315 | my $p = Net::Pcap::dump_open($pd, $self->file); | ||
| 316 | unless ($p) { | ||
| 317 | print("[-] ".__PACKAGE__.": dump_open: ". Net::Pcap::geterr($pd). "\n"); | ||
| 318 | return; | ||
| 319 | } | ||
| 320 | Net::Pcap::dump_flush($p); | ||
| 321 | |||
| 322 | my $pid = fork(); | ||
| 323 | die("[-] ".__PACKAGE__.": fork: $!\n") unless defined $pid; | ||
| 324 | if ($pid) { # Parent | ||
| 325 | $self->_son(0); | ||
| 326 | $self->_pid($pid); | ||
| 327 | $SIG{CHLD} = 'IGNORE'; | ||
| 328 | return 1; | ||
| 329 | } | ||
| 330 | else { # Son | ||
| 331 | $self->_son(1); | ||
| 332 | $self->_pcapd($pd); | ||
| 333 | $SIG{HUP} = sub { $self->_sonPrintStats }; | ||
| 334 | $self->cgDebugPrint(1, "dev: [@{[$self->dev]}]\n". | ||
| 335 | "file: [@{[$self->file]}]\n". | ||
| 336 | "filter: [@{[$self->filter]}]"); | ||
| 337 | Net::Pcap::loop($pd, -1, \&_tcpdumpCallback, [ $p, $self ]); | ||
| 338 | Net::Pcap::close($pd); | ||
| 339 | exit(0); | ||
| 340 | } | ||
| 341 | } | ||
| 342 | |||
| 343 | sub _tcpdumpCallback { | ||
| 344 | my ($data, $hdr, $pkt) = @_; | ||
| 345 | my $p = $data->[0]; | ||
| 346 | my $self = $data->[1]; | ||
| 347 | |||
| 348 | Net::Pcap::dump($p, $hdr, $pkt); | ||
| 349 | Net::Pcap::dump_flush($p); | ||
| 350 | |||
| 351 | my $n = $self->_sRetrieve; | ||
| 352 | $self->_sStore(++$n); | ||
| 353 | } | ||
| 354 | |||
| 355 | sub _killTcpdump { | ||
| 356 | my $self = shift; | ||
| 357 | return if $self->isSon; | ||
| 358 | kill('KILL', $self->_pid); | ||
| 359 | $self->_pid(undef); | ||
| 360 | } | ||
| 361 | |||
| 362 | sub _openFile { | ||
| 363 | my $self = shift; | ||
| 364 | |||
| 365 | my $err; | ||
| 366 | my $pcapd = Net::Pcap::open_offline($self->file, \$err); | ||
| 367 | if (!defined($pcapd)) { | ||
| 368 | print("[-] ".__PACKAGE__.": Net::Pcap::open_offline: ". | ||
| 369 | "@{[$self->file]}: $err\n"); | ||
| 370 | return; | ||
| 371 | } | ||
| 372 | $self->_pcapd($pcapd); | ||
| 373 | |||
| 374 | return $self->getFirstLayer; | ||
| 375 | } | ||
| 376 | |||
| 377 | sub _getNextAwaitingFrame { | ||
| 378 | my $self = shift; | ||
| 379 | my $last = $self->_sDataAwaiting; | ||
| 380 | my $new = $self->_sRetrieve; | ||
| 381 | |||
| 382 | # Return if nothing new is awaiting | ||
| 383 | return if ($new <= $last); | ||
| 384 | |||
| 385 | $self->_sDataAwaiting($self->_sDataAwaiting + 1); | ||
| 386 | return $self->SUPER::next; | ||
| 387 | } | ||
| 388 | |||
| 389 | sub _nextTimeoutHandle { | ||
| 390 | my $self = shift; | ||
| 391 | |||
| 392 | # Handle timeout | ||
| 393 | my $thisTime = gettimeofday(); | ||
| 394 | if ($self->timeoutOnNext && !$self->_firstTime) { | ||
| 395 | $self->_firstTime($thisTime); | ||
| 396 | } | ||
| 397 | |||
| 398 | if ($self->timeoutOnNext && $self->_firstTime) { | ||
| 399 | if (($thisTime - $self->_firstTime) > $self->timeoutOnNext) { | ||
| 400 | $self->timeout(1); | ||
| 401 | $self->_firstTime(0); | ||
| 402 | $self->cgDebugPrint(1, "Timeout occurred"); | ||
| 403 | return; | ||
| 404 | } | ||
| 405 | } | ||
| 406 | |||
| 407 | return 1; | ||
| 408 | } | ||
| 409 | |||
| 410 | sub _nextTimeoutReset { shift->_firstTime(0) } | ||
| 411 | |||
| 412 | sub timeoutReset { shift->timeout(0) } | ||
| 413 | |||
| 414 | sub next { | ||
| 415 | my $self = shift; | ||
| 416 | |||
| 417 | $self->_nextTimeoutHandle or return; | ||
| 418 | |||
| 419 | my $frame = $self->_getNextAwaitingFrame; | ||
| 420 | $self->_nextTimeoutReset if $frame; | ||
| 421 | |||
| 422 | return $frame; | ||
| 423 | } | ||
| 424 | |||
| 425 | 1; | ||
| 426 | |||
| 427 | __END__ | ||