#!/usr/bin/env ruby

msfbase = File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__
$:.unshift(File.join(File.dirname(msfbase), 'lib'))

require 'optparse'

class OptsConsole
	#
	# Return a hash describing the options.
	#
	def self.parse(args)
		options = {}

		opts = OptionParser.new do |opts|
			opts.banner = "Usage: msfwebfuzz [options]"

			opts.separator ""
			opts.separator "Specific options:"

			
			opts.on("-r", "-r <filename>", "HTTP/Fuzz Request file") do |r|
				options['requestfile'] = r
			end

			opts.on("-f", "-f <fuzzlist>", "Fuzz list") do |f|
				options['fuzzlist'] = f
			end


			# Boolean switches
			opts.on("-v", "--verbose", "Verbose output") do |v|
				options['verbose'] = true				
			end

			opts.separator ""
			opts.separator "Common options:"

			opts.on_tail("-h", "--help", "Show this message") do
				puts opts
				exit
			end
		end

		begin
			opts.parse!(args)
		rescue OptionParser::InvalidOption
			puts "Invalid option, try -h for usage"
			exit
		end

		options
	end
end

options = OptsConsole.parse(ARGV)

if (!options['requestfile'] or !options['requestfile'])
	puts "Invalid input: Use -h for options"
	exit 1
end


require 'rex'
require 'msf/core'
require 'nokogiri'

host = ''
method = ''
soap = false
scheme = 'http'
ssl = nil
port = 80
uri = '/'
ua = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.18) Gecko/20110319 Firefox/3.5.18'
cookie = nil
referer = nil
keep_alive = nil

content_type = nil
content_length = nil
soap_action = nil

accept = '*/*'
accept_language = 'en-us,en;q=0.5'
accept_encoding = 'gzip,deflate'
accept_charset = 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'

data = ''
data_string  = ''
File.open(options['requestfile'], "rb").each do |log|
    if log.match(/^(GET|POST)\s+(\/\S*)\s+HTTP\/\d\.\d/i)
        method = $1
        uri = $2
        #puts "method is #{method}"
        #puts "uri is #{uri}"
    elsif log.match(/^Host:\s+([A-Za-z0-9.]+):?(\d{1,5})?/i)
    	#puts "host" 
    	#puts $1
        host = $1
        port = $2
    elsif log.match(/User-Agent:\s+(\S+)/i)
        ua = $1
    elsif log.match(/Cookie:\s+(\S+)/i)
        cookie = $1
    elsif log.match(/Referer:\s+(\S+)/i)
        referer = $1
    elsif log.match(/Keep-Alive:\s+(\d+)/i)
        keep_alive = $1
    ## Accept
	elsif log.match(/Accept:\s+(\S+)/i)
        accept = $1
	elsif log.match(/Accept-Language:\s+(\S+)/i)
        accept_language = $1
	elsif log.match(/Accept-Encoding:\s+(\S+)/i)
        accept_encoding = $1
	elsif log.match(/Accept-Charset:\s+(\S+)/i)
        accept_charset = $1
    elsif log.match(/Content-Type:\s+(\S+)/i)
        content_type = $1
	elsif log.match(/Content-Length:\s+(\S+)/i)
       	next
       	#content_length = $1
	elsif log.match(/SOAPAction:\s+(\S+)/i)
        soap_action = $1
        soap = true
    elsif log.match(/Proxy-Connection: Keep-Alive/i)
    	next
	else
		if (log.match(/:/))
			if (data == 'read')
				data = ''
			end
			data_string << log
		else
			if (data == 'read')
				line = log
			else
				if (not log.match(/\S+/i))
					read_match = log
					data = 'read'
				end
			end
		end
	end
end

fuzz_array = Array.new
File.open(options['fuzzlist'], "rb").each do |f|
	i = 0
	matches = data_string.scan(/(\[FUZZ\][0-9A-Za-z\!@#\$%^&*(){};':",><\/\?\-\=\_\+ \.]+\[\/FUZZ\])/im)
	if (matches)
		matches.each do |m|
			tmp = data_string.sub("#{m}",f.chomp)
			no_stub = tmp.gsub(/\[\/?FUZZ\]/,'')
			fuzz_array.push(no_stub)
		end
	end
end

if (not port) 
    port = 80
end

def print_response(r,soap)
	response = r.clone
	if (soap)
		doc = Nokogiri::XML(response.body) { |x| x.noblanks }
		response.body = ''
		puts response
		puts doc
	else
		puts response
	end
end

fuzz_array.each do |d|
    client = Rex::Proto::Http::Client.new(host,port,{})
	headiers = {}
	request = nil
	opts = {}
	opts['uri'] = uri
	opts['method'] = method

	if (method == 'POST')
		opts['uri'] = uri
		opts['method'] = method
		opts['data'] = "\r\n#{d}"
		opts['ctype'] = content_type

		unless (soap_action == nil)
			opts['raw_headers'] = "SOAPAction: #{soap_action}"
		end

	elsif (method == 'GET') 
	
	else
		puts "Error: method #{method} not supported\n"
		exit 1
	end
	request = client.request_cgi(opts)
    begin
		response = client.send_recv(request)
		rescue ::Interrupt
		rescue ::Exception => e
	end

	if (response)
		response_matches = response.body.scan(/C:\\|uid=|Error|Exception|Illegal|Fault|ODBC|ADBC|not found|quotation mark|stack|invalid|ORA-|syntax|varchar/im)
		if (options['verbose'])
			puts "REQUEST:"
			puts request
			puts "\n\n"		
			puts "RESPONSE:"    
			print_response(response,soap)
		end

		if (response_matches.length > 0)
			puts "Response contains interesting error!"
			puts "REQUEST:"
			puts request
			puts "\n\n"
			puts "RESPONSE:"
			print_response(response,soap)
			puts "----"
		end
	else
		print "Error: no response"
	end
end

