bin/webperimeter

filter: Statementstmt 98.3% 120 / 122 Branchbran 83.3% 45 / 54 Conditioncond 90.0% 9 / 10 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 20 / 20 Podpod n/a totaltotal 93.8% 198 / 211 CC 13 SCAR 12.8
CC
13
Cov
94%
dry_run
8.2
parse_ports
6.0
BEGIN
2.1
CRAP 3.6
« lib/App/WebPerimeterScan/Findings.pm ↑ Summary lib/App/WebPerimeterScan/Source.pm »
1 #!/usr/bin/env perl
2  
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 21 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 21 ▶use FindBin qw($Bin);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # Run from a checkout without installing: prefer the lib/ beside this script.
10 21 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.6
11 21 ▶ unshift @INC, "$Bin/../lib" if -d "$Bin/../lib/App/WebPerimeterScan";
Branch: 1 branch
BranchTrueFalseTotal
if -d "$main::Bin/../lib/App/WebPerimeterScan" 21 0 21
12 }
13  
14 21 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 21 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 21 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 21 ▶use App::WebPerimeterScan;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 21 ▶use App::WebPerimeterScan::I18N;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 21 ▶use App::WebPerimeterScan::Ports;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 21 ▶use App::WebPerimeterScan::Report;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 21 ▶use App::WebPerimeterScan::Scope;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # -v and -V must stay distinct; Getopt::Long ignores case by default.
25 21 Getopt::Long::Configure(qw(no_ignore_case));
26  
27 # Exit statuses, as documented in the usage text.
28 21 Readonly::Scalar my $EXIT_CLEAN => 0;
29 21 Readonly::Scalar my $EXIT_FINDING => 1;
30 21 Readonly::Scalar my $EXIT_USAGE => 2;
31  
32 21 Readonly::Hash my %FORMAT => (
33 text => \&App::WebPerimeterScan::Report::as_text,
34 json => \&App::WebPerimeterScan::Report::as_json,
35 html => \&App::WebPerimeterScan::Report::as_html,
36 );
37  
38 # --fail-on accepts every severity, plus "never".
39 21 Readonly::Scalar my $NEVER => 'never';
40 21 Readonly::Hash my %FAIL_ON => map { $_ => 1 } @App::WebPerimeterScan::Findings::SEVERITIES, $NEVER;
41  
42 21 Readonly::Scalar my $MIN_PORT => 1;
43 21 Readonly::Scalar my $MAX_PORT => 65_535;
44 21 Readonly::Scalar my $PORT_SEPARATOR => qr/[,\s]+/;
45  
46 21 my %opt = (
47 format => 'text',
48 fail_on => 'high',
49 );
50  
51 GetOptions(
52 'scope|s=s' => \$opt{scope},
53 'authorized|authorised' => \$opt{authorized},
54 'format|f=s' => \$opt{format},
55 'output|o=s' => \$opt{output},
56 'ports=s' => \$opt{ports},
57 'docroot=s@' => \$opt{docroot},
58 'source=s@' => \$opt{source},
59 'timeout=i' => \$opt{timeout},
60 'connect-timeout=i' => \$opt{connect_timeout},
61 'delay=f' => \$opt{delay},
62 'depth|d=i' => \$opt{depth},
63 'max-urls=i' => \$opt{max_urls},
64 5 ▶ 'no-path-probes' => sub { $opt{path_probes} = 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
65 'reflection-probe' => \$opt{reflection},
66 'allow-private' => \$opt{allow_private},
67 'dry-run' => \$opt{dry_run},
68 'fail-on=s' => \$opt{fail_on},
69 'color|colour!' => \$opt{color}, # either spelling, and --no-colour
70 'verbose|v' => \$opt{verbose},
71 'help|h' => \$opt{help},
72 'version|V' => \$opt{version},
73 21 ▶) or usage($EXIT_USAGE);
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("scope|s=s", \$opt{"scope"}, "authorized|authorised", \$opt{"authorized"}, "format|f=s", \$opt{"format"}, "output|o=s", \$opt{"output"}, "ports=s", \$opt{"ports"}, "docroot=s\@", \$opt{"docroot"}, "source=s\@", \$opt{"source"}, "timeout=i", \$opt{"timeout"}, "connect-timeout=i", \$opt{"connect_timeout"}, "delay=f", \$opt{"delay"}, "depth|d=i", \$opt{"depth"}, "max-urls=i", \$opt{"max_urls"}, "no-path-probes", sub { $opt{"path_probes"} = 0; } , "reflection-probe", \$opt{"reflection"}, "allow-private", \$opt{"allow_private"}, "dry-run", \$opt{"dry_run"}, "fail-on=s", \$opt{"fail_on"}, "color|colour!", \$opt{"color"}, "verbose|v", \$opt{"verbose"}, "help|h", \$opt{"help"}, "version|V", \$opt{"version"}) or usage($EXIT_USAGE) 20 1 21
74  
75 20 ▶usage($EXIT_CLEAN) if $opt{help};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"help"} 1 19 20
76  
77 19 ▶if($opt{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"version"}) 2 17 19
78 2 print msg('cli.version', { version => $App::WebPerimeterScan::VERSION }), "\n";
79 2 exit $EXIT_CLEAN;
80 }
81  
82 # Every option is checked before anything is sent: a typo in --fail-on used
83 # to be noticed only after the whole scan had run.
84 17 ▶usage($EXIT_USAGE) unless $opt{scope};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"scope"} 1 16 17
85 16 ▶fail('cli.bad_format') unless $FORMAT{ $opt{format} };
Branch: 1 branch
BranchTrueFalseTotal
unless $FORMAT{$opt{"format"}} 1 15 16
86 15 ▶fail('cli.bad_fail_on') unless $FAIL_ON{ $opt{fail_on} };
Branch: 1 branch
BranchTrueFalseTotal
unless $FAIL_ON{$opt{"fail_on"}} 1 14 15
87  
88 14 my $ports = parse_ports($opt{ports});
89  
90 11 for my $option (qw(docroot source)) {
91 21 ▶ for my $dir (@{ $opt{$option} || [] }) {
Condition100%2/2
$opt{$option} || []
l!l
219
MC/DC100%1/1
$opt{$option} || []
$opt{$option}
Truth table100%2/2
A
A= $opt{$option}
Aresult
11
00
92 2 ▶ fail('cli.not_directory', { option => $option, dir => $dir }) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 2 0 2
93 }
94 }
95  
96 9 ▶my $scope = eval {
Branch: 1 branch
BranchTrueFalseTotal
my $scope = eval { do { "App::WebPerimeterScan::Scope"->new("allow_private", $opt{"allow_private"} ? 1 : 0)->load_file($opt{"scope"}) } } or fail_with($@) 8 1 9
97 9 ▶ App::WebPerimeterScan::Scope->new(allow_private => $opt{allow_private} ? 1 : 0)->load_file($opt{scope});
Branch: 1 branch
BranchTrueFalseTotal
$opt{'allow_private'} ? : 5 4 9
98 } or fail_with($@);
99  
100 8 ▶if($opt{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"dry_run"}) 1 7 8
101 1 dry_run($scope, $ports);
102 1 exit $EXIT_CLEAN;
103 }
104  
105 7 ▶unless($opt{authorized}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"authorized"}) 1 6 7
106 1 print STDERR msg('cli.unauthorized');
107 1 exit $EXIT_USAGE;
108 }
109  
110 # Only the options actually given are passed, so the defaults live in one
111 # place: %App::WebPerimeterScan::config.
112 my %scan_options = (
113 scope => $scope,
114 ports => $ports,
115 docroots => $opt{docroot},
116 source_dirs => $opt{source},
117 36 ▶ (map { defined $opt{$_} ? ($_ => $opt{$_}) : () } qw(timeout connect_timeout delay depth max_urls path_probes)),
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{$_} ? : 16 20 36
118 6 ▶ (map { $opt{$_} ? ($_ => 1) : () } qw(reflection verbose)),
Branch: 1 branch
BranchTrueFalseTotal
$opt{$_} ? : 0 12 12
119 );
120 6 delete @scan_options{ grep { !defined $scan_options{$_} } keys %scan_options };
121  
122 6 my $scanner = App::WebPerimeterScan->new(%scan_options);
123 6 my $findings = $scanner->run;
124  
125 my $rendered = $FORMAT{ $opt{format} }->($findings,
126 version => "$App::WebPerimeterScan::VERSION",
127 generated => strftime('%Y-%m-%dT%H:%M:%SZ', gmtime),
128 scope_source => $scope->source,
129 requests => $scanner->http->requests,
130 hosts => $scanner->host_reports,
131 # Colour is for a terminal; a file or a pipe gets plain text.
132 6 ▶ (defined $opt{color} ? (color => $opt{color} ? 1 : 0) : $opt{output} ? (color => 0) : ()),
Branch: 3 branches
BranchTrueFalseTotal
defined $opt{'color'} ? : 3 3 6
$opt{'color'} ? : 1 2 3
$opt{'output'} ? : 1 2 3
133 );
134  
135 6 ▶if($opt{output}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'output'}) { } 1 5 6
136 # autodie reports a failed open or close with the file name and reason.
137 1 open my $fh, '>', $opt{output};
138 1 print {$fh} $rendered;
139 1 close $fh;
140 1 ▶ print msg('cli.written', { file => $opt{output} }), "\n" unless $opt{verbose};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"verbose"} 1 0 1
141 } else {
142 5 print $rendered;
143 }
144  
145 6 ▶exit $EXIT_CLEAN if $opt{fail_on} eq $NEVER;
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"fail_on"} eq $NEVER 4 2 6
146 2 ▶exit(@{ $findings->at_or_above($opt{fail_on}) } ? $EXIT_FINDING : $EXIT_CLEAN);
Branch: 1 branch
BranchTrueFalseTotal
@{$findings->at_or_above($opt{'fail_on'});} ? : 2 0 2
147  
148 # msg
149 # Purpose: catalogue text for the command line.
150 # Entry: as App::WebPerimeterScan::I18N::i18n.
151 # Exit: the rendered string.
152 # Side effects: none.
153 sub msg
154 25 ▶{
Subroutine msg: called CC=1 SCAR=0.0
155 25 my ($key, $args) = @_;
156  
157 25 return App::WebPerimeterScan::I18N->i18n($key, $args);
158 }
159  
160 # fail
161 # Purpose: report a usage or setup error and exit 2.
162 # Entry: $key is a catalogue key.
163 # Exit: never returns.
164 # Side effects: prints to STDERR; exits.
165 sub fail
166 7 ▶{
Subroutine fail: called CC=1 SCAR=0.0
167 7 my ($key, $args) = @_;
168  
169 7 print STDERR msg('cli.error', { error => msg($key, $args) }), "\n";
170 7 exit $EXIT_USAGE;
171 }
172  
173 # fail_with
174 # Purpose: report an exception as a setup error and exit 2.
175 # Entry: $error is $@.
176 # Exit: never returns.
177 # Side effects: prints to STDERR; exits.
178 sub fail_with
179 1 ▶{
Subroutine fail_with: called CC=1 SCAR=0.0
180 1 my ($error) = @_;
181  
182 1 print STDERR msg('cli.error', { error => App::WebPerimeterScan::I18N::clean_error($error) }), "\n";
183 1 exit $EXIT_USAGE;
184 }
185  
186 # parse_ports
187 # Purpose: turn --ports into a list of port numbers.
188 # Entry: $list is the option value or undef.
189 # Exit: arrayref of integers, or undef for the default list.
190 # Side effects: exits 2 on a value that is not a port number.
191 sub parse_ports
192 14 ▶{
Subroutine parse_ports: called CC=6 SCAR=17.9
193 14 my ($list) = @_;
194  
195 14 ▶ return undef unless defined $list;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $list 4 10 14
196  
197 10 my @ports = grep { length } split $PORT_SEPARATOR, $list;
198 10 for my $port (@ports) {
199 12 ▶ fail('cli.bad_port', { port => $port })
Branch: 1 branch
BranchTrueFalseTotal
unless $port =~ /\A\d+\z/ and $port >= $MIN_PORT and $port <= $MAX_PORT 3 9 12
Condition100%6/6
$port =~ /\A\d+\z/ and $port >= $MIN_PORT and $port <= $MAX_PORT
!ll&&!rl&&r
219
$port =~ /\A\d+\z/ and $port >= $MIN_PORT
!ll&&!rl&&r
1110
MC/DC100%3/3
$port =~ /\A\d+\z/ and $port >= $MIN_PORT and $port <= $MAX_PORT
$port =~ /\A\d+\z/ $port >= $MIN_PORT $port <= $MAX_PORT
Truth table100%4/4
A and B and C
A= $port =~ /\A\d+\z/B= $port >= $MIN_PORTC= $port <= $MAX_PORT
ABCresult
0XX0
10X0
1100
1111
200 unless $port =~ /\A\d+\z/ && $port >= $MIN_PORT && $port <= $MAX_PORT;
201 }
202  
203 7 return [ map { $_ + 0 } @ports ];
204 }
205  
206 # dry_run
207 # Purpose: show what would be scanned without sending anything.
208 # Entry: $scope is loaded; $ports from parse_ports.
209 # Exit: nothing useful.
210 # Side effects: prints to STDOUT; DNS lookups for the private check.
211 sub dry_run
212 1 ▶{
Subroutine dry_run: called CC=7 SCAR=21.1
213 1 my ($scope, $ports) = @_;
214  
215 1 print msg('cli.dry_run.header'), "\n";
216 1 for my $entry (@{ $scope->hosts }) {
217 1 ▶ my $where = $entry->{port} ? "$entry->{host}:$entry->{port}" : $entry->{host};
Branch: 1 branch
BranchTrueFalseTotal
$entry->{'port'} ? : 0 1 1
218 1 ▶ my $key = $scope->is_private_target($entry->{host}) ? 'cli.dry_run.private' : 'cli.dry_run.host';
Branch: 1 branch
BranchTrueFalseTotal
$scope->is_private_target($entry->{'host'}) ? : 1 0 1
219 1 print msg($key, { where => $where }), "\n";
220 }
221  
222 1 ▶ print "\n", msg('cli.dry_run.ports', { ports => join(',', @{ $ports || \@App::WebPerimeterScan::Ports::DEFAULT_PORTS }) }), "\n";
Condition50%1/2
$ports || \@App::WebPerimeterScan::Ports::DEFAULT_PORTS
l!l
10
MC/DC0%0/1
$ports || \@App::WebPerimeterScan::Ports::DEFAULT_PORTS
$ports
Truth table50%1/2
A
A= $ports
Aresult
11
00
223 0 ▶ print msg('cli.dry_run.docroots', { dirs => join(', ', @{ $opt{docroot} }) }), "\n" if $opt{docroot};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"docroot"} 0 1 1
224 0 ▶ print msg('cli.dry_run.source', { dirs => join(', ', @{ $opt{source} }) }), "\n" if $opt{source};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"source"} 0 1 1
225  
226 1 return;
227 }
228  
229 # usage
230 # Purpose: print the usage text and exit.
231 # Entry: $status is the exit status.
232 # Exit: never returns.
233 # Side effects: prints to STDERR; exits.
234 sub usage
235 3 ▶{
Subroutine usage: called CC=1 SCAR=0.0
236 3 my ($status) = @_;
237  
238 3 print STDERR msg('cli.usage');
239 3 exit $status;
240 }
241  
242 __END__
« lib/App/WebPerimeterScan/Findings.pm ↑ Summary lib/App/WebPerimeterScan/Source.pm »