| 1 | package HTML::Form::XSS; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =pod | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | HTML::Form::XSS - Test HTML forms for cross site scripting vulnerabilities. | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | use HTML::Form::XSS; | ||||||||||||||||||||||||||||
| 12 | use WWW::Mechanize; | ||||||||||||||||||||||||||||
| 13 | my $mech = WWW::Mechanize->new(); | ||||||||||||||||||||||||||||
| 14 | my $checker = HTML::Form::XSS->new($mech, config => '../root/config.xml'); | ||||||||||||||||||||||||||||
| 15 | $mech->get("http://www.site.com/pagewithform.html"); | ||||||||||||||||||||||||||||
| 16 | my @forms = $mech->forms(); | ||||||||||||||||||||||||||||
| 17 | foreach my $form (@forms){ | ||||||||||||||||||||||||||||
| 18 | my @results = $checker->do_audit($form); | ||||||||||||||||||||||||||||
| 19 | foreach my $result (@results){ | ||||||||||||||||||||||||||||
| 20 | if($result->vulnerable()){ | ||||||||||||||||||||||||||||
| 21 | my $example = $result->example(); | ||||||||||||||||||||||||||||
| 22 | print "Example of vulnerable URL: $example\n"; | ||||||||||||||||||||||||||||
| 23 | last; | ||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | Provides a simple way to test HTML forms for cross site scripting (XSS) | ||||||||||||||||||||||||||||
| 31 | vulnerabilities. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | Checks to perform are given in a XML config file with the results of each | ||||||||||||||||||||||||||||
| 34 | test returned. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 METHODS | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =cut | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 3 | ▶ | use XML::Simple; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 3 | ▶ | use HTML::Form::XSS::Result; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 3 | ▶ | use parent qw(HTML::XSSLint); #we use this module as a base | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 3 | our $VERSION = 1.00; | |||||||||||||||||||||||||||
| 46 | 3 | my $BROWSER = 'Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'; #emulate MS IE | |||||||||||||||||||||||||||
| 47 | ################################### | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =pod | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =head2 new() | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | my $mech = WWW::Mechanize->new(); | ||||||||||||||||||||||||||||
| 54 | my $checker = HTML::Form::XSS->new($mech, config => '../root/config.xml'); | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | Creates a new HTML::Form::XSS object using two required parameters. Firstly a | ||||||||||||||||||||||||||||
| 57 | <WWW::Mechanize> or compatible object, secondly the path to the XML config file. | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | Please see the example config.xml included in this distribution for details. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =cut | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | ################################### | ||||||||||||||||||||||||||||
| 64 | 1 | ▶ | sub new{ | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=12.3
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 1 | my($class, $mech, %params) = @_; | |||||||||||||||||||||||||||
| 66 | 1 | ▶ | if($mech){ #we need this someday | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 1 | ▶ | if(defined($params{'config'})){ #how can we setup without this | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | my $self = { | ||||||||||||||||||||||||||||
| 69 | '_mech' => $mech, | ||||||||||||||||||||||||||||
| 70 | 1 | '_configFile' => $params{'config'} | |||||||||||||||||||||||||||
| 71 | }; | ||||||||||||||||||||||||||||
| 72 | 1 | bless $self, $class; | |||||||||||||||||||||||||||
| 73 | 1 | $self->_loadConfig(); | |||||||||||||||||||||||||||
| 74 | 1 | return $self; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | else{ | ||||||||||||||||||||||||||||
| 77 | 0 | die("No Config file option given"); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | else{ | ||||||||||||||||||||||||||||
| 81 | 0 | die("No WWW::Mechanize compatible object given"); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 0 | return undef; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | ################################### | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | sub make_params { #passing a check value here, so we can do many checks | ||||||||||||||||||||||||||
|
Subroutine
make_params: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 87 | 0 | my($self, $check, @inputs) = @_; | |||||||||||||||||||||||||||
| 88 | 0 | my %params; | |||||||||||||||||||||||||||
| 89 | 0 | foreach my $input (@inputs){ | |||||||||||||||||||||||||||
| 90 | 0 | ▶ | if(defined($input->name()) && length($input->name())){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $input->name and length $input->name
MC/DC0%0/2
defined $input->name and length $input->name
defined $input->name
length $input->name
Truth table0%0/3
A and B
A= defined $input->nameB= length $input->name
| |||||||||||||||||||||||||||||
| 91 | 0 | my $value = $self->random_string(); | |||||||||||||||||||||||||||
| 92 | 0 | $params{$input->name()} = $check . $value; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 0 | return \%params; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | ################################### | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =pod | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =head2 do_audit() | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | my @results = $checker->do_audit($form); | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | Using the provided <HTML::Form> object the form is tested for all the | ||||||||||||||||||||||||||||
| 106 | XSS attacks in the XML config file. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | An array of <HTML::Form::XSS::Result> objects are returned, one for | ||||||||||||||||||||||||||||
| 109 | each check. | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =cut | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | ####################################################### | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | sub do_audit { #we do many checks here not just one | ||||||||||||||||||||||||||
|
Subroutine
do_audit: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | 0 | my($self, $form) = @_; | |||||||||||||||||||||||||||
| 116 | 0 | my @results; | |||||||||||||||||||||||||||
| 117 | 0 | print "Checking...\n"; | |||||||||||||||||||||||||||
| 118 | 0 | foreach my $check ($self->_getChecks()){ | |||||||||||||||||||||||||||
| 119 | 0 | my $params = $self->make_params($check, $form->inputs); | |||||||||||||||||||||||||||
| 120 | 0 | my $request = $self->fillin_and_click($form, $params); | |||||||||||||||||||||||||||
| 121 | 0 | $request->header('User-Agent' => $BROWSER); | |||||||||||||||||||||||||||
| 122 | 0 | my $response = $self->request($request); | |||||||||||||||||||||||||||
| 123 | 0 | print "Status: " . $response->code() . "\n"; | |||||||||||||||||||||||||||
| 124 | 0 | ▶ | $response->is_success or die("Can't fetch " . $form->action); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | my @names = $self->compare($response->content, $params); | |||||||||||||||||||||||||||
| 126 | 0 | my $result = HTML::Form::XSS::Result->new( #using are modified result class | |||||||||||||||||||||||||||
| 127 | form => $form, | ||||||||||||||||||||||||||||
| 128 | names => \@names, | ||||||||||||||||||||||||||||
| 129 | check => $check | ||||||||||||||||||||||||||||
| 130 | ); | ||||||||||||||||||||||||||||
| 131 | 0 | push(@results, $result); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 0 | print "\n"; | |||||||||||||||||||||||||||
| 134 | 0 | return @results; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | ################################### | ||||||||||||||||||||||||||||
| 137 | 0 | ▶ | sub compare{ #we need to make the patterns regex safe | ||||||||||||||||||||||||||
|
Subroutine
compare: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 138 | 0 | my($self, $html, $params) = @_; | |||||||||||||||||||||||||||
| 139 | 0 | my @names; | |||||||||||||||||||||||||||
| 140 | 0 | foreach my $param (keys(%{$params})){ | |||||||||||||||||||||||||||
| 141 | 0 | my $pattern = $self->_makeRegexpSafe($params->{$param}); | |||||||||||||||||||||||||||
| 142 | 0 | ▶ | if($html =~ m/$pattern/){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | push(@names, $param); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | 0 | return @names; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | ################################### | ||||||||||||||||||||||||||||
| 149 | # | ||||||||||||||||||||||||||||
| 150 | #private methods | ||||||||||||||||||||||||||||
| 151 | # | ||||||||||||||||||||||||||||
| 152 | ################################### | ||||||||||||||||||||||||||||
| 153 | 0 | ▶ | sub _getChecks{ | ||||||||||||||||||||||||||
|
Subroutine
_getChecks: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 154 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 155 | 0 | my $config = $self->_getConfig(); | |||||||||||||||||||||||||||
| 156 | 0 | my $checks = $config->{'checks'}->{'check'}; | |||||||||||||||||||||||||||
| 157 | 0 | return @{$checks}; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | ################################### | ||||||||||||||||||||||||||||
| 160 | 1 | ▶ | sub _getConfigFile{ | ||||||||||||||||||||||||||
|
Subroutine
_getConfigFile: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 161 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 162 | 1 | return $self->{'_configFile'}; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | ################################### | ||||||||||||||||||||||||||||
| 165 | 0 | ▶ | sub _getConfig{ | ||||||||||||||||||||||||||
|
Subroutine
_getConfig: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 166 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 167 | 0 | return $self->{'_config'}; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | ################################### | ||||||||||||||||||||||||||||
| 170 | 1 | ▶ | sub _loadConfig{ | ||||||||||||||||||||||||||
|
Subroutine
_loadConfig: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 171 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 172 | 1 | my $file = $self->_getConfigFile(); | |||||||||||||||||||||||||||
| 173 | 1 | my $simple = XML::Simple->new(); | |||||||||||||||||||||||||||
| 174 | 1 | my $ref = $simple->XMLin($file); | |||||||||||||||||||||||||||
| 175 | 1 | $self->{'_config'} = $ref; | |||||||||||||||||||||||||||
| 176 | 1 | return 1; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | ################################### | ||||||||||||||||||||||||||||
| 179 | 0 | ▶ | sub _makeRegexpSafe{ | ||||||||||||||||||||||||||
|
Subroutine
_makeRegexpSafe: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 180 | 0 | my($self, $pattern) = @_; | |||||||||||||||||||||||||||
| 181 | 0 | $pattern =~ s/([\(\)])/\\$1/g; #add back slashes where required | |||||||||||||||||||||||||||
| 182 | 0 | return $pattern; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | ################################### | ||||||||||||||||||||||||||||
| 185 | 0 | ▶ | sub _getMech{ | ||||||||||||||||||||||||||
|
Subroutine
_getMech: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 186 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 187 | 0 | return $self->{'_mech'}; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | ################################### | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =pod | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | L<WWW::Mechanize|WWW::Mechanize>, | ||||||||||||||||||||||||||||
| 196 | L<HTML::Form|HTML::Form>, | ||||||||||||||||||||||||||||
| 197 | L<HTML::XSSLint|HTML::XSSLint> | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | MacGyveR <dumb@cpan.org> | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | Development questions, bug reports, and patches are welcome to the above address | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | Copyright (c) 2016 MacGyveR. All rights reserved. | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =cut | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | #################################################### | ||||||||||||||||||||||||||||
| 214 | 3 | return 1; | |||||||||||||||||||||||||||