236
  5   #ˆl–Ü4ý( ©¤ýÔâ‚fà�pS�ÿ�þB g�}ñ€ Ž?ç¿ƒh¹ÉÁÆ      #236
  W   %ˆl–Ým_J	õ4Å8 ]¸\jbÑ¿�þY­,ãE#hmùÀ„›d_Ê_ˆI|¥�+V[(a–ßi~”Ji?u8 pÜ*bÑ¿É 0ô    %#!/usr/bin/perl -T

#############################################
# -=[ Virtual Patching Converter Script ]=- #
#      Converts OWASP ZAP XML Output         #
#   https://code.google.com/p/zaproxy/      #
#                                           #
#             zap2modsec.pl                 #
#             Version: 1.0                  #
#                                           #
#            Copyright 2011                 #
#   Trustwave's SpiderLabs Research Team    #
#           www.trustwave.com               #
#                                           #
#   Based On Code Originally Created by:    #
#            The Denim Group                #
#          www.denimgroup.com               #
#############################################

use XML::Smart;
use Switch;
use Data::Types qw(:all);
use Data::Validate::URI qw(is_uri);
use Getopt::Std;
use Acme::Comment type=>'C++', one_line=>1; #Block commenting, can be removed later

#############
# Variables #
#############

# [Configuration Vars]
my %param;
getopt("f",\%param);
$filename = $param{f};
my $all_vulnerabilities_filename = "$filename";

unless ($filename) {
    print "Flag:\n\n\t -f:\t path to ZAP xml report file\nUsage:\n\n\t./zap2modsec.pl -f ./zap_report.xml\n\n";
    exit;
}


my $modsec_rules_file = "./modsecurity_crs_48_virtual_patches.conf";

# [End Config Vars]

my $VULN_CLASS_XSS = "Cross Site Scripting";
my $VULN_CLASS_SQLI = "SQL Injection";
my $VULN_CLASS_SQLI_FINGERPRINT = "SQL Injection Fingerprinting";
my $VULN_CLASS_LFI = "Path Traversal";
my $VULN_CLASS_RFI = "Remote File Inclusion";
my $VULN_CLASS_HTTPRS = "HTTP Response Splitting";

# Only the vulnerabilities in this array will have
# rules generated for them.
my @supported_vulns = ($VULN_CLASS_XSS, $VULN_CLASS_SQLI, $VULN_CLASS_SQLI_FINGERPRINT, $VULN_CLASS_LFI, $VULN_CLASS_RFI, $VULN_CLASS_HTTPRS);

my $num_rules_generated=0;
my $num_not_supported=0;
my $num_bad_urls=0;

my $wait_for_keypress=1;
my $request_failed=0;

my $all_vulns_xml;
my @type;
my @id;
my $vuln_count;

my $num_attacks_flag=0;
my $num_attacks_noflag=0;

# End Vars ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#############
#   Main    #
#############

# Clean up env so perl doesn't complain
# when trying to run the restart snort
# script.
delete @ENV{qw(IFS CDPATH ENV BASH_ENV PATH)};

$all_vulns_xml = XML::Smart->new($all_vulnerabilities_filename);

@type = $all_vulns_xml->{OWASPZAPReport}{site}{alerts}{alertitem}('[@]','alert');
@url = $all_vulns_xml->{OWASPZAPReport}{site}{alerts}{alertitem}('[@]','uri');
@param = $all_vulns_xml->{OWASPZAPReport}{site}{alerts}{alertitem}('[@]','param');

open(my $MODSEC_RULES, '>' , $modsec_rules_file) || die "Unable to open modsecurity rules file $modsec_rules_file";
$MODSEC_RULES->autoflush(1);

$vuln_count = 0;

foreach my $current_type (@type){
	print "==================================================================================================\n";
	print "Vulnerability[$vuln_count] -  Type: $current_type\n";

	if(exists {map { $_ => 1 } @supported_vulns}->{$current_type}){
		parseData(to_string($current_type));
	}else {
 		print "Vulnerability Type: $type is not supported in this version.\n";
		$num_not_supported++;
	}
	$vuln_count++;
}

close($MODSEC_RULES);

print "==================================================================================================\n";

print "\n\n************ END OF SCRIPT RESULTS *****************\n";
print "Number of Vulnerabilities Processed:	$vuln_count\n";
print "Number of ModSecurity rules generated:   $num_rules_generated\n";
print "Number of Unsupported vulns skipped:     $num_not_supported\n";
print "Number of bad URLs (rules not gen):      $num_bad_urls\n";
print "****************************************************\n\n";
print "----------------------------------------------------\n";
print "To activate the virtua