| 1 | package App::WebPerimeterScan::Ports; | ||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||
| 3 | 29 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | 29 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | 29 | ▶ | use autodie qw(:all); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||
| 7 | # A refused or filtered port is the answer the scan is looking for, not an | ||||||||||||||||||||||||||||||
| 8 | # error, and a non-blocking connect() "fails" with EINPROGRESS by design. | ||||||||||||||||||||||||||||||
| 9 | # These builtins therefore keep their ordinary return values. | ||||||||||||||||||||||||||||||
| 10 | 29 | ▶ | no autodie qw(socket connect fcntl getsockopt close sysread); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||
| 12 | 29 | ▶ | use Errno qw(EINPROGRESS EWOULDBLOCK); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 13 | 29 | ▶ | use Fcntl (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 14 | 29 | ▶ | use IO::Select; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 15 | 29 | ▶ | use IO::Socket::INET (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 16 | 29 | ▶ | use Params::Get qw(get_params); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 17 | 29 | ▶ | use Params::Validate::Strict qw(validate_strict); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 18 | 29 | ▶ | use Readonly; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 19 | 29 | ▶ | use Socket qw(AF_INET SOCK_STREAM SOL_SOCKET SO_ERROR inet_aton sockaddr_in); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 20 | 29 | ▶ | use Sub::Private { mode => 'enforce' }; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 21 | 29 | ▶ | use Time::HiRes qw(time); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||
| 23 | 29 | ▶ | use App::WebPerimeterScan::I18N (); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 24 | 29 | ▶ | use parent -norequire, 'App::WebPerimeterScan::I18N'; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||
| 26 | 29 | our $VERSION = '0.001.0'; | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||
| 28 | # Defaults for scan(); a plain hash so Object::Configure or a caller can | ||||||||||||||||||||||||||||||
| 29 | # change them. | ||||||||||||||||||||||||||||||
| 30 | 29 | our %config = ( | |||||||||||||||||||||||||||||
| 31 | timeout => 3, # seconds for the whole connect batch | ||||||||||||||||||||||||||||||
| 32 | banner_timeout => 0.6, # seconds to wait for a service greeting | ||||||||||||||||||||||||||||||
| 33 | banner_bytes => 200, # bytes of greeting read | ||||||||||||||||||||||||||||||
| 34 | banner_length => 120, # characters of greeting kept after cleaning | ||||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||
| 37 | # F_GETFL and friends are not defined on Windows, where Fcntl dies with "Your | ||||||||||||||||||||||||||||||
| 38 | # vendor has not defined Fcntl macro" as soon as the constant is reached. The | ||||||||||||||||||||||||||||||
| 39 | # flags are probed once here, and the parallel batch is only used where they | ||||||||||||||||||||||||||||||
| 40 | # exist; everywhere else the scan connects one port at a time instead. Not | ||||||||||||||||||||||||||||||
| 41 | # Readonly: the tests force the serial path with local(). | ||||||||||||||||||||||||||||||
| 42 | 29 | ▶ | our $NONBLOCKING = eval { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 43 | 29 | Fcntl::F_GETFL() + Fcntl::F_SETFL() + Fcntl::O_NONBLOCK() >= 0; | |||||||||||||||||||||||||||||
| 44 | } ? 1 : 0; | ||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||
| 46 | # Published services worth knowing about. Keep this list short: a long list | ||||||||||||||||||||||||||||||
| 47 | # is slow and noisy, and this is an attack-surface summary, not nmap. | ||||||||||||||||||||||||||||||
| 48 | 29 | Readonly::Array our @DEFAULT_PORTS => ( | |||||||||||||||||||||||||||||
| 49 | 21, 22, 23, 25, 53, 80, 110, 143, 443, 445, 587, 993, 995, | ||||||||||||||||||||||||||||||
| 50 | 1433, 3000, 3306, 3389, 5432, 5900, 6379, 8000, 8080, 8443, | ||||||||||||||||||||||||||||||
| 51 | 8888, 9200, 11211, 27017, | ||||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||
| 54 | # How bad it is for each port to be reachable from the internet. The reason | ||||||||||||||||||||||||||||||
| 55 | # is text, so it lives in the message catalogue under port-open.detail. | ||||||||||||||||||||||||||||||
| 56 | 29 | Readonly::Hash our %RISKY => ( | |||||||||||||||||||||||||||||
| 57 | 21 => 'critical', # FTP | ||||||||||||||||||||||||||||||
| 58 | 23 => 'critical', # Telnet | ||||||||||||||||||||||||||||||
| 59 | 445 => 'high', # SMB | ||||||||||||||||||||||||||||||
| 60 | 1433 => 'high', # MS SQL | ||||||||||||||||||||||||||||||
| 61 | 3306 => 'high', # MySQL | ||||||||||||||||||||||||||||||
| 62 | 3389 => 'high', # RDP | ||||||||||||||||||||||||||||||
| 63 | 5432 => 'high', # PostgreSQL | ||||||||||||||||||||||||||||||
| 64 | 5900 => 'medium', # VNC | ||||||||||||||||||||||||||||||
| 65 | 6379 => 'high', # Redis | ||||||||||||||||||||||||||||||
| 66 | 9200 => 'high', # Elasticsearch | ||||||||||||||||||||||||||||||
| 67 | 11211 => 'high', # memcached | ||||||||||||||||||||||||||||||
| 68 | 27017 => 'high', # MongoDB | ||||||||||||||||||||||||||||||
| 69 | 993 => 'info', # IMAPS | ||||||||||||||||||||||||||||||
| 70 | 995 => 'info', # POP3S | ||||||||||||||||||||||||||||||
| 71 | 587 => 'info', # submission | ||||||||||||||||||||||||||||||
| 72 | 25 => 'low', # SMTP | ||||||||||||||||||||||||||||||
| 73 | 22 => 'low', # SSH | ||||||||||||||||||||||||||||||
| 74 | 3000 => 'low', # development servers | ||||||||||||||||||||||||||||||
| 75 | 8000 => 'low', # alternate HTTP | ||||||||||||||||||||||||||||||
| 76 | 8080 => 'low', | ||||||||||||||||||||||||||||||
| 77 | 8443 => 'low', # alternate HTTPS | ||||||||||||||||||||||||||||||
| 78 | 8888 => 'low', | ||||||||||||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||
| 81 | 29 | Readonly::Scalar my $MIN_PORT => 1; | |||||||||||||||||||||||||||||
| 82 | 29 | Readonly::Scalar my $MAX_PORT => 65_535; | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||
| 84 | # SO_ERROR is an int; 0 means the handshake completed. | ||||||||||||||||||||||||||||||
| 85 | 29 | Readonly::Scalar my $SO_ERROR_FORMAT => 'i'; | |||||||||||||||||||||||||||||
| 86 | 29 | Readonly::Scalar my $CONNECTED => 0; | |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||
| 88 | # Printable ASCII, for cleaning a banner before it reaches a report. | ||||||||||||||||||||||||||||||
| 89 | 29 | Readonly::Scalar my $UNPRINTABLE => qr/[^\x20-\x7e]/; | |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||
| 91 | =encoding utf8 | ||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||
| 93 | =head1 NAME | ||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||
| 95 | App::WebPerimeterScan::Ports - parallel TCP connect scan | ||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||
| 97 | =head1 VERSION | ||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||
| 99 | Version 0.001.0 | ||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||
| 101 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||
| 103 | Scan the default list of ports and print what answered: | ||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||
| 105 | use App::WebPerimeterScan::Ports; | ||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||
| 107 | my $scan = App::WebPerimeterScan::Ports->scan(host => 'example.com'); | ||||||||||||||||||||||||||||||
| 108 | die "scan failed: $scan->{error}\n" if $scan->{error}; | ||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||
| 110 | for my $open (@{ $scan->{open} }) { | ||||||||||||||||||||||||||||||
| 111 | print "$open->{port} $open->{banner}\n"; | ||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||
| 114 | Check only a few ports, quickly, without reading greetings: | ||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||
| 116 | my $web = App::WebPerimeterScan::Ports->scan( | ||||||||||||||||||||||||||||||
| 117 | host => 'example.com', | ||||||||||||||||||||||||||||||
| 118 | ports => [ 80, 443, 8443 ], | ||||||||||||||||||||||||||||||
| 119 | timeout => 1, | ||||||||||||||||||||||||||||||
| 120 | banner => 0, | ||||||||||||||||||||||||||||||
| 121 | ); | ||||||||||||||||||||||||||||||
| 122 | my %is_open = map { $_->{port} => 1 } @{ $web->{open} }; | ||||||||||||||||||||||||||||||
| 123 | print "HTTPS is reachable\n" if $is_open{443}; | ||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||
| 125 | Check the scope before you scan. This module does not do it for you: | ||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||
| 127 | $scope->assert_target_allowed($host); | ||||||||||||||||||||||||||||||
| 128 | my $result = App::WebPerimeterScan::Ports->scan(host => $host); | ||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||
| 130 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||
| 132 | This module finds out which TCP ports on a host accept a connection. It | ||||||||||||||||||||||||||||||
| 133 | tries to connect to each port. It does not send any data except to read a | ||||||||||||||||||||||||||||||
| 134 | greeting. | ||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||
| 136 | All the connections are started at the same time, and the module waits for | ||||||||||||||||||||||||||||||
| 137 | all of them together. So a scan of the whole default list takes about one | ||||||||||||||||||||||||||||||
| 138 | C<timeout>, not one C<timeout> for each port. | ||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||
| 140 | For each open port, the module then makes one more short connection and | ||||||||||||||||||||||||||||||
| 141 | reads the first line the service sends, if it sends one. This line is | ||||||||||||||||||||||||||||||
| 142 | called the I<banner>. A banner such as C<SSH-2.0-OpenSSH_7.4> often tells | ||||||||||||||||||||||||||||||
| 143 | you the software and its version, which is how an old, unpatched service | ||||||||||||||||||||||||||||||
| 144 | is found. | ||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||
| 146 | The default list of ports is in C<@App::WebPerimeterScan::Ports::DEFAULT_PORTS>. | ||||||||||||||||||||||||||||||
| 147 | It contains the usual web, mail, file-sharing, remote-desktop and database | ||||||||||||||||||||||||||||||
| 148 | ports. C<%App::WebPerimeterScan::Ports::RISKY> gives the severity of | ||||||||||||||||||||||||||||||
| 149 | each port that should not be open to the internet. | ||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||
| 151 | =head1 REQUIREMENTS | ||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||
| 153 | C<Socket> 2.020: the core of 5.10.1 ships 2.018, and C<SO_ERROR> is what | ||||||||||||||||||||||||||||||
| 154 | separates a refused connection from an open one. C<IO::Select> 1.17 and | ||||||||||||||||||||||||||||||
| 155 | C<IO::Socket::INET> 1.31 are the versions this is tested against. | ||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||
| 157 | The parallel path needs a working C<fcntl(F_SETFL)> and C<O_NONBLOCK>. Where | ||||||||||||||||||||||||||||||
| 158 | they are missing, as on Windows, the scan connects one port at a time | ||||||||||||||||||||||||||||||
| 159 | instead. | ||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||
| 161 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||
| 163 | =head2 scan | ||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||
| 165 | Purpose: find which of a list of TCP ports accept a connection. | ||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||
| 167 | Args: named C<host> (required), C<ports> (array reference; default | ||||||||||||||||||||||||||||||
| 168 | C<@DEFAULT_PORTS>), C<timeout> (seconds for the whole batch; default 3) and | ||||||||||||||||||||||||||||||
| 169 | C<banner> (boolean, read a greeting from each open port; default 1). A | ||||||||||||||||||||||||||||||
| 170 | class method. | ||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||
| 172 | Returns: a hash reference: C<host>, C<open> (array reference of | ||||||||||||||||||||||||||||||
| 173 | C<< { port => N, banner => TEXT } >>, sorted by port) and C<error> (C<undef>, | ||||||||||||||||||||||||||||||
| 174 | or why the scan could not run). Port numbers outside 1-65535 are ignored. | ||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||
| 176 | Side Effects: TCP connections to the host; a second short connection to each | ||||||||||||||||||||||||||||||
| 177 | open port when C<banner> is set. | ||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||
| 179 | Usage: C<< my $scan = App::WebPerimeterScan::Ports->scan(host => $h, ports => [22, 443]); >> | ||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||
| 181 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||
| 183 | my $scan = App::WebPerimeterScan::Ports->scan( | ||||||||||||||||||||||||||||||
| 184 | host => 'example.com', ports => [ 22, 80, 443 ], timeout => 2, | ||||||||||||||||||||||||||||||
| 185 | ); | ||||||||||||||||||||||||||||||
| 186 | die "$scan->{error}\n" if $scan->{error}; | ||||||||||||||||||||||||||||||
| 187 | print join(', ', map { $_->{port} } @{ $scan->{open} }), "\n"; | ||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||
| 189 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||
| 191 | =head4 INPUT | ||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||
| 193 | { | ||||||||||||||||||||||||||||||
| 194 | host => { type => 'string', min => 1 }, | ||||||||||||||||||||||||||||||
| 195 | ports => { type => 'arrayref', element_type => 'integer', optional => 1 }, | ||||||||||||||||||||||||||||||
| 196 | timeout => { type => 'number', min => 0, optional => 1 }, | ||||||||||||||||||||||||||||||
| 197 | banner => { type => 'boolean', optional => 1 }, | ||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||
| 200 | =head4 OUTPUT | ||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||
| 202 | { | ||||||||||||||||||||||||||||||
| 203 | type => 'hashref', | ||||||||||||||||||||||||||||||
| 204 | schema => { | ||||||||||||||||||||||||||||||
| 205 | host => { type => 'string' }, | ||||||||||||||||||||||||||||||
| 206 | open => { type => 'arrayref' }, | ||||||||||||||||||||||||||||||
| 207 | error => { type => 'string', optional => 1 }, | ||||||||||||||||||||||||||||||
| 208 | }, | ||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||
| 211 | =head3 MESSAGES | ||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||
| 213 | Returned in C<error>, not thrown: | ||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||
| 215 | Message Meaning / resolution | ||||||||||||||||||||||||||||||
| 216 | ------------------------ ------------------------------------------- | ||||||||||||||||||||||||||||||
| 217 | cannot resolve 'H' H has no IPv4 address; check the name. | ||||||||||||||||||||||||||||||
| 218 | no tcp protocol The system has no "tcp" protocol entry. | ||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||
| 220 | =head3 PSEUDOCODE | ||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||
| 222 | validate the arguments; apply defaults from %config | ||||||||||||||||||||||||||||||
| 223 | resolve the host to an IPv4 address, or return an error | ||||||||||||||||||||||||||||||
| 224 | keep only port numbers in 1..65535 | ||||||||||||||||||||||||||||||
| 225 | connect to them all in parallel where fcntl allows, else one at a time | ||||||||||||||||||||||||||||||
| 226 | sort the open ports | ||||||||||||||||||||||||||||||
| 227 | read a greeting from each open port if banners were asked for | ||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||
| 229 | =cut | ||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||
| 231 | sub scan | ||||||||||||||||||||||||||||||
| 232 | 10 | ▶ | { | ||||||||||||||||||||||||||||
|
Subroutine
scan: called CC=10 SCAR=30.7
Pod
documented
| |||||||||||||||||||||||||||||||
| 233 | 10 | my $class = shift; | |||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||
| 235 | 10 | ▶ | my $args = validate_strict( | ||||||||||||||||||||||||||||
|
Condition50%1/2
get_params(undef, \@_) // {}
MC/DC0%0/1
get_params(undef, \@_) // {}
get_params(undef, \@_)
Truth table50%1/2
A
A= get_params(undef, \@_)
| |||||||||||||||||||||||||||||||
| 236 | schema => { | ||||||||||||||||||||||||||||||
| 237 | host => { type => 'string', min => 1 }, | ||||||||||||||||||||||||||||||
| 238 | ports => { type => 'arrayref', element_type => 'integer', optional => 1 }, | ||||||||||||||||||||||||||||||
| 239 | timeout => { type => 'number', min => 0, optional => 1 }, | ||||||||||||||||||||||||||||||
| 240 | banner => { type => 'boolean', optional => 1, default => 1 }, | ||||||||||||||||||||||||||||||
| 241 | }, | ||||||||||||||||||||||||||||||
| 242 | input => get_params(undef, \@_) // {}, | ||||||||||||||||||||||||||||||
| 243 | ); | ||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||
| 245 | 10 | my $host = $args->{host}; | |||||||||||||||||||||||||||||
| 246 | 10 | ▶ | my $timeout = $args->{timeout} || $config{timeout}; | ||||||||||||||||||||||||||||
|
Condition33%1/3
$args->{'timeout'} || $App::WebPerimeterScan::Ports::config{'timeout'}
MC/DC0%0/2
$args->{'timeout'} || $App::WebPerimeterScan::Ports::config{'timeout'}
$args->{'timeout'}
$App::WebPerimeterScan::Ports::config{'timeout'}
Truth table33%1/3
A || B
A= $args->{'timeout'}B= $App::WebPerimeterScan::Ports::config{'timeout'}
| |||||||||||||||||||||||||||||||
| 247 | 10 | ▶ | my @ports = grep { $_ >= $MIN_PORT && $_ <= $MAX_PORT } @{ $args->{ports} || \@DEFAULT_PORTS }; | ||||||||||||||||||||||||||||
|
Condition40%2/5
$_ >= $MIN_PORT && $_ <= $MAX_PORT
$args->{'ports'} || \@App::WebPerimeterScan::Ports::DEFAULT_PORTS
MC/DC0%0/2
$_ >= $MIN_PORT && $_ <= $MAX_PORT
$_ >= $MIN_PORT
$_ <= $MAX_PORT
MC/DC0%0/1
$args->{'ports'} || \@App::WebPerimeterScan::Ports::DEFAULT_PORTS
$args->{'ports'}
Truth table33%1/3
A && B
A= $_ >= $MIN_PORTB= $_ <= $MAX_PORT
Truth table50%1/2
A
A= $args->{'ports'}
| |||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||
| 249 | # inet_aton resolves names as well as parsing dotted quads. | ||||||||||||||||||||||||||||||
| 250 | 10 | my $ip = inet_aton($host); | |||||||||||||||||||||||||||||
| 251 | 10 | ▶ | return _result($host, [], $class->i18n('ports.unresolvable', { host => $host })) unless defined $ip; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||
| 253 | 9 | my $proto = getprotobyname('tcp'); | |||||||||||||||||||||||||||||
| 254 | 9 | ▶ | return _result($host, [], $class->i18n('ports.no_tcp')) unless $proto; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||
| 256 | 0 | ▶ | my $open = $NONBLOCKING | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 257 | ? _connect_parallel($ip, $proto, \@ports, $timeout) | ||||||||||||||||||||||||||||||
| 258 | : _connect_serial($host, \@ports, $timeout); | ||||||||||||||||||||||||||||||
| 259 | 0 | my @sorted = sort { $a->{port} <=> $b->{port} } @{$open}; | |||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||
| 261 | 0 | ▶ | $_->{banner} = _banner($host, $_->{port}) for $args->{banner} ? @sorted : (); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||
| 263 | 0 | return _result($host, \@sorted, undef); | |||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||
| 266 | # _result | ||||||||||||||||||||||||||||||
| 267 | # Purpose: the hash scan() returns, built in one place. | ||||||||||||||||||||||||||||||
| 268 | # Entry: $open is an arrayref; $error is a string or undef. | ||||||||||||||||||||||||||||||
| 269 | # Exit: hashref of host, open, error. | ||||||||||||||||||||||||||||||
| 270 | # Side effects: none. | ||||||||||||||||||||||||||||||
| 271 | 10 | ▶ | sub _result :Private | ||||||||||||||||||||||||||||
|
Subroutine
_result: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 272 | { | ||||||||||||||||||||||||||||||
| 273 | 10 | my ($host, $open, $error) = @_; | |||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||
| 275 | 10 | return { host => $host, open => $open, error => $error }; | |||||||||||||||||||||||||||||
| 276 | 29 | ▶ | } | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||
| 278 | # _open_port | ||||||||||||||||||||||||||||||
| 279 | # Purpose: the record for one open port, built in one place. | ||||||||||||||||||||||||||||||
| 280 | # Entry: $port is numeric. | ||||||||||||||||||||||||||||||
| 281 | # Exit: hashref of port and an empty banner. | ||||||||||||||||||||||||||||||
| 282 | # Side effects: none. | ||||||||||||||||||||||||||||||
| 283 | 0 | ▶ | sub _open_port :Private | ||||||||||||||||||||||||||||
|
Subroutine
_open_port: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 284 | { | ||||||||||||||||||||||||||||||
| 285 | 0 | my ($port) = @_; | |||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||
| 287 | 0 | return { port => $port + 0, banner => '' }; | |||||||||||||||||||||||||||||
| 288 | 29 | ▶ | } | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||
| 290 | # _handshake_ok | ||||||||||||||||||||||||||||||
| 291 | # Purpose: did the connect on this socket complete? | ||||||||||||||||||||||||||||||
| 292 | # Entry: $sock has had connect() called on it and is writable. | ||||||||||||||||||||||||||||||
| 293 | # Exit: 1 if SO_ERROR is 0, else 0. | ||||||||||||||||||||||||||||||
| 294 | # Side effects: none. | ||||||||||||||||||||||||||||||
| 295 | # | ||||||||||||||||||||||||||||||
| 296 | # SO_ERROR holds the result of a connect: 0 means the handshake completed. | ||||||||||||||||||||||||||||||
| 297 | # connect() returning true only means it started, which is how a refused | ||||||||||||||||||||||||||||||
| 298 | # port was once reported as open on macOS, so both connect paths ask here. | ||||||||||||||||||||||||||||||
| 299 | 0 | ▶ | sub _handshake_ok :Private | ||||||||||||||||||||||||||||
|
Subroutine
_handshake_ok: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||
| 300 | { | ||||||||||||||||||||||||||||||
| 301 | 0 | my ($sock) = @_; | |||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||
| 303 | 0 | my $error = getsockopt($sock, SOL_SOCKET, SO_ERROR); | |||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||
| 305 | 0 | ▶ | return (defined $error && unpack($SO_ERROR_FORMAT, $error) == $CONNECTED) ? 1 : 0; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $error and unpack($SO_ERROR_FORMAT, $error) == $CONNECTED
MC/DC0%0/2
defined $error and unpack($SO_ERROR_FORMAT, $error) == $CONNECTED
defined $error
unpack($SO_ERROR_FORMAT, $error) == $CONNECTED
Truth table0%0/3
A and B
A= defined $errorB= unpack($SO_ERROR_FORMAT, $error) == $CONNECTED
| |||||||||||||||||||||||||||||||
| 306 | 29 | ▶ | } | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||
| 308 | # _connect_parallel | ||||||||||||||||||||||||||||||
| 309 | # Purpose: connect to every port in one non-blocking batch. | ||||||||||||||||||||||||||||||
| 310 | # Entry: $ip is a packed IPv4 address; $NONBLOCKING is true. | ||||||||||||||||||||||||||||||
| 311 | # Exit: arrayref of open-port records, unsorted. | ||||||||||||||||||||||||||||||
| 312 | # Side effects: opens and closes one socket per port. | ||||||||||||||||||||||||||||||
| 313 | 0 | ▶ | sub _connect_parallel :Private | ||||||||||||||||||||||||||||
|
Subroutine
_connect_parallel: not called CC=13 SCAR=52.0
| |||||||||||||||||||||||||||||||
| 314 | { | ||||||||||||||||||||||||||||||
| 315 | 0 | my ($ip, $proto, $ports, $timeout) = @_; | |||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||
| 317 | 0 | my (%pending, @open); | |||||||||||||||||||||||||||||
| 318 | 0 | my $select = IO::Select->new; | |||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||
| 320 | # Start every connect. Most return EINPROGRESS and are resolved by | ||||||||||||||||||||||||||||||
| 321 | # the select loop below; a loopback connect may finish at once. | ||||||||||||||||||||||||||||||
| 322 | 0 | for my $port (@{$ports}) { | |||||||||||||||||||||||||||||
| 323 | 0 | ▶ | socket(my $sock, AF_INET, SOCK_STREAM, $proto) or next; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 324 | 0 | ▶ | my $flags = fcntl($sock, Fcntl::F_GETFL(), 0) // 0; | ||||||||||||||||||||||||||||
|
Condition0%0/2
fcntl($sock, 3, 0) // 0
MC/DC0%0/1
fcntl($sock, 3, 0) // 0
fcntl($sock, 3, 0)
Truth table0%0/2
A
A= fcntl($sock, 3, 0)
| |||||||||||||||||||||||||||||||
| 325 | 0 | fcntl($sock, Fcntl::F_SETFL(), $flags | Fcntl::O_NONBLOCK()); | |||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||
| 327 | 0 | ▶ | if(connect($sock, sockaddr_in($port, $ip))) { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 328 | 0 | push @open, _open_port($port); | |||||||||||||||||||||||||||||
| 329 | 0 | close $sock; | |||||||||||||||||||||||||||||
| 330 | ▶ | } elsif($! == EINPROGRESS || $! == EWOULDBLOCK) { | |||||||||||||||||||||||||||||
|
Condition0%0/3
$! == 115 or $! == 11
MC/DC0%0/2
$! == 115 or $! == 11
$! == 115
$! == 11
Truth table0%0/3
A or B
A= $! == 115B= $! == 11
| |||||||||||||||||||||||||||||||
| 331 | 0 | $pending{ fileno $sock } = { sock => $sock, port => $port }; | |||||||||||||||||||||||||||||
| 332 | 0 | $select->add($sock); | |||||||||||||||||||||||||||||
| 333 | } else { | ||||||||||||||||||||||||||||||
| 334 | 0 | close $sock; # refused outright | |||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||
| 338 | # A socket becomes writable when its connect finishes either way; | ||||||||||||||||||||||||||||||
| 339 | # SO_ERROR says which way. | ||||||||||||||||||||||||||||||
| 340 | 0 | my $deadline = time + $timeout; | |||||||||||||||||||||||||||||
| 341 | 0 | ▶ | while($select->count && (my $wait = $deadline - time) > 0) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$select->count and (my $wait = $deadline - &App::WebPerimeterScan::Ports::time()) > 0
MC/DC0%0/2
$select->count and (my $wait = $deadline - &App::WebPerimeterScan::Ports::time()) > 0
$select->count
(my $wait = $deadline - &App::WebPerimeterScan::Ports::time()) > 0
Truth table0%0/3
A and B
A= $select->countB= (my $wait = $deadline - &App::WebPerimeterScan::Ports::time()) > 0
| |||||||||||||||||||||||||||||||
| 342 | 0 | for my $sock ($select->can_write($wait)) { | |||||||||||||||||||||||||||||
| 343 | 0 | ▶ | my $entry = delete $pending{ fileno $sock } or next; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 344 | 0 | $select->remove($sock); | |||||||||||||||||||||||||||||
| 345 | 0 | ▶ | push @open, _open_port($entry->{port}) if _handshake_ok($sock); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 346 | 0 | close $sock; | |||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||
| 350 | # Whatever is still pending was filtered: no answer within the timeout. | ||||||||||||||||||||||||||||||
| 351 | 0 | close $_->{sock} for values %pending; | |||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||
| 353 | 0 | return \@open; | |||||||||||||||||||||||||||||
| 354 | 29 | ▶ | } | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||
| 356 | # _connect_serial | ||||||||||||||||||||||||||||||
| 357 | # Purpose: connect to one port at a time, for platforms without fcntl | ||||||||||||||||||||||||||||||
| 358 | # flags (Windows). Slower against a firewall, but correct. | ||||||||||||||||||||||||||||||
| 359 | # Entry: $host is a name or address. | ||||||||||||||||||||||||||||||
| 360 | # Exit: arrayref of open-port records. | ||||||||||||||||||||||||||||||
| 361 | # Side effects: opens and closes one socket per port. | ||||||||||||||||||||||||||||||
| 362 | 0 | ▶ | sub _connect_serial :Private | ||||||||||||||||||||||||||||
|
Subroutine
_connect_serial: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||
| 363 | { | ||||||||||||||||||||||||||||||
| 364 | 0 | my ($host, $ports, $timeout) = @_; | |||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||
| 366 | 0 | my @open; | |||||||||||||||||||||||||||||
| 367 | 0 | for my $port (@{$ports}) { | |||||||||||||||||||||||||||||
| 368 | 0 | ▶ | my $sock = IO::Socket::INET->new( | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 369 | PeerAddr => $host, | ||||||||||||||||||||||||||||||
| 370 | PeerPort => $port, | ||||||||||||||||||||||||||||||
| 371 | Proto => 'tcp', | ||||||||||||||||||||||||||||||
| 372 | Timeout => $timeout, | ||||||||||||||||||||||||||||||
| 373 | ) or next; | ||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||
| 375 | # IO::Socket::INET can hand back a socket whose connect is still in | ||||||||||||||||||||||||||||||
| 376 | # progress, so a refused port looks open; wait for it to become | ||||||||||||||||||||||||||||||
| 377 | # writable before asking how the handshake ended. | ||||||||||||||||||||||||||||||
| 378 | 0 | ▶ | push @open, _open_port($port) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
"IO::Select"->new($sock)->can_write($timeout) and _handshake_ok($sock)
MC/DC0%0/2
"IO::Select"->new($sock)->can_write($timeout) and _handshake_ok($sock)
"IO::Select"->new($sock)->can_write($timeout)
_handshake_ok($sock)
Truth table0%0/3
A and B
A= "IO::Select"->new($sock)->can_write($timeout)B= _handshake_ok($sock)
| |||||||||||||||||||||||||||||||
| 379 | if IO::Select->new($sock)->can_write($timeout) && _handshake_ok($sock); | ||||||||||||||||||||||||||||||
| 380 | 0 | close $sock; | |||||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||
| 383 | 0 | return \@open; | |||||||||||||||||||||||||||||
| 384 | 29 | ▶ | } | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||
| 386 | # _banner | ||||||||||||||||||||||||||||||
| 387 | # Purpose: read whatever a service says first (SSH, SMTP, FTP, MySQL). | ||||||||||||||||||||||||||||||
| 388 | # Entry: $port is known to be open. | ||||||||||||||||||||||||||||||
| 389 | # Exit: printable, single-line text; '' when the service is silent. | ||||||||||||||||||||||||||||||
| 390 | # Side effects: one short TCP connection. Read-only: nothing is sent. | ||||||||||||||||||||||||||||||
| 391 | 0 | ▶ | sub _banner :Private | ||||||||||||||||||||||||||||
|
Subroutine
_banner: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||
| 392 | { | ||||||||||||||||||||||||||||||
| 393 | 0 | my ($host, $port) = @_; | |||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||
| 395 | my $sock = IO::Socket::INET->new( | ||||||||||||||||||||||||||||||
| 396 | PeerAddr => $host, | ||||||||||||||||||||||||||||||
| 397 | PeerPort => $port, | ||||||||||||||||||||||||||||||
| 398 | Proto => 'tcp', | ||||||||||||||||||||||||||||||
| 399 | Timeout => $config{banner_timeout}, | ||||||||||||||||||||||||||||||
| 400 | 0 | ▶ | ) or return ''; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||
| 402 | # Silence is a valid answer: many services wait for the client. | ||||||||||||||||||||||||||||||
| 403 | 0 | my $buffer = ''; | |||||||||||||||||||||||||||||
| 404 | sysread($sock, $buffer, $config{banner_bytes}) | ||||||||||||||||||||||||||||||
| 405 | 0 | ▶ | if IO::Select->new($sock)->can_read($config{banner_timeout}); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 406 | 0 | close $sock; | |||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||
| 408 | # One line of printable ASCII, so a banner cannot corrupt a report. | ||||||||||||||||||||||||||||||
| 409 | 0 | $buffer =~ s/[\r\n]+/ /g; | |||||||||||||||||||||||||||||
| 410 | 0 | $buffer =~ s/$UNPRINTABLE//g; | |||||||||||||||||||||||||||||
| 411 | 0 | $buffer =~ s/\s+/ /g; | |||||||||||||||||||||||||||||
| 412 | 0 | $buffer =~ s/\A\s+|\s+\z//g; | |||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||
| 414 | 0 | return substr($buffer, 0, $config{banner_length}); | |||||||||||||||||||||||||||||
| 415 | 29 | ▶ | } | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||
| 417 | 29 | 1; | |||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||
| 419 | __END__ | ||||||||||||||||||||||||||||||