App-WebPerimeterScan
view release on metacpan or search on metacpan
lib/App/WebPerimeterScan/Ports.pm view on Meta::CPAN
# Side effects: opens and closes one socket per port.
sub _connect_parallel :Private
{
my ($ip, $proto, $ports, $timeout) = @_;
my (%pending, @open);
my $select = IO::Select->new;
# Start every connect. Most return EINPROGRESS and are resolved by
# the select loop below; a loopback connect may finish at once.
for my $port (@{$ports}) {
socket(my $sock, AF_INET, SOCK_STREAM, $proto) or next;
my $flags = fcntl($sock, Fcntl::F_GETFL(), 0) // 0;
fcntl($sock, Fcntl::F_SETFL(), $flags | Fcntl::O_NONBLOCK());
if(connect($sock, sockaddr_in($port, $ip))) {
push @open, _open_port($port);
close $sock;
} elsif($! == EINPROGRESS || $! == EWOULDBLOCK) {
$pending{ fileno $sock } = { sock => $sock, port => $port };
$select->add($sock);
} else {
close $sock; # refused outright
}
}
# A socket becomes writable when its connect finishes either way;
# SO_ERROR says which way.
my $deadline = time + $timeout;
while($select->count && (my $wait = $deadline - time) > 0) {
for my $sock ($select->can_write($wait)) {
my $entry = delete $pending{ fileno $sock } or next;
$select->remove($sock);
push @open, _open_port($entry->{port}) if _handshake_ok($sock);
close $sock;
}
}
# Whatever is still pending was filtered: no answer within the timeout.
close $_->{sock} for values %pending;
return \@open;
}
# _connect_serial
# Purpose: connect to one port at a time, for platforms without fcntl
# flags (Windows). Slower against a firewall, but correct.
# Entry: $host is a name or address.
# Exit: arrayref of open-port records.
# Side effects: opens and closes one socket per port.
sub _connect_serial :Private
{
my ($host, $ports, $timeout) = @_;
my @open;
for my $port (@{$ports}) {
my $sock = IO::Socket::INET->new(
PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp',
Timeout => $timeout,
) or next;
# IO::Socket::INET can hand back a socket whose connect is still in
# progress, so a refused port looks open; wait for it to become
# writable before asking how the handshake ended.
push @open, _open_port($port)
if IO::Select->new($sock)->can_write($timeout) && _handshake_ok($sock);
close $sock;
}
return \@open;
}
# _banner
# Purpose: read whatever a service says first (SSH, SMTP, FTP, MySQL).
# Entry: $port is known to be open.
# Exit: printable, single-line text; '' when the service is silent.
# Side effects: one short TCP connection. Read-only: nothing is sent.
sub _banner :Private
{
my ($host, $port) = @_;
my $sock = IO::Socket::INET->new(
PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp',
Timeout => $config{banner_timeout},
) or return '';
# Silence is a valid answer: many services wait for the client.
my $buffer = '';
sysread($sock, $buffer, $config{banner_bytes})
if IO::Select->new($sock)->can_read($config{banner_timeout});
close $sock;
# One line of printable ASCII, so a banner cannot corrupt a report.
$buffer =~ s/[\r\n]+/ /g;
$buffer =~ s/$UNPRINTABLE//g;
$buffer =~ s/\s+/ /g;
$buffer =~ s/\A\s+|\s+\z//g;
return substr($buffer, 0, $config{banner_length});
}
1;
__END__
=head1 COMMON PITFALLS
=over 4
=item * B<The scope is not checked here.> C<scan> connects to any host it
is given. Call C<< $scope->assert_target_allowed($host) >> first. The
main scanner, L<App::WebPerimeterScan>, always does.
=item * B<Not in C<open> does not mean closed.> A port that refused the
connection and a port behind a firewall that dropped it look the same: both
are missing from C<open>.
=item * B<Check C<error> first.> When the host name cannot be resolved,
C<error> says so and C<open> is empty. An empty C<open> with no C<error>
means that the scan ran and nothing answered.
=item * B<Bad port numbers are dropped without a warning.> Ports outside
1 to 65535 are ignored. If no valid port is left, C<open> is empty.
=item * B<C<timeout> is for the whole batch.> It is not for each port. A
long list on a slow network may need a larger value.
=item * B<The banner is an empty string, not C<undef>, when there is none.>
Many services (HTTP, for example) wait for the client to speak first and
send no greeting. With C<< banner => 0 >> every banner is empty.
=item * B<A banner is a claim, not a fact.> A service can send any text.
Use it as a hint about the software, not as proof of the version.
=item * B<It is a class method.> Call it as
C<< App::WebPerimeterScan::Ports->scan(...) >>. There is no object to
create.
=back
=head1 ENCODING
=over 4
( run in 0.508 second using v1.01-cache-2.11-cpan-234fa220139 )