| 1 | # | ||||||||||||||
| 2 | # $Id: SinFP3.pm,v 008243d3e89a 2018/07/21 14:54:07 gomor $ | ||||||||||||||
| 3 | # | ||||||||||||||
| 4 | package Net::SinFP3::DB::SinFP3; | ||||||||||||||
| 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::SinFP3::DB); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 1 | our @AS = qw( | |||||||||||||
| 10 | file | ||||||||||||||
| 11 | _dbh | ||||||||||||||
| 12 | _prepared | ||||||||||||||
| 13 | ); | ||||||||||||||
| 14 | 1 | our @AA = qw( | |||||||||||||
| 15 | _PatternBinary | ||||||||||||||
| 16 | _PatternTcpFlags | ||||||||||||||
| 17 | _PatternTcpWindow | ||||||||||||||
| 18 | _PatternTcpOptions | ||||||||||||||
| 19 | _PatternTcpMss | ||||||||||||||
| 20 | _PatternTcpWScale | ||||||||||||||
| 21 | _PatternTcpOLength | ||||||||||||||
| 22 | ); | ||||||||||||||
| 23 | 1 | __PACKAGE__->cgBuildIndices; | |||||||||||||
| 24 | 1 | __PACKAGE__->cgBuildAccessorsScalar(\@AS); | |||||||||||||
| 25 | 1 | __PACKAGE__->cgBuildAccessorsArray (\@AA); | |||||||||||||
| 26 | |||||||||||||||
| 27 | 1 | ▶ | use DBI; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 28 | 1 | ▶ | use Data::Dumper; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 29 | |||||||||||||||
| 30 | 1 | ▶ | use FindBin qw($Bin); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 31 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 32 | 1 | ▶ | use Digest::MD5; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 33 | |||||||||||||||
| 34 | 0 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||
| 35 | 0 | my $self = shift->SUPER::new( | |||||||||||||
| 36 | _dbh => 0, | ||||||||||||||
| 37 | _PatternBinary => [], | ||||||||||||||
| 38 | _PatternTcpFlags => [], | ||||||||||||||
| 39 | _PatternTcpWindow => [], | ||||||||||||||
| 40 | _PatternTcpOptions => [], | ||||||||||||||
| 41 | _PatternTcpMss => [], | ||||||||||||||
| 42 | _PatternTcpWScale => [], | ||||||||||||||
| 43 | _PatternTcpOLength => [], | ||||||||||||||
| 44 | @_, | ||||||||||||||
| 45 | ); | ||||||||||||||
| 46 | |||||||||||||||
| 47 | 0 | my $log = $self->global->log; | |||||||||||||
| 48 | |||||||||||||||
| 49 | 0 | my $file = $self->file; | |||||||||||||
| 50 | 0 | ▶ | if (!defined($file)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 51 | 0 | for ("$Bin/", "$Bin/../db/") { | |||||||||||||
| 52 | 0 | ▶ | if (-f $_.'sinfp3.db') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 53 | 0 | $file = $_.'sinfp3.db'; | |||||||||||||
| 54 | 0 | last; | |||||||||||||
| 55 | } | ||||||||||||||
| 56 | } | ||||||||||||||
| 57 | } | ||||||||||||||
| 58 | |||||||||||||||
| 59 | 0 | ▶ | if (!defined($file)) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 60 | 0 | $log->fatal("No database file found"); | |||||||||||||
| 61 | } | ||||||||||||||
| 62 | elsif (!-f $file) { | ||||||||||||||
| 63 | 0 | $log->fatal("Database file not found [$file]: $!"); | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | 0 | $self->file($file); | |||||||||||||
| 67 | |||||||||||||||
| 68 | 0 | return $self; | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | |||||||||||||||
| 71 | 0 | ▶ | sub getOsVersionChildrenList { | ||||||||||||
|
Subroutine
getOsVersionChildrenList: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 72 | 0 | my $self = shift; | |||||||||||||
| 73 | 0 | my ($id) = @_; | |||||||||||||
| 74 | |||||||||||||||
| 75 | 0 | my $dbh = $self->_dbh; | |||||||||||||
| 76 | 0 | my $idOsVersion = $self->_prepared->{idOsVersion}; | |||||||||||||
| 77 | 0 | my $rv = $idOsVersion->execute($id); | |||||||||||||
| 78 | 0 | my $h = $idOsVersion->fetchall_hashref('idOsVersion'); | |||||||||||||
| 79 | |||||||||||||||
| 80 | 0 | my @osVersionList = (); | |||||||||||||
| 81 | 0 | my $sOsVersion = $self->_prepared->{osVersion}; | |||||||||||||
| 82 | 0 | for my $k (keys %$h) { | |||||||||||||
| 83 | 0 | my $rv = $sOsVersion->execute($k); | |||||||||||||
| 84 | 0 | my $h = $sOsVersion->fetchrow_hashref; | |||||||||||||
| 85 | 0 | push @osVersionList, $h->{osVersion}; | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | |||||||||||||||
| 88 | 0 | return \@osVersionList; | |||||||||||||
| 89 | } | ||||||||||||||
| 90 | |||||||||||||||
| 91 | 0 | ▶ | sub getOsVersionChildrenPList { | ||||||||||||
|
Subroutine
getOsVersionChildrenPList: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 92 | 0 | my $self = shift; | |||||||||||||
| 93 | 0 | my ($id) = @_; | |||||||||||||
| 94 | |||||||||||||||
| 95 | 0 | my $dbh = $self->_dbh; | |||||||||||||
| 96 | 0 | my $s = $dbh->prepare(qq{SELECT idOsVersion FROM OsVersionChildren WHERE idSignatureP=?}); | |||||||||||||
| 97 | 0 | my $rv = $s->execute($id); | |||||||||||||
| 98 | 0 | my $h = $s->fetchall_hashref('idOsVersion'); | |||||||||||||
| 99 | |||||||||||||||
| 100 | 0 | my @osVersionList = (); | |||||||||||||
| 101 | 0 | my $sOsVersion = $dbh->prepare(qq{SELECT osVersion FROM OsVersion WHERE idOsVersion=?}); | |||||||||||||
| 102 | 0 | for my $k (keys %$h) { | |||||||||||||
| 103 | 0 | my $rv = $sOsVersion->execute($k); | |||||||||||||
| 104 | 0 | my $h = $sOsVersion->fetchrow_hashref; | |||||||||||||
| 105 | 0 | push @osVersionList, $h->{osVersion}; | |||||||||||||
| 106 | } | ||||||||||||||
| 107 | |||||||||||||||
| 108 | 0 | return \@osVersionList; | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | |||||||||||||||
| 111 | 0 | ▶ | sub init { | ||||||||||||
|
Subroutine
init: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||
| 112 | 0 | ▶ | my $self = shift->SUPER::init(@_) or return; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 113 | |||||||||||||||
| 114 | 0 | my $global = $self->global; | |||||||||||||
| 115 | 0 | my $log = $global->log; | |||||||||||||
| 116 | |||||||||||||||
| 117 | 0 | $log->verbose("Using database file: ".$self->file); | |||||||||||||
| 118 | |||||||||||||||
| 119 | my $dbh = DBI->connect( | ||||||||||||||
| 120 | "dbi:SQLite:dbname=".$self->file, '', '', { | ||||||||||||||
| 121 | sqlite_use_immediate_transaction => 0, # Avoid lock | ||||||||||||||
| 122 | RaiseError => 0, | ||||||||||||||
| 123 | PrintError => 0, | ||||||||||||||
| 124 | AutoCommit => 0, | ||||||||||||||
| 125 | 0 | ▶ | HandleError => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 126 | 0 | my ($errstr, $dbh, $arg) = @_; | |||||||||||||
| 127 | # Let's keep fatal() for all errors as a debugging mechanism for now | ||||||||||||||
| 128 | 0 | $log->fatal("Database error: [$errstr]"); | |||||||||||||
| 129 | 0 | return 1; | |||||||||||||
| 130 | }, | ||||||||||||||
| 131 | 0 | ▶ | }) or $log->fatal("Database error: [".$DBI::errstr."]"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 132 | 0 | $self->_dbh($dbh); | |||||||||||||
| 133 | |||||||||||||||
| 134 | 0 | my $sSignature = $dbh->prepare(qq{SELECT count(*) from Signature}); | |||||||||||||
| 135 | 0 | my $sSignatureP = $dbh->prepare(qq{SELECT count(*) from SignatureP}); | |||||||||||||
| 136 | |||||||||||||||
| 137 | # We fail if Signature or SignatureP is empty | ||||||||||||||
| 138 | # The problem may be solved by using the latest DBD::SQLite module | ||||||||||||||
| 139 | 0 | my $rv = $sSignature->execute; | |||||||||||||
| 140 | 0 | my $h = $sSignature->fetchrow_hashref; | |||||||||||||
| 141 | 0 | my ($k, $v) = each(%$h); | |||||||||||||
| 142 | 0 | ▶ | return unless $v > 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 143 | |||||||||||||||
| 144 | 0 | $rv = $sSignatureP->execute; | |||||||||||||
| 145 | 0 | $h = $sSignatureP->fetchrow_hashref; | |||||||||||||
| 146 | 0 | ($k, $v) = each(%$h); | |||||||||||||
| 147 | 0 | ▶ | return unless $v > 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 148 | |||||||||||||||
| 149 | # Cache tables for future matching | ||||||||||||||
| 150 | 0 | for my $tPattern (qw( | |||||||||||||
| 151 | PatternBinary | ||||||||||||||
| 152 | PatternTcpFlags | ||||||||||||||
| 153 | PatternTcpWindow | ||||||||||||||
| 154 | PatternTcpOptions | ||||||||||||||
| 155 | PatternTcpMss | ||||||||||||||
| 156 | PatternTcpWScale | ||||||||||||||
| 157 | PatternTcpOLength | ||||||||||||||
| 158 | )) { | ||||||||||||||
| 159 | 0 | my $_table = "_$tPattern"; | |||||||||||||
| 160 | 0 | my $id = 'id'.$tPattern; | |||||||||||||
| 161 | 0 | my $s = $dbh->prepare(qq{SELECT * FROM $tPattern}); | |||||||||||||
| 162 | 0 | my $rv = $s->execute; | |||||||||||||
| 163 | 0 | my $h = $s->fetchall_hashref($id); | |||||||||||||
| 164 | 0 | my @ary = (); | |||||||||||||
| 165 | 0 | for my $k (keys %$h) { | |||||||||||||
| 166 | 0 | push @ary, $h->{$k}; | |||||||||||||
| 167 | } | ||||||||||||||
| 168 | #print Dumper(\@ary),"\n"; | ||||||||||||||
| 169 | 0 | $self->$_table(\@ary); | |||||||||||||
| 170 | } | ||||||||||||||
| 171 | |||||||||||||||
| 172 | # Create prepared statements | ||||||||||||||
| 173 | 0 | $self->_prepare; | |||||||||||||
| 174 | |||||||||||||||
| 175 | 0 | return 1; | |||||||||||||
| 176 | } | ||||||||||||||
| 177 | |||||||||||||||
| 178 | 0 | ▶ | sub _prepare { | ||||||||||||
|
Subroutine
_prepare: not called CC=2 SCAR=17.9
| |||||||||||||||
| 179 | 0 | my $self = shift; | |||||||||||||
| 180 | |||||||||||||||
| 181 | 0 | my $dbh = $self->_dbh; | |||||||||||||
| 182 | |||||||||||||||
| 183 | 0 | my %select = ( | |||||||||||||
| 184 | idOsVersion => qq{SELECT idOsVersion FROM OsVersionChildren WHERE idSignature=?}, | ||||||||||||||
| 185 | |||||||||||||||
| 186 | osVersion => qq{SELECT osVersion FROM OsVersion WHERE idOsVersion=?}, | ||||||||||||||
| 187 | ipVersion => qq{SELECT ipVersion FROM IpVersion WHERE idIpVersion=?}, | ||||||||||||||
| 188 | os => qq{SELECT os FROM Os WHERE idOs=?}, | ||||||||||||||
| 189 | osVersionFamily => qq{SELECT osVersionFamily FROM OsVersionFamily WHERE idOsVersionFamily=?}, | ||||||||||||||
| 190 | systemClass => qq{SELECT systemClass FROM SystemClass WHERE idSystemClass=?}, | ||||||||||||||
| 191 | vendor => qq{SELECT vendor FROM Vendor WHERE idVendor=?}, | ||||||||||||||
| 192 | |||||||||||||||
| 193 | patternBinary => qq{SELECT * FROM PatternBinary WHERE idPatternBinary=?}, | ||||||||||||||
| 194 | patternTcpFlags => qq{SELECT * FROM PatternTcpFlags WHERE idPatternTcpFlags=?}, | ||||||||||||||
| 195 | patternTcpWindow => qq{SELECT * FROM PatternTcpWindow WHERE idPatternTcpWindow=?}, | ||||||||||||||
| 196 | patternTcpOptions => qq{SELECT * FROM PatternTcpOptions WHERE idPatternTcpOptions=?}, | ||||||||||||||
| 197 | patternTcpMss => qq{SELECT * FROM PatternTcpMss WHERE idPatternTcpMss=?}, | ||||||||||||||
| 198 | patternTcpWScale => qq{SELECT * FROM PatternTcpWScale WHERE idPatternTcpWScale=?}, | ||||||||||||||
| 199 | patternTcpOLength => qq{SELECT * FROM PatternTcpOLength WHERE idPatternTcpOLength=?}, | ||||||||||||||
| 200 | signature => qq{SELECT * FROM Signature WHERE idSignature=?}, | ||||||||||||||
| 201 | |||||||||||||||
| 202 | idS1PatternBinary => qq{SELECT idSignature FROM Signature WHERE idS1PatternBinary=?}, | ||||||||||||||
| 203 | idS1PatternTcpFlags => qq{SELECT idSignature FROM Signature WHERE idS1PatternTcpFlags=?}, | ||||||||||||||
| 204 | idS1PatternTcpWindow => qq{SELECT idSignature FROM Signature WHERE idS1PatternTcpWindow=?}, | ||||||||||||||
| 205 | idS1PatternTcpOptions => qq{SELECT idSignature FROM Signature WHERE idS1PatternTcpOptions=?}, | ||||||||||||||
| 206 | idS1PatternTcpMss => qq{SELECT idSignature FROM Signature WHERE idS1PatternTcpMss=?}, | ||||||||||||||
| 207 | idS1PatternTcpWScale => qq{SELECT idSignature FROM Signature WHERE idS1PatternTcpWScale=?}, | ||||||||||||||
| 208 | idS1PatternTcpOLength => qq{SELECT idSignature FROM Signature WHERE idS1PatternTcpOLength=?}, | ||||||||||||||
| 209 | idS2PatternBinary => qq{SELECT idSignature FROM Signature WHERE idS2PatternBinary=?}, | ||||||||||||||
| 210 | idS2PatternTcpFlags => qq{SELECT idSignature FROM Signature WHERE idS2PatternTcpFlags=?}, | ||||||||||||||
| 211 | idS2PatternTcpWindow => qq{SELECT idSignature FROM Signature WHERE idS2PatternTcpWindow=?}, | ||||||||||||||
| 212 | idS2PatternTcpOptions => qq{SELECT idSignature FROM Signature WHERE idS2PatternTcpOptions=?}, | ||||||||||||||
| 213 | idS2PatternTcpMss => qq{SELECT idSignature FROM Signature WHERE idS2PatternTcpMss=?}, | ||||||||||||||
| 214 | idS2PatternTcpWScale => qq{SELECT idSignature FROM Signature WHERE idS2PatternTcpWScale=?}, | ||||||||||||||
| 215 | idS2PatternTcpOLength => qq{SELECT idSignature FROM Signature WHERE idS2PatternTcpOLength=?}, | ||||||||||||||
| 216 | idS3PatternBinary => qq{SELECT idSignature FROM Signature WHERE idS3PatternBinary=?}, | ||||||||||||||
| 217 | idS3PatternTcpFlags => qq{SELECT idSignature FROM Signature WHERE idS3PatternTcpFlags=?}, | ||||||||||||||
| 218 | idS3PatternTcpWindow => qq{SELECT idSignature FROM Signature WHERE idS3PatternTcpWindow=?}, | ||||||||||||||
| 219 | idS3PatternTcpOptions => qq{SELECT idSignature FROM Signature WHERE idS3PatternTcpOptions=?}, | ||||||||||||||
| 220 | idS3PatternTcpMss => qq{SELECT idSignature FROM Signature WHERE idS3PatternTcpMss=?}, | ||||||||||||||
| 221 | idS3PatternTcpWScale => qq{SELECT idSignature FROM Signature WHERE idS3PatternTcpWScale=?}, | ||||||||||||||
| 222 | idS3PatternTcpOLength => qq{SELECT idSignature FROM Signature WHERE idS3PatternTcpWScale=?}, | ||||||||||||||
| 223 | all => qq{SELECT idSignature FROM Signature}, | ||||||||||||||
| 224 | idPatternTcpFlags => qq{SELECT idSignatureP from SignatureP WHERE idPatternTcpFlags=?}, | ||||||||||||||
| 225 | idPatternTcpWindow => qq{SELECT idSignatureP from SignatureP WHERE idPatternTcpWindow=?}, | ||||||||||||||
| 226 | idPatternTcpOptions => qq{SELECT idSignatureP from SignatureP WHERE idPatternTcpOptions=?}, | ||||||||||||||
| 227 | idPatternTcpMss => qq{SELECT idSignatureP from SignatureP WHERE idPatternTcpMss=?}, | ||||||||||||||
| 228 | idPatternTcpWScale => qq{SELECT idSignatureP from SignatureP WHERE idPatternTcpWScale=?}, | ||||||||||||||
| 229 | idPatternTcpOLength => qq{SELECT idSignatureP from SignatureP WHERE idPatternTcpOLength=?}, | ||||||||||||||
| 230 | allP => qq{SELECT idSignatureP FROM SignatureP}, | ||||||||||||||
| 231 | ); | ||||||||||||||
| 232 | |||||||||||||||
| 233 | 0 | my %prepared = (); | |||||||||||||
| 234 | 0 | for my $this (keys %select) { | |||||||||||||
| 235 | 0 | my $select = $dbh->prepare($select{$this}); | |||||||||||||
| 236 | 0 | $prepared{$this} = $select; | |||||||||||||
| 237 | } | ||||||||||||||
| 238 | |||||||||||||||
| 239 | 0 | $self->_prepared(\%prepared); | |||||||||||||
| 240 | |||||||||||||||
| 241 | 0 | return 1; | |||||||||||||
| 242 | } | ||||||||||||||
| 243 | |||||||||||||||
| 244 | 0 | ▶ | sub searchSignatureIds { | ||||||||||||
|
Subroutine
searchSignatureIds: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||
| 245 | 0 | my $self = shift; | |||||||||||||
| 246 | 0 | my ($key, $value) = @_; | |||||||||||||
| 247 | |||||||||||||||
| 248 | 0 | my $dbh = $self->_dbh; | |||||||||||||
| 249 | |||||||||||||||
| 250 | 0 | ▶ | my $select = $self->_prepared->{$key || 'all'}; | ||||||||||||
|
Condition0%0/2
$key || 'all'
MC/DC0%0/1
$key || 'all'
$key
Truth table0%0/2
A
A= $key
| |||||||||||||||
| 251 | |||||||||||||||
| 252 | 0 | my $rv; | |||||||||||||
| 253 | # First case, we want only a subset of all signatures | ||||||||||||||
| 254 | 0 | ▶ | if ($value) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 255 | 0 | $rv = $select->execute($value); | |||||||||||||
| 256 | } | ||||||||||||||
| 257 | # Second case, we want all signature IDs | ||||||||||||||
| 258 | else { | ||||||||||||||
| 259 | 0 | $rv = $select->execute; | |||||||||||||
| 260 | } | ||||||||||||||
| 261 | |||||||||||||||
| 262 | 0 | my @list = (); | |||||||||||||
| 263 | 0 | my $a = $select->fetchall_arrayref; | |||||||||||||
| 264 | 0 | for my $id (@$a) { | |||||||||||||
| 265 | 0 | push @list, @$id; | |||||||||||||
| 266 | } | ||||||||||||||
| 267 | |||||||||||||||
| 268 | 0 | return \@list; | |||||||||||||
| 269 | } | ||||||||||||||
| 270 | |||||||||||||||
| 271 | 0 | ▶ | sub searchSignaturePIds { | ||||||||||||
|
Subroutine
searchSignaturePIds: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||
| 272 | 0 | my $self = shift; | |||||||||||||
| 273 | 0 | my ($key, $value) = @_; | |||||||||||||
| 274 | |||||||||||||||
| 275 | 0 | my $dbh = $self->_dbh; | |||||||||||||
| 276 | |||||||||||||||
| 277 | 0 | ▶ | my $select = $self->_prepared->{$key || 'allP'}; | ||||||||||||
|
Condition0%0/2
$key || 'allP'
MC/DC0%0/1
$key || 'allP'
$key
Truth table0%0/2
A
A= $key
| |||||||||||||||
| 278 | |||||||||||||||
| 279 | 0 | my $rv; | |||||||||||||
| 280 | # First case, we want only a subset of all signatures | ||||||||||||||
| 281 | 0 | ▶ | if ($value) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 282 | 0 | $rv = $select->execute($value); | |||||||||||||
| 283 | } | ||||||||||||||
| 284 | # Second case, we want all signature IDs | ||||||||||||||
| 285 | else { | ||||||||||||||
| 286 | 0 | $rv = $select->execute; | |||||||||||||
| 287 | } | ||||||||||||||
| 288 | |||||||||||||||
| 289 | 0 | my @list = (); | |||||||||||||
| 290 | 0 | my $a = $select->fetchall_arrayref; | |||||||||||||
| 291 | 0 | for my $id (@$a) { | |||||||||||||
| 292 | 0 | push @list, @$id; | |||||||||||||
| 293 | } | ||||||||||||||
| 294 | |||||||||||||||
| 295 | 0 | return \@list; | |||||||||||||
| 296 | } | ||||||||||||||
| 297 | |||||||||||||||
| 298 | 0 | ▶ | sub _lookupSignature { | ||||||||||||
|
Subroutine
_lookupSignature: not called CC=2 SCAR=17.9
| |||||||||||||||
| 299 | 0 | my $self = shift; | |||||||||||||
| 300 | 0 | my ($h) = @_; | |||||||||||||
| 301 | |||||||||||||||
| 302 | 0 | my $dbh = $self->_dbh; | |||||||||||||
| 303 | |||||||||||||||
| 304 | 0 | my $prepared = $self->_prepared; | |||||||||||||
| 305 | |||||||||||||||
| 306 | 0 | my $sIpVersion = $prepared->{ipVersion}; | |||||||||||||
| 307 | 0 | my $sOs = $prepared->{os}; | |||||||||||||
| 308 | 0 | my $sOsVersion = $prepared->{osVersion}; | |||||||||||||
| 309 | 0 | my $sOsVersionFamily = $prepared->{osVersionFamily}; | |||||||||||||
| 310 | 0 | my $sSystemClass = $prepared->{systemClass}; | |||||||||||||
| 311 | 0 | my $sVendor = $prepared->{vendor}; | |||||||||||||
| 312 | |||||||||||||||
| 313 | 0 | my $rv; | |||||||||||||
| 314 | 0 | $rv = $sIpVersion->execute($h->{idIpVersion}); | |||||||||||||
| 315 | 0 | $rv = $sOs->execute($h->{idOs}); | |||||||||||||
| 316 | 0 | $rv = $sOsVersion->execute($h->{idOsVersion}); | |||||||||||||
| 317 | 0 | $rv = $sOsVersionFamily->execute($h->{idOsVersionFamily}); | |||||||||||||
| 318 | 0 | $rv = $sSystemClass->execute($h->{idSystemClass}); | |||||||||||||
| 319 | 0 | $rv = $sVendor->execute($h->{idVendor}); | |||||||||||||
| 320 | |||||||||||||||
| 321 | 0 | my $ipVersion = $sIpVersion->fetchrow_hashref; | |||||||||||||
| 322 | 0 | my $os = $sOs->fetchrow_hashref; | |||||||||||||
| 323 | 0 | my $osVersion = $sOsVersion->fetchrow_hashref; | |||||||||||||
| 324 | 0 | my $osVersionFamily = $sOsVersionFamily->fetchrow_hashref; | |||||||||||||
| 325 | 0 | my $systemClass = $sSystemClass->fetchrow_hashref; | |||||||||||||
| 326 | 0 | my $vendor = $sVendor->fetchrow_hashref; | |||||||||||||
| 327 | |||||||||||||||
| 328 | my %l = ( | ||||||||||||||
| 329 | %$h, | ||||||||||||||
| 330 | trusted => $h->{trusted}, | ||||||||||||||
| 331 | ipVersion => $ipVersion->{ipVersion}, | ||||||||||||||
| 332 | os => $os->{os}, | ||||||||||||||
| 333 | osVersion => $osVersion->{osVersion}, | ||||||||||||||
| 334 | osVersionFamily => $osVersionFamily->{osVersionFamily}, | ||||||||||||||
| 335 | systemClass => $systemClass->{systemClass}, | ||||||||||||||
| 336 | vendor => $vendor->{vendor}, | ||||||||||||||
| 337 | 0 | ); | |||||||||||||
| 338 | 0 | ▶ | if (exists($h->{idSignature})) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 339 | 0 | $l{idSignature} = $h->{idSignature}; | |||||||||||||
| 340 | } | ||||||||||||||
| 341 | else { | ||||||||||||||
| 342 | 0 | $l{idSignatureP} = $h->{idSignatureP}; | |||||||||||||
| 343 | } | ||||||||||||||
| 344 | |||||||||||||||
| 345 | 0 | return \%l; | |||||||||||||
| 346 | } | ||||||||||||||
| 347 | |||||||||||||||
| 348 | 0 | ▶ | sub lookupPatterns { | ||||||||||||
|
Subroutine
lookupPatterns: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 349 | 0 | my $self = shift; | |||||||||||||
| 350 | 0 | my ($signature) = @_; | |||||||||||||
| 351 | |||||||||||||||
| 352 | 0 | my $dbh = $self->_dbh; | |||||||||||||
| 353 | |||||||||||||||
| 354 | 0 | my $prepared = $self->_prepared; | |||||||||||||
| 355 | |||||||||||||||
| 356 | 0 | my $sBinary = $prepared->{patternBinary}; | |||||||||||||
| 357 | 0 | my $sTcpFlags = $prepared->{patternTcpFlags}; | |||||||||||||
| 358 | 0 | my $sTcpWindow = $prepared->{patternTcpWindow}; | |||||||||||||
| 359 | 0 | my $sTcpOptions = $prepared->{patternTcpOptions}; | |||||||||||||
| 360 | 0 | my $sTcpMss = $prepared->{patternTcpMss}; | |||||||||||||
| 361 | 0 | my $sTcpWScale = $prepared->{patternTcpWScale}; | |||||||||||||
| 362 | 0 | my $sTcpOLength = $prepared->{patternTcpOLength}; | |||||||||||||
| 363 | |||||||||||||||
| 364 | 0 | for my $p ('S1', 'S2', 'S3') { | |||||||||||||
| 365 | 0 | my $idBinary = 'id'.$p.'PatternBinary'; | |||||||||||||
| 366 | 0 | my $idTcpFlags = 'id'.$p.'PatternTcpFlags'; | |||||||||||||
| 367 | 0 | my $idTcpWindow = 'id'.$p.'PatternTcpWindow'; | |||||||||||||
| 368 | 0 | my $idTcpOptions = 'id'.$p.'PatternTcpOptions'; | |||||||||||||
| 369 | 0 | my $idTcpMss = 'id'.$p.'PatternTcpMss'; | |||||||||||||
| 370 | 0 | my $idTcpWScale = 'id'.$p.'PatternTcpWScale'; | |||||||||||||
| 371 | 0 | my $idTcpOLength = 'id'.$p.'PatternTcpOLength'; | |||||||||||||
| 372 | |||||||||||||||
| 373 | 0 | my $rv = $sBinary->execute($signature->{$idBinary}); | |||||||||||||
| 374 | 0 | my $binary = $sBinary->fetchrow_hashref; | |||||||||||||
| 375 | |||||||||||||||
| 376 | 0 | $rv = $sTcpFlags->execute($signature->{$idTcpFlags}); | |||||||||||||
| 377 | 0 | my $tcpFlags = $sTcpFlags->fetchrow_hashref; | |||||||||||||
| 378 | |||||||||||||||
| 379 | 0 | $rv = $sTcpWindow->execute($signature->{$idTcpWindow}); | |||||||||||||
| 380 | 0 | my $tcpWindow = $sTcpWindow->fetchrow_hashref; | |||||||||||||
| 381 | |||||||||||||||
| 382 | 0 | $rv = $sTcpOptions->execute($signature->{$idTcpOptions}); | |||||||||||||
| 383 | 0 | my $tcpOptions = $sTcpOptions->fetchrow_hashref; | |||||||||||||
| 384 | |||||||||||||||
| 385 | 0 | $rv = $sTcpMss->execute($signature->{$idTcpMss}); | |||||||||||||
| 386 | 0 | my $tcpMss = $sTcpMss->fetchrow_hashref; | |||||||||||||
| 387 | |||||||||||||||
| 388 | 0 | $rv = $sTcpWScale->execute($signature->{$idTcpWScale}); | |||||||||||||
| 389 | 0 | my $tcpWScale = $sTcpWScale->fetchrow_hashref; | |||||||||||||
| 390 | |||||||||||||||
| 391 | 0 | $rv = $sTcpOLength->execute($signature->{$idTcpOLength}); | |||||||||||||
| 392 | 0 | my $tcpOLength = $sTcpOLength->fetchrow_hashref; | |||||||||||||
| 393 | |||||||||||||||
| 394 | 0 | for my $h ('Heuristic0', 'Heuristic1', 'Heuristic2') { | |||||||||||||
| 395 | 0 | my $mBinaryHeuristic = 'patternBinary'.$h; | |||||||||||||
| 396 | 0 | my $mTcpFlagsHeuristic = 'patternTcpFlags'.$h; | |||||||||||||
| 397 | 0 | my $mTcpWindowHeuristic = 'patternTcpWindow'.$h; | |||||||||||||
| 398 | 0 | my $mTcpOptionsHeuristic = 'patternTcpOptions'.$h; | |||||||||||||
| 399 | 0 | my $mTcpMssHeuristic = 'patternTcpMss'.$h; | |||||||||||||
| 400 | 0 | my $mTcpWScaleHeuristic = 'patternTcpWScale'.$h; | |||||||||||||
| 401 | 0 | my $mTcpOLengthHeuristic = 'patternTcpOLength'.$h; | |||||||||||||
| 402 | |||||||||||||||
| 403 | 0 | $signature->{$p.$mBinaryHeuristic} = $binary->{$mBinaryHeuristic}; | |||||||||||||
| 404 | 0 | $signature->{$p.$mTcpFlagsHeuristic} = $tcpFlags->{$mTcpFlagsHeuristic}; | |||||||||||||
| 405 | 0 | $signature->{$p.$mTcpWindowHeuristic} = $tcpWindow->{$mTcpWindowHeuristic}; | |||||||||||||
| 406 | 0 | $signature->{$p.$mTcpOptionsHeuristic} = $tcpOptions->{$mTcpOptionsHeuristic}; | |||||||||||||
| 407 | 0 | $signature->{$p.$mTcpMssHeuristic} = $tcpMss->{$mTcpMssHeuristic}; | |||||||||||||
| 408 | 0 | $signature->{$p.$mTcpWScaleHeuristic} = $tcpWScale->{$mTcpWScaleHeuristic}; | |||||||||||||
| 409 | 0 | $signature->{$p.$mTcpOLengthHeuristic} = $tcpOLength->{$mTcpOLengthHeuristic}; | |||||||||||||
| 410 | } | ||||||||||||||
| 411 | } | ||||||||||||||
| 412 | |||||||||||||||
| 413 | 0 | return $signature; | |||||||||||||
| 414 | } | ||||||||||||||
| 415 | |||||||||||||||
| 416 | 0 | ▶ | sub lookupPatternsP { | ||||||||||||
|
Subroutine
lookupPatternsP: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 417 | 0 | my $self = shift; | |||||||||||||
| 418 | 0 | my ($signature) = @_; | |||||||||||||
| 419 | |||||||||||||||
| 420 | 0 | my $dbh = $self->_dbh; | |||||||||||||
| 421 | |||||||||||||||
| 422 | 0 | my $sBinary = $dbh->prepare( | |||||||||||||
| 423 | qq{SELECT * FROM PatternBinary WHERE idPatternBinary=?} | ||||||||||||||
| 424 | ); | ||||||||||||||
| 425 | 0 | my $sTcpFlags = $dbh->prepare( | |||||||||||||
| 426 | qq{SELECT * FROM PatternTcpFlags WHERE idPatternTcpFlags=?} | ||||||||||||||
| 427 | ); | ||||||||||||||
| 428 | 0 | my $sTcpWindow = $dbh->prepare( | |||||||||||||
| 429 | qq{SELECT * FROM PatternTcpWindow WHERE idPatternTcpWindow=?} | ||||||||||||||
| 430 | ); | ||||||||||||||
| 431 | 0 | my $sTcpOptions = $dbh->prepare( | |||||||||||||
| 432 | qq{SELECT * FROM PatternTcpOptions WHERE idPatternTcpOptions=?} | ||||||||||||||
| 433 | ); | ||||||||||||||
| 434 | 0 | my $sTcpMss = $dbh->prepare( | |||||||||||||
| 435 | qq{SELECT * FROM PatternTcpMss WHERE idPatternTcpMss=?} | ||||||||||||||
| 436 | ); | ||||||||||||||
| 437 | 0 | my $sTcpWScale = $dbh->prepare( | |||||||||||||
| 438 | qq{SELECT * FROM PatternTcpWScale WHERE idPatternTcpWScale=?} | ||||||||||||||
| 439 | ); | ||||||||||||||
| 440 | 0 | my $sTcpOLength = $dbh->prepare( | |||||||||||||
| 441 | qq{SELECT * FROM PatternTcpOLength WHERE idPatternTcpOLength=?} | ||||||||||||||
| 442 | ); | ||||||||||||||
| 443 | |||||||||||||||
| 444 | 0 | my $idTcpFlags = 'idPatternTcpFlags'; | |||||||||||||
| 445 | 0 | my $idTcpWindow = 'idPatternTcpWindow'; | |||||||||||||
| 446 | 0 | my $idTcpOptions = 'idPatternTcpOptions'; | |||||||||||||
| 447 | 0 | my $idTcpMss = 'idPatternTcpMss'; | |||||||||||||
| 448 | 0 | my $idTcpWScale = 'idPatternTcpWScale'; | |||||||||||||
| 449 | 0 | my $idTcpOLength = 'idPatternTcpOLength'; | |||||||||||||
| 450 | |||||||||||||||
| 451 | 0 | my $rv = $sTcpFlags->execute($signature->{$idTcpFlags}); | |||||||||||||
| 452 | 0 | my $tcpFlags = $sTcpFlags->fetchrow_hashref; | |||||||||||||
| 453 | |||||||||||||||
| 454 | 0 | $rv = $sTcpWindow->execute($signature->{$idTcpWindow}); | |||||||||||||
| 455 | 0 | my $tcpWindow = $sTcpWindow->fetchrow_hashref; | |||||||||||||
| 456 | |||||||||||||||
| 457 | 0 | $rv = $sTcpOptions->execute($signature->{$idTcpOptions}); | |||||||||||||
| 458 | 0 | my $tcpOptions = $sTcpOptions->fetchrow_hashref; | |||||||||||||
| 459 | |||||||||||||||
| 460 | 0 | $rv = $sTcpMss->execute($signature->{$idTcpMss}); | |||||||||||||
| 461 | 0 | my $tcpMss = $sTcpMss->fetchrow_hashref; | |||||||||||||
| 462 | |||||||||||||||
| 463 | 0 | $rv = $sTcpWScale->execute($signature->{$idTcpWScale}); | |||||||||||||
| 464 | 0 | my $tcpWScale = $sTcpWScale->fetchrow_hashref; | |||||||||||||
| 465 | |||||||||||||||
| 466 | 0 | $rv = $sTcpOLength->execute($signature->{$idTcpOLength}); | |||||||||||||
| 467 | 0 | my $tcpOLength = $sTcpOLength->fetchrow_hashref; | |||||||||||||
| 468 | |||||||||||||||
| 469 | 0 | for my $h ('Heuristic0', 'Heuristic1', 'Heuristic2') { | |||||||||||||
| 470 | 0 | my $mTcpFlagsHeuristic = 'patternTcpFlags'.$h; | |||||||||||||
| 471 | 0 | my $mTcpWindowHeuristic = 'patternTcpWindow'.$h; | |||||||||||||
| 472 | 0 | my $mTcpOptionsHeuristic = 'patternTcpOptions'.$h; | |||||||||||||
| 473 | 0 | my $mTcpMssHeuristic = 'patternTcpMss'.$h; | |||||||||||||
| 474 | 0 | my $mTcpWScaleHeuristic = 'patternTcpWScale'.$h; | |||||||||||||
| 475 | 0 | my $mTcpOLengthHeuristic = 'patternTcpOLength'.$h; | |||||||||||||
| 476 | |||||||||||||||
| 477 | 0 | $signature->{$mTcpFlagsHeuristic} = $tcpFlags->{$mTcpFlagsHeuristic}; | |||||||||||||
| 478 | 0 | $signature->{$mTcpWindowHeuristic} = $tcpWindow->{$mTcpWindowHeuristic}; | |||||||||||||
| 479 | 0 | $signature->{$mTcpOptionsHeuristic} = $tcpOptions->{$mTcpOptionsHeuristic}; | |||||||||||||
| 480 | 0 | $signature->{$mTcpMssHeuristic} = $tcpMss->{$mTcpMssHeuristic}; | |||||||||||||
| 481 | 0 | $signature->{$mTcpWScaleHeuristic} = $tcpWScale->{$mTcpWScaleHeuristic}; | |||||||||||||
| 482 | 0 | $signature->{$mTcpOLengthHeuristic} = $tcpOLength->{$mTcpOLengthHeuristic}; | |||||||||||||
| 483 | } | ||||||||||||||
| 484 | |||||||||||||||
| 485 | 0 | return $signature; | |||||||||||||
| 486 | } | ||||||||||||||
| 487 | |||||||||||||||
| 488 | 0 | ▶ | sub retrieveSignature { | ||||||||||||
|
Subroutine
retrieveSignature: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 489 | 0 | my $self = shift; | |||||||||||||
| 490 | 0 | my ($id) = @_; | |||||||||||||
| 491 | |||||||||||||||
| 492 | 0 | my $select = $self->_prepared->{signature}; | |||||||||||||
| 493 | 0 | my $rv = $select->execute($id); | |||||||||||||
| 494 | 0 | my $h = $select->fetchrow_hashref; | |||||||||||||
| 495 | |||||||||||||||
| 496 | 0 | my $signature = $self->_lookupSignature($h); | |||||||||||||
| 497 | |||||||||||||||
| 498 | 0 | return $signature; | |||||||||||||
| 499 | } | ||||||||||||||
| 500 | |||||||||||||||
| 501 | 0 | ▶ | sub retrieveSignatureP { | ||||||||||||
|
Subroutine
retrieveSignatureP: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 502 | 0 | my $self = shift; | |||||||||||||
| 503 | 0 | my ($id) = @_; | |||||||||||||
| 504 | |||||||||||||||
| 505 | 0 | my $dbh = $self->_dbh; | |||||||||||||
| 506 | 0 | my $select = $dbh->prepare(qq{SELECT * FROM SignatureP WHERE idSignatureP=?}); | |||||||||||||
| 507 | 0 | my $rv = $select->execute($id); | |||||||||||||
| 508 | 0 | my $h = $select->fetchrow_hashref; | |||||||||||||
| 509 | |||||||||||||||
| 510 | 0 | my $signature = $self->_lookupSignature($h); | |||||||||||||
| 511 | |||||||||||||||
| 512 | 0 | return $signature; | |||||||||||||
| 513 | } | ||||||||||||||
| 514 | |||||||||||||||
| 515 | 0 | ▶ | sub post { | ||||||||||||
|
Subroutine
post: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 516 | 0 | my $self = shift; | |||||||||||||
| 517 | |||||||||||||||
| 518 | 0 | ▶ | if ($self->_dbh) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 519 | 0 | my $prepared = $self->_prepared; | |||||||||||||
| 520 | 0 | for (keys %$prepared) { | |||||||||||||
| 521 | 0 | undef($prepared->{$_}); | |||||||||||||
| 522 | } | ||||||||||||||
| 523 | 0 | $self->_dbh->disconnect; | |||||||||||||
| 524 | } | ||||||||||||||
| 525 | |||||||||||||||
| 526 | 0 | return 1; | |||||||||||||
| 527 | } | ||||||||||||||
| 528 | |||||||||||||||
| 529 | 0 | ▶ | sub _updateDb { | ||||||||||||
|
Subroutine
_updateDb: not called CC=3 SCAR=24.8
| |||||||||||||||
| 530 | 0 | my $self = shift; | |||||||||||||
| 531 | 0 | my ($ua) = @_; | |||||||||||||
| 532 | |||||||||||||||
| 533 | 0 | my $log = $self->global->log; | |||||||||||||
| 534 | |||||||||||||||
| 535 | 0 | my $dbFile = $self->file; | |||||||||||||
| 536 | |||||||||||||||
| 537 | 0 | my $url = "http://www.metabrik.org/wp-content/files/sinfp/sinfp3-latest.db"; | |||||||||||||
| 538 | 0 | my $db = $ua->get($url); | |||||||||||||
| 539 | 0 | ▶ | if ($db->is_success) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 540 | 0 | ▶ | open(my $out, '>', $dbFile) or $log->fatal( | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 541 | "open2: $dbFile: $!" | ||||||||||||||
| 542 | ); | ||||||||||||||
| 543 | 0 | print $out $db->decoded_content; | |||||||||||||
| 544 | 0 | CORE::close($out); | |||||||||||||
| 545 | } | ||||||||||||||
| 546 | else { | ||||||||||||||
| 547 | 0 | $log->fatal("GET [$url]: ".$db->status_line); | |||||||||||||
| 548 | } | ||||||||||||||
| 549 | 0 | $log->info("Update complete for [$dbFile]"); | |||||||||||||
| 550 | |||||||||||||||
| 551 | 0 | return 1; | |||||||||||||
| 552 | } | ||||||||||||||
| 553 | |||||||||||||||
| 554 | 0 | ▶ | sub update { | ||||||||||||
|
Subroutine
update: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||
| 555 | 0 | my $self = shift; | |||||||||||||
| 556 | |||||||||||||||
| 557 | 0 | my $log = $self->global->log; | |||||||||||||
| 558 | |||||||||||||||
| 559 | 0 | my $ua = LWP::UserAgent->new; | |||||||||||||
| 560 | 0 | $ua->timeout(10); | |||||||||||||
| 561 | 0 | $ua->env_proxy; | |||||||||||||
| 562 | 0 | $ua->agent("Net::SinFP3 ".$Net::SinFP3::VERSION); | |||||||||||||
| 563 | |||||||||||||||
| 564 | 0 | my $dbFile = $self->file; | |||||||||||||
| 565 | |||||||||||||||
| 566 | 0 | my $url = "http://www.metabrik.org/wp-content/files/sinfp/sinfp3-latest.db.md5"; | |||||||||||||
| 567 | 0 | my $db = $ua->get($url); | |||||||||||||
| 568 | 0 | ▶ | if ($db->is_success) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 569 | 0 | (my $md5 = $db->decoded_content) =~ s/^.*=\s+(.*)$/$1/; | |||||||||||||
| 570 | 0 | chomp($md5); | |||||||||||||
| 571 | 0 | ▶ | open(my $in, '<', $dbFile) or $log->fatal( | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 572 | "open1: $dbFile: $!" | ||||||||||||||
| 573 | ); | ||||||||||||||
| 574 | 0 | my $old = Digest::MD5->new; | |||||||||||||
| 575 | 0 | $old->addfile($in); | |||||||||||||
| 576 | 0 | my $oldmd5 = $old->hexdigest; | |||||||||||||
| 577 | 0 | CORE::close($in); | |||||||||||||
| 578 | 0 | ▶ | if ($oldmd5 ne $md5) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 579 | 0 | $self->_updateDb($ua); | |||||||||||||
| 580 | } | ||||||||||||||
| 581 | else { | ||||||||||||||
| 582 | 0 | $log->info("Database already up-to-date"); | |||||||||||||
| 583 | } | ||||||||||||||
| 584 | } | ||||||||||||||
| 585 | else { | ||||||||||||||
| 586 | 0 | $log->fatal("GET [$url]: ". $db->status_line); | |||||||||||||
| 587 | } | ||||||||||||||
| 588 | |||||||||||||||
| 589 | 0 | return 1; | |||||||||||||
| 590 | } | ||||||||||||||
| 591 | |||||||||||||||
| 592 | 1 | 1; | |||||||||||||
| 593 | |||||||||||||||
| 594 | __END__ | ||||||||||||||