How to detect XSS Reflect with Perl

Viewed 65

I created this simple script to request a URL with a JavaScript payload.

#!/usr/bin/perl
use LWP::UserAgent;
use HTTP::Request;
use LWP::Simple;

my $req=HTTP::Request->new(GET=>"http://example.com.br/search.asp?CI=%27%3E%3Cscript%3Ealert('XSS')%3C/script%3E");
my $ua=LWP::UserAgent->new();
$ua->timeout(15);
my $res=$ua->request($req);
print $res->content;

How can I identify if there is an XSS vulnerability in this example?

1 Answers

To quickly answer how it could work for your particular example:

#!/usr/bin/perl
use strict;

my $url = "http://example.com.br/search.asp?CI=%27%3E%3Cscript%3Ealert('XSS')%3C/script%3E";

#### decode url-encoding
my $decoded_url = $url=~s/%([A-F\d]{2})/chr(hex($1))/iegr;

if ( $decoded_url=~/<script>.*<\/script>/ ) {
    print "XSS detected: $& \n";   
}

You'd get:

$ ./xssdetect.pl
XSS detected: <script>alert('XSS')</script>

Caveat: This alone is no XSS protection of real-world use. XSS is a complicated topic with lots of irregularities and special rules. I suggest reading up on XSS, for example at OWASP.

Related