require 'rex/service_manager' require 'rex/exploitation/obfuscatejs' require 'rex/exploitation/encryptjs' require 'rex/exploitation/heaplib' module Msf ### # # This module provides methods for acting as an HTTP client when # exploiting an HTTP server. # ### module Exploit::Remote::HttpClient # # Initializes an exploit module that exploits a vulnerability in an HTTP # server. # def initialize(info = {}) super register_options( [ Opt::RHOST, Opt::RPORT(80), OptString.new('VHOST', [ false, "HTTP server virtual host" ]), Opt::Proxies ], self.class ) register_advanced_options( [ OptString.new('UserAgent', [false, 'The User-Agent header to use for all requests']), OptString.new('BasicAuthUser', [false, 'The HTTP username to specify for basic authentication']), OptString.new('BasicAuthPass', [false, 'The HTTP password to specify for basic authentication']), OptBool.new('SSL', [ false, 'Negotiate SSL for outgoing connections', false]), OptEnum.new('SSLVersion', [ false, 'Specify the version of SSL that should be used', 'SSL3', ['SSL2', 'SSL3', 'TLS1']]) ], self.class ) register_evasion_options( [ OptEnum.new('HTTP::uri_encode_mode', [false, 'Enable URI encoding', 'hex-normal', ['none', 'hex-normal', 'hex-all', 'hex-random', 'u-normal', 'u-all', 'u-random']]), OptBool.new('HTTP::uri_full_url', [false, 'Use the full URL for all HTTP requests', false]), OptInt.new('HTTP::pad_method_uri_count', [false, 'How many whitespace characters to use between the method and uri', 1]), OptInt.new('HTTP::pad_uri_version_count', [false, 'How many whitespace characters to use between the uri and version', 1]), OptEnum.new('HTTP::pad_method_uri_type', [false, 'What type of whitespace to use between the method and uri', 'space', ['space', 'tab', 'apache']]), OptEnum.new('HTTP::pad_uri_version_type', [false, 'What type of whitespace to use between the uri and version', 'space', ['space', 'tab', 'apache']]), OptBool.new('HTTP::method_random_valid', [false, 'Use a random, but valid, HTTP method for request', false]), OptBool.new('HTTP::method_random_invalid', [false, 'Use a random invalid, HTTP method for request', false]), OptBool.new('HTTP::method_random_case', [false, 'Use random casing for the HTTP method', false]), OptBool.new('HTTP::uri_dir_self_reference', [false, 'Insert self-referential directories into the uri', false]), OptBool.new('HTTP::uri_dir_fake_relative', [false, 'Insert fake relative directories into the uri', false]), OptBool.new('HTTP::uri_use_backslashes', [false, 'Use back slashes instead of forward slashes in the uri ', false]), OptBool.new('HTTP::pad_fake_headers', [false, 'Insert random, fake headers into the HTTP request', false]), OptInt.new('HTTP::pad_fake_headers_count', [false, 'How many fake headers to insert into the HTTP request', 0]), OptBool.new('HTTP::pad_get_params', [false, 'Insert random, fake query string variables into the request', false]), OptInt.new('HTTP::pad_get_params_count', [false, 'How many fake query string variables to insert into the request', 16]), OptBool.new('HTTP::pad_post_params', [false, 'Insert random, fake post variables into the request', false]), OptInt.new('HTTP::pad_post_params_count', [false, 'How many fake post variables to insert into the request', 16]), OptBool.new('HTTP::uri_fake_end', [false, 'Add a fake end of URI (eg: /%20HTTP/1.0/../../)', false]), OptBool.new('HTTP::uri_fake_params_start', [false, 'Add a fake start of params to the URI (eg: /%3fa=b/../)', false]), OptBool.new('HTTP::header_folding', [false, 'Enable folding of HTTP headers', false]) # # Remaining evasions to implement # # OptBool.new('HTTP::chunked', [false, 'Enable chunking of HTTP request via "Transfer-Encoding: chunked"', 'false']), # OptInt.new('HTTP::junk_pipeline', [true, 'Insert the specified number of junk pipeline requests', 0]), ], self.class ) register_autofilter_ports([ 80, 8080, 443, 8000, 8888, 8880, 8008, 3000, 8443 ]) register_autofilter_services(%W{ http https }) end # # Connects to an HTTP server. # def connect(opts={}) dossl = false if(opts.has_key?('SSL')) dossl = opts['SSL'] else dossl = ssl if (datastore.default?('SSL') and rport.to_i == 443) dossl = true end end nclient = Rex::Proto::Http::Client.new( rhost, rport.to_i, { 'Msf' => framework, 'MsfExploit' => self, }, dossl, ssl_version, proxies ) # Configure the HTTP client with the supplied parameter nclient.set_config( 'vhost' => self.vhost(), 'agent' => datastore['UserAgent'], 'basic_auth' => self.basic_auth, 'uri_encode_mode' => datastore['HTTP::uri_encode_mode'], 'uri_full_url' => datastore['HTTP::uri_full_url'], 'pad_method_uri_count' => datastore['HTTP::pad_method_uri_count'], 'pad_uri_version_count' => datastore['HTTP::pad_uri_version_count'], 'pad_method_uri_type' => datastore['HTTP::pad_method_uri_type'], 'pad_uri_version_type' => datastore['HTTP::pad_uri_version_type'], 'method_random_valid' => datastore['HTTP::method_random_valid'], 'method_random_invalid' => datastore['HTTP::method_random_invalid'], 'method_random_case' => datastore['HTTP::method_random_case'], 'uri_dir_self_reference' => datastore['HTTP::uri_dir_self_reference'], 'uri_dir_fake_relative' => datastore['HTTP::uri_dir_fake_relative'], 'uri_use_backslashes' => datastore['HTTP::uri_use_backslashes'], 'pad_fake_headers' => datastore['HTTP::pad_fake_headers'], 'pad_fake_headers_count' => datastore['HTTP::pad_fake_headers_count'], 'pad_get_params' => datastore['HTTP::pad_get_params'], 'pad_get_params_count' => datastore['HTTP::pad_get_params_count'], 'pad_post_params' => datastore['HTTP::pad_post_params'], 'pad_post_params_count' => datastore['HTTP::pad_post_params_count'], 'uri_fake_end' => datastore['HTTP::uri_fake_end'], 'uri_fake_params_start' => datastore['HTTP::uri_fake_params_start'], 'header_folding' => datastore['HTTP::header_folding'] ) # If this connection is global, persist it # Required for findsock on these sockets if (opts['global']) if (self.client) disconnect end self.client = nclient end return nclient end # # Passes the client connection down to the handler to see if it's of any # use. # def handler(nsock = nil) # If no socket was provided, try the global one. if ((!nsock) and (self.client)) nsock = self.client.conn end # If the parent claims the socket associated with the HTTP client, then # we rip the socket out from under the HTTP client. if (((rv = super(nsock)) == Handler::Claimed) and (self.client) and (nsock == self.client.conn)) self.client.conn = nil end rv end # # Disconnects the HTTP client # def disconnect(nclient = self.client) if (nclient) nclient.close end if (nclient == self.client) self.client = nil end end # # Performs cleanup as necessary, disconnecting the HTTP client if it's # still established. # def cleanup super disconnect end # # Connects to the server, creates a request, sends the request, reads the response # def send_request_raw(opts={}, timeout = -1) begin c = connect(opts) r = c.request_raw(opts) c.send_recv(r, opts[:timeout] ? opts[:timeout] : timeout) rescue ::Errno::EPIPE, ::Timeout::Error nil end end # # Connects to the server, creates a request, sends the request, reads the response # def send_request_cgi(opts={}, timeout = -1) begin c = connect(opts) r = c.request_cgi(opts) c.send_recv(r, opts[:timeout] ? opts[:timeout] : timeout) rescue ::Errno::EPIPE, ::Timeout::Error nil end end # # Combine the user/pass into an auth string for the HTTP Client # def basic_auth return if not datastore['BasicAuthUser'] datastore['BasicAuthUser'] + ":" + (datastore['BasicAuthPass'] || '') end ## # # Wrappers for getters # ## # # Returns the target host # def rhost datastore['RHOST'] end # # Returns the remote port # def rport datastore['RPORT'] end # # Returns the VHOST of the HTTP server. # def vhost datastore['VHOST'] || datastore['RHOST'] end # # Returns the boolean indicating SSL # def ssl datastore['SSL'] end # # Returns the string indicating SSL version # def ssl_version datastore['SSLVersion'] end # # Returns the configured proxy list # def proxies datastore['Proxies'] end protected attr_accessor :client end ### # # This module provides methods for exploiting an HTTP client by acting # as an HTTP server. # ### module Exploit::Remote::HttpServer include Msf::Exploit::Remote::TcpServer include Msf::Auxiliary::Report def initialize(info = {}) super register_options( [ OptString.new('URIPATH', [ false, "The URI to use for this exploit (default is random)"]), ], Exploit::Remote::HttpServer ) register_evasion_options( [ OptBool.new('HTTP::chunked', [false, 'Enable chunking of HTTP responses via "Transfer-Encoding: chunked"', 'false']), OptBool.new('HTTP::header_folding', [false, 'Enable folding of HTTP headers', 'false']), OptBool.new('HTTP::junk_headers', [false, 'Enable insertion of random junk HTTP headers', 'false']), OptEnum.new('HTTP::compression', [false, 'Enable compression of HTTP responses via content encoding', 'none', ['none','gzip','deflate']]), ], Exploit::Remote::HttpServer) @service_path = nil end # # By default, all HTTP servers are not subject to automatic exploitation # def autofilter false end # # Ensures that gzip can be used. If not, an exception is generated. The # exception is only raised if the DisableGzip advanced option has not been # set. # def use_zlib if (!Rex::Text.zlib_present? and datastore['HTTP::compression'] == true) raise RuntimeError, "zlib support was not detected, yet the HTTP::compression option was set. Don't do that!" end end # # This method gives a derived class the opportunity to ensure that all # dependencies are present before initializing the service. # # By default, all HTTP server mixins will try to use zlib. # def check_dependencies use_zlib end # # This mixin starts the HTTP server listener. This routine takes a few # different hash parameters: # # ServerHost => Override the server host to listen on (default to SRVHOST). # ServerPort => Override the server port to listen on (default to SRVPORT). # Uri => The URI to handle and the associated procedure to call. # def start_service(opts = {}) check_dependencies # Default the server host and port to what is required by the mixin. opts = { 'ServerHost' => datastore['SRVHOST'], 'ServerPort' => datastore['SRVPORT'], }.update(opts) # Start the HTTP server service. self.service = Rex::ServiceManager.start( Rex::Proto::Http::Server, opts['ServerPort'].to_i, opts['ServerHost'], { 'Msf' => framework, 'MsfExploit' => self, } ) self.service.server_name = 'Apache' # Default the procedure of the URI to on_request_uri if one isn't # provided. uopts = { 'Proc' => Proc.new { |cli, req| on_request_uri(cli, req) }, 'Path' => resource_uri }.update(opts['Uri'] || {}) print_status("Using URL: http://#{opts['ServerHost']}:#{opts['ServerPort']}#{uopts['Path']}") if (opts['ServerHost'] == '0.0.0.0') print_status(" Local IP: http://#{Rex::Socket.source_address('1.2.3.4')}:#{opts['ServerPort']}#{uopts['Path']}") end add_resource(uopts) end # # Store the results of server-side User-Agent fingerprinting in the DB. # # Returns an array containing host and client information. # def report_user_agent(address, request) ua = request['User-Agent'].downcase # always check for IE last because everybody tries to # look like IE case (ua) when /version\/(\d+\.\d+\.\d+).*safari/ ua_name = HttpClients::SAFARI ua_ver = $1 when /firefox\/((:?[0-9]+\.)+[0-9]+)/ ua_name = HttpClients::FF ua_ver = $1 when /mozilla\/[0-9]\.[0-9] \(compatible; msie ([0-9]\.[0-9]+)/ ua_name = HttpClients::IE ua_ver = $1 else ua_name = HttpClients::UNKNOWN end case (ua) when /(en-us|en-gb)/ os_lang = $1 end case (ua) when /windows/ os_name = OperatingSystems::WINDOWS arch = ARCH_X86 when /linux/ os_name = OperatingSystems::LINUX when /iphone/ os_name = OperatingSystems::MAC_OSX arch = 'armle' when /mac os x/ os_name = OperatingSystems::MAC_OSX else os_name = OperatingSystems::UNKNOWN end case (ua) when /windows 95/ os_flavor = '95' when /windows 98/ os_flavor = '98' when /windows nt 4/ os_flavor = 'NT' when /windows nt 5.0/ os_flavor = '2000' when /windows nt 5.1/ os_flavor = 'XP' when /windows nt 5.2/ os_flavor = '2003' when /windows nt 6.0/ os_flavor = 'Vista' when /windows nt 6.1/ os_flavor = '7' when /gentoo/ os_flavor = 'Gentoo' when /debian/ os_flavor = 'Debian' when /ubuntu/ os_flavor = 'Ubuntu' else os_flavor = '' end case (ua) when /ppc/ arch = ARCH_PPC when /x64|x86_64/ arch = ARCH_X86_64 when /i.86|wow64/ # WOW64 means "Windows on Windows64" and is present # in the useragent of 32-bit IE running on 64-bit # Windows arch = ARCH_X86 else arch = ARCH_X86 end host = { :address => address, :host => address, :os_name => os_name, :os_flavor => os_flavor, :os_lang => os_lang || 'en', :arch => arch } report_host(host) client = { :host => address, :ua_string => request['User-Agent'], :ua_name => ua_name, :ua_ver => ua_ver } report_client(client) report_note( :host => address, :type => 'http_request', :data => "#{@myhost}:#{@myport} #{request.method} #{request.resource} #{os_name} #{ua_name} #{ua_ver}" ) return [host, client] end # # Adds a URI resource using the supplied hash parameters. # # Path => The path to associate the procedure with. # Proc => The procedure to call when the URI is requested. # LongCall => Indicates that the request is a long call. # def add_resource(opts) @service_path = opts['Path'] service.add_resource(opts['Path'], opts) end # # Returns the last-used resource path # def get_resource # We don't want modules modifying their service_path inadvertantly, so # give them a dup. Can be nil during module setup. @service_path ? @service_path.dup : nil end # # Removes a URI resource. # def remove_resource(name) service.remove_resource(name) end # # Closes a client connection. # def close_client(cli) service.close_client(cli) end # # Creates an HTTP response packet. # def create_response(code = 200, message = "OK", proto = Rex::Proto::Http::DefaultProtocol) res = Rex::Proto::Http::Response.new(code, message, proto); res['Content-Type'] = 'text/html' res end # # Transmits a response to the supplied client, default content-type is text/html # # Payload evasions are implemented here! # def send_response(cli, body, headers = {}) response = create_response response['Content-Type'] = 'text/html' response.body = body if (datastore['HTTP::compression']) self.use_zlib # make sure... response.compress = datastore['HTTP::compression'] end if (datastore['HTTP::chunked'] == true) response.auto_cl = false response.transfer_chunked = true end if (datastore['HTTP::header_folding'] == true) response.headers.fold = 1 end if (datastore['HTTP::junk_headers'] == true) response.headers.junk_headers = 1 end headers.each_pair { |k,v| response[k] = v } cli.send_response(response) end # # Sends a 302 redirect to the client # def send_redirect(cli, location='/', body='') response = create_response(302, 'Moved') response['Content-Type'] = 'text/html' response['Location'] = location response.body = body cli.send_response(response) end # # Sends a 302 redirect relative to our base path # def send_local_redirect(cli, location) send_redirect(cli, get_resource + location) end # # Sends a 404 # def send_not_found(cli) resp_404 = create_response(404, 'Not Found') resp_404.body = %Q{
The requested URL was not found on this server.