App-WebPerimeterScan

 view release on metacpan or  search on metacpan

lib/App/WebPerimeterScan/Checks.pm  view on Meta::CPAN

Readonly::Hash my %CORS_SEVERITY => (credentials => 'high', reflected => 'medium', wildcard => 'low');

# Script sources that allow any origin: a bare wildcard or a whole scheme.
Readonly::Hash my %ANY_SOURCE => map { $_ => 1 } ('*', 'http:', 'https:', 'data:');

# A nonce or hash makes CSP2+ browsers ignore 'unsafe-inline'.
Readonly::Scalar my $NONCE_OR_HASH => qr/\A'(?:nonce|sha256|sha384|sha512)-/i;

# Protocol versions deprecated by RFC 8996, worst first.
Readonly::Array my @LEGACY_PROTOCOLS => qw(TLSv1 TLSv1_1);

# Cipher suites that are broken or unauthenticated.
Readonly::Scalar my $WEAK_CIPHER => qr/RC4|3DES|DES-CBC3|NULL|EXPORT|MD5|anon/i;

# Error-page fingerprints, in the order they are listed in a title.
Readonly::Array my @ERROR_MARKERS => (
	[ php    => qr/(?:Fatal error|Parse error|Warning):\s+\S+ in \S+ on line \d+/i ],
	[ perl   => qr/\bat \S+\.p[lm] line \d+/i ],
	[ python => qr/Traceback \(most recent call last\)/ ],
	[ java   => qr/\bat [\w.\$]+\([\w.]+\.java:\d+\)/ ],
	[ sql    => qr/(?:SQL syntax|mysql_fetch|ORA-\d{5}|unclosed quotation mark)/i ],
	[ stack  => qr/Stack trace:/i ],
);
Readonly::Scalar my $ERROR_HINT => qr/(?:Warning|Fatal error):\s+\S+/i;

# Resources a page loads: tag => [ URL attribute, mixed-content class ].
# A link's class depends on its rel, so it is decided separately.
Readonly::Hash my %RESOURCE => (
	script => [ src    => 'active' ],
	iframe => [ src    => 'active' ],
	frame  => [ src    => 'active' ],
	embed  => [ src    => 'active' ],
	object => [ data   => 'active' ],
	form   => [ action => 'active' ],
	img    => [ src    => 'passive' ],
	audio  => [ src    => 'passive' ],
	video  => [ src    => 'passive' ],
	source => [ src    => 'passive' ],
	link   => [ href   => undef ],
);

# Only these link relations load anything.  A stylesheet is blockable
# ("active") mixed content; canonical, alternate and the like load nothing
# and are ignored.
Readonly::Hash my %LINK_REL => (stylesheet => 'active', icon => 'passive');

Readonly::Scalar my $PLAINTEXT_URL => qr{\Ahttp://}i;

# Well-known paths that should never be served.  The match regexp is what
# turns "the server returned 200" into "the file is really there": a SPA that
# answers 200 to everything must not produce twenty findings.  Titles and
# advice are in the message catalogue under each id.
Readonly::Array my @PROBES => (
	{ id => 'git-exposed',             path => '/.git/config',        severity => 'critical', match => qr/\[core\]|repositoryformatversion/, reference => 'https://cwe.mitre.org/data/definitions/538.html' },
	{ id => 'git-head',                path => '/.git/HEAD',          severity => 'high',     match => qr/\Aref:\s+refs\// },
	{ id => 'svn-exposed',             path => '/.svn/entries',       severity => 'high',     match => qr/^\d+\s*$/m },
	{ id => 'env-exposed',             path => '/.env',               severity => 'critical', match => qr/^\s*[A-Z][A-Z0-9_]{2,}\s*=\s*\S/m, reference => 'https://cwe.mitre.org/data/definitions/200.html' },
	{ id => 'htpasswd-exposed',        path => '/.htpasswd',          severity => 'critical', match => qr/^[^:\s]+:\$?(?:apr1|2y|1|5|6|\{SHA\})/m },
	{ id => 'htaccess-exposed',        path => '/.htaccess',          severity => 'medium',   match => qr/<IfModule|RewriteEngine|Options\s+[-+]/i },
	{ id => 'web-config-exposed',      path => '/web.config',         severity => 'high',     match => qr/<configuration/i },
	{ id => 'php-config-backup',       path => '/wp-config.php.bak',  severity => 'critical', match => qr/DB_PASSWORD|DB_NAME|AUTH_KEY/ },
	{ id => 'sql-dump-exposed',        path => '/backup.sql',         severity => 'critical', match => qr/CREATE TABLE|INSERT INTO/i, reference => 'https://cwe.mitre.org/data/definitions/538.html' },
	{ id => 'ds-store-exposed',        path => '/.DS_Store',          severity => 'low',      match => qr/\x00\x00\x00\x01Bud1/ },
	{ id => 'server-status-exposed',   path => '/server-status',      severity => 'high',     match => qr/Apache Server Status|requests currently being processed/i },
	{ id => 'server-info-exposed',     path => '/server-info',        severity => 'medium',   match => qr/Apache Server Information|Server Settings/i },
	{ id => 'phpinfo-exposed',         path => '/phpinfo.php',        severity => 'high',     match => qr/PHP Version|phpinfo\(\)|Configuration File \(php\.ini\)/i },
	{ id => 'spring-actuator-exposed', path => '/actuator/env',       severity => 'critical', match => qr/"propertySources"|"activeProfiles"/ },
	{ id => 'spring-health-exposed',   path => '/actuator/health',    severity => 'low',      match => qr/"status"\s*:\s*"(?:UP|DOWN)"/ },
	{ id => 'composer-json-exposed',   path => '/composer.json',      severity => 'low',      match => qr/"require"\s*:|"autoload"\s*:/ },
	{ id => 'package-json-exposed',    path => '/package.json',       severity => 'low',      match => qr/"dependencies"\s*:|"devDependencies"\s*:/ },
	{ id => 'crossdomain-xml',         path => '/crossdomain.xml',    severity => 'medium',   match => qr/allow-access-from\s+domain\s*=\s*"\*"/ },
);

Readonly::Scalar my $SECURITY_TXT => '/.well-known/security.txt';
Readonly::Scalar my $ROBOTS_TXT   => '/robots.txt';

=encoding utf8

=head1 NAME

App::WebPerimeterScan::Checks - the rule set

=head1 VERSION

Version 0.001.0

=head1 SYNOPSIS

Check one page that you have already fetched:

	use App::WebPerimeterScan::Checks;
	use App::WebPerimeterScan::Findings;
	use App::WebPerimeterScan::HTTP;

	my $http     = App::WebPerimeterScan::HTTP->new;
	my $findings = App::WebPerimeterScan::Findings->new;
	my $url      = 'https://example.com/';

	App::WebPerimeterScan::Checks::check_page($findings,
		host => 'example.com', url => $url, res => $http->fetch($url));

	print "$_->{severity} $_->{title}\n" for @{ $findings->sorted };

Check the TLS service and the open ports:

	App::WebPerimeterScan::Checks::check_tls($findings,
		host => 'example.com',
		tls  => App::WebPerimeterScan::TLS->inspect(host => 'example.com'));
	App::WebPerimeterScan::Checks::check_ports($findings,
		host => 'example.com',
		scan => App::WebPerimeterScan::Ports->scan(host => 'example.com'));

Test a CORS policy with the probe origin:

	my $origin = App::WebPerimeterScan::Checks::probe_origin();
	my $res = $http->fetch($url, headers => [ "Origin: $origin" ]);
	App::WebPerimeterScan::Checks::check_cors($findings,
		host => 'example.com', url => $url, res => $res);

Make the thresholds stricter, for every later check:

	$App::WebPerimeterScan::Checks::config{tls_expiry_warn_days} = 60;

lib/App/WebPerimeterScan/Checks.pm  view on Meta::CPAN

C<data:>.  A host wildcard such as C<https://*.example.com> is not any
origin and is not reported.  C<'strict-dynamic'> turns the rule off,
because browsers that support it ignore host and scheme sources.

=item * C<csp-unsafe> reports C<'unsafe-inline'> only when the same
directive has no nonce or hash (C<'nonce-...'>, C<'sha256-...'> and so
on), because browsers then ignore C<'unsafe-inline'>.  C<'unsafe-eval'>
is always reported.

=item * Several C<Content-Security-Policy> headers are joined and read as
one policy; see L</LIMITATIONS>.

=back

Probe rules (C<check_cors>, C<check_methods>):

	cors-misconfiguration        high      the probe origin may read responses
	                                       with credentials
	                             medium    the probe origin is reflected
	                             low       Access-Control-Allow-Origin is *
	trace-enabled                medium    OPTIONS lists TRACE
	write-methods-enabled        low       OPTIONS lists PUT, DELETE or PATCH

TLS rules (C<check_tls>):

	tls-unavailable              medium    no TLS handshake was possible
	tls-invalid-certificate      high      the chain or the name does not verify
	tls-self-signed              high      the certificate is self-signed
	tls-expired                  high      the certificate has expired
	tls-expiring                 high      it expires within 14 days
	                             medium    it expires within 30 days
	tls-legacy-protocol          high      TLS 1.0 is accepted
	                             medium    TLS 1.1 is accepted, and not 1.0
	tls-weak-cipher              high      the default cipher is weak (RC4,
	                                       3DES, NULL, EXPORT, MD5 or an
	                                       anonymous suite)

Port rules (C<check_ports>):

	port-open-N                  varies    port N is one of the risky ports in
	                                       %App::WebPerimeterScan::Ports::RISKY;
	                                       critical for 21 (FTP) and 23 (Telnet),
	                                       medium for 5900 (VNC), high for the
	                                       others
	ports-other-open             info      other ports than 80 and 443 are open
	outdated-ssh-banner          medium    an OpenSSH banner is older than 9.0
	portscan-failed              info      the port scan could not run

Path rules (C<check_paths>).  Each probe is reported only when the path
answers 200, the body looks like the real file (each probe has its own
pattern), and, when a C<baseline> is given, the response differs from the
server's answer for a path that does not exist:

	git-exposed                  critical  /.git/config
	git-head                     high      /.git/HEAD
	svn-exposed                  high      /.svn/entries
	env-exposed                  critical  /.env
	htpasswd-exposed             critical  /.htpasswd
	htaccess-exposed             medium    /.htaccess
	web-config-exposed           high      /web.config
	php-config-backup            critical  /wp-config.php.bak
	sql-dump-exposed             critical  /backup.sql
	ds-store-exposed             low       /.DS_Store
	server-status-exposed        high      /server-status
	server-info-exposed          medium    /server-info
	phpinfo-exposed              high      /phpinfo.php
	spring-actuator-exposed      critical  /actuator/env
	spring-health-exposed        low       /actuator/health
	composer-json-exposed        low       /composer.json
	package-json-exposed         low       /package.json
	crossdomain-xml              medium    /crossdomain.xml
	security-txt-present         info      /.well-known/security.txt exists
	                                       and has a Contact: line
	robots-disclosure            info      robots.txt lists Disallow paths

=head1 COMMON PITFALLS

=over 4

=item * B<These are functions, not methods.>  Call
C<App::WebPerimeterScan::Checks::check_page($findings, ...)>.  The findings
collection is always the first argument.

=item * B<C<check_paths> sends requests; the scope is not checked here.>
It requests about twenty paths on C<base>.  Check that the host is in
scope before you call it.

=item * B<A failed response is not a finding.>  When C<res> has an
C<error>, the page, CORS and OPTIONS checks add nothing.  A host that
could not be reached is reported by the runner, not here.

=item * B<A header rule is reported once for each host.>  C<check_page>
records header findings such as C<csp-missing> with an empty C<url>.  So
calling it for ten pages that all lack the header gives one finding, not
ten.  It also means that a page that I<has> the header does not cancel
the finding from a page that lacks it.

=item * B<Pass a CORS response from the probe origin.>  C<check_cors>
judges the response to a request that sent
C<Origin: https://ws-probe.invalid> (from C<probe_origin>).  A response to
a normal request proves nothing.

=item * B<C<%config> is shared.>  A change to
C<%App::WebPerimeterScan::Checks::config> affects every later check in the
program.  Use C<local> to change it for one block only.

=item * B<Leave C<tls> or C<scan> out when there is nothing to judge.>
When the argument is missing, C<check_tls> and C<check_ports> add nothing.
An empty hash is different for C<check_tls>: it has no C<ok>, so it is
read as a failed handshake and C<tls-unavailable> is reported.
C<portscan-failed> is reported only when the scan result has an C<error>.

=back

=head1 ENCODING

=over 4

=item * B<Page bodies are bytes.>  The checks search the raw bytes, and the
markers they look for (headers, tag names, error messages) are ASCII.  A
page in any character set, including UTF-8 with emoji, is checked
correctly.



( run in 0.819 second using v1.01-cache-2.11-cpan-99fa8243a42 )