| 1 | # | ||
| 2 | # $Id: Passive.pm,v 008243d3e89a 2018/07/21 14:54:07 gomor $ | ||
| 3 | # | ||
| 4 | package Net::SinFP3::Search::Passive; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use base qw(Net::SinFP3::Search); | ||
| 9 | our @AS = qw( | ||
| 10 | sp | ||
| 11 | ); | ||
| 12 | __PACKAGE__->cgBuildIndices; | ||
| 13 | __PACKAGE__->cgBuildAccessorsScalar(\@AS); | ||
| 14 | |||
| 15 | use Net::SinFP3 qw(:matchMask); | ||
| 16 | |||
| 17 | use Net::SinFP3::Ext::SP; | ||
| 18 | use Net::SinFP3::Result::Passive; | ||
| 19 | use Net::SinFP3::Result::PortError; | ||
| 20 | use Net::SinFP3::Result::Unknown; | ||
| 21 | |||
| 22 | use Net::Frame::Layer::TCP qw(:consts); | ||
| 23 | |||
| 24 | sub take { | ||
| 25 | return [ | ||
| 26 | 'Net::SinFP3::Mode::Passive', | ||
| 27 | ]; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub new { | ||
| 31 | my $self = shift->SUPER::new( | ||
| 32 | @_, | ||
| 33 | ); | ||
| 34 | |||
| 35 | return $self; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub _getPossibleSignaturePIds { | ||
| 39 | my $self = shift; | ||
| 40 | my ($sp) = @_; | ||
| 41 | |||
| 42 | my $global = $self->global; | ||
| 43 | my $log = $global->log; | ||
| 44 | my $db = $global->db; | ||
| 45 | |||
| 46 | my %patterns = ( | ||
| 47 | PatternTcpFlags => 'F', | ||
| 48 | PatternTcpWindow => 'W', | ||
| 49 | PatternTcpOptions => 'O', | ||
| 50 | PatternTcpMss => 'M', | ||
| 51 | PatternTcpWScale => 'S', | ||
| 52 | PatternTcpOLength => 'L', | ||
| 53 | ); | ||
| 54 | |||
| 55 | my %results = (); | ||
| 56 | for my $tPattern (keys %patterns) { | ||
| 57 | my $pId = "id$tPattern"; | ||
| 58 | |||
| 59 | # $sp->{F} for instance | ||
| 60 | my $p = $sp->{$patterns{$tPattern}}; | ||
| 61 | |||
| 62 | my $_table = "_$tPattern"; | ||
| 63 | my %ids = (); | ||
| 64 | for my $h ('Heuristic0', 'Heuristic1', 'Heuristic2') { | ||
| 65 | for my $t ($db->$_table) { | ||
| 66 | (my $method = $tPattern.$h) =~ s/^(.)(.*)$/@{[lc($1)]}$2/; | ||
| 67 | # We match either using regexp from DB, | ||
| 68 | # or regexp built in passive mode | ||
| 69 | if ($p =~ /^@{[$t->{$method}]}$/ || $t->{$method} =~ /$p/) { | ||
| 70 | #print "DEBUG: [$p] against [".$t->{$method}."]\n"; | ||
| 71 | my $id = $t->{$pId}; | ||
| 72 | my $list = $db->searchSignaturePIds( | ||
| 73 | $pId => $id, | ||
| 74 | ); | ||
| 75 | for (@$list) { | ||
| 76 | $ids{$h}->{$_}++; | ||
| 77 | #print "DEBUG: possibleId [$id]\n"; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } | ||
| 81 | } | ||
| 82 | $results{$patterns{$tPattern}} = \%ids; | ||
| 83 | } | ||
| 84 | |||
| 85 | #use Data::Dumper; | ||
| 86 | #print "DEBUG: _getPossibleSignaturePIds: ", | ||
| 87 | #Dumper(\%results),"\n"; | ||
| 88 | |||
| 89 | return \%results; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub _searchSmallestHeuristicHash { | ||
| 93 | my $self = shift; | ||
| 94 | my ($heuristic, @patterns) = @_; | ||
| 95 | |||
| 96 | my @ids = (); | ||
| 97 | my $last = 10_000; # Invalid huge number | ||
| 98 | for my $pList (@patterns) { | ||
| 99 | my $count = keys %{$pList->{$heuristic}}; | ||
| 100 | next if $count == 0; # We need to skip, otherwise there is a bug ;) | ||
| 101 | if ($count < $last) { | ||
| 102 | @ids = keys %{$pList->{$heuristic}}; | ||
| 103 | $last = $count; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | return \@ids; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub _searchSmallestHashWithMask { | ||
| 110 | my $self = shift; | ||
| 111 | |||
| 112 | my @ids = (); | ||
| 113 | my $last = 10_000; # Invalid huge number | ||
| 114 | for my $pList (@_) { | ||
| 115 | my $count = keys %$pList; | ||
| 116 | next if $count == 0; # We need to skip, otherwise there is a bug ;) | ||
| 117 | if ($count < $last) { | ||
| 118 | @ids = keys %$pList; | ||
| 119 | $last = $count; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | return \@ids; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub _getIntersection { | ||
| 126 | my $self = shift; | ||
| 127 | my ($fList, $wList, $oList, $mList, $sList, $lList) = @_; | ||
| 128 | |||
| 129 | my $inter; | ||
| 130 | for my $h ('Heuristic0', 'Heuristic1', 'Heuristic2') { | ||
| 131 | my $smallest = $self->_searchSmallestHeuristicHash( | ||
| 132 | $h, $fList, $wList, $oList, $mList, $sList, $lList, | ||
| 133 | ); | ||
| 134 | #print "[*] DEBUG: _getIntersection: _searchSmallestHeuristicHash[$h]: ",Dumper($smallest),"\n"; | ||
| 135 | for my $id (@$smallest) { | ||
| 136 | if (($fList->{Heuristic0}->{$id} || $fList->{Heuristic1}->{$id} || $fList->{Heuristic2}->{$id}) | ||
| 137 | && ($wList->{Heuristic0}->{$id} || $wList->{Heuristic1}->{$id} || $wList->{Heuristic2}->{$id}) | ||
| 138 | && ($oList->{Heuristic0}->{$id} || $oList->{Heuristic1}->{$id} || $oList->{Heuristic2}->{$id}) | ||
| 139 | && ($mList->{Heuristic0}->{$id} || $mList->{Heuristic1}->{$id} || $mList->{Heuristic2}->{$id}) | ||
| 140 | && ($sList->{Heuristic0}->{$id} || $sList->{Heuristic1}->{$id} || $sList->{Heuristic2}->{$id}) | ||
| 141 | && ($lList->{Heuristic0}->{$id} || $lList->{Heuristic1}->{$id} || $lList->{Heuristic2}->{$id})) { | ||
| 142 | my $f = $fList->{Heuristic0}->{$id} && 'FH0' | ||
| 143 | || $fList->{Heuristic1}->{$id} && 'FH1' | ||
| 144 | || $fList->{Heuristic2}->{$id} && 'FH2'; | ||
| 145 | my $w = $wList->{Heuristic0}->{$id} && 'WH0' | ||
| 146 | || $wList->{Heuristic1}->{$id} && 'WH1' | ||
| 147 | || $wList->{Heuristic2}->{$id} && 'WH2'; | ||
| 148 | my $o = $oList->{Heuristic0}->{$id} && 'OH0' | ||
| 149 | || $oList->{Heuristic1}->{$id} && 'OH1' | ||
| 150 | || $oList->{Heuristic2}->{$id} && 'OH2'; | ||
| 151 | my $m = $mList->{Heuristic0}->{$id} && 'MH0' | ||
| 152 | || $mList->{Heuristic1}->{$id} && 'MH1' | ||
| 153 | || $mList->{Heuristic2}->{$id} && 'MH2'; | ||
| 154 | my $s = $sList->{Heuristic0}->{$id} && 'SH0' | ||
| 155 | || $sList->{Heuristic1}->{$id} && 'SH1' | ||
| 156 | || $sList->{Heuristic2}->{$id} && 'SH2'; | ||
| 157 | my $l = $lList->{Heuristic0}->{$id} && 'LH0' | ||
| 158 | || $lList->{Heuristic1}->{$id} && 'LH1' | ||
| 159 | || $lList->{Heuristic2}->{$id} && 'LH2'; | ||
| 160 | $inter->{"$f$w$o$m$s$l"}->{$id}++; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | # Stop if we found matches with this smallest heuristic level | ||
| 164 | #last if keys %$inter > 0; | ||
| 165 | } | ||
| 166 | return $inter; | ||
| 167 | } | ||
| 168 | |||
| 169 | sub _getIntersectionWithMask { | ||
| 170 | my $self = shift; | ||
| 171 | my ($fList, $wList, $oList, $mList, $sList, $lList, $mask) = @_; | ||
| 172 | |||
| 173 | my $f = $mask =~ /FH0/ && 'Heuristic0' | ||
| 174 | || $mask =~ /FH1/ && 'Heuristic1' | ||
| 175 | || $mask =~ /FH2/ && 'Heuristic2'; | ||
| 176 | my $w = $mask =~ /WH0/ && 'Heuristic0' | ||
| 177 | || $mask =~ /WH1/ && 'Heuristic1' | ||
| 178 | || $mask =~ /WH2/ && 'Heuristic2'; | ||
| 179 | my $o = $mask =~ /OH0/ && 'Heuristic0' | ||
| 180 | || $mask =~ /OH1/ && 'Heuristic1' | ||
| 181 | || $mask =~ /OH2/ && 'Heuristic2'; | ||
| 182 | my $m = $mask =~ /MH0/ && 'Heuristic0' | ||
| 183 | || $mask =~ /MH1/ && 'Heuristic1' | ||
| 184 | || $mask =~ /MH2/ && 'Heuristic2'; | ||
| 185 | my $s = $mask =~ /SH0/ && 'Heuristic0' | ||
| 186 | || $mask =~ /SH1/ && 'Heuristic1' | ||
| 187 | || $mask =~ /SH2/ && 'Heuristic2'; | ||
| 188 | my $l = $mask =~ /LH0/ && 'Heuristic0' | ||
| 189 | || $mask =~ /LH1/ && 'Heuristic1' | ||
| 190 | || $mask =~ /LH2/ && 'Heuristic2'; | ||
| 191 | |||
| 192 | # We force a search using a very specific heuristic mask | ||
| 193 | my $smallest = $self->_searchSmallestHashWithMask( | ||
| 194 | $fList->{$f}, $wList->{$w}, $oList->{$o}, $mList->{$m}, | ||
| 195 | $sList->{$s}, $lList->{$l}, | ||
| 196 | ); | ||
| 197 | |||
| 198 | my $inter; | ||
| 199 | for my $id (@$smallest) { | ||
| 200 | if ($fList->{$f}->{$id} | ||
| 201 | && $wList->{$w}->{$id} | ||
| 202 | && $oList->{$o}->{$id} | ||
| 203 | && $mList->{$m}->{$id} | ||
| 204 | && $sList->{$s}->{$id} | ||
| 205 | && $lList->{$l}->{$id}) { | ||
| 206 | $inter->{$mask}->{$id}++; | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | return $inter; | ||
| 211 | } | ||
| 212 | |||
| 213 | sub _searchSmallestInterWithMask { | ||
| 214 | my $self = shift; | ||
| 215 | |||
| 216 | my @ids = (); | ||
| 217 | my $last = 10_000; # Invalid huge number | ||
| 218 | for my $inter (@_) { | ||
| 219 | my $count = keys %$inter; | ||
| 220 | next if $count == 0; # We need to skip, otherwise there is a bug ;) | ||
| 221 | if ($count < $last) { | ||
| 222 | @ids = keys %$inter; | ||
| 223 | $last = $count; | ||
| 224 | } | ||
| 225 | } | ||
| 226 | return \@ids; | ||
| 227 | } | ||
| 228 | |||
| 229 | sub _tohash { | ||
| 230 | my $self = shift; | ||
| 231 | my ($s) = @_; | ||
| 232 | return { | ||
| 233 | F => $self->$s->F, | ||
| 234 | W => $self->$s->W, | ||
| 235 | O => $self->$s->O, | ||
| 236 | M => $self->$s->M, | ||
| 237 | S => $self->$s->S, | ||
| 238 | L => $self->$s->L, | ||
| 239 | }; | ||
| 240 | } | ||
| 241 | |||
| 242 | sub _countInter { | ||
| 243 | my $self = shift; | ||
| 244 | my ($ids) = @_; | ||
| 245 | $ids->{nInter} = keys %{$ids->{Inter}}; | ||
| 246 | return $ids; | ||
| 247 | } | ||
| 248 | |||
| 249 | sub search { | ||
| 250 | my $self = shift; | ||
| 251 | |||
| 252 | my $global = $self->global; | ||
| 253 | my $log = $global->log; | ||
| 254 | |||
| 255 | # Convert it to optimize a bit | ||
| 256 | my $sp = $self->_tohash('sp'); | ||
| 257 | |||
| 258 | my $ids = {}; | ||
| 259 | my $res = $self->_getPossibleSignaturePIds($sp); | ||
| 260 | my $inter = $self->_getIntersection( | ||
| 261 | $res->{F}, $res->{W}, $res->{O}, $res->{M}, $res->{S}, $res->{L}, | ||
| 262 | ); | ||
| 263 | $ids->{Ids} = $res; | ||
| 264 | $ids->{Inter} = $inter; | ||
| 265 | |||
| 266 | # Make masks unique | ||
| 267 | my %maskList = map { $_ => 1 } (keys %{$ids->{Inter}}); | ||
| 268 | |||
| 269 | # Update number of resulting intersection | ||
| 270 | $self->_countInter($ids); | ||
| 271 | |||
| 272 | # For all masks, expand possible Signature IDs to make | ||
| 273 | # all of them comparable | ||
| 274 | for my $mask (keys %maskList) { | ||
| 275 | #print "[*] ".Dumper($ids->{$p}{Ids})."\n"; | ||
| 276 | if (!exists $ids->{Inter}->{$mask}) { | ||
| 277 | #print "[*] Running with [$mask] against [$p]\n"; | ||
| 278 | my $interNew = $self->_getIntersectionWithMask( | ||
| 279 | $ids->{Ids}->{F}, $ids->{Ids}->{W}, $ids->{Ids}->{O}, | ||
| 280 | $ids->{Ids}->{M}, $ids->{Ids}->{S}, $ids->{Ids}->{L}, | ||
| 281 | $mask, | ||
| 282 | ); | ||
| 283 | if ($interNew) { | ||
| 284 | use Data::Dumper; | ||
| 285 | $log->debug("interNew: ".Dumper($interNew)); | ||
| 286 | $ids->{Inter}->{$mask} = $interNew->{$mask}; | ||
| 287 | } | ||
| 288 | } | ||
| 289 | } | ||
| 290 | |||
| 291 | # Update number of resulting intersections | ||
| 292 | # after we have expanded mask list | ||
| 293 | $self->_countInter($ids); | ||
| 294 | |||
| 295 | my @resultList = (); | ||
| 296 | |||
| 297 | #print "DEBUG: NS_MATCH_TYPE_P2\n"; | ||
| 298 | my $results = $self->_buildResultList($ids->{Inter}); | ||
| 299 | push @resultList, @$results; | ||
| 300 | |||
| 301 | my $clean4 = $self->_cleanResults(\@resultList, 'IPv4'); | ||
| 302 | my $clean6 = $self->_cleanResults(\@resultList, 'IPv6'); | ||
| 303 | |||
| 304 | # We keep IPv4 signatures in IPv6 mode only if no IPv6 matchs | ||
| 305 | # Else in IPv4 mode, we only keep IPv4 matchs | ||
| 306 | my @clean = (); | ||
| 307 | if ($self->global->ipv6 && @$clean6 > 0) { | ||
| 308 | push @clean, @$clean6; | ||
| 309 | } | ||
| 310 | elsif (@$clean4 > 0) { | ||
| 311 | push @clean, @$clean4; | ||
| 312 | } | ||
| 313 | |||
| 314 | return \@clean; | ||
| 315 | } | ||
| 316 | |||
| 317 | sub _cleanResults { | ||
| 318 | my $self = shift; | ||
| 319 | my ($results, $ip) = @_; | ||
| 320 | |||
| 321 | my $global = $self->global; | ||
| 322 | |||
| 323 | # Sort to easily filter out | ||
| 324 | my $sorted = {}; | ||
| 325 | for my $r (@$results) { | ||
| 326 | if ($r->ipVersion ne $ip) { | ||
| 327 | next; | ||
| 328 | } | ||
| 329 | if ($global->threshold != 0 && $r->matchScore < $global->threshold) { | ||
| 330 | next; | ||
| 331 | } | ||
| 332 | push @{$sorted->{$r->matchScore}}, $r; | ||
| 333 | } | ||
| 334 | |||
| 335 | my $p2 = $sorted; | ||
| 336 | |||
| 337 | my @sorted2 = (); | ||
| 338 | if (keys %$p2 > 0) { | ||
| 339 | # Sort results by IP version and score, keep only highest score for an ID | ||
| 340 | my %idList = (); | ||
| 341 | my $bestScore = 0; | ||
| 342 | for my $p (sort { $b <=> $a } keys %$sorted) { | ||
| 343 | for my $r (@{$sorted->{$p}}) { | ||
| 344 | if (! exists($idList{$r->idSignatureP})) { | ||
| 345 | if ($global->bestScore) { | ||
| 346 | if ($r->matchScore >= $bestScore) { | ||
| 347 | push @sorted2, $r; | ||
| 348 | $idList{$r->idSignatureP}++; | ||
| 349 | if (! $bestScore) { | ||
| 350 | $bestScore = $r->matchScore; | ||
| 351 | } | ||
| 352 | } | ||
| 353 | } | ||
| 354 | else { | ||
| 355 | push @sorted2, $r; | ||
| 356 | $idList{$r->idSignatureP}++; | ||
| 357 | } | ||
| 358 | } | ||
| 359 | } | ||
| 360 | } | ||
| 361 | } | ||
| 362 | else { | ||
| 363 | # Or no matchs at all | ||
| 364 | return []; | ||
| 365 | } | ||
| 366 | |||
| 367 | return \@sorted2; | ||
| 368 | } | ||
| 369 | |||
| 370 | sub _buildResultList { | ||
| 371 | my $self = shift; | ||
| 372 | my ($result) = @_; | ||
| 373 | |||
| 374 | my $global = $self->global; | ||
| 375 | my $log = $global->log; | ||
| 376 | my $db = $global->db; | ||
| 377 | |||
| 378 | my @resultList = (); | ||
| 379 | for my $mask (keys %$result) { | ||
| 380 | for my $id (keys %{$result->{$mask}}) { | ||
| 381 | my %args = (); | ||
| 382 | my $sig = $db->retrieveSignatureP($id); | ||
| 383 | my $result = Net::SinFP3::Result::Passive->new( | ||
| 384 | global => $self->global, | ||
| 385 | sp => $self->sp, | ||
| 386 | trusted => $sig->{trusted}, | ||
| 387 | idSignatureP => $sig->{idSignatureP}, | ||
| 388 | ipVersion => $sig->{ipVersion}, | ||
| 389 | systemClass => $sig->{systemClass}, | ||
| 390 | vendor => $sig->{vendor}, | ||
| 391 | os => $sig->{os}, | ||
| 392 | osVersion => $sig->{osVersion}, | ||
| 393 | osVersionFamily => $sig->{osVersionFamily}, | ||
| 394 | matchType => 'P2', | ||
| 395 | matchMask => $mask, | ||
| 396 | osVersionChildrenList => $db->getOsVersionChildrenPList( | ||
| 397 | $id, | ||
| 398 | ), | ||
| 399 | ); | ||
| 400 | $result->updateMatchScore; | ||
| 401 | push @resultList, $result; | ||
| 402 | } | ||
| 403 | } | ||
| 404 | |||
| 405 | return \@resultList; | ||
| 406 | } | ||
| 407 | |||
| 408 | sub run { | ||
| 409 | my $self = shift->SUPER::run(@_) or return; | ||
| 410 | |||
| 411 | my $global = $self->global; | ||
| 412 | my $log = $global->log; | ||
| 413 | my $mode = $global->mode; | ||
| 414 | |||
| 415 | if (! defined($mode->sp)) { | ||
| 416 | $log->error("Nothing to search"); | ||
| 417 | return; | ||
| 418 | } | ||
| 419 | |||
| 420 | $self->sp($mode->sp); | ||
| 421 | |||
| 422 | my $result = $self->search; | ||
| 423 | if (@$result == 0) { | ||
| 424 | my $r = Net::SinFP3::Result::Unknown->new( | ||
| 425 | global => $self->global, | ||
| 426 | sp => $self->sp, | ||
| 427 | ); | ||
| 428 | $result = [ $r ]; | ||
| 429 | } | ||
| 430 | |||
| 431 | # Fill frame attribute if available | ||
| 432 | if ($mode->frame) { | ||
| 433 | my $f = $mode->frame; | ||
| 434 | my $ip = $f->ref->{IPv4} || $f->ref->{IPv6}; | ||
| 435 | my $tcp = $f->ref->{TCP}; | ||
| 436 | for my $r (@$result) { | ||
| 437 | $r->frame($mode->frame); | ||
| 438 | $r->ip($ip->dst); | ||
| 439 | $r->port($tcp->dst); | ||
| 440 | if ($global->dnsReverse) { | ||
| 441 | $r->reverse($global->getAddrReverse(addr => $r->ip) || 'unknown'); | ||
| 442 | } | ||
| 443 | } | ||
| 444 | } | ||
| 445 | |||
| 446 | return $result; | ||
| 447 | } | ||
| 448 | |||
| 449 | 1; | ||
| 450 | |||
| 451 | __END__ |