2005-07-25 02:18:37 +00:00
|
|
|
require 'rex/socket'
|
|
|
|
require 'rex/proto/http'
|
2005-11-24 02:02:10 +00:00
|
|
|
require 'rex/proto/http/handler'
|
2005-07-25 02:18:37 +00:00
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Proto
|
|
|
|
module Http
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# Runtime extension of the HTTP clients that connect to the server.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
module ServerClient
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Initialize a new request instance.
|
2005-07-25 02:18:37 +00:00
|
|
|
#
|
|
|
|
def init_cli(server)
|
|
|
|
self.request = Request.new
|
|
|
|
self.server = server
|
|
|
|
self.keepalive = false
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Resets the parsing state.
|
2005-07-25 02:18:37 +00:00
|
|
|
#
|
|
|
|
def reset_cli
|
|
|
|
self.request.reset
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Transmits a response and adds the appropriate headers.
|
2005-07-25 02:18:37 +00:00
|
|
|
#
|
|
|
|
def send_response(response)
|
|
|
|
# Set the connection to close or keep-alive depending on what the client
|
|
|
|
# can support.
|
|
|
|
response['Connection'] = (keepalive) ? 'Keep-Alive' : 'close'
|
|
|
|
|
|
|
|
# Add any other standard response headers.
|
|
|
|
server.add_response_headers(response)
|
|
|
|
|
|
|
|
# Send it off.
|
|
|
|
put(response.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# The current request context.
|
2005-07-25 02:18:37 +00:00
|
|
|
#
|
|
|
|
attr_accessor :request
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Boolean that indicates whether or not the connection supports keep-alive.
|
2005-07-25 02:18:37 +00:00
|
|
|
#
|
|
|
|
attr_accessor :keepalive
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# A reference to the server the client is associated with.
|
2005-07-25 02:18:37 +00:00
|
|
|
#
|
|
|
|
attr_accessor :server
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# Acts as an HTTP server, processing requests and dispatching them to
|
2005-11-24 02:02:10 +00:00
|
|
|
# registered procs. Some of this server was modeled after webrick.
|
2005-07-25 02:18:37 +00:00
|
|
|
#
|
|
|
|
###
|
|
|
|
class Server
|
|
|
|
|
2005-08-22 04:34:40 +00:00
|
|
|
include Proto
|
|
|
|
|
2005-11-24 03:31:23 +00:00
|
|
|
#
|
|
|
|
# A hash that associated a file extension with a mime type for use as the
|
|
|
|
# content type of responses.
|
|
|
|
#
|
|
|
|
ExtensionMimeTypes =
|
|
|
|
{
|
|
|
|
"rhtml" => "text/html",
|
|
|
|
"html" => "text/html",
|
|
|
|
"htm" => "text/htm",
|
|
|
|
"jpg" => "image/jpeg",
|
|
|
|
"jpeg" => "image/jpeg",
|
|
|
|
"jpeg" => "image/jpeg",
|
|
|
|
"gif" => "image/gif",
|
|
|
|
"png" => "image/png",
|
|
|
|
"bmp" => "image/bmp",
|
|
|
|
"txt" => "text/plain",
|
|
|
|
"css" => "text/css",
|
2005-11-24 04:23:43 +00:00
|
|
|
"ico" => "image/x-icon",
|
2005-11-24 03:31:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# The default server name that will be returned in the Server attribute of
|
|
|
|
# a response.
|
|
|
|
#
|
2005-07-25 02:18:37 +00:00
|
|
|
DefaultServer = "Rex"
|
|
|
|
|
2005-11-24 03:31:23 +00:00
|
|
|
#
|
|
|
|
# Initializes an HTTP server as listening on the provided port and
|
|
|
|
# hostname.
|
|
|
|
#
|
2006-01-17 04:35:44 +00:00
|
|
|
def initialize(port = 80, listen_host = '0.0.0.0', context = {})
|
2005-07-25 02:18:37 +00:00
|
|
|
self.listen_host = listen_host
|
2005-09-22 03:24:32 +00:00
|
|
|
self.listen_port = port
|
2006-01-17 04:35:44 +00:00
|
|
|
self.context = context
|
2005-07-25 02:18:37 +00:00
|
|
|
self.listener = nil
|
|
|
|
self.resources = {}
|
2006-01-11 22:16:20 +00:00
|
|
|
self.server_name = DefaultServer
|
2009-10-20 17:24:33 +00:00
|
|
|
self.ssl = false
|
2005-07-25 02:18:37 +00:00
|
|
|
end
|
|
|
|
|
2006-08-08 03:29:26 +00:00
|
|
|
#
|
|
|
|
# Returns the hardcore alias for the HTTP service
|
|
|
|
#
|
|
|
|
def self.hardcore_alias(*args)
|
|
|
|
"#{(args[0] || '')}#{(args[1] || '')}"
|
|
|
|
end
|
|
|
|
|
2005-08-22 04:34:40 +00:00
|
|
|
#
|
|
|
|
# HTTP server.
|
|
|
|
#
|
|
|
|
def alias
|
|
|
|
super || "HTTP Server"
|
|
|
|
end
|
|
|
|
|
2005-07-25 02:18:37 +00:00
|
|
|
#
|
|
|
|
# Listens on the defined port and host and starts monitoring for clients.
|
|
|
|
#
|
|
|
|
def start
|
2008-11-16 21:00:44 +00:00
|
|
|
|
2005-07-25 02:18:37 +00:00
|
|
|
self.listener = Rex::Socket::TcpServer.create(
|
|
|
|
'LocalHost' => self.listen_host,
|
2006-01-17 04:35:44 +00:00
|
|
|
'LocalPort' => self.listen_port,
|
2006-04-24 18:49:00 +00:00
|
|
|
'Context' => self.context,
|
|
|
|
'SSL' => self.ssl
|
2006-01-17 04:35:44 +00:00
|
|
|
)
|
2005-08-22 02:36:09 +00:00
|
|
|
|
|
|
|
# Register callbacks
|
|
|
|
self.listener.on_client_connect_proc = Proc.new { |cli|
|
|
|
|
on_client_connect(cli)
|
2005-07-25 02:18:37 +00:00
|
|
|
}
|
2005-08-22 02:36:09 +00:00
|
|
|
self.listener.on_client_data_proc = Proc.new { |cli|
|
|
|
|
on_client_data(cli)
|
2005-07-25 02:18:37 +00:00
|
|
|
}
|
2005-08-22 02:36:09 +00:00
|
|
|
|
|
|
|
self.listener.start
|
2005-07-25 02:18:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Terminates the monitor thread and turns off the listener.
|
|
|
|
#
|
|
|
|
def stop
|
2005-08-22 02:36:09 +00:00
|
|
|
self.listener.stop
|
2005-07-25 02:18:37 +00:00
|
|
|
self.listener.close
|
|
|
|
end
|
|
|
|
|
2008-11-16 21:00:44 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Waits for the HTTP service to terminate
|
|
|
|
#
|
|
|
|
def wait
|
|
|
|
self.listener.wait if self.listener
|
|
|
|
end
|
|
|
|
|
2005-07-25 02:18:37 +00:00
|
|
|
#
|
2005-08-22 02:36:09 +00:00
|
|
|
# Closes the supplied client, if valid.
|
2005-07-25 02:18:37 +00:00
|
|
|
#
|
|
|
|
def close_client(cli)
|
2005-08-22 02:36:09 +00:00
|
|
|
listener.close_client(cli)
|
2005-07-25 02:18:37 +00:00
|
|
|
end
|
|
|
|
|
2005-11-24 02:02:10 +00:00
|
|
|
#
|
|
|
|
# Mounts a directory or resource as being serviced by the supplied handler.
|
|
|
|
#
|
|
|
|
def mount(root, handler, long_call = false, *args)
|
|
|
|
resources[root] = [ handler, long_call, args ]
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Remove the mount point.
|
|
|
|
#
|
|
|
|
def unmount(root)
|
|
|
|
resources.delete(root)
|
|
|
|
end
|
|
|
|
|
2005-07-25 02:39:14 +00:00
|
|
|
#
|
|
|
|
# Adds a resource handler, such as one for /, which will be called whenever
|
|
|
|
# the resource is requested. The ``opts'' parameter can have any of the
|
|
|
|
# following:
|
|
|
|
#
|
2005-11-23 17:06:16 +00:00
|
|
|
# Proc (proc) - The procedure to call when a request comes in for this resource.
|
|
|
|
# LongCall (bool) - Hints to the server that this resource may have long
|
|
|
|
# request processing times.
|
2005-07-25 02:39:14 +00:00
|
|
|
#
|
|
|
|
def add_resource(name, opts)
|
2005-11-24 02:02:10 +00:00
|
|
|
if (resources[name])
|
2005-07-25 02:39:14 +00:00
|
|
|
raise RuntimeError,
|
|
|
|
"The supplied resource '#{name}' is already added.", caller
|
|
|
|
end
|
|
|
|
|
2005-11-24 02:02:10 +00:00
|
|
|
# If a procedure was passed, mount the resource with it.
|
|
|
|
if (opts['Proc'])
|
2006-01-07 10:04:30 +00:00
|
|
|
mount(name, Handler::Proc, false, opts['Proc'], opts['VirtualDirectory'])
|
2005-11-24 02:02:10 +00:00
|
|
|
else
|
|
|
|
raise ArgumentError, "You must specify a procedure."
|
|
|
|
end
|
2005-07-25 02:39:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Removes the supplied resource handler.
|
|
|
|
#
|
|
|
|
def remove_resource(name)
|
|
|
|
self.resources.delete(name)
|
|
|
|
end
|
|
|
|
|
2005-07-25 02:18:37 +00:00
|
|
|
#
|
2005-08-22 02:36:09 +00:00
|
|
|
# Adds Server headers and stuff.
|
2005-07-25 02:18:37 +00:00
|
|
|
#
|
|
|
|
def add_response_headers(resp)
|
2006-01-11 22:16:20 +00:00
|
|
|
resp['Server'] = self.server_name
|
2005-07-25 02:18:37 +00:00
|
|
|
end
|
|
|
|
|
2005-11-24 03:31:23 +00:00
|
|
|
#
|
|
|
|
# Returns the mime type associated with the supplied file. Right now the
|
|
|
|
# set of mime types is fairly limited.
|
|
|
|
#
|
|
|
|
def mime_type(file)
|
|
|
|
type = nil
|
|
|
|
|
|
|
|
if (file =~ /\.(.+?)$/)
|
|
|
|
type = ExtensionMimeTypes[$1.downcase]
|
|
|
|
end
|
|
|
|
|
|
|
|
type || "text/plain"
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Sends a 404 error to the client for a given request.
|
|
|
|
#
|
|
|
|
def send_e404(cli, request)
|
|
|
|
resp = Response::E404.new
|
|
|
|
|
2005-11-25 04:16:43 +00:00
|
|
|
resp['Content-Type'] = 'text/html'
|
|
|
|
|
2005-11-24 03:31:23 +00:00
|
|
|
resp.body =
|
|
|
|
"<html><head>" +
|
2005-11-24 05:51:20 +00:00
|
|
|
"<title>404 Not Found</title>" +
|
2005-11-24 03:31:23 +00:00
|
|
|
"</head><body>" +
|
|
|
|
"<h1>Not found</h1>" +
|
2005-11-25 04:16:43 +00:00
|
|
|
"The requested URL #{html_escape(request.resource)} was not found on this server.<p><hr>" +
|
2005-11-24 03:31:23 +00:00
|
|
|
"</body></html>"
|
|
|
|
|
|
|
|
# Send the response to the client like what
|
|
|
|
cli.send_response(resp)
|
|
|
|
end
|
|
|
|
|
2006-04-24 18:49:00 +00:00
|
|
|
attr_accessor :listen_port, :listen_host, :server_name, :context, :ssl
|
2005-07-25 02:18:37 +00:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
2005-08-22 02:36:09 +00:00
|
|
|
attr_accessor :listener, :resources
|
2005-07-25 02:18:37 +00:00
|
|
|
|
|
|
|
#
|
2005-08-22 02:36:09 +00:00
|
|
|
# Extends new clients with the ServerClient module and initializes them.
|
2005-07-25 02:18:37 +00:00
|
|
|
#
|
2005-08-22 02:36:09 +00:00
|
|
|
def on_client_connect(cli)
|
|
|
|
cli.extend(ServerClient)
|
2005-07-25 02:18:37 +00:00
|
|
|
|
2005-08-22 02:36:09 +00:00
|
|
|
cli.init_cli(self)
|
2005-07-25 02:18:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-08-22 02:36:09 +00:00
|
|
|
# Processes data coming in from a client.
|
2005-07-25 02:18:37 +00:00
|
|
|
#
|
2005-08-22 02:36:09 +00:00
|
|
|
def on_client_data(cli)
|
2005-07-25 02:18:37 +00:00
|
|
|
begin
|
2009-12-03 07:40:37 +00:00
|
|
|
#
|
|
|
|
# XXX: Handle ParseCode::Partial
|
|
|
|
#
|
2005-07-25 02:18:37 +00:00
|
|
|
case cli.request.parse(cli.get)
|
|
|
|
when Packet::ParseCode::Completed
|
|
|
|
dispatch_request(cli, cli.request)
|
|
|
|
|
|
|
|
cli.reset_cli
|
|
|
|
when Packet::ParseCode::Error
|
|
|
|
close_client(cli)
|
|
|
|
end
|
|
|
|
rescue EOFError
|
|
|
|
if (cli.request.completed?)
|
|
|
|
dispatch_request(cli, cli.request)
|
|
|
|
|
|
|
|
cli.reset_cli
|
|
|
|
end
|
|
|
|
|
|
|
|
close_client(cli)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Dispatches the supplied request for a given connection.
|
2005-07-25 02:18:37 +00:00
|
|
|
#
|
|
|
|
def dispatch_request(cli, request)
|
|
|
|
# Is the client requesting keep-alive?
|
|
|
|
if ((request['Connection']) and
|
|
|
|
(request['Connection'].downcase == 'Keep-Alive'.downcase))
|
|
|
|
cli.keepalive = true
|
|
|
|
end
|
|
|
|
|
2005-11-24 02:02:10 +00:00
|
|
|
# Search for the resource handler for the requested URL. This is pretty
|
|
|
|
# inefficient right now, but we can spruce it up later.
|
|
|
|
p = nil
|
|
|
|
len = 0
|
|
|
|
root = nil
|
|
|
|
|
|
|
|
resources.each_pair { |k, val|
|
|
|
|
if (request.resource =~ /^#{k}/ and k.length > len)
|
|
|
|
p = val
|
|
|
|
len = k.length
|
|
|
|
root = k
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2005-11-23 17:06:16 +00:00
|
|
|
if (p)
|
2005-11-24 02:02:10 +00:00
|
|
|
# Create an instance of the handler for this resource
|
|
|
|
handler = p[0].new(self, *p[2])
|
|
|
|
|
|
|
|
# If the handler class requires a relative resource...
|
2006-01-07 10:04:30 +00:00
|
|
|
if (handler.relative_resource_required?)
|
2005-11-24 02:02:10 +00:00
|
|
|
# Substituted the mount point root in the request to make things
|
|
|
|
# relative to the mount point.
|
2005-11-24 03:31:23 +00:00
|
|
|
request.relative_resource = request.resource.gsub(/^#{root}/, '')
|
2005-11-24 02:02:10 +00:00
|
|
|
request.relative_resource = '/' + request.relative_resource if (request.relative_resource !~ /^\//)
|
|
|
|
end
|
2005-11-24 03:31:23 +00:00
|
|
|
|
2005-11-24 02:02:10 +00:00
|
|
|
|
|
|
|
# If we found the resource handler for this resource, call its
|
|
|
|
# procedure.
|
|
|
|
if (p[1] == true)
|
2005-07-25 02:18:37 +00:00
|
|
|
Thread.new {
|
2005-11-24 02:02:10 +00:00
|
|
|
handler.on_request(cli, request)
|
2005-07-25 02:18:37 +00:00
|
|
|
}
|
|
|
|
else
|
2005-11-24 02:02:10 +00:00
|
|
|
handler.on_request(cli, request)
|
2005-07-25 02:18:37 +00:00
|
|
|
end
|
|
|
|
else
|
2005-11-24 02:02:10 +00:00
|
|
|
elog("Failed to find handler for resource: #{request.resource}",
|
|
|
|
LogSource)
|
|
|
|
|
2005-07-25 02:18:37 +00:00
|
|
|
send_e404(cli, request)
|
|
|
|
end
|
|
|
|
|
|
|
|
# If keep-alive isn't enabled for this client, close the connection
|
|
|
|
if (cli.keepalive == false)
|
|
|
|
close_client(cli)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2008-11-16 21:00:44 +00:00
|
|
|
end
|