| 1 | # | ||
| 2 | # $Id: Active.pm,v 008243d3e89a 2018/07/21 14:54:07 gomor $ | ||
| 3 | # | ||
| 4 | package Net::SinFP3::Search::Active; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use base qw(Net::SinFP3::Search); | ||
| 9 | our @AS = qw( | ||
| 10 | s1 | ||
| 11 | s2 | ||
| 12 | s3 | ||
| 13 | _cache | ||
| 14 | ); | ||
| 15 | __PACKAGE__->cgBuildIndices; | ||
| 16 | __PACKAGE__->cgBuildAccessorsScalar(\@AS); | ||
| 17 | |||
| 18 | use Net::SinFP3 qw(:matchType :matchMask); | ||
| 19 | |||
| 20 | use Net::SinFP3::Ext::S; | ||
| 21 | use Net::SinFP3::Result::Active; | ||
| 22 | use Net::SinFP3::Result::PortError; | ||
| 23 | use Net::SinFP3::Result::Unknown; | ||
| 24 | |||
| 25 | use Net::Frame::Layer::TCP qw(:consts); | ||
| 26 | |||
| 27 | use Data::Dumper; | ||
| 28 | |||
| 29 | sub take { | ||
| 30 | return [ | ||
| 31 | 'Net::SinFP3::Mode::Active', | ||
| 32 | ]; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub new { | ||
| 36 | my $self = shift->SUPER::new( | ||
| 37 | @_, | ||
| 38 | ); | ||
| 39 | |||
| 40 | return $self; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub __patternBinary { | ||
| 44 | my ($probe, $sig) = @_; | ||
| 45 | |||
| 46 | return { | ||
| 47 | table => 'patternBinary', | ||
| 48 | idPattern => 'idPatternBinary', | ||
| 49 | idSPattern => 'id'.$probe.'PatternBinary', | ||
| 50 | pattern => $sig->{$probe}->B, | ||
| 51 | tPattern => 'PatternBinary', | ||
| 52 | cPattern => '_PatternBinary', | ||
| 53 | }; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub __patternTcpFlags { | ||
| 57 | my ($probe, $sig) = @_; | ||
| 58 | |||
| 59 | return { | ||
| 60 | table => 'patternTcpFlags', | ||
| 61 | idPattern => 'idPatternTcpFlags', | ||
| 62 | idSPattern => 'id'.$probe.'PatternTcpFlags', | ||
| 63 | pattern => $sig->{$probe}->F, | ||
| 64 | tPattern => 'PatternTcpFlags', | ||
| 65 | cPattern => '_PatternTcpFlags', | ||
| 66 | }; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub __patternTcpWindow { | ||
| 70 | my ($probe, $sig) = @_; | ||
| 71 | |||
| 72 | return { | ||
| 73 | table => 'patternTcpWindow', | ||
| 74 | idPattern => 'idPatternTcpWindow', | ||
| 75 | idSPattern => 'id'.$probe.'PatternTcpWindow', | ||
| 76 | pattern => $sig->{$probe}->W, | ||
| 77 | tPattern => 'PatternTcpWindow', | ||
| 78 | cPattern => '_PatternTcpWindow', | ||
| 79 | }; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub __patternTcpOptions { | ||
| 83 | my ($probe, $sig) = @_; | ||
| 84 | |||
| 85 | return { | ||
| 86 | table => 'patternTcpOptions', | ||
| 87 | idPattern => 'idPatternTcpOptions', | ||
| 88 | idSPattern => 'id'.$probe.'PatternTcpOptions', | ||
| 89 | pattern => $sig->{$probe}->O, | ||
| 90 | tPattern => 'PatternTcpOptions', | ||
| 91 | cPattern => '_PatternTcpOptions', | ||
| 92 | }; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub __patternTcpMss { | ||
| 96 | my ($probe, $sig) = @_; | ||
| 97 | |||
| 98 | return { | ||
| 99 | table => 'patternTcpMss', | ||
| 100 | idPattern => 'idPatternTcpMss', | ||
| 101 | idSPattern => 'id'.$probe.'PatternTcpMss', | ||
| 102 | pattern => $sig->{$probe}->M, | ||
| 103 | tPattern => 'PatternTcpMss', | ||
| 104 | cPattern => '_PatternTcpMss', | ||
| 105 | }; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub __patternTcpWScale { | ||
| 109 | my ($probe, $sig) = @_; | ||
| 110 | |||
| 111 | return { | ||
| 112 | table => 'patternTcpWScale', | ||
| 113 | idPattern => 'idPatternTcpWScale', | ||
| 114 | idSPattern => 'id'.$probe.'PatternTcpWScale', | ||
| 115 | pattern => $sig->{$probe}->S, | ||
| 116 | tPattern => 'PatternTcpWScale', | ||
| 117 | cPattern => '_PatternTcpWScale', | ||
| 118 | }; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub __patternTcpOLength { | ||
| 122 | my ($probe, $sig) = @_; | ||
| 123 | |||
| 124 | return { | ||
| 125 | table => 'patternTcpOLength', | ||
| 126 | idPattern => 'idPatternTcpOLength', | ||
| 127 | idSPattern => 'id'.$probe.'PatternTcpOLength', | ||
| 128 | pattern => $sig->{$probe}->L, | ||
| 129 | tPattern => 'PatternTcpOLength', | ||
| 130 | cPattern => '_PatternTcpOLength', | ||
| 131 | }; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub _getPossibleSignatureIds { | ||
| 135 | my $self = shift; | ||
| 136 | my ($probe, $sig) = @_; | ||
| 137 | |||
| 138 | my $global = $self->global; | ||
| 139 | my $log = $global->log; | ||
| 140 | my $db = $global->db; | ||
| 141 | |||
| 142 | my $cache = $self->_cache; | ||
| 143 | |||
| 144 | my %patterns = ( | ||
| 145 | PatternBinary => 'B', | ||
| 146 | PatternTcpFlags => 'F', | ||
| 147 | PatternTcpWindow => 'W', | ||
| 148 | PatternTcpOptions => 'O', | ||
| 149 | PatternTcpMss => 'M', | ||
| 150 | PatternTcpWScale => 'S', | ||
| 151 | PatternTcpOLength => 'L', | ||
| 152 | ); | ||
| 153 | |||
| 154 | my %results = (); | ||
| 155 | for my $sub ( | ||
| 156 | qw( | ||
| 157 | __patternTcpWindow __patternTcpOptions __patternBinary | ||
| 158 | __patternTcpWScale __patternTcpMss __patternTcpOLength | ||
| 159 | __patternTcpFlags | ||
| 160 | ) | ||
| 161 | ) { | ||
| 162 | my $fields; | ||
| 163 | { | ||
| 164 | no strict 'refs'; | ||
| 165 | $fields = &$sub($probe, $sig); | ||
| 166 | } | ||
| 167 | |||
| 168 | my $table = $fields->{table}; | ||
| 169 | my $idPattern = $fields->{idPattern}; | ||
| 170 | my $idSPattern = $fields->{idSPattern}; | ||
| 171 | my $pattern = $fields->{pattern}; | ||
| 172 | my $tPattern = $fields->{tPattern}; | ||
| 173 | my $cPattern = $fields->{cPattern}; | ||
| 174 | |||
| 175 | my %ids = (); | ||
| 176 | for my $h ('Heuristic0', 'Heuristic1', 'Heuristic2') { | ||
| 177 | my $method = $table.$h; # patternBinaryHeuristic0 | ||
| 178 | for my $t ($db->$cPattern) { | ||
| 179 | my $id = $t->{$idPattern}; | ||
| 180 | my $ent = "$idSPattern-$id"; | ||
| 181 | my $match = $t->{$method}; | ||
| 182 | |||
| 183 | # We match either using regexp from DB, | ||
| 184 | # or regexp built in passive mode | ||
| 185 | if ($pattern =~ /^$match$/ || $match =~ /$pattern/) { | ||
| 186 | #print "DEBUG: [$pattern] against [$match]\n"; | ||
| 187 | my $list; | ||
| 188 | if (exists $cache->{$ent}) { | ||
| 189 | $list = $cache->{$ent}; | ||
| 190 | } | ||
| 191 | else { | ||
| 192 | $list = $db->searchSignatureIds($idSPattern => $id); | ||
| 193 | $cache->{$ent} = $list; | ||
| 194 | } | ||
| 195 | for (@$list) { | ||
| 196 | $ids{$h}->{$_}++; | ||
| 197 | #print "DEBUG: possibleId [$id]\n"; | ||
| 198 | } | ||
| 199 | } | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | # $results{B} = \%ids; | ||
| 204 | $results{$patterns{$tPattern}} = \%ids; | ||
| 205 | } | ||
| 206 | |||
| 207 | #print "DEBUG: [$probe]_getPossibleSignatureIds: ", | ||
| 208 | #Dumper(\%results),"\n"; | ||
| 209 | |||
| 210 | return \%results; | ||
| 211 | } | ||
| 212 | |||
| 213 | sub _searchSmallestHeuristicHash { | ||
| 214 | my $self = shift; | ||
| 215 | my ($heuristic, @patterns) = @_; | ||
| 216 | |||
| 217 | my @ids = (); | ||
| 218 | my $last = 10_000; # Invalid huge number | ||
| 219 | for my $pList (@patterns) { | ||
| 220 | my $count = keys %{$pList->{$heuristic}}; | ||
| 221 | next if $count == 0; # We need to skip, otherwise there is a bug ;) | ||
| 222 | if ($count < $last) { | ||
| 223 | @ids = keys %{$pList->{$heuristic}}; | ||
| 224 | $last = $count; | ||
| 225 | } | ||
| 226 | } | ||
| 227 | return \@ids; | ||
| 228 | } | ||
| 229 | |||
| 230 | sub _searchSmallestHashWithMask { | ||
| 231 | my $self = shift; | ||
| 232 | |||
| 233 | my @ids = (); | ||
| 234 | my $last = 10_000; # Invalid huge number | ||
| 235 | for my $pList (@_) { | ||
| 236 | my $count = keys %$pList; | ||
| 237 | next if $count == 0; # We need to skip, otherwise there is a bug ;) | ||
| 238 | if ($count < $last) { | ||
| 239 | @ids = keys %$pList; | ||
| 240 | $last = $count; | ||
| 241 | } | ||
| 242 | } | ||
| 243 | return \@ids; | ||
| 244 | } | ||
| 245 | |||
| 246 | sub _getIntersection { | ||
| 247 | my $self = shift; | ||
| 248 | my ($bList, $fList, $wList, $oList, $mList, $sList, $lList) = @_; | ||
| 249 | |||
| 250 | my $inter; | ||
| 251 | for my $h ('Heuristic0', 'Heuristic1', 'Heuristic2') { | ||
| 252 | my $smallest = $self->_searchSmallestHeuristicHash( | ||
| 253 | $h, $bList, $fList, $wList, $oList, $mList, $sList, $lList, | ||
| 254 | ); | ||
| 255 | #print "[*] DEBUG: _getIntersection: _searchSmallestHeuristicHash[$h]: ",Dumper($smallest),"\n"; | ||
| 256 | for my $id (@$smallest) { | ||
| 257 | if (($bList->{Heuristic0}->{$id} || $bList->{Heuristic1}->{$id} || $bList->{Heuristic2}->{$id}) | ||
| 258 | && ($fList->{Heuristic0}->{$id} || $fList->{Heuristic1}->{$id} || $fList->{Heuristic2}->{$id}) | ||
| 259 | && ($wList->{Heuristic0}->{$id} || $wList->{Heuristic1}->{$id} || $wList->{Heuristic2}->{$id}) | ||
| 260 | && ($oList->{Heuristic0}->{$id} || $oList->{Heuristic1}->{$id} || $oList->{Heuristic2}->{$id}) | ||
| 261 | && ($mList->{Heuristic0}->{$id} || $mList->{Heuristic1}->{$id} || $mList->{Heuristic2}->{$id}) | ||
| 262 | && ($sList->{Heuristic0}->{$id} || $sList->{Heuristic1}->{$id} || $sList->{Heuristic2}->{$id}) | ||
| 263 | && ($lList->{Heuristic0}->{$id} || $lList->{Heuristic1}->{$id} || $lList->{Heuristic2}->{$id})) { | ||
| 264 | my $b = $bList->{Heuristic0}->{$id} && 'BH0' | ||
| 265 | || $bList->{Heuristic1}->{$id} && 'BH1' | ||
| 266 | || $bList->{Heuristic2}->{$id} && 'BH2'; | ||
| 267 | my $f = $fList->{Heuristic0}->{$id} && 'FH0' | ||
| 268 | || $fList->{Heuristic1}->{$id} && 'FH1' | ||
| 269 | || $fList->{Heuristic2}->{$id} && 'FH2'; | ||
| 270 | my $w = $wList->{Heuristic0}->{$id} && 'WH0' | ||
| 271 | || $wList->{Heuristic1}->{$id} && 'WH1' | ||
| 272 | || $wList->{Heuristic2}->{$id} && 'WH2'; | ||
| 273 | my $o = $oList->{Heuristic0}->{$id} && 'OH0' | ||
| 274 | || $oList->{Heuristic1}->{$id} && 'OH1' | ||
| 275 | || $oList->{Heuristic2}->{$id} && 'OH2'; | ||
| 276 | my $m = $mList->{Heuristic0}->{$id} && 'MH0' | ||
| 277 | || $mList->{Heuristic1}->{$id} && 'MH1' | ||
| 278 | || $mList->{Heuristic2}->{$id} && 'MH2'; | ||
| 279 | my $s = $sList->{Heuristic0}->{$id} && 'SH0' | ||
| 280 | || $sList->{Heuristic1}->{$id} && 'SH1' | ||
| 281 | || $sList->{Heuristic2}->{$id} && 'SH2'; | ||
| 282 | my $l = $lList->{Heuristic0}->{$id} && 'LH0' | ||
| 283 | || $lList->{Heuristic1}->{$id} && 'LH1' | ||
| 284 | || $lList->{Heuristic2}->{$id} && 'LH2'; | ||
| 285 | $inter->{"$b$f$w$o$m$s$l"}->{$id}++; | ||
| 286 | } | ||
| 287 | } | ||
| 288 | # Stop if we found matches with this smallest heuristic level | ||
| 289 | #last if keys %$inter > 0; | ||
| 290 | } | ||
| 291 | return $inter; | ||
| 292 | } | ||
| 293 | |||
| 294 | sub _getIntersectionWithMask { | ||
| 295 | my $self = shift; | ||
| 296 | my ($bList, $fList, $wList, $oList, $mList, $sList, $lList, $mask) = @_; | ||
| 297 | |||
| 298 | my %map = ( | ||
| 299 | 'BH0' => 'Heuristic0', | ||
| 300 | 'BH1' => 'Heuristic1', | ||
| 301 | 'BH2' => 'Heuristic2', | ||
| 302 | 'FH0' => 'Heuristic0', | ||
| 303 | 'FH1' => 'Heuristic1', | ||
| 304 | 'FH2' => 'Heuristic2', | ||
| 305 | 'WH0' => 'Heuristic0', | ||
| 306 | 'WH1' => 'Heuristic1', | ||
| 307 | 'WH2' => 'Heuristic2', | ||
| 308 | 'OH0' => 'Heuristic0', | ||
| 309 | 'OH1' => 'Heuristic1', | ||
| 310 | 'OH2' => 'Heuristic2', | ||
| 311 | 'MH0' => 'Heuristic0', | ||
| 312 | 'MH1' => 'Heuristic1', | ||
| 313 | 'MH2' => 'Heuristic2', | ||
| 314 | 'SH0' => 'Heuristic0', | ||
| 315 | 'SH1' => 'Heuristic1', | ||
| 316 | 'SH2' => 'Heuristic2', | ||
| 317 | 'LH0' => 'Heuristic0', | ||
| 318 | 'LH1' => 'Heuristic1', | ||
| 319 | 'LH2' => 'Heuristic2', | ||
| 320 | ); | ||
| 321 | |||
| 322 | my @mask = unpack('a3a3a3a3a3a3a3', $mask); | ||
| 323 | |||
| 324 | my $b = $map{$mask[0]}; | ||
| 325 | my $f = $map{$mask[1]}; | ||
| 326 | my $w = $map{$mask[2]}; | ||
| 327 | my $o = $map{$mask[3]}; | ||
| 328 | my $m = $map{$mask[4]}; | ||
| 329 | my $s = $map{$mask[5]}; | ||
| 330 | my $l = $map{$mask[6]}; | ||
| 331 | |||
| 332 | # We force a search using a very specific heuristic mask | ||
| 333 | my $smallest = $self->_searchSmallestHashWithMask( | ||
| 334 | $bList->{$b}, $fList->{$f}, $wList->{$w}, $oList->{$o}, $mList->{$m}, | ||
| 335 | $sList->{$s}, $lList->{$l}, | ||
| 336 | ); | ||
| 337 | |||
| 338 | my $inter; | ||
| 339 | for my $id (@$smallest) { | ||
| 340 | if ($bList->{$b}->{$id} | ||
| 341 | && $fList->{$f}->{$id} | ||
| 342 | && $wList->{$w}->{$id} | ||
| 343 | && $oList->{$o}->{$id} | ||
| 344 | && $mList->{$m}->{$id} | ||
| 345 | && $sList->{$s}->{$id} | ||
| 346 | && $lList->{$l}->{$id}) { | ||
| 347 | $inter->{$mask}->{$id}++; | ||
| 348 | } | ||
| 349 | } | ||
| 350 | |||
| 351 | return $inter; | ||
| 352 | } | ||
| 353 | |||
| 354 | sub _searchCommonMasksS1S2S3 { | ||
| 355 | my $self = shift; | ||
| 356 | my ($s1Inter, $s2Inter, $s3Inter) = @_; | ||
| 357 | |||
| 358 | my %maskList = map { $_ => 1 } | ||
| 359 | ( keys %$s1Inter, keys %$s2Inter, keys %$s3Inter ); | ||
| 360 | |||
| 361 | my @maskList = (); | ||
| 362 | for my $mask (keys %maskList) { | ||
| 363 | if (exists $s1Inter->{$mask} | ||
| 364 | && exists $s2Inter->{$mask} | ||
| 365 | && exists $s3Inter->{$mask}) { | ||
| 366 | push @maskList, $mask; | ||
| 367 | } | ||
| 368 | } | ||
| 369 | return \@maskList; | ||
| 370 | } | ||
| 371 | |||
| 372 | sub _searchCommonMasksS1S2 { | ||
| 373 | my $self = shift; | ||
| 374 | my ($s1Inter, $s2Inter) = @_; | ||
| 375 | |||
| 376 | my %maskList = map { $_ => 1 } ( keys %$s1Inter, keys %$s2Inter ); | ||
| 377 | |||
| 378 | my @maskList = (); | ||
| 379 | for my $mask (keys %maskList) { | ||
| 380 | if (exists $s1Inter->{$mask} && exists $s2Inter->{$mask}) { | ||
| 381 | push @maskList, $mask; | ||
| 382 | } | ||
| 383 | } | ||
| 384 | return \@maskList; | ||
| 385 | } | ||
| 386 | |||
| 387 | sub _searchSmallestInterWithMask { | ||
| 388 | my $self = shift; | ||
| 389 | |||
| 390 | my @ids = (); | ||
| 391 | my $last = 10_000; # Invalid huge number | ||
| 392 | for my $inter (@_) { | ||
| 393 | my $count = keys %$inter; | ||
| 394 | next if $count == 0; # We need to skip, otherwise there is a bug ;) | ||
| 395 | if ($count < $last) { | ||
| 396 | @ids = keys %$inter; | ||
| 397 | $last = $count; | ||
| 398 | } | ||
| 399 | } | ||
| 400 | return \@ids; | ||
| 401 | } | ||
| 402 | |||
| 403 | sub _getIntersectionS1S2S3 { | ||
| 404 | my $self = shift; | ||
| 405 | my ($s1Inter, $s2Inter, $s3Inter) = @_; | ||
| 406 | |||
| 407 | # Search common masks | ||
| 408 | my $maskList = $self->_searchCommonMasksS1S2S3( | ||
| 409 | $s1Inter, $s2Inter, $s3Inter, | ||
| 410 | ); | ||
| 411 | #print Dumper($s1Inter),"\n"; | ||
| 412 | #print Dumper($s2Inter),"\n"; | ||
| 413 | #print Dumper($s3Inter),"\n"; | ||
| 414 | return unless @$maskList > 0; | ||
| 415 | #print "DEBUG: found commonMasksS1S2S3 [@$maskList]\n"; | ||
| 416 | |||
| 417 | my $inter = {}; | ||
| 418 | for my $mask (@$maskList) { | ||
| 419 | my $smallest = $self->_searchSmallestInterWithMask( | ||
| 420 | $s1Inter->{$mask}, $s2Inter->{$mask}, $s3Inter->{$mask}, | ||
| 421 | ); | ||
| 422 | |||
| 423 | for my $id (@$smallest) { | ||
| 424 | if ($s1Inter->{$mask}->{$id} | ||
| 425 | && $s2Inter->{$mask}->{$id} | ||
| 426 | && $s3Inter->{$mask}->{$id}) { | ||
| 427 | #print "DEBUG: interS1S2S3 [$id] [$mask]\n"; | ||
| 428 | $inter->{$mask}->{$id}++; | ||
| 429 | } | ||
| 430 | } | ||
| 431 | } | ||
| 432 | return $inter; | ||
| 433 | } | ||
| 434 | |||
| 435 | sub _getIntersectionS1S2 { | ||
| 436 | my $self = shift; | ||
| 437 | my ($s1Inter, $s2Inter) = @_; | ||
| 438 | |||
| 439 | # Search common masks | ||
| 440 | my $maskList = $self->_searchCommonMasksS1S2($s1Inter, $s2Inter); | ||
| 441 | return unless @$maskList > 0; | ||
| 442 | #print "DEBUG: found commonMasksS1S2 [@$maskList]\n"; | ||
| 443 | |||
| 444 | my $inter = {}; | ||
| 445 | for my $mask (@$maskList) { | ||
| 446 | my $smallest = $self->_searchSmallestInterWithMask( | ||
| 447 | $s1Inter->{$mask}, $s2Inter->{$mask}, | ||
| 448 | ); | ||
| 449 | |||
| 450 | for my $id (@$smallest) { | ||
| 451 | if ($s1Inter->{$mask}->{$id} && $s2Inter->{$mask}->{$id}) { | ||
| 452 | #print "DEBUG: interS1S2 [$id] [$mask]\n"; | ||
| 453 | $inter->{$mask}->{$id}++; | ||
| 454 | } | ||
| 455 | } | ||
| 456 | } | ||
| 457 | return $inter; | ||
| 458 | } | ||
| 459 | |||
| 460 | sub _countInter { | ||
| 461 | my $self = shift; | ||
| 462 | my ($ids) = @_; | ||
| 463 | for ('S1', 'S2', 'S3') { | ||
| 464 | $ids->{$_}{nInter} = keys %{$ids->{$_}{Inter}}; | ||
| 465 | #print "[*] _countInter[$_]: ".$ids->{$_}{nInter}."\n"; | ||
| 466 | } | ||
| 467 | return $ids; | ||
| 468 | } | ||
| 469 | |||
| 470 | sub search { | ||
| 471 | my $self = shift; | ||
| 472 | |||
| 473 | my $global = $self->global; | ||
| 474 | my $log = $global->log; | ||
| 475 | |||
| 476 | # Only keep received signatures | ||
| 477 | my %sig = (); | ||
| 478 | if ($self->s1 && $self->s1->B !~ 'B00000') { | ||
| 479 | $sig{S1} = $self->s1; | ||
| 480 | } | ||
| 481 | if ($self->s2 && $self->s2->B !~ 'B00000') { | ||
| 482 | $sig{S2} = $self->s2; | ||
| 483 | } | ||
| 484 | if ($self->s3 && $self->s3->B !~ 'B00000') { | ||
| 485 | $sig{S3} = $self->s3; | ||
| 486 | } | ||
| 487 | |||
| 488 | # Search intersection for each probe response | ||
| 489 | my $ids = {}; | ||
| 490 | for my $s (keys %sig) { | ||
| 491 | my $res = $self->_getPossibleSignatureIds($s, \%sig); | ||
| 492 | #$log->debug("_getPossibleSignatureIds[$s]: ".Dumper($res)); | ||
| 493 | my $inter = $self->_getIntersection( | ||
| 494 | $res->{B}, $res->{F}, $res->{W}, $res->{O}, $res->{M}, | ||
| 495 | $res->{S}, $res->{L}, | ||
| 496 | ); | ||
| 497 | $ids->{$s}{Ids} = $res; | ||
| 498 | $ids->{$s}{Inter} = $inter; | ||
| 499 | #$log->debug("inter[$s]: ".Dumper($inter)); | ||
| 500 | } | ||
| 501 | |||
| 502 | # Make masks unique | ||
| 503 | my %maskList = map { $_ => 1 } ( | ||
| 504 | keys %{$ids->{S1}{Inter}}, | ||
| 505 | keys %{$ids->{S2}{Inter}}, | ||
| 506 | keys %{$ids->{S3}{Inter}}, | ||
| 507 | ); | ||
| 508 | |||
| 509 | # Update number of resulting intersection for S1, S2 and S3 | ||
| 510 | $self->_countInter($ids); | ||
| 511 | |||
| 512 | #print "[*] maskList: ".join(' ', keys %maskList)."\n"; | ||
| 513 | |||
| 514 | # For all masks, expand possible Signature IDs to make | ||
| 515 | # all of them comparable | ||
| 516 | for my $mask (keys %maskList) { | ||
| 517 | for my $p ('S1', 'S2', 'S3') { | ||
| 518 | #if ($ids->{S1}{nInter} > 0 && ! exists $ids->{S1}{Ier}->{$mk}) { | ||
| 519 | #print "[*] ".Dumper($ids->{$p}{Ids})."\n"; | ||
| 520 | if (!exists $ids->{$p}{Inter}->{$mask}) { | ||
| 521 | #print "[*] Running with [$mask] against [$p]\n"; | ||
| 522 | my $interNew = $self->_getIntersectionWithMask( | ||
| 523 | $ids->{$p}{Ids}->{B}, $ids->{$p}{Ids}->{F}, | ||
| 524 | $ids->{$p}{Ids}->{W}, $ids->{$p}{Ids}->{O}, | ||
| 525 | $ids->{$p}{Ids}->{M}, $ids->{$p}{Ids}->{S}, | ||
| 526 | $ids->{$p}{Ids}->{L}, $mask, | ||
| 527 | ); | ||
| 528 | if ($interNew) { | ||
| 529 | #print "[*] interNew[$p]: ".Dumper($interNew)."\n"; | ||
| 530 | $ids->{$p}{Inter}->{$mask} = $interNew->{$mask}; | ||
| 531 | } | ||
| 532 | } | ||
| 533 | } | ||
| 534 | } | ||
| 535 | #$log->debug("s1InterNew: ".Dumper($ids->{S1}{Inter})); | ||
| 536 | #$log->debug("s2InterNew: ".Dumper($ids->{S2}{Inter})); | ||
| 537 | #$log->debug("s3InterNew: ".Dumper($ids->{S3}{Inter})); | ||
| 538 | |||
| 539 | # Update number of resulting intersection for S1, S2 and S3 | ||
| 540 | # after we have expanded mask list | ||
| 541 | $self->_countInter($ids); | ||
| 542 | |||
| 543 | my @resultList = (); | ||
| 544 | |||
| 545 | # Some matchs were found for all probes | ||
| 546 | if ($ids->{S1}{nInter} > 0 | ||
| 547 | && $ids->{S2}{nInter} > 0 | ||
| 548 | && $ids->{S3}{nInter} > 0) { | ||
| 549 | my $s1s2s3 = $self->_getIntersectionS1S2S3( | ||
| 550 | $ids->{S1}{Inter}, $ids->{S2}{Inter}, $ids->{S3}{Inter}, | ||
| 551 | ); | ||
| 552 | if (keys %$s1s2s3 > 0) { | ||
| 553 | #print "DEBUG: NS_MATCH_TYPE_S1S2S3\n"; | ||
| 554 | my $results = $self->_buildResultList( | ||
| 555 | $s1s2s3, NS_MATCH_TYPE_S1S2S3, | ||
| 556 | ); | ||
| 557 | push @resultList, @$results; | ||
| 558 | } | ||
| 559 | } | ||
| 560 | |||
| 561 | # Matchs for only S1 and S2 | ||
| 562 | if ($ids->{S1}{nInter} > 0 && $ids->{S2}{nInter} > 0) { | ||
| 563 | my $s1s2 = $self->_getIntersectionS1S2( | ||
| 564 | $ids->{S1}{Inter}, $ids->{S2}{Inter}, | ||
| 565 | ); | ||
| 566 | if (keys %$s1s2 > 0) { | ||
| 567 | #print "DEBUG: NS_MATCH_TYPE_S1S2\n"; | ||
| 568 | my $results = $self->_buildResultList($s1s2, NS_MATCH_TYPE_S1S2); | ||
| 569 | push @resultList, @$results; | ||
| 570 | } | ||
| 571 | } | ||
| 572 | |||
| 573 | # Match with S2 only | ||
| 574 | if ($ids->{S2}{nInter} > 0) { | ||
| 575 | #print "DEBUG: NS_MATCH_TYPE_S2\n"; | ||
| 576 | my $results = $self->_buildResultList( | ||
| 577 | $ids->{S2}{Inter}, NS_MATCH_TYPE_S2, | ||
| 578 | ); | ||
| 579 | push @resultList, @$results; | ||
| 580 | } | ||
| 581 | |||
| 582 | my $clean4 = $self->_cleanResults(\@resultList, 'IPv4'); | ||
| 583 | my $clean6 = $self->_cleanResults(\@resultList, 'IPv6'); | ||
| 584 | |||
| 585 | # We keep IPv4 signatures in IPv6 mode only if no IPv6 matchs | ||
| 586 | # Else in IPv4 mode, we only keep IPv4 matchs | ||
| 587 | my @clean = (); | ||
| 588 | if ($self->global->ipv6 && @$clean6 > 0) { | ||
| 589 | push @clean, @$clean6; | ||
| 590 | } | ||
| 591 | elsif (@$clean4 > 0) { | ||
| 592 | push @clean, @$clean4; | ||
| 593 | } | ||
| 594 | |||
| 595 | return \@clean; | ||
| 596 | } | ||
| 597 | |||
| 598 | sub _cleanResults { | ||
| 599 | my $self = shift; | ||
| 600 | my ($results, $ip) = @_; | ||
| 601 | |||
| 602 | my $global = $self->global; | ||
| 603 | my $log = $global->log; | ||
| 604 | |||
| 605 | # Sort to easily filter out | ||
| 606 | my $sorted = {}; | ||
| 607 | for my $r (@$results) { | ||
| 608 | if ($r->ipVersion ne $ip) { | ||
| 609 | next; | ||
| 610 | } | ||
| 611 | if ($global->threshold != 0 && $r->matchScore < $global->threshold) { | ||
| 612 | next; | ||
| 613 | } | ||
| 614 | push @{$sorted->{$r->matchType}{$r->matchScore}}, $r; | ||
| 615 | } | ||
| 616 | |||
| 617 | my $s1s2s3 = $sorted->{NS_MATCH_TYPE_S1S2S3()}; | ||
| 618 | my $s1s2 = $sorted->{NS_MATCH_TYPE_S1S2()}; | ||
| 619 | my $s2 = $sorted->{NS_MATCH_TYPE_S2()}; | ||
| 620 | |||
| 621 | # If some scores are lower than or equal for lower matchTypes, we remove | ||
| 622 | # First case we have some S1S2S3 matchs | ||
| 623 | my @sorted2 = (); | ||
| 624 | if (keys %$s1s2s3 > 0) { | ||
| 625 | for my $src (keys %$s1s2s3) { | ||
| 626 | for my $dst (keys %$s1s2, keys %$s2) { | ||
| 627 | #print "DEBUG: [$src] [$dst]\n"; | ||
| 628 | if ($dst <= $src) { | ||
| 629 | $sorted->{NS_MATCH_TYPE_S1S2()}{$dst} = []; | ||
| 630 | $sorted->{NS_MATCH_TYPE_S2()}{$dst} = []; | ||
| 631 | } | ||
| 632 | } | ||
| 633 | } | ||
| 634 | |||
| 635 | # Sort results by IP version and score, keep only highest score for an ID | ||
| 636 | my %idList = (); | ||
| 637 | my $bestScore = 0; | ||
| 638 | for my $p (sort { $b <=> $a } keys %{$sorted->{NS_MATCH_TYPE_S1S2S3()}}) { | ||
| 639 | for my $r (@{$sorted->{NS_MATCH_TYPE_S1S2S3()}{$p}}, | ||
| 640 | @{$sorted->{NS_MATCH_TYPE_S1S2()}{$p}}, | ||
| 641 | @{$sorted->{NS_MATCH_TYPE_S2()}{$p}}) { | ||
| 642 | if (! exists($idList{$r->idSignature})) { | ||
| 643 | if ($global->bestScore) { | ||
| 644 | if ($r->matchScore >= $bestScore) { | ||
| 645 | push @sorted2, $r; | ||
| 646 | $idList{$r->idSignature}++; | ||
| 647 | if (! $bestScore) { | ||
| 648 | $bestScore = $r->matchScore; | ||
| 649 | } | ||
| 650 | } | ||
| 651 | } | ||
| 652 | else { | ||
| 653 | push @sorted2, $r; | ||
| 654 | $idList{$r->idSignature}++; | ||
| 655 | } | ||
| 656 | } | ||
| 657 | } | ||
| 658 | } | ||
| 659 | } | ||
| 660 | elsif (keys %$s1s2 > 0) { | ||
| 661 | # Second case we have some S1S2 matchs | ||
| 662 | for my $src (keys %$s1s2) { | ||
| 663 | for my $dst (keys %$s2) { | ||
| 664 | if ($dst <= $src) { | ||
| 665 | $sorted->{NS_MATCH_TYPE_S2()}{$dst} = []; | ||
| 666 | } | ||
| 667 | } | ||
| 668 | } | ||
| 669 | |||
| 670 | # Sort results by IP version and score, keep only highest score for an ID | ||
| 671 | my %idList = (); | ||
| 672 | my $bestScore = 0; | ||
| 673 | for my $p (sort { $b <=> $a } keys %{$sorted->{NS_MATCH_TYPE_S1S2()}}) { | ||
| 674 | for my $r (@{$sorted->{NS_MATCH_TYPE_S1S2()}{$p}}, | ||
| 675 | @{$sorted->{NS_MATCH_TYPE_S2()}{$p}}) { | ||
| 676 | if (! exists($idList{$r->idSignature})) { | ||
| 677 | if ($global->bestScore) { | ||
| 678 | if ($r->matchScore >= $bestScore) { | ||
| 679 | push @sorted2, $r; | ||
| 680 | $idList{$r->idSignature}++; | ||
| 681 | if (! $bestScore) { | ||
| 682 | $bestScore = $r->matchScore; | ||
| 683 | } | ||
| 684 | } | ||
| 685 | } | ||
| 686 | else { | ||
| 687 | push @sorted2, $r; | ||
| 688 | $idList{$r->idSignature}++; | ||
| 689 | } | ||
| 690 | } | ||
| 691 | } | ||
| 692 | } | ||
| 693 | } | ||
| 694 | elsif (keys %$s2 > 0) { | ||
| 695 | # Third case we have some S2 matchs | ||
| 696 | # Sort results by IP version and score, keep only highest score for an ID | ||
| 697 | my %idList = (); | ||
| 698 | my $bestScore = 0; | ||
| 699 | for my $p (sort { $b <=> $a } keys %{$sorted->{NS_MATCH_TYPE_S2()}}) { | ||
| 700 | for my $r (@{$sorted->{NS_MATCH_TYPE_S2()}{$p}}) { | ||
| 701 | if (! exists($idList{$r->idSignature})) { | ||
| 702 | if ($global->bestScore) { | ||
| 703 | if ($r->matchScore >= $bestScore) { | ||
| 704 | push @sorted2, $r; | ||
| 705 | $idList{$r->idSignature}++; | ||
| 706 | if (! $bestScore) { | ||
| 707 | $bestScore = $r->matchScore; | ||
| 708 | } | ||
| 709 | } | ||
| 710 | } | ||
| 711 | else { | ||
| 712 | push @sorted2, $r; | ||
| 713 | $idList{$r->idSignature}++; | ||
| 714 | } | ||
| 715 | } | ||
| 716 | } | ||
| 717 | } | ||
| 718 | } | ||
| 719 | else { | ||
| 720 | # Or no matchs at all | ||
| 721 | return []; | ||
| 722 | } | ||
| 723 | |||
| 724 | return \@sorted2; | ||
| 725 | } | ||
| 726 | |||
| 727 | sub _buildResultList { | ||
| 728 | my $self = shift; | ||
| 729 | my ($result, $matchType) = @_; | ||
| 730 | |||
| 731 | my $global = $self->global; | ||
| 732 | my $log = $global->log; | ||
| 733 | my $db = $global->db; | ||
| 734 | my $next = $global->next; | ||
| 735 | |||
| 736 | my @resultList = (); | ||
| 737 | for my $mask (keys %$result) { | ||
| 738 | #$log->debug("MASK[$mask]"); | ||
| 739 | for my $id (keys %{$result->{$mask}}) { | ||
| 740 | my %args = (); | ||
| 741 | my $sig = $db->retrieveSignature($id); | ||
| 742 | my $result = Net::SinFP3::Result::Active->new( | ||
| 743 | global => $self->global, | ||
| 744 | trusted => $sig->{trusted}, | ||
| 745 | idSignature => $sig->{idSignature}, | ||
| 746 | ipVersion => $sig->{ipVersion}, | ||
| 747 | systemClass => $sig->{systemClass}, | ||
| 748 | vendor => $sig->{vendor}, | ||
| 749 | os => $sig->{os}, | ||
| 750 | osVersion => $sig->{osVersion}, | ||
| 751 | osVersionFamily => $sig->{osVersionFamily}, | ||
| 752 | matchType => $matchType, | ||
| 753 | matchMask => $mask, | ||
| 754 | osVersionChildrenList => $db->getOsVersionChildrenList( | ||
| 755 | $id, | ||
| 756 | ), | ||
| 757 | ) or $log->fatal("new() result failed"); | ||
| 758 | $result->s1($self->s1) if $self->s1; | ||
| 759 | $result->s2($self->s2) if $self->s2; | ||
| 760 | $result->s3($self->s3) if $self->s3; | ||
| 761 | $result->updateMatchScore; | ||
| 762 | push @resultList, $result; | ||
| 763 | } | ||
| 764 | } | ||
| 765 | |||
| 766 | return \@resultList; | ||
| 767 | } | ||
| 768 | |||
| 769 | sub _checkForTest { | ||
| 770 | my $self = shift; | ||
| 771 | my ($mode, $do, $pkt) = @_; | ||
| 772 | |||
| 773 | # If this mode does not support testing | ||
| 774 | if (! $mode->can($do) && ! $mode->can($pkt)) { | ||
| 775 | return; | ||
| 776 | } | ||
| 777 | |||
| 778 | # We did not want this test | ||
| 779 | if (($mode->can($do) && ! $mode->$do) || ! $mode->$pkt) { | ||
| 780 | return; | ||
| 781 | } | ||
| 782 | |||
| 783 | # We wanted it, but we had a problem in the reply | ||
| 784 | my $flags = $mode->$pkt->reply | ||
| 785 | ? $mode->$pkt->reply->ref->{TCP}->flags | ||
| 786 | : undef; | ||
| 787 | |||
| 788 | if (! $flags) { | ||
| 789 | return "no response (filtered port)"; | ||
| 790 | } | ||
| 791 | elsif ($flags & NF_TCP_FLAGS_RST) { | ||
| 792 | return "RESET by peer (closed port)"; | ||
| 793 | } | ||
| 794 | |||
| 795 | return; | ||
| 796 | } | ||
| 797 | |||
| 798 | sub _checkPort { | ||
| 799 | my $self = shift; | ||
| 800 | my ($mode, $reasons) = @_; | ||
| 801 | |||
| 802 | my $p1 = $self->_checkForTest($mode, 'doP1', 'p1'); | ||
| 803 | my $p2 = $self->_checkForTest($mode, 'doP2', 'p2'); | ||
| 804 | |||
| 805 | if ($p1 && $p2) { | ||
| 806 | $reasons->{p1} = $p1; | ||
| 807 | $reasons->{p2} = $p2; | ||
| 808 | return; | ||
| 809 | } | ||
| 810 | |||
| 811 | # The port can be fingerprinted | ||
| 812 | return 1; | ||
| 813 | } | ||
| 814 | |||
| 815 | sub run { | ||
| 816 | my $self = shift->SUPER::run(@_) or return; | ||
| 817 | |||
| 818 | my $global = $self->global; | ||
| 819 | my $log = $global->log; | ||
| 820 | my $mode = $global->mode; | ||
| 821 | |||
| 822 | if (! $mode->s1 && ! $mode->s2 && ! $mode->s3) { | ||
| 823 | $log->error("Nothing to search"); | ||
| 824 | return; | ||
| 825 | } | ||
| 826 | |||
| 827 | $self->s1($mode->s1); | ||
| 828 | $self->s2($mode->s2); | ||
| 829 | $self->s3($mode->s3); | ||
| 830 | |||
| 831 | # If the port appears to be closed or filtered | ||
| 832 | # We cannot fingerprint and return another Result object | ||
| 833 | my $reasons = {}; | ||
| 834 | my $result = []; | ||
| 835 | # XXX: _checkPort() should be in Mode::Active? | ||
| 836 | if (!$self->_checkPort($mode, $reasons)) { | ||
| 837 | my $r = Net::SinFP3::Result::PortError->new( | ||
| 838 | global => $self->global, | ||
| 839 | p1Reason => $reasons->{p1}, | ||
| 840 | p2Reason => $reasons->{p2}, | ||
| 841 | ); | ||
| 842 | $result = [ $r ]; | ||
| 843 | } | ||
| 844 | else { | ||
| 845 | $result = $self->search; | ||
| 846 | if (@$result == 0) { | ||
| 847 | my $r = Net::SinFP3::Result::Unknown->new( | ||
| 848 | global => $self->global, | ||
| 849 | s1 => $mode->s1 || '0', | ||
| 850 | s2 => $mode->s2 || '0', | ||
| 851 | s3 => $mode->s3 || '0', | ||
| 852 | ); | ||
| 853 | $result = [ $r ]; | ||
| 854 | } | ||
| 855 | } | ||
| 856 | |||
| 857 | # Fill IP/port attributes if available | ||
| 858 | if ($mode->p2 && $mode->p2->reply) { | ||
| 859 | my $reply = $mode->p2->reply; | ||
| 860 | my $ip = $reply->ref->{IPv4} || $reply->ref->{IPv6}; | ||
| 861 | my $tcp = $reply->ref->{TCP}; | ||
| 862 | for my $r (@$result) { | ||
| 863 | $r->ip($ip->src); | ||
| 864 | $r->port($tcp->src); | ||
| 865 | if ($global->dnsReverse) { | ||
| 866 | $r->reverse($global->getAddrReverse(addr => $r->ip) || 'unknown'); | ||
| 867 | } | ||
| 868 | } | ||
| 869 | } | ||
| 870 | elsif ($mode->p2) { | ||
| 871 | my $p2 = $mode->p2; | ||
| 872 | my $ip = $p2->ref->{IPv4} || $p2->ref->{IPv6}; | ||
| 873 | my $tcp = $p2->ref->{TCP}; | ||
| 874 | for my $r (@$result) { | ||
| 875 | $r->ip($ip->dst); | ||
| 876 | $r->port($tcp->dst); | ||
| 877 | if ($global->dnsReverse) { | ||
| 878 | $r->reverse($global->getAddrReverse(addr => $r->ip) || 'unknown'); | ||
| 879 | } | ||
| 880 | } | ||
| 881 | } | ||
| 882 | |||
| 883 | return $result; | ||
| 884 | } | ||
| 885 | |||
| 886 | 1; | ||
| 887 | |||
| 888 | __END__ |