#!/usr/bin/env ruby

msfbase = __FILE__
while File.symlink?(msfbase)
	msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
end

$:.unshift(File.join(File.expand_path(File.dirname(msfbase)), 'lib'))
$:.unshift(ENV['MSF_LOCAL_LIB']) if ENV['MSF_LOCAL_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 Request file") do |r|
				options['requestfile'] = r 
			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)

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

if (! options['requestfile'])
	puts "Usage -h for help info"
	exit 1
end
host = ''
method = ''

soap = true
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

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

if (not port) 
    port = 80
end

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#{data_string}"
	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)
puts "\n"
puts "REQUEST:"
puts request

begin
	response = client.send_recv(request)
	rescue ::Interrupt
	rescue ::Exception => e
end

puts "\n"
#puts "REQUEST:"
#puts request
puts "RESPONSE:"
print_response(response,soap)
