#!/usr/bin/perl -w


#####################################################################################
# All information, data, source code, compiled binaries, information, documentation #
# contained within this document is the sole property of "WhiteHat Security, Inc"   #     
# and may not be sold, reused or distributed.                                       #
# Copyright 2002 WhiteHat Security, Inc.  ALL RIGHTS RESERVED                       #
#####################################################################################

use strict;
use LW;
require "./Subs.pm";



### Global Variables #################################################################
$| = 1;
my $Debug = 0;

my %FORM = ();

my $content_type = "text/html";         # Results Content Type
my $bgcolor = "FFFFFF";                 # Page BGCOLOR
my $link = "FF3300";                    # Page LINK COLOR
my $vlink = "3300FF";                   # Page VLINK COLOR


#####################################################################################

### MAIN ####################################################################
print "Content-Type: text/html\n\n";

# Start HTML 
print "<HTML><TITLE>WhiteHat Arsenal 1.50 Beta Tools</TITLE>\n";
print "<BODY BGCOLOR=\"$bgcolor\" LINK=\"$link\" VLINK=\"$vlink\">\n";

RequestMethod();


print "<CENTER><TABLE BORDER=0 CELLPADDING=2 CELLSPACING=0><TR><TD BGCOLOR=000000>\n";
print "<TABLE BORDER=0 CELLPADDING=2 CELLSPACING=0 WIDTH=600><TR><TD BGCOLOR=FFFFFF VALIGN=TOP ALIGN=CENTER>\n";

titlebarHTMLBlock("HTTP Response Codes", "Display the specified HTTP Response Code description.");


print <<EOF;
<TABLE BGCOLOR="#fdbe50" BORDER="0" CELLSPACING="2" CELLPADDING="0">
<TR><TD WIDTH="450" ALIGN="LEFT" VALIGN="TOP">
	
	<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
	<TR><TD BGCOLOR="#FFFFFF" ALIGN="LEFT" VALIGN="TOP">
	
		<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="2">
EOF
	 
	 my $flag = 0;
	 if (defined $FORM{'rc'}) {
	 
		
		if (($FORM{'rc'} eq "100") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Continue<BR><I>Informational - Request received, continuing process.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("100", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		} 
		
		
		if (($FORM{'rc'} eq "101") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Switching Protocols<BR><I>Informational - Request received, continuing process.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("101", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}	
			
			
		if (($FORM{'rc'} eq "200") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "OK<BR><I>Success - The action was successfully received, understood, and accepted.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("200", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
		
		
		if (($FORM{'rc'} eq "201") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Created<BR><I>Success - The action was successfully received, understood, and accepted.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("201", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
		
		
		if (($FORM{'rc'} eq "202") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Accepted<BR><I>Success - The action was successfully received, understood, and accepted.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("202", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
		
		if (($FORM{'rc'} eq "203") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Non-Authoritative Information<BR><I>Success - The action was successfully received, understood, and accepted.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("203", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
		
		if (($FORM{'rc'} eq "204") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "No Content<BR><I>Success - The action was successfully received, understood, and accepted.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("204", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
		
		if (($FORM{'rc'} eq "205") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Reset Content<BR><I>Success - The action was successfully received, understood, and accepted.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("205", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
		
		if (($FORM{'rc'} eq "206") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Partial Content<BR><I>Success - The action was successfully received, understood, and accepted.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("206", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
		
		if (($FORM{'rc'} eq "300") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Multiple Choices<BR><I>Redirection - Further action must be taken in order to complete the request.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("300", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "301") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Moved Permanently<BR><I>Redirection - Further action must be taken in order to complete the request.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("301", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "302") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Found<BR><I>Redirection - Further action must be taken in order to complete the request.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("302", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "303") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "See Other<BR><I>Redirection - Further action must be taken in order to complete the request.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("303", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "304") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Not Modified<BR><I>Redirection - Further action must be taken in order to complete the request.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("304", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "305") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Use Proxy<BR><I>Redirection - Further action must be taken in order to complete the request.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("305", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "307") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Temporary Redirect<BR><I>Redirection - Further action must be taken in order to complete the request.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("307", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "400") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Bad Request<BR><I>Client Error - The request contains bad syntax or cannot be fulfilled.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("400", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "401") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Unauthorized<BR><I>Client Error - The request contains bad syntax or cannot be fulfilled.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("401", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "402") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Payment Required<BR><I>Client Error - The request contains bad syntax or cannot be fulfilled.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("402", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "403") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Forbidden<BR><I>Client Error - The request contains bad syntax or cannot be fulfilled.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("403", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "404") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Not Found<BR><I>Client Error - The request contains bad syntax or cannot be fulfilled.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("404", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "405") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Method Not Allowed<BR><I>Client Error - The request contains bad syntax or cannot be fulfilled.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("405", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "406") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Not Acceptable<BR><I>Client Error - The request contains bad syntax or cannot be fulfilled.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("406", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "407") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Proxy Authentication Required<BR><I>Client Error - The request contains bad syntax or cannot be fulfilled.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("407", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "408") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Request Time-out<BR><I>Client Error - The request contains bad syntax or cannot be fulfilled.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("408", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "409") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Conflict<BR><I>Client Error - The request contains bad syntax or cannot be fulfilled.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("409", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "410") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Gone<BR><I>Client Error - The request contains bad syntax or cannot be fulfilled.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("410", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "411") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Length Required<BR><I>Client Error - The request contains bad syntax or cannot be fulfilled.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("411", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "412") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Precondition Failed<BR><I>Client Error - The request contains bad syntax or cannot be fulfilled.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("412", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "413") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Request Entity Too Large<BR><I>Client Error - The request contains bad syntax or cannot be fulfilled.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("413", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "414") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Request-URI Too Large<BR><I>Client Error - The request contains bad syntax or cannot be fulfilled.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("414", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "415") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Unsupported Media Type<BR><I>Client Error - The request contains bad syntax or cannot be fulfilled.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("415", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "416") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Requested range not satisfiable<BR><I>Client Error - The request contains bad syntax or cannot be fulfilled.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("416", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "417") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Expectation Failed<BR><I>Client Error - The request contains bad syntax or cannot be fulfilled.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("417", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "500") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Internal Server Error<BR><I>Server Error - The server failed to fulfill an apparently valid request.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("500", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "501") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Not Implemented<BR><I>Server Error - The server failed to fulfill an apparently valid request.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("501", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "502") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Bad Gateway<BR><I>Server Error - The server failed to fulfill an apparently valid request.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("502", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "503") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Service Unavailable<BR><I>Server Error - The server failed to fulfill an apparently valid request.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("503", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "504") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "Gateway Time-out<BR><I>Server Error - The server failed to fulfill an apparently valid request.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("504", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
			
		if (($FORM{'rc'} eq "505") || ($FORM{'rc'} eq "ALL")) {	
			my $description = "HTTP Version not supported<BR><I>Server Error - The server failed to fulfill an apparently valid request.</I>";
			if ($FORM{'rc'} eq "ALL") {
				rcHTMLBlock("505", $description);
			} else {
				rcHTMLBlock($FORM{'rc'}, $description);
			}
			$flag = 1;
		}
		
		if ($flag == 0) {
			my $description = "Error: The given HTTP Response code could not be found and no description could be given.";	
			rcHTMLBlock("HTTP Response Code Not Found", $description);
		}
		
	} else {

		print "<TR WIDTH=\"450\" BGCOLOR=\"#fdbe50\"><TD ALIGN=\"LEFT\" VALIGN=\"TOP\">\n";
		printFont("<B>Enter an HTTP Response Code</B> ", "14");
		print "</TD></TR>\n";
	
		print "<TR BGCOLOR=\"#fef5c7\" ALIGN=\"LEFT\" VALIGN=\"TOP\">\n"; 
		print "<TD WIDTH=\"450\" ALIGN=\"LEFT\" VALIGN=\"TOP\">\n";
		print "<FORM METHOD=\"POST\" NAME=\"response_code\" ACTION=\"http_rc.pl\">\n";
		print "<INPUT TYPE=TEXT VALUE=\"\" NAME=\"rc\" style=\"WIDTH: 100px; font-size: 10px; font-family:arial,sans-serif\">\n";
		print "<INPUT TYPE=SUBMIT VALUE=\" Enter \" NAME=\"enter\" style=\"font-size: 12px; font-family:arial,sans-serif\">\n";
		print "</FORM>\n";
		printFont("<A HREF=\"http_rc.pl?rc=ALL\">View</A> all HTTP Response Codes.", "12");
		print "</TD></TR>\n";
	
	
	}
				
	
print <<EOF;


		</TABLE>
		
	</TD></TR>
	</TABLE>
		
</TD></TR>
</TABLE>
<BR>
EOF
	
	
	
print "</TD></TR></TABLE>\n";
print "</TD></TR></TABLE></CENTER>\n";

print "</BODY></HTML>\n"; # End HTML
############################################################################

###########################
### Response Code HTML Block ###
###########################
# 
sub rcHTMLBlock {

	my $rc = shift || "";
	my $description = shift || "";	
		
	print "<TR WIDTH=\"450\" BGCOLOR=\"#fdbe50\"><TD ALIGN=\"LEFT\" VALIGN=\"TOP\">\n";
	printFont("<B>$rc</B> ", "14");
	print "</TD></TR>\n";
	
	print "<TR BGCOLOR=\"#fef5c7\" ALIGN=\"LEFT\" VALIGN=\"TOP\">\n"; 
	print "<TD WIDTH=\"450\" ALIGN=\"LEFT\" VALIGN=\"TOP\">\n";
	printFont("$description", "12");
	print "</TD></TR>\n";
	
}



#####################
### RequestMethod ###
#####################
# This will populate the %FORM hash with paramaters and values.
sub RequestMethod {
	# Determine the request method and then get the input
	
	my $buffer = "";
	
	if (!defined $ENV{'REQUEST_METHOD'}) {
		$ENV{'REQUEST_METHOD'} = "GET";
	}
	
	if ($ENV{'REQUEST_METHOD'} eq 'POST') {
        	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); # For POST methods
	} else {        
        	$buffer = $ENV{'QUERY_STRING'} || ""; # for GET methods
	} # end if
	
	# Data Clean-up
	if ($buffer ne "") {
		$buffer =~ s/(\0|%00)//g;	# Remove Hidden Nulls
        	$buffer =~ s/[\n]/%0A/g;	# URL Encode Newline
        	$buffer =~ s/[\r]/%0D/g; 	# URL Encode Carriage Returns
	}
	
	# Split the name-value pairs
	my @pairs = split(/&/, $buffer);
 
	foreach my $pair (@pairs) {
        	my ($name, $value) = split(/=/, $pair);
 
		# What happens to Lack of name or value after/before the equals?
		
       		# Un-Webify plus signs and %-encoding
        	$value =~ tr/+/ /;
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; # URL Decode Name

        	$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; # URL Decode Name
		$name =~ s/[\n\r]//g;	# Don't allow newlines
		
		print "NAME: $name VALUE: $value<BR>\n" if ($Debug == 1);
		
                $FORM{$name} = $value; # Build a hash the input parameters
		
	} # end foreach


} # End sub RequestMethod
