2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2005-07-24 20:53:54 +00:00
|
|
|
require 'rex/socket'
|
|
|
|
require 'rex/proto/http'
|
2006-12-19 07:11:55 +00:00
|
|
|
require 'rex/text'
|
2013-01-31 22:49:52 +00:00
|
|
|
require 'digest'
|
|
|
|
require 'rex/proto/ntlm/crypt'
|
|
|
|
require 'rex/proto/ntlm/constants'
|
|
|
|
require 'rex/proto/ntlm/utils'
|
|
|
|
require 'rex/proto/ntlm/exceptions'
|
2005-07-24 20:53:54 +00:00
|
|
|
|
2013-02-26 23:38:03 +00:00
|
|
|
require 'rex/proto/http/client_request'
|
|
|
|
|
2005-07-24 20:53:54 +00:00
|
|
|
module Rex
|
|
|
|
module Proto
|
|
|
|
module Http
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
2010-09-11 17:31:26 +00:00
|
|
|
# Acts as a client to an HTTP server, sending requests and receiving responses.
|
2012-05-24 23:10:26 +00:00
|
|
|
#
|
|
|
|
# See the RFC: http://www.w3.org/Protocols/rfc2616/rfc2616.html
|
2005-07-24 20:53:54 +00:00
|
|
|
#
|
|
|
|
###
|
|
|
|
class Client
|
|
|
|
|
2013-02-27 23:54:31 +00:00
|
|
|
DefaultUserAgent = ClientRequest::DefaultUserAgent
|
2011-07-28 22:57:47 +00:00
|
|
|
|
2005-12-08 05:55:44 +00:00
|
|
|
#
|
2006-12-19 07:11:55 +00:00
|
|
|
# Creates a new client instance
|
2005-12-08 05:55:44 +00:00
|
|
|
#
|
2013-01-31 19:34:32 +00:00
|
|
|
def initialize(host, port = 80, context = {}, ssl = nil, ssl_version = nil, proxies = nil, username = '', password = '')
|
2005-07-24 20:53:54 +00:00
|
|
|
self.hostname = host
|
|
|
|
self.port = port.to_i
|
2006-01-17 04:35:44 +00:00
|
|
|
self.context = context
|
2006-01-20 18:59:24 +00:00
|
|
|
self.ssl = ssl
|
2009-10-26 19:48:15 +00:00
|
|
|
self.ssl_version = ssl_version
|
2007-09-24 04:44:44 +00:00
|
|
|
self.proxies = proxies
|
2013-01-31 19:34:32 +00:00
|
|
|
self.username = username
|
|
|
|
self.password = password
|
2013-02-26 23:38:03 +00:00
|
|
|
|
2013-02-28 19:47:30 +00:00
|
|
|
# Take ClientRequest's defaults, but override with our own
|
|
|
|
self.config = Http::ClientRequest::DefaultConfig.merge({
|
2006-12-19 07:11:55 +00:00
|
|
|
'read_max_data' => (1024*1024*1),
|
|
|
|
'vhost' => self.hostname,
|
2013-02-28 19:47:30 +00:00
|
|
|
})
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2013-02-28 19:47:30 +00:00
|
|
|
# XXX: This info should all be controlled by ClientRequest
|
2013-02-27 00:16:44 +00:00
|
|
|
self.config_types = {
|
|
|
|
'uri_encode_mode' => ['hex-normal', 'hex-all', 'hex-random', 'u-normal', 'u-random', 'u-all'],
|
|
|
|
'uri_encode_count' => 'integer',
|
|
|
|
'uri_full_url' => 'bool',
|
|
|
|
'pad_method_uri_count' => 'integer',
|
|
|
|
'pad_uri_version_count' => 'integer',
|
|
|
|
'pad_method_uri_type' => ['space', 'tab', 'apache'],
|
|
|
|
'pad_uri_version_type' => ['space', 'tab', 'apache'],
|
|
|
|
'method_random_valid' => 'bool',
|
|
|
|
'method_random_invalid' => 'bool',
|
|
|
|
'method_random_case' => 'bool',
|
|
|
|
'version_random_valid' => 'bool',
|
|
|
|
'version_random_invalid' => 'bool',
|
|
|
|
'version_random_case' => 'bool',
|
|
|
|
'uri_dir_self_reference' => 'bool',
|
|
|
|
'uri_dir_fake_relative' => 'bool',
|
|
|
|
'uri_use_backslashes' => 'bool',
|
|
|
|
'pad_fake_headers' => 'bool',
|
|
|
|
'pad_fake_headers_count' => 'integer',
|
|
|
|
'pad_get_params' => 'bool',
|
|
|
|
'pad_get_params_count' => 'integer',
|
|
|
|
'pad_post_params' => 'bool',
|
|
|
|
'pad_post_params_count' => 'integer',
|
|
|
|
'uri_fake_end' => 'bool',
|
|
|
|
'uri_fake_params_start' => 'bool',
|
|
|
|
'header_folding' => 'bool',
|
|
|
|
'chunked_size' => 'integer'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-15 23:37:38 +00:00
|
|
|
end
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2005-09-15 23:37:38 +00:00
|
|
|
#
|
2006-12-19 07:11:55 +00:00
|
|
|
# Set configuration options
|
2005-09-15 23:37:38 +00:00
|
|
|
#
|
2006-12-19 07:11:55 +00:00
|
|
|
def set_config(opts = {})
|
|
|
|
opts.each_pair do |var,val|
|
2012-01-07 03:05:29 +00:00
|
|
|
# Default type is string
|
2009-11-30 21:15:06 +00:00
|
|
|
typ = self.config_types[var] || 'string'
|
|
|
|
|
2012-01-07 03:05:29 +00:00
|
|
|
# These are enum types
|
2009-11-30 21:15:06 +00:00
|
|
|
if(typ.class.to_s == 'Array')
|
|
|
|
if not typ.include?(val)
|
|
|
|
raise RuntimeError, "The specified value for #{var} is not one of the valid choices"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-07 03:05:29 +00:00
|
|
|
# The caller should have converted these to proper ruby types, but
|
|
|
|
# take care of the case where they didn't before setting the
|
|
|
|
# config.
|
|
|
|
|
2009-11-30 21:15:06 +00:00
|
|
|
if(typ == 'bool')
|
2012-01-07 03:05:29 +00:00
|
|
|
val = (val =~ /^(t|y|1)$/i ? true : false || val === true)
|
2009-11-30 21:15:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if(typ == 'integer')
|
|
|
|
val = val.to_i
|
|
|
|
end
|
|
|
|
|
2007-02-18 22:35:07 +00:00
|
|
|
self.config[var]=val
|
2006-12-19 07:11:55 +00:00
|
|
|
end
|
2005-09-15 23:37:38 +00:00
|
|
|
end
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2005-09-15 23:37:38 +00:00
|
|
|
#
|
2006-12-19 07:11:55 +00:00
|
|
|
# Create an arbitrary HTTP request
|
|
|
|
#
|
2013-02-07 00:56:06 +00:00
|
|
|
# @param opts [Hash]
|
|
|
|
# @option opts 'agent' [String] User-Agent header value
|
|
|
|
# @option opts 'connection' [String] Connection header value
|
|
|
|
# @option opts 'cookie' [String] Cookie header value
|
|
|
|
# @option opts 'data' [String] HTTP data (only useful with some methods, see rfc2616)
|
|
|
|
# @option opts 'encode' [Bool] URI encode the supplied URI, default: false
|
|
|
|
# @option opts 'headers' [Hash] HTTP headers, e.g. <code>{ "X-MyHeader" => "value" }</code>
|
|
|
|
# @option opts 'method' [String] HTTP method to use in the request, not limited to standard methods defined by rfc2616, default: GET
|
|
|
|
# @option opts 'proto' [String] protocol, default: HTTP
|
|
|
|
# @option opts 'query' [String] raw query string
|
|
|
|
# @option opts 'raw_headers' [Hash] HTTP headers
|
|
|
|
# @option opts 'uri' [String] the URI to request
|
|
|
|
# @option opts 'version' [String] version of the protocol, default: 1.1
|
|
|
|
# @option opts 'vhost' [String] Host header value
|
|
|
|
#
|
2013-02-26 19:31:24 +00:00
|
|
|
# @return [ClientRequest]
|
2006-12-19 07:11:55 +00:00
|
|
|
def request_raw(opts={})
|
2013-03-01 17:01:00 +00:00
|
|
|
opts = self.config.merge(opts)
|
2013-03-08 00:16:57 +00:00
|
|
|
|
2013-03-01 17:01:00 +00:00
|
|
|
opts['ssl'] = self.ssl
|
2013-02-19 23:19:16 +00:00
|
|
|
opts['cgi'] = false
|
|
|
|
opts['port'] = self.port
|
2013-03-08 00:16:57 +00:00
|
|
|
|
2013-02-20 01:41:42 +00:00
|
|
|
req = ClientRequest.new(opts)
|
2006-12-19 07:11:55 +00:00
|
|
|
end
|
|
|
|
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2006-12-19 07:11:55 +00:00
|
|
|
#
|
|
|
|
# Create a CGI compatible request
|
|
|
|
#
|
2013-02-07 00:56:06 +00:00
|
|
|
# @param (see #request_raw)
|
|
|
|
# @option opts (see #request_raw)
|
|
|
|
# @option opts 'ctype' [String] Content-Type header value, default: +application/x-www-form-urlencoded+
|
|
|
|
# @option opts 'encode_params' [Bool] URI encode the GET or POST variables (names and values), default: true
|
|
|
|
# @option opts 'vars_get' [Hash] GET variables as a hash to be translated into a query string
|
|
|
|
# @option opts 'vars_post' [Hash] POST variables as a hash to be translated into POST data
|
2010-09-11 17:31:26 +00:00
|
|
|
#
|
2013-02-26 19:31:24 +00:00
|
|
|
# @return [ClientRequest]
|
2006-12-19 07:11:55 +00:00
|
|
|
def request_cgi(opts={})
|
2013-03-01 17:01:00 +00:00
|
|
|
opts = self.config.merge(opts)
|
2013-03-08 00:16:57 +00:00
|
|
|
|
2013-02-28 19:47:30 +00:00
|
|
|
opts['ctype'] ||= 'application/x-www-form-urlencoded'
|
2013-02-26 17:01:16 +00:00
|
|
|
opts['ssl'] = self.ssl
|
2013-02-19 23:19:16 +00:00
|
|
|
opts['cgi'] = true
|
|
|
|
opts['port'] = self.port
|
2013-02-20 01:41:42 +00:00
|
|
|
|
|
|
|
req = ClientRequest.new(opts)
|
2013-03-04 17:24:26 +00:00
|
|
|
req
|
2009-11-02 18:14:57 +00:00
|
|
|
end
|
2005-09-15 23:37:38 +00:00
|
|
|
|
2005-07-24 20:53:54 +00:00
|
|
|
#
|
|
|
|
# Connects to the remote server if possible.
|
|
|
|
#
|
2013-02-07 00:56:06 +00:00
|
|
|
# @param t [Fixnum] Timeout
|
|
|
|
# @see Rex::Socket::Tcp.create
|
|
|
|
# @return [Rex::Socket::Tcp]
|
2011-10-21 15:41:37 +00:00
|
|
|
def connect(t = -1)
|
2005-07-24 20:53:54 +00:00
|
|
|
# If we already have a connection and we aren't pipelining, close it.
|
* add junk pipelined request support
* fix socket creation on pipelined requests
* when a server says that the connection should be closed (Connection: closed), then close the connection, since its going to regardless, and we don't want to loose our state
* support non-standard line termination in headers. ie \n instead of \r\n
* add junk headers (X-rand: rand)
* add header folding (for evasion)
* add parse_header_re (still leaving parse_header around, though its dead code ATM) that does the right thing on non-standard line endings
* move 'gzip' to a 'compression' option
* add 'deflate' compression option (really, just raw zlib, and only firefox does deflate right)
* fix a bunch of TE:chunked decoding bugs based based on the fact that Apache doesn't always close chunks appropriately
* modify parse_body to not return state, since it doesn't always do that, and the return isn't used... self.state is.
* add TE:chunked request support
* normalize URIs in requests before saving them
* Move params out of the URI, but when the uri is requested, and the method is GET, and there are params, return a URI that has the params that are approrpiately encoded (needed for junk_params, see below)
* move request.to_s support of params to use the request params array when a POST, allows use of junk params support (see below). NOTE: If the body is provided, use the body instead of params, in case you want to hardcode the params in a POST request, eg: php_xmlrpc_eval.rb
* Add junk params when building a param list, eg: a=b becomes asdfasdf=asdrt32a&asdfad=okhgasd&a=b&hjklasdf=hkasgd
* add URI junk slash support (eg: /////foo.html)
* param splitting now supports both '&', and ';', which CGI.pm and PHP both allow
* add URI junk directory support, eg: /asdf/../foo.html
* add param encoding support, eg: param A with value '=' is A=%3d
* add URI junk self referring directory support, eg: /././foo.html
git-svn-id: file:///home/svn/incoming/trunk@3457 4d416f70-5f16-0410-b530-b9f4589650da
2006-01-27 21:57:44 +00:00
|
|
|
if (self.conn)
|
|
|
|
if !pipelining?
|
|
|
|
close
|
|
|
|
else
|
|
|
|
return self.conn
|
|
|
|
end
|
2005-07-24 20:53:54 +00:00
|
|
|
end
|
|
|
|
|
2011-10-21 15:41:37 +00:00
|
|
|
timeout = (t.nil? or t == -1) ? 0 : t
|
|
|
|
|
2005-07-24 20:53:54 +00:00
|
|
|
self.conn = Rex::Socket::Tcp.create(
|
|
|
|
'PeerHost' => self.hostname,
|
|
|
|
'PeerPort' => self.port.to_i,
|
|
|
|
'LocalHost' => self.local_host,
|
2006-01-17 04:35:44 +00:00
|
|
|
'LocalPort' => self.local_port,
|
2006-01-20 18:59:24 +00:00
|
|
|
'Context' => self.context,
|
2007-09-24 04:44:44 +00:00
|
|
|
'SSL' => self.ssl,
|
2009-10-26 19:48:15 +00:00
|
|
|
'SSLVersion'=> self.ssl_version,
|
2011-10-21 15:41:37 +00:00
|
|
|
'Proxies' => self.proxies,
|
|
|
|
'Timeout' => timeout
|
2006-01-17 04:35:44 +00:00
|
|
|
)
|
2005-07-24 20:53:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Closes the connection to the remote server.
|
|
|
|
#
|
|
|
|
def close
|
2005-09-22 03:24:32 +00:00
|
|
|
if (self.conn)
|
|
|
|
self.conn.shutdown
|
2009-11-02 18:14:57 +00:00
|
|
|
self.conn.close
|
2005-09-22 03:24:32 +00:00
|
|
|
end
|
|
|
|
|
2005-07-24 20:53:54 +00:00
|
|
|
self.conn = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2013-02-05 17:55:12 +00:00
|
|
|
# Sends a request and gets a response back
|
2013-02-07 00:56:06 +00:00
|
|
|
#
|
|
|
|
# If the request is a 401, and we have creds, it will attempt to complete
|
|
|
|
# authentication and return the final response
|
2005-07-24 20:53:54 +00:00
|
|
|
#
|
2013-02-27 22:27:37 +00:00
|
|
|
# @return (see #_send_recv)
|
2010-05-22 17:58:01 +00:00
|
|
|
def send_recv(req, t = -1, persist=false)
|
2013-01-31 19:34:32 +00:00
|
|
|
res = _send_recv(req,t,persist)
|
2013-02-20 03:25:37 +00:00
|
|
|
if res and res.code == 401 and res.headers['WWW-Authenticate']
|
2013-02-18 03:35:19 +00:00
|
|
|
res = send_auth(res, req.opts, t, persist)
|
2013-01-31 19:34:32 +00:00
|
|
|
end
|
2013-01-31 20:44:06 +00:00
|
|
|
res
|
2013-01-31 19:34:32 +00:00
|
|
|
end
|
|
|
|
|
2013-02-05 17:55:12 +00:00
|
|
|
#
|
|
|
|
# Transmit an HTTP request and receive the response
|
|
|
|
#
|
2013-02-07 00:56:06 +00:00
|
|
|
# If persist is set, then the request will attempt to reuse an existing
|
|
|
|
# connection.
|
|
|
|
#
|
|
|
|
# Call this directly instead of {#send_recv} if you don't want automatic
|
|
|
|
# authentication handling.
|
|
|
|
#
|
2013-02-27 22:27:37 +00:00
|
|
|
# @return (see #read_response)
|
2013-01-31 19:34:32 +00:00
|
|
|
def _send_recv(req, t = -1, persist=false)
|
2010-05-22 17:58:01 +00:00
|
|
|
@pipeline = persist
|
2011-10-21 15:41:37 +00:00
|
|
|
send_request(req, t)
|
2010-11-13 05:25:39 +00:00
|
|
|
res = read_response(t)
|
2010-12-01 16:32:40 +00:00
|
|
|
res.request = req.to_s if res
|
2010-11-13 05:25:39 +00:00
|
|
|
res
|
2005-07-24 20:53:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2010-06-11 16:12:05 +00:00
|
|
|
# Send an HTTP request to the server
|
2009-11-30 21:15:06 +00:00
|
|
|
#
|
2013-02-26 19:31:24 +00:00
|
|
|
# @param req [Request,ClientRequest,#to_s] The request to send
|
2013-02-07 00:56:06 +00:00
|
|
|
# @param t (see #connect)
|
2013-02-20 00:40:39 +00:00
|
|
|
#
|
2013-02-27 22:27:37 +00:00
|
|
|
# @return [void]
|
2011-10-21 15:41:37 +00:00
|
|
|
def send_request(req, t = -1)
|
|
|
|
connect(t)
|
2009-11-30 21:15:06 +00:00
|
|
|
conn.put(req.to_s)
|
2006-12-19 07:11:55 +00:00
|
|
|
end
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2013-02-20 00:40:39 +00:00
|
|
|
# Resends an HTTP Request with the propper authentcation headers
|
|
|
|
# set. If we do not support the authentication type the server requires
|
|
|
|
# we return the original response object
|
2013-02-26 19:31:24 +00:00
|
|
|
#
|
2013-02-20 00:40:39 +00:00
|
|
|
# @param res [Response] the HTTP Response object
|
|
|
|
# @param opts [Hash] the options used to generate the original HTTP request
|
|
|
|
# @param t [Fixnum] the timeout for the request in seconds
|
|
|
|
# @param persist [Boolean] whether or not to persist the TCP connection (pipelining)
|
2013-02-27 22:27:37 +00:00
|
|
|
#
|
2013-02-20 00:40:39 +00:00
|
|
|
# @return [Response] the last valid HTTP response object we received
|
2013-01-31 19:34:32 +00:00
|
|
|
def send_auth(res, opts, t, persist)
|
2013-02-20 03:25:37 +00:00
|
|
|
if opts['username'].nil? or opts['username'] == ''
|
|
|
|
if self.username and not (self.username == '')
|
|
|
|
opts['username'] = self.username
|
|
|
|
opts['password'] = self.password
|
|
|
|
else
|
2013-03-04 15:09:11 +00:00
|
|
|
opts['username'] = nil
|
2013-02-20 03:25:37 +00:00
|
|
|
opts['password'] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-20 05:50:30 +00:00
|
|
|
return res if opts['username'].nil? or opts['username'] == ''
|
2013-01-31 19:34:32 +00:00
|
|
|
supported_auths = res.headers['WWW-Authenticate']
|
|
|
|
if supported_auths.include? 'Basic'
|
2013-02-28 19:47:30 +00:00
|
|
|
opts['headers'] ||= {}
|
|
|
|
opts['headers']['Authorization'] = basic_auth_header(opts['username'],opts['password'] )
|
2013-01-31 19:34:32 +00:00
|
|
|
req = request_cgi(opts)
|
|
|
|
res = _send_recv(req,t,persist)
|
|
|
|
return res
|
|
|
|
elsif supported_auths.include? "Digest"
|
|
|
|
temp_response = digest_auth(opts)
|
|
|
|
if temp_response.kind_of? Rex::Proto::Http::Response
|
|
|
|
res = temp_response
|
|
|
|
end
|
|
|
|
return res
|
|
|
|
elsif supported_auths.include? "NTLM"
|
|
|
|
opts['provider'] = 'NTLM'
|
|
|
|
temp_response = negotiate_auth(opts)
|
|
|
|
if temp_response.kind_of? Rex::Proto::Http::Response
|
|
|
|
res = temp_response
|
|
|
|
end
|
|
|
|
return res
|
|
|
|
elsif supported_auths.include? "Negotiate"
|
|
|
|
opts['provider'] = 'Negotiate'
|
|
|
|
temp_response = negotiate_auth(opts)
|
|
|
|
if temp_response.kind_of? Rex::Proto::Http::Response
|
|
|
|
res = temp_response
|
|
|
|
end
|
|
|
|
return res
|
|
|
|
end
|
2013-02-05 15:57:33 +00:00
|
|
|
return res
|
2013-01-31 19:34:32 +00:00
|
|
|
end
|
|
|
|
|
2013-02-27 22:27:37 +00:00
|
|
|
# Converts username and password into the HTTP Basic authorization
|
|
|
|
# string.
|
|
|
|
#
|
|
|
|
# @return [String] A value suitable for use as an Authorization header
|
2013-01-31 22:49:52 +00:00
|
|
|
def basic_auth_header(username,password)
|
|
|
|
auth_str = username.to_s + ":" + password.to_s
|
|
|
|
auth_str = "Basic " + Rex::Text.encode_base64(auth_str)
|
|
|
|
end
|
|
|
|
|
2013-02-20 00:48:03 +00:00
|
|
|
# Send a series of requests to complete Digest Authentication
|
2013-02-27 22:27:37 +00:00
|
|
|
#
|
2013-02-20 00:48:03 +00:00
|
|
|
# @param opts [Hash] the options used to build an HTTP request
|
2013-02-27 22:27:37 +00:00
|
|
|
#
|
2013-02-20 00:48:03 +00:00
|
|
|
# @return [Response] the last valid HTTP response we received
|
2013-01-31 19:34:32 +00:00
|
|
|
def digest_auth(opts={})
|
|
|
|
@nonce_count = 0
|
|
|
|
|
2013-01-31 22:49:52 +00:00
|
|
|
to = opts['timeout'] || 20
|
|
|
|
|
2013-02-20 00:40:39 +00:00
|
|
|
digest_user = opts['username'] || ""
|
|
|
|
digest_password = opts['password'] || ""
|
2013-01-31 19:34:32 +00:00
|
|
|
|
|
|
|
method = opts['method']
|
|
|
|
path = opts['uri']
|
|
|
|
iis = true
|
2013-02-01 21:49:18 +00:00
|
|
|
if (opts['DigestAuthIIS'] == false or self.config['DigestAuthIIS'] == false)
|
2013-01-31 19:34:32 +00:00
|
|
|
iis = false
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
@nonce_count += 1
|
|
|
|
|
|
|
|
resp = opts['response']
|
|
|
|
|
|
|
|
if not resp
|
|
|
|
# Get authentication-challenge from server, and read out parameters required
|
|
|
|
r = request_cgi(opts.merge({
|
|
|
|
'uri' => path,
|
|
|
|
'method' => method }))
|
2013-02-04 22:42:24 +00:00
|
|
|
resp = _send_recv(r, to)
|
2013-01-31 19:34:32 +00:00
|
|
|
unless resp.kind_of? Rex::Proto::Http::Response
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if resp.code != 401
|
|
|
|
return resp
|
|
|
|
end
|
|
|
|
return resp unless resp.headers['WWW-Authenticate']
|
|
|
|
end
|
|
|
|
|
|
|
|
# Don't anchor this regex to the beginning of string because header
|
|
|
|
# folding makes it appear later when the server presents multiple
|
|
|
|
# WWW-Authentication options (such as is the case with IIS configured
|
|
|
|
# for Digest or NTLM).
|
|
|
|
resp['www-authenticate'] =~ /Digest (.*)/
|
|
|
|
|
|
|
|
parameters = {}
|
|
|
|
$1.split(/,[[:space:]]*/).each do |p|
|
|
|
|
k, v = p.split("=", 2)
|
|
|
|
parameters[k] = v.gsub('"', '')
|
|
|
|
end
|
|
|
|
|
|
|
|
qop = parameters['qop']
|
|
|
|
|
|
|
|
if parameters['algorithm'] =~ /(.*?)(-sess)?$/
|
|
|
|
algorithm = case $1
|
|
|
|
when 'MD5' then Digest::MD5
|
|
|
|
when 'SHA1' then Digest::SHA1
|
|
|
|
when 'SHA2' then Digest::SHA2
|
|
|
|
when 'SHA256' then Digest::SHA256
|
|
|
|
when 'SHA384' then Digest::SHA384
|
|
|
|
when 'SHA512' then Digest::SHA512
|
|
|
|
when 'RMD160' then Digest::RMD160
|
|
|
|
else raise Error, "unknown algorithm \"#{$1}\""
|
|
|
|
end
|
|
|
|
algstr = parameters["algorithm"]
|
|
|
|
sess = $2
|
|
|
|
else
|
|
|
|
algorithm = Digest::MD5
|
|
|
|
algstr = "MD5"
|
|
|
|
sess = false
|
|
|
|
end
|
|
|
|
|
|
|
|
a1 = if sess then
|
|
|
|
[
|
|
|
|
algorithm.hexdigest("#{digest_user}:#{parameters['realm']}:#{digest_password}"),
|
|
|
|
parameters['nonce'],
|
|
|
|
@cnonce
|
|
|
|
].join ':'
|
|
|
|
else
|
|
|
|
"#{digest_user}:#{parameters['realm']}:#{digest_password}"
|
|
|
|
end
|
|
|
|
|
|
|
|
ha1 = algorithm.hexdigest(a1)
|
|
|
|
ha2 = algorithm.hexdigest("#{method}:#{path}")
|
|
|
|
|
|
|
|
request_digest = [ha1, parameters['nonce']]
|
|
|
|
request_digest.push(('%08x' % @nonce_count), @cnonce, qop) if qop
|
|
|
|
request_digest << ha2
|
|
|
|
request_digest = request_digest.join ':'
|
|
|
|
|
|
|
|
# Same order as IE7
|
|
|
|
auth = [
|
|
|
|
"Digest username=\"#{digest_user}\"",
|
|
|
|
"realm=\"#{parameters['realm']}\"",
|
|
|
|
"nonce=\"#{parameters['nonce']}\"",
|
|
|
|
"uri=\"#{path}\"",
|
|
|
|
"cnonce=\"#{@cnonce}\"",
|
|
|
|
"nc=#{'%08x' % @nonce_count}",
|
|
|
|
"algorithm=#{algstr}",
|
|
|
|
"response=\"#{algorithm.hexdigest(request_digest)[0, 32]}\"",
|
|
|
|
# The spec says the qop value shouldn't be enclosed in quotes, but
|
|
|
|
# some versions of IIS require it and Apache accepts it. Chrome
|
|
|
|
# and Firefox both send it without quotes but IE does it this way.
|
|
|
|
# Use the non-compliant-but-everybody-does-it to be as compatible
|
|
|
|
# as possible by default. The user can override if they don't like
|
|
|
|
# it.
|
|
|
|
if qop.nil? then
|
|
|
|
elsif iis then
|
|
|
|
"qop=\"#{qop}\""
|
|
|
|
else
|
|
|
|
"qop=#{qop}"
|
|
|
|
end,
|
|
|
|
if parameters.key? 'opaque' then
|
|
|
|
"opaque=\"#{parameters['opaque']}\""
|
|
|
|
end
|
|
|
|
].compact
|
|
|
|
|
|
|
|
headers ={ 'Authorization' => auth.join(', ') }
|
|
|
|
headers.merge!(opts['headers']) if opts['headers']
|
|
|
|
|
|
|
|
# Send main request with authentication
|
|
|
|
r = request_cgi(opts.merge({
|
|
|
|
'uri' => path,
|
|
|
|
'method' => method,
|
|
|
|
'headers' => headers }))
|
2013-01-31 22:49:52 +00:00
|
|
|
resp = _send_recv(r, to, true)
|
2013-01-31 19:34:32 +00:00
|
|
|
unless resp.kind_of? Rex::Proto::Http::Response
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
return resp
|
|
|
|
|
|
|
|
rescue ::Errno::EPIPE, ::Timeout::Error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 16:36:51 +00:00
|
|
|
#
|
|
|
|
# Builds a series of requests to complete Negotiate Auth. Works essentially
|
|
|
|
# the same way as Digest auth. Same pipelining concerns exist.
|
|
|
|
#
|
2013-02-27 22:27:37 +00:00
|
|
|
# @option opts (see #send_request_cgi)
|
|
|
|
# @option opts provider ["Negotiate","NTLM"] What Negotiate provider to use
|
|
|
|
#
|
|
|
|
# @return [Response] the last valid HTTP response we received
|
2013-01-31 19:34:32 +00:00
|
|
|
def negotiate_auth(opts={})
|
|
|
|
ntlm_options = {
|
|
|
|
:signing => false,
|
|
|
|
:usentlm2_session => self.config['usentlm2_session'],
|
|
|
|
:use_ntlmv2 => self.config['use_ntlmv2'],
|
|
|
|
:send_lm => self.config['send_lm'],
|
|
|
|
:send_ntlm => self.config['send_ntlm']
|
|
|
|
}
|
|
|
|
|
2013-01-31 22:49:52 +00:00
|
|
|
to = opts['timeout'] || 20
|
2013-02-20 00:40:39 +00:00
|
|
|
opts['username'] ||= ''
|
|
|
|
opts['password'] ||= ''
|
2013-01-31 22:49:52 +00:00
|
|
|
|
2013-01-31 19:34:32 +00:00
|
|
|
if opts['provider'] and opts['provider'].include? 'Negotiate'
|
2013-02-07 00:56:06 +00:00
|
|
|
provider = "Negotiate "
|
2013-01-31 19:34:32 +00:00
|
|
|
else
|
|
|
|
provider = 'NTLM '
|
|
|
|
end
|
|
|
|
|
|
|
|
opts['method']||= 'GET'
|
|
|
|
opts['headers']||= {}
|
|
|
|
|
2013-01-31 22:49:52 +00:00
|
|
|
ntlmssp_flags = ::Rex::Proto::NTLM::Utils.make_ntlm_flags(ntlm_options)
|
2013-01-31 19:34:32 +00:00
|
|
|
workstation_name = Rex::Text.rand_text_alpha(rand(8)+1)
|
|
|
|
domain_name = self.config['domain']
|
|
|
|
|
|
|
|
b64_blob = Rex::Text::encode_base64(
|
2013-01-31 22:49:52 +00:00
|
|
|
::Rex::Proto::NTLM::Utils::make_ntlmssp_blob_init(
|
2013-01-31 19:34:32 +00:00
|
|
|
domain_name,
|
|
|
|
workstation_name,
|
|
|
|
ntlmssp_flags
|
|
|
|
))
|
|
|
|
|
|
|
|
ntlm_message_1 = provider + b64_blob
|
|
|
|
|
|
|
|
begin
|
|
|
|
# First request to get the challenge
|
|
|
|
opts['headers']['Authorization'] = ntlm_message_1
|
|
|
|
r = request_cgi(opts)
|
2013-02-04 22:42:24 +00:00
|
|
|
resp = _send_recv(r, to)
|
2013-01-31 19:34:32 +00:00
|
|
|
unless resp.kind_of? Rex::Proto::Http::Response
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
return resp unless resp.code == 401 && resp.headers['WWW-Authenticate']
|
|
|
|
|
|
|
|
# Get the challenge and craft the response
|
|
|
|
ntlm_challenge = resp.headers['WWW-Authenticate'].scan(/#{provider}([A-Z0-9\x2b\x2f=]+)/i).flatten[0]
|
|
|
|
return resp unless ntlm_challenge
|
|
|
|
|
|
|
|
ntlm_message_2 = Rex::Text::decode_base64(ntlm_challenge)
|
2013-01-31 22:49:52 +00:00
|
|
|
blob_data = ::Rex::Proto::NTLM::Utils.parse_ntlm_type_2_blob(ntlm_message_2)
|
2013-01-31 19:34:32 +00:00
|
|
|
|
|
|
|
challenge_key = blob_data[:challenge_key]
|
|
|
|
server_ntlmssp_flags = blob_data[:server_ntlmssp_flags] #else should raise an error
|
|
|
|
default_name = blob_data[:default_name] || '' #netbios name
|
|
|
|
default_domain = blob_data[:default_domain] || '' #netbios domain
|
|
|
|
dns_host_name = blob_data[:dns_host_name] || '' #dns name
|
|
|
|
dns_domain_name = blob_data[:dns_domain_name] || '' #dns domain
|
|
|
|
chall_MsvAvTimestamp = blob_data[:chall_MsvAvTimestamp] || '' #Client time
|
|
|
|
|
|
|
|
spnopt = {:use_spn => self.config['SendSPN'], :name => self.hostname}
|
|
|
|
|
2013-01-31 22:49:52 +00:00
|
|
|
resp_lm, resp_ntlm, client_challenge, ntlm_cli_challenge = ::Rex::Proto::NTLM::Utils.create_lm_ntlm_responses(
|
2013-01-31 19:34:32 +00:00
|
|
|
opts['username'],
|
|
|
|
opts['password'],
|
|
|
|
challenge_key,
|
|
|
|
domain_name,
|
|
|
|
default_name,
|
|
|
|
default_domain,
|
|
|
|
dns_host_name,
|
|
|
|
dns_domain_name,
|
|
|
|
chall_MsvAvTimestamp,
|
|
|
|
spnopt,
|
|
|
|
ntlm_options
|
|
|
|
)
|
|
|
|
|
2013-01-31 22:49:52 +00:00
|
|
|
ntlm_message_3 = ::Rex::Proto::NTLM::Utils.make_ntlmssp_blob_auth(
|
2013-01-31 19:34:32 +00:00
|
|
|
domain_name,
|
|
|
|
workstation_name,
|
|
|
|
opts['username'],
|
|
|
|
resp_lm,
|
|
|
|
resp_ntlm,
|
|
|
|
'',
|
|
|
|
ntlmssp_flags
|
|
|
|
)
|
|
|
|
|
|
|
|
ntlm_message_3 = Rex::Text::encode_base64(ntlm_message_3)
|
|
|
|
|
|
|
|
# Send the response
|
|
|
|
opts['headers']['Authorization'] = "#{provider}#{ntlm_message_3}"
|
|
|
|
r = request_cgi(opts)
|
|
|
|
resp = _send_recv(r, to, true)
|
|
|
|
unless resp.kind_of? Rex::Proto::Http::Response
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
return resp
|
|
|
|
|
|
|
|
rescue ::Errno::EPIPE, ::Timeout::Error
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
2006-12-19 07:11:55 +00:00
|
|
|
#
|
|
|
|
# Read a response from the server
|
|
|
|
#
|
2013-02-27 22:27:37 +00:00
|
|
|
# @return [Response]
|
2012-10-29 03:57:18 +00:00
|
|
|
def read_response(t = -1, opts = {})
|
2007-02-22 07:33:00 +00:00
|
|
|
|
2006-12-19 07:11:55 +00:00
|
|
|
resp = Response.new
|
|
|
|
resp.max_data = config['read_max_data']
|
2005-07-24 20:53:54 +00:00
|
|
|
|
|
|
|
# Wait at most t seconds for the full response to be read in. We only
|
|
|
|
# do this if t was specified as a negative value indicating an infinite
|
|
|
|
# wait cycle. If t were specified as nil it would indicate that no
|
|
|
|
# response parsing is required.
|
* add junk pipelined request support
* fix socket creation on pipelined requests
* when a server says that the connection should be closed (Connection: closed), then close the connection, since its going to regardless, and we don't want to loose our state
* support non-standard line termination in headers. ie \n instead of \r\n
* add junk headers (X-rand: rand)
* add header folding (for evasion)
* add parse_header_re (still leaving parse_header around, though its dead code ATM) that does the right thing on non-standard line endings
* move 'gzip' to a 'compression' option
* add 'deflate' compression option (really, just raw zlib, and only firefox does deflate right)
* fix a bunch of TE:chunked decoding bugs based based on the fact that Apache doesn't always close chunks appropriately
* modify parse_body to not return state, since it doesn't always do that, and the return isn't used... self.state is.
* add TE:chunked request support
* normalize URIs in requests before saving them
* Move params out of the URI, but when the uri is requested, and the method is GET, and there are params, return a URI that has the params that are approrpiately encoded (needed for junk_params, see below)
* move request.to_s support of params to use the request params array when a POST, allows use of junk params support (see below). NOTE: If the body is provided, use the body instead of params, in case you want to hardcode the params in a POST request, eg: php_xmlrpc_eval.rb
* Add junk params when building a param list, eg: a=b becomes asdfasdf=asdrt32a&asdfad=okhgasd&a=b&hjklasdf=hkasgd
* add URI junk slash support (eg: /////foo.html)
* param splitting now supports both '&', and ';', which CGI.pm and PHP both allow
* add URI junk directory support, eg: /asdf/../foo.html
* add param encoding support, eg: param A with value '=' is A=%3d
* add URI junk self referring directory support, eg: /././foo.html
git-svn-id: file:///home/svn/incoming/trunk@3457 4d416f70-5f16-0410-b530-b9f4589650da
2006-01-27 21:57:44 +00:00
|
|
|
|
2009-11-30 21:15:06 +00:00
|
|
|
return resp if not t
|
|
|
|
|
|
|
|
Timeout.timeout((t < 0) ? nil : t) do
|
|
|
|
|
|
|
|
rv = nil
|
|
|
|
while (
|
|
|
|
rv != Packet::ParseCode::Completed and
|
|
|
|
rv != Packet::ParseCode::Error
|
|
|
|
)
|
2010-12-11 06:19:36 +00:00
|
|
|
|
2009-11-30 21:15:06 +00:00
|
|
|
begin
|
|
|
|
|
2010-12-11 07:37:09 +00:00
|
|
|
buff = conn.get_once(-1, 1)
|
2010-12-11 06:19:36 +00:00
|
|
|
rv = resp.parse( buff || '' )
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2010-08-18 22:41:07 +00:00
|
|
|
##########################################################################
|
|
|
|
# XXX: NOTE: BUG: get_once currently (as of r10042) rescues "Exception"
|
2012-10-29 03:57:18 +00:00
|
|
|
# As such, the following rescue block will never be reached. -jjd
|
2010-08-18 22:41:07 +00:00
|
|
|
##########################################################################
|
|
|
|
|
2009-11-30 21:15:06 +00:00
|
|
|
# Handle unexpected disconnects
|
|
|
|
rescue ::Errno::EPIPE, ::EOFError, ::IOError
|
|
|
|
case resp.state
|
|
|
|
when Packet::ParseState::ProcessingHeader
|
2009-12-10 16:54:27 +00:00
|
|
|
resp = nil
|
|
|
|
when Packet::ParseState::ProcessingBody
|
|
|
|
# truncated request, good enough
|
|
|
|
resp.error = :truncated
|
|
|
|
end
|
|
|
|
break
|
|
|
|
end
|
2010-03-25 21:35:37 +00:00
|
|
|
|
|
|
|
# This is a dirty hack for broken HTTP servers
|
|
|
|
if rv == Packet::ParseCode::Completed
|
|
|
|
rbody = resp.body
|
|
|
|
rbufq = resp.bufq
|
|
|
|
|
|
|
|
rblob = rbody.to_s + rbufq.to_s
|
|
|
|
tries = 0
|
|
|
|
begin
|
2010-11-05 16:20:13 +00:00
|
|
|
# XXX: This doesn't deal with chunked encoding or "Content-type: text/html; charset=..."
|
2010-07-06 23:08:28 +00:00
|
|
|
while tries < 1000 and resp.headers["Content-Type"]== "text/html" and rblob !~ /<\/html>/i
|
2010-03-25 21:35:37 +00:00
|
|
|
buff = conn.get_once(-1, 0.05)
|
|
|
|
break if not buff
|
|
|
|
rblob += buff
|
2010-06-07 23:56:35 +00:00
|
|
|
tries += 1
|
2010-03-25 21:35:37 +00:00
|
|
|
end
|
|
|
|
rescue ::Errno::EPIPE, ::EOFError, ::IOError
|
|
|
|
end
|
|
|
|
|
|
|
|
resp.bufq = ""
|
|
|
|
resp.body = rblob
|
|
|
|
end
|
2009-12-10 16:54:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-05 16:20:13 +00:00
|
|
|
return resp if not resp
|
|
|
|
|
|
|
|
# As a last minute hack, we check to see if we're dealing with a 100 Continue here.
|
2012-10-29 03:57:18 +00:00
|
|
|
# Most of the time this is handled by the parser via check_100()
|
|
|
|
if resp.proto == '1.1' and resp.code == 100 and not opts[:skip_100]
|
|
|
|
# Read the real response from the body if we found one
|
|
|
|
# If so, our real response became the body, so we re-parse it.
|
|
|
|
if resp.body.to_s =~ /^HTTP/
|
|
|
|
body = resp.body
|
|
|
|
resp = Response.new
|
|
|
|
resp.max_data = config['read_max_data']
|
|
|
|
rv = resp.parse(body)
|
|
|
|
# We found a 100 Continue but didn't read the real reply yet
|
|
|
|
# Otherwise reread the reply, but don't try this hack again
|
|
|
|
else
|
|
|
|
resp = read_response(t, :skip_100 => true)
|
|
|
|
end
|
2005-09-15 23:37:38 +00:00
|
|
|
end
|
2010-11-05 16:20:13 +00:00
|
|
|
|
2009-11-30 21:15:06 +00:00
|
|
|
resp
|
2005-07-24 20:53:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Cleans up any outstanding connections and other resources.
|
2005-07-24 20:53:54 +00:00
|
|
|
#
|
|
|
|
def stop
|
|
|
|
close
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns whether or not the conn is valid.
|
|
|
|
#
|
|
|
|
def conn?
|
|
|
|
conn != nil
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Whether or not connections should be pipelined.
|
2005-07-24 20:53:54 +00:00
|
|
|
#
|
|
|
|
def pipelining?
|
|
|
|
pipeline
|
|
|
|
end
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2006-12-19 07:11:55 +00:00
|
|
|
#
|
|
|
|
# The client request configuration
|
|
|
|
#
|
|
|
|
attr_accessor :config
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2007-03-17 20:10:57 +00:00
|
|
|
# The client request configuration classes
|
|
|
|
#
|
|
|
|
attr_accessor :config_types
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Whether or not pipelining is in use.
|
|
|
|
#
|
2005-07-24 20:53:54 +00:00
|
|
|
attr_accessor :pipeline
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# The local host of the client.
|
|
|
|
#
|
2005-07-24 20:53:54 +00:00
|
|
|
attr_accessor :local_host
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# The local port of the client.
|
|
|
|
#
|
2005-07-24 20:53:54 +00:00
|
|
|
attr_accessor :local_port
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# The underlying connection.
|
|
|
|
#
|
2005-09-22 03:24:32 +00:00
|
|
|
attr_accessor :conn
|
2006-01-17 04:35:44 +00:00
|
|
|
#
|
|
|
|
# The calling context to pass to the socket
|
|
|
|
#
|
|
|
|
attr_accessor :context
|
2007-09-24 04:44:44 +00:00
|
|
|
#
|
|
|
|
# The proxy list
|
|
|
|
#
|
|
|
|
attr_accessor :proxies
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2013-01-31 19:34:32 +00:00
|
|
|
# Auth
|
|
|
|
attr_accessor :username, :password
|
|
|
|
|
* add junk pipelined request support
* fix socket creation on pipelined requests
* when a server says that the connection should be closed (Connection: closed), then close the connection, since its going to regardless, and we don't want to loose our state
* support non-standard line termination in headers. ie \n instead of \r\n
* add junk headers (X-rand: rand)
* add header folding (for evasion)
* add parse_header_re (still leaving parse_header around, though its dead code ATM) that does the right thing on non-standard line endings
* move 'gzip' to a 'compression' option
* add 'deflate' compression option (really, just raw zlib, and only firefox does deflate right)
* fix a bunch of TE:chunked decoding bugs based based on the fact that Apache doesn't always close chunks appropriately
* modify parse_body to not return state, since it doesn't always do that, and the return isn't used... self.state is.
* add TE:chunked request support
* normalize URIs in requests before saving them
* Move params out of the URI, but when the uri is requested, and the method is GET, and there are params, return a URI that has the params that are approrpiately encoded (needed for junk_params, see below)
* move request.to_s support of params to use the request params array when a POST, allows use of junk params support (see below). NOTE: If the body is provided, use the body instead of params, in case you want to hardcode the params in a POST request, eg: php_xmlrpc_eval.rb
* Add junk params when building a param list, eg: a=b becomes asdfasdf=asdrt32a&asdfad=okhgasd&a=b&hjklasdf=hkasgd
* add URI junk slash support (eg: /////foo.html)
* param splitting now supports both '&', and ';', which CGI.pm and PHP both allow
* add URI junk directory support, eg: /asdf/../foo.html
* add param encoding support, eg: param A with value '=' is A=%3d
* add URI junk self referring directory support, eg: /././foo.html
git-svn-id: file:///home/svn/incoming/trunk@3457 4d416f70-5f16-0410-b530-b9f4589650da
2006-01-27 21:57:44 +00:00
|
|
|
|
|
|
|
# When parsing the request, thunk off the first response from the server, since junk
|
|
|
|
attr_accessor :junk_pipeline
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2005-07-24 20:53:54 +00:00
|
|
|
protected
|
|
|
|
|
2006-01-27 05:33:08 +00:00
|
|
|
# https
|
2009-10-26 19:48:15 +00:00
|
|
|
attr_accessor :ssl, :ssl_version # :nodoc:
|
2006-01-20 18:59:24 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
attr_accessor :hostname, :port # :nodoc:
|
2006-12-19 07:11:55 +00:00
|
|
|
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2005-07-24 20:53:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2009-03-08 07:55:47 +00:00
|
|
|
end
|
2009-11-02 18:14:57 +00:00
|
|
|
|