2005-07-11 02:03:48 +00:00
|
|
|
require 'msf/core'
|
|
|
|
|
|
|
|
module Msf
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This module acts as a base for all handler pseudo-modules. They aren't
|
|
|
|
# really modules, so don't get the wrong idea champs! They're merely
|
|
|
|
# mixed into dynamically generated payloads to handle monitoring for
|
|
|
|
# a connection. Handlers are layered in between the base payload
|
|
|
|
# class and any other payload class. A super cool ASCII diagram would
|
|
|
|
# look something like this
|
|
|
|
#
|
|
|
|
# Module
|
|
|
|
# ^
|
|
|
|
# |
|
|
|
|
# Payload
|
|
|
|
# ^
|
|
|
|
# |
|
|
|
|
# Handler
|
|
|
|
# ^
|
|
|
|
# |
|
|
|
|
# Stager
|
|
|
|
# ^
|
|
|
|
# |
|
|
|
|
# Stage
|
|
|
|
#
|
|
|
|
###
|
|
|
|
module Handler
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
##
|
2005-07-15 23:46:05 +00:00
|
|
|
#
|
|
|
|
# Constants used with the ``handler'' method to indicate whether or not the
|
2005-11-15 15:11:43 +00:00
|
|
|
# connection was used.
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returned by handlers to indicate that a socket has been claimed for use
|
|
|
|
# by the payload.
|
2005-07-15 23:46:05 +00:00
|
|
|
#
|
|
|
|
Claimed = "claimed"
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# Returned by handlers to indicate that a socket has not been claimed for
|
|
|
|
# use.
|
|
|
|
#
|
2005-07-15 23:46:05 +00:00
|
|
|
Unused = "unused"
|
|
|
|
|
2005-07-11 04:07:52 +00:00
|
|
|
#
|
2005-11-15 15:11:43 +00:00
|
|
|
# Returns the handler type.
|
2005-07-11 04:07:52 +00:00
|
|
|
#
|
|
|
|
def self.handler_type
|
|
|
|
return "none"
|
|
|
|
end
|
|
|
|
|
2005-10-19 01:48:10 +00:00
|
|
|
#
|
|
|
|
# Returns the transport-independent handler type.
|
|
|
|
#
|
|
|
|
def self.general_handler_type
|
|
|
|
"none"
|
|
|
|
end
|
|
|
|
|
2005-07-18 23:32:34 +00:00
|
|
|
#
|
|
|
|
# Returns the handler's name, if any.
|
|
|
|
#
|
|
|
|
def handler_name
|
|
|
|
module_info['HandlerName']
|
|
|
|
end
|
|
|
|
|
2005-07-16 07:32:11 +00:00
|
|
|
#
|
|
|
|
# Initializes the session waiter event and other fun stuff.
|
|
|
|
#
|
|
|
|
def initialize(info = {})
|
|
|
|
super
|
|
|
|
|
2005-11-11 01:22:03 +00:00
|
|
|
# Initialize the pending_connections counter to 0
|
|
|
|
self.pending_connections = 0
|
|
|
|
|
2005-07-16 07:32:11 +00:00
|
|
|
# Create the waiter event with auto_reset set to false so that
|
|
|
|
# if a session is ever created, waiting on it returns immediately.
|
|
|
|
self.session_waiter_event = Rex::Sync::Event.new(false, false)
|
|
|
|
end
|
|
|
|
|
2005-07-11 02:03:48 +00:00
|
|
|
#
|
2005-11-15 15:11:43 +00:00
|
|
|
# Sets up the connection handler.
|
2005-07-11 02:03:48 +00:00
|
|
|
#
|
|
|
|
def setup_handler
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 15:11:43 +00:00
|
|
|
# Terminates the connection handler.
|
2005-07-11 02:03:48 +00:00
|
|
|
#
|
|
|
|
def cleanup_handler
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 15:11:43 +00:00
|
|
|
# Start monitoring for a connection.
|
2005-07-11 02:03:48 +00:00
|
|
|
#
|
|
|
|
def start_handler
|
|
|
|
end
|
|
|
|
|
2007-01-18 03:51:15 +00:00
|
|
|
#
|
|
|
|
# Start another connection monitor
|
|
|
|
#
|
|
|
|
def add_handler(opts={})
|
|
|
|
end
|
|
|
|
|
2005-07-11 02:03:48 +00:00
|
|
|
#
|
2005-11-15 15:11:43 +00:00
|
|
|
# Stop monitoring for a connection.
|
2005-07-11 02:03:48 +00:00
|
|
|
#
|
|
|
|
def stop_handler
|
|
|
|
end
|
|
|
|
|
2005-07-15 23:46:05 +00:00
|
|
|
#
|
|
|
|
# Checks to see if a payload connection has been established on
|
|
|
|
# the supplied connection. This is necessary for find-sock style
|
|
|
|
# payloads.
|
|
|
|
#
|
|
|
|
def handler(sock)
|
|
|
|
end
|
2006-07-29 22:37:39 +00:00
|
|
|
|
2005-07-11 02:03:48 +00:00
|
|
|
#
|
|
|
|
# Handles an established connection supplied in the in and out
|
|
|
|
# handles. The handles are passed as parameters in case this
|
|
|
|
# handler is capable of handling multiple simultaneous
|
2005-07-18 05:13:21 +00:00
|
|
|
# connections. The default behavior is to attempt to create a session for
|
|
|
|
# the payload. This path will not be taken for mutli-staged payloads.
|
2005-07-16 07:32:11 +00:00
|
|
|
#
|
|
|
|
def handle_connection(conn)
|
2005-07-18 05:13:21 +00:00
|
|
|
create_session(conn)
|
2005-07-16 07:32:11 +00:00
|
|
|
end
|
|
|
|
|
2005-07-11 02:03:48 +00:00
|
|
|
#
|
2005-07-16 07:32:11 +00:00
|
|
|
# The amount of time to wait for a session to come in.
|
|
|
|
#
|
|
|
|
def wfs_delay
|
2005-07-17 07:43:24 +00:00
|
|
|
2
|
2005-07-11 02:03:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-07-16 07:32:11 +00:00
|
|
|
# Waits for a session to be created as the result of a handler connection
|
|
|
|
# coming in. The return value is a session object instance on success or
|
2005-11-15 15:11:43 +00:00
|
|
|
# nil if the timeout expires.
|
2005-07-11 02:03:48 +00:00
|
|
|
#
|
2005-07-16 07:32:11 +00:00
|
|
|
def wait_for_session(t = wfs_delay)
|
|
|
|
session = nil
|
|
|
|
|
|
|
|
begin
|
|
|
|
session = session_waiter_event.wait(t)
|
|
|
|
rescue ::TimeoutError
|
2005-11-11 01:22:03 +00:00
|
|
|
end
|
2005-11-28 20:41:11 +00:00
|
|
|
|
2005-11-11 01:22:03 +00:00
|
|
|
# If a connection has arrived, wait longer...
|
|
|
|
if (pending_connections > 0)
|
2005-11-11 01:27:57 +00:00
|
|
|
session = session_waiter_event.wait
|
2005-11-11 01:22:03 +00:00
|
|
|
end
|
2005-07-16 07:32:11 +00:00
|
|
|
|
|
|
|
return session
|
2005-07-11 02:03:48 +00:00
|
|
|
end
|
2006-07-29 22:37:39 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Set by the exploit module to configure handler
|
|
|
|
#
|
|
|
|
attr_accessor :exploit_config
|
2005-07-11 02:03:48 +00:00
|
|
|
|
2006-08-26 02:13:25 +00:00
|
|
|
#
|
|
|
|
# This will be non-nil if the handler has a parent payload that it
|
|
|
|
# was spawned from. Right now, this is only the case with generic
|
|
|
|
# payloads. The parent payload is used to create a session
|
|
|
|
# rather than using the instance itself.
|
|
|
|
#
|
|
|
|
attr_accessor :parent_payload
|
|
|
|
|
2005-07-11 02:03:48 +00:00
|
|
|
protected
|
|
|
|
|
2005-07-18 05:13:21 +00:00
|
|
|
#
|
|
|
|
# Creates a session, if necessary, for the connection that's been handled.
|
|
|
|
# Sessions are only created if the payload that's been mixed in has an
|
|
|
|
# associated session.
|
|
|
|
#
|
|
|
|
def create_session(conn)
|
2006-08-26 02:13:25 +00:00
|
|
|
# If there is a parent payload, then use that in preference.
|
|
|
|
return parent_payload.create_session(conn) if (parent_payload)
|
|
|
|
|
2005-07-18 05:13:21 +00:00
|
|
|
# If the payload we merged in with has an associated session factory,
|
|
|
|
# allocate a new session.
|
|
|
|
if (self.session)
|
|
|
|
s = self.session.new(conn)
|
|
|
|
|
|
|
|
# Pass along the framework context
|
|
|
|
s.framework = framework
|
|
|
|
|
2006-08-22 07:19:59 +00:00
|
|
|
# Call the payload's on_session handler. We run this prior to
|
|
|
|
# registering the session because the act of registering the session
|
|
|
|
# may lead to the termination of this thread.
|
|
|
|
on_session(s)
|
|
|
|
|
2005-07-18 05:13:21 +00:00
|
|
|
# If the session is valid, register it with the framework and
|
|
|
|
# notify any waiters we may have.
|
|
|
|
if (s)
|
|
|
|
register_session(s)
|
2005-11-24 18:50:33 +00:00
|
|
|
end
|
|
|
|
|
2005-12-30 06:49:25 +00:00
|
|
|
return s
|
2005-07-18 05:13:21 +00:00
|
|
|
end
|
2005-12-30 06:49:25 +00:00
|
|
|
|
2005-07-18 05:13:21 +00:00
|
|
|
end
|
|
|
|
|
2005-07-16 07:32:11 +00:00
|
|
|
#
|
|
|
|
# Registers a session with the framework and notifies any waiters of the
|
|
|
|
# new session.
|
|
|
|
#
|
|
|
|
def register_session(session)
|
2005-07-16 08:12:58 +00:00
|
|
|
# Register the session with the framework
|
|
|
|
framework.sessions.register(session)
|
2005-07-16 07:32:11 +00:00
|
|
|
|
2005-07-16 08:12:58 +00:00
|
|
|
# Notify waiters that they should be ready to rock
|
|
|
|
session_waiter_event.notify(session)
|
2005-11-11 01:22:03 +00:00
|
|
|
|
|
|
|
# Decrement the pending connections counter now that we've processed
|
|
|
|
# one session.
|
|
|
|
self.pending_connections -= 1
|
2005-07-16 07:32:11 +00:00
|
|
|
end
|
|
|
|
|
2005-10-19 03:37:22 +00:00
|
|
|
attr_accessor :session_waiter_event # :nodoc:
|
2005-11-11 01:22:03 +00:00
|
|
|
attr_accessor :pending_connections # :nodoc:
|
2006-07-29 22:37:39 +00:00
|
|
|
|
2005-07-11 02:03:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2005-07-17 06:01:11 +00:00
|
|
|
|
|
|
|
# The default none handler
|
|
|
|
require 'msf/core/handler/none'
|