App-WebPerimeterScan

 view release on metacpan or  search on metacpan

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


The remaining prerequisites are C<autodie> with C<IPC::System::Simple>,
C<Getopt::Long> 2.50, C<HTML::Parser> 3.71, C<HTML::SimpleLinkExtor> 1.20,
C<JSON::PP> 4.00, C<Readonly> 2.05, C<Time::HiRes> 1.97 and C<URI> 1.70, plus
C<Test::More> 1.302 for the test suite.

=head1 USAGE

	bin/webperimeter --scope scope.txt --authorized

The scope file lists the hosts you are allowed to test, one per line.  Without
C<--authorized> the tool refuses to send any traffic.  Add C<--docroot DIR> and
C<--source DIR> to resolve findings back to the file on disk and the code that
serves them.

=head1 QUICK TEST

To check the tool works where you installed it, scan example.com.  RFC 2606
reserves the name for documentation and it is always available, so it makes a
stable target.  It is not a licence to scan other people's hosts: the scope
file is still the boundary.

	$ printf 'example.com\n' > scope.txt
	$ bin/webperimeter --scope scope.txt --authorized --ports 80,443 \
	    --max-urls 1 --no-path-probes --no-color

	webperimeter 0.001.0  2026-11-06T01:05:02Z
	Scope: scope.txt
	Requests: 4

	============================================================
	example.com
	------------------------------------------------------------
	  TLS: TLSv1_3, TLS_AES_256_GCM_SHA384, expires Dec 25 22:56:35 2026 GMT (49 days)
	  Certificate: /CN=example.com issued by /C=US/O=SSL Corporation/CN=Cloudflare TLS Issuing ECC CA 3
	  Open ports: 80, 443
	  URLs scanned: 1


	MEDIUM (4)
	----------
	[csp-missing] Content-Security-Policy is missing
	  Evidence: No Content-Security-Policy header on https://example.com/

	[...]

	Summary: 0 critical, 0 high, 4 medium, 2 low, 1 info

C<--max-urls 1> holds the crawl to the front page and C<--no-path-probes>
skips the sensitive path checks, so the run costs four requests.  Leave the
probes on when the target is a deployment of your own.

The findings are the point of the exercise.  example.com is a good smoke test
because it is not hardened: a run reporting missing CSP, HSTS and nosniff
shows the checks are wired up, and a run that reports nothing - or a critical
finding - means the scan is broken, not the site.

=head1 EXAMPLE

A run against a local application, with its source tree supplied so the
report can name the endpoints it never reached.  The output is trimmed in the
middle for length.

	$ echo localhost:3000 > scope.txt
	$ bin/webperimeter --scope scope.txt --authorized --allow-private --ports 3000 \
	    --source ~/src/njh/Database-BI --no-path-probes --max-urls 6 --delay 0.2 --no-color

	webperimeter 0.001.0  2026-11-06T01:05:47Z
	Scope: scope.txt
	Requests: 8

	============================================================
	localhost
	------------------------------------------------------------
	  Open ports: 3000
	  URLs scanned: 6


	LOW (1)
	-------
	[port-open-3000] Port 3000 accepts connections from outside the host
	  Evidence: TCP connect succeeded, no banner


	INFO (1)
	--------
	[source-routes-unvisited] 71 endpoints defined in source were not scanned
	  Evidence: ~/src/njh/Database-BI/lib/Database/BI.pm:2713 GET /login; ~/src/njh/Database-BI/lib/Database/BI.pm:2714 POST /login; ~/src/njh/Database-BI/lib/Database/BI.pm:2715 POST /logout; [...]
	  Fix: Name them explicitly, raise --max-urls, or review them by hand.

	Summary: 0 critical, 0 high, 0 medium, 1 low, 1 info

That application sends C<style-src 'self' 'unsafe-inline'> with a
nonce-based C<script-src>.  Only the script sources are judged, so the
inline-style allowance is not reported as a cross-site scripting gap.

C<--allow-private> is required for a loopback or private target; without it
the host is refused before any traffic is sent, and the refusal is reported
as a high-severity C<scan-error> so the exit status shows that nothing was
scanned.  The exit status is 0 here because C<--fail-on> defaults to
C<high> and nothing reached it.

C<--no-path-probes> skips the well-known sensitive path checks, which ask
whether a deployed document root exposes files such as C</.git/config>.  The
target here is local, so those paths can be inspected directly on disk - with
C<--docroot>, or by reading the tree - and the probes would only add requests.
Against a real deployment, leave them on.

C<source-routes-unvisited> is the reason to pass C<--source>: 71 routes exist in
the code that no crawled page linked to, so none of the checks above saw how
they behave.  That is exactly the surface a link-following scan cannot reach.
Raise C<--max-urls>, or review them by hand.

Findings also carry C<File:> and C<In source:> lines when C<--docroot> or
C<--source> lets the scan resolve them.

=head1 REPORT FORMAT

C<--format json> records the response header block behind each header finding,
so a report can be audited later without repeating the requests.  Abridged to
one header:

	"response": {
	   "headers" : {
	      "content-security-policy" : [ "default-src 'self'; script-src 'self' 'nonce-...' https://cdnjs.cloudflare.com https://unpkg.com; ..." ]
	   },
	   "http_version" : "HTTP/1.1",
	   "method" : "GET",
	   "note" : "main page",
	   "raw" : "HTTP/1.1 200 OK\r\nServer: ...",
	   "reason" : "OK",
	   "status" : 200,
	   "url" : "http://localhost:3000/"
	},
	"responses" : [ ... ]

A header appears once per value, so a second C<Content-Security-Policy> or
C<Set-Cookie> is not lost.  C<raw> is the block exactly as it arrived.
C<responses> holds the same structure for the CORS and C<OPTIONS> probes, which
is where the CORS and method findings come from.

=head1 SUBROUTINES/METHODS

=head2 new

Purpose: configure a scan.

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

# _path_probes
# Purpose:     the sensitive-path probes, against a not-found baseline.
# Entry:       $report has a base URL.
# Exit:        $self.
# Side effects: requests; adds findings.
sub _path_probes :Private
{
	my ($self, $report) = @_;

	my ($host, $base) = @{$report}{qw(host base)};

	# A path that certainly does not exist.  Its fingerprint tells us whether
	# this application answers 200 for everything, in which case a 200 on
	# /.git/config proves nothing.
	my $path = '/ws-baseline-' . _random_token() . '.html';
	my $miss = $self->{http}->fetch(($base =~ s{/+\z}{}r) . $path);

	$self->_report('catch-all-200', host => $host, vars => { path => $path })
		if !$miss->{error} && ($miss->{status} // 0) == $HTTP_OK;

	App::WebPerimeterScan::Checks::check_paths($self->{findings},
		http     => $self->{http},
		host     => $host,
		base     => $base,
		baseline => App::WebPerimeterScan::Checks::fingerprint($miss),
	);

	return $self;
}

# _reflection_probe
# Purpose:     opt-in: send one harmless marker in a query parameter and see
#              whether it comes back unencoded.  A hint for a human, not a
#              proof of cross-site scripting.
# Entry:       $report has a base URL.
# Exit:        $self.
# Side effects: one request; may add reflected-input.
sub _reflection_probe :Private
{
	my ($self, $report) = @_;

	my $marker = 'wsp' . _random_token();
	my $sent = "$REFLECTION_PARAMETER=$marker$REFLECTION_ENCODED";

	my $uri = URI->new($report->{base});
	$uri->query(join '&', grep { defined && length } $uri->query, $sent);

	my $res = $self->{http}->fetch($uri->as_string);
	my $reflected = !$res->{error} && index($res->{body} // '', $marker . $REFLECTION_RAW) >= 0;

	$self->_report('reflected-input',
		host => $report->{host}, url => $uri->as_string, vars => { parameter => $sent },
	) if $reflected;

	return $self;
}

# _source_pass
# Purpose:     the source-aware pass.  A black-box scan can only say that a
#              URL answered; given a docroot and a source tree it can also
#              say which file is served and which line defines the endpoint.
# Entry:       the scan has finished.
# Exit:        $self.
# Side effects: annotates findings; may add source-file-in-docroot.
sub _source_pass :Private
{
	my ($self) = @_;

	my $source = $self->{source};
	return $self unless $source;

	for my $finding (@{ $self->{findings}->all }) {
		my $path = _url_path($finding->{url}) // next;
		my $file  = $source->file_for($path);
		my $route = $source->route_for($path);

		$self->{findings}->annotate($finding,
			($file  ? (file   => $file->{path}) : ()),
			($route ? (source => "$route->{file}:$route->{line}") : ()),
		);
	}

	return $self->_docroot_files($source);
}

# _docroot_files
# Purpose:     sensitive files present under a docroot but not served.
# Entry:       $source has docroots.
# Exit:        $self.
# Side effects: may add source-file-in-docroot, keyed on the file so
#              several probes cannot repeat it.
sub _docroot_files :Private
{
	my ($self, $source) = @_;

	# Only the serious probes, and only those the wire did not already find.
	my %served = map { $_->{id} => 1 } @{ $self->{findings}->all };
	my @wanted = grep { $DOCROOT_SEVERITY{ $_->{severity} } && !$served{ $_->{id} } }
		@{ App::WebPerimeterScan::Checks::sensitive_probes() };

	for my $hit (@{ $source->sensitive_on_disk(\@wanted) }) {
		$self->_report('source-file-in-docroot',
			key      => $hit->{path},
			severity => $DOCROOT_SEVERITY{ $hit->{probe}{severity} },
			evidence => $hit->{path},
			vars     => { file => $hit->{relative} },
		);
	}

	return $self;
}

# _unvisited_routes
# Purpose:     the routes in the source that the scan never requested: the
#              coverage gap a link-following scan cannot see.
# Entry:       $self->{source} is set; the crawl and probes are done.
# Exit:        $self.
# Side effects: may add source-routes-unvisited.
sub _unvisited_routes :Private
{
	my ($self, $report) = @_;



( run in 1.190 second using v1.01-cache-2.11-cpan-eb134014566 )