2005-12-17 06:46:23 +00:00
|
|
|
#!/usr/bin/env ruby
|
2005-04-10 02:05:43 +00:00
|
|
|
|
2005-07-09 21:18:49 +00:00
|
|
|
require 'rex/post/meterpreter/packet_response_waiter'
|
2006-08-21 21:35:03 +00:00
|
|
|
require 'rex/logging'
|
2006-08-24 22:54:06 +00:00
|
|
|
require 'rex/exceptions'
|
2005-04-10 02:05:43 +00:00
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Post
|
|
|
|
module Meterpreter
|
|
|
|
|
2005-07-19 04:21:15 +00:00
|
|
|
###
|
|
|
|
#
|
|
|
|
# Exception thrown when a request fails.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class RequestError < ArgumentError
|
2010-10-20 01:05:50 +00:00
|
|
|
def initialize(method, einfo, ecode=nil)
|
2005-07-19 04:21:15 +00:00
|
|
|
@method = method
|
2010-10-20 01:05:50 +00:00
|
|
|
@result = einfo
|
|
|
|
@code = ecode || einfo
|
2005-07-19 04:21:15 +00:00
|
|
|
end
|
2006-09-16 07:21:41 +00:00
|
|
|
|
2005-07-19 04:21:15 +00:00
|
|
|
def to_s
|
|
|
|
"#{@method}: Operation failed: #{@result}"
|
|
|
|
end
|
2006-09-16 07:21:41 +00:00
|
|
|
|
|
|
|
# The method that failed.
|
|
|
|
attr_reader :method
|
|
|
|
|
2010-10-20 01:05:50 +00:00
|
|
|
# The error result that occurred, typically a windows error message.
|
2006-09-16 07:21:41 +00:00
|
|
|
attr_reader :result
|
2011-06-28 21:26:43 +00:00
|
|
|
|
2010-10-20 01:05:50 +00:00
|
|
|
# The error result that occurred, typically a windows error code.
|
2011-06-28 21:26:43 +00:00
|
|
|
attr_reader :code
|
2005-07-19 04:21:15 +00:00
|
|
|
end
|
|
|
|
|
2005-04-10 02:05:43 +00:00
|
|
|
###
|
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
# Handles packet transmission, reception, and correlation,
|
|
|
|
# and processing
|
2005-04-10 02:05:43 +00:00
|
|
|
#
|
|
|
|
###
|
|
|
|
module PacketDispatcher
|
|
|
|
|
2010-10-08 05:00:03 +00:00
|
|
|
PacketTimeout = 600
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2011-06-28 21:26:43 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Synchronization
|
|
|
|
#
|
|
|
|
##
|
|
|
|
attr_accessor :comm_mutex
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# Passive Dispatching
|
|
|
|
#
|
|
|
|
##
|
|
|
|
attr_accessor :passive_service, :send_queue, :recv_queue
|
|
|
|
|
|
|
|
def initialize_passive_dispatcher
|
|
|
|
self.send_queue = []
|
|
|
|
self.recv_queue = []
|
|
|
|
self.waiters = []
|
|
|
|
self.alive = true
|
|
|
|
|
|
|
|
self.passive_service = self.passive_dispatcher
|
|
|
|
self.passive_service.remove_resource("/" + self.conn_id + "/")
|
|
|
|
self.passive_service.add_resource("/" + self.conn_id + "/",
|
|
|
|
'Proc' => Proc.new { |cli, req| on_passive_request(cli, req) },
|
|
|
|
'VirtualDirectory' => true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def shutdown_passive_dispatcher
|
|
|
|
return if not self.passive_service
|
|
|
|
self.passive_service.remove_resource("/" + self.conn_id + "/")
|
|
|
|
|
|
|
|
self.alive = false
|
|
|
|
self.send_queue = []
|
|
|
|
self.recv_queue = []
|
|
|
|
self.waiters = []
|
|
|
|
|
|
|
|
self.passive_service = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_passive_request(cli, req)
|
|
|
|
|
|
|
|
begin
|
|
|
|
|
|
|
|
resp = Rex::Proto::Http::Response.new(200, "OK")
|
|
|
|
resp['Content-Type'] = 'application/octet-stream'
|
|
|
|
resp['Connection'] = 'close'
|
|
|
|
|
|
|
|
# If the first 4 bytes are "RECV", return the oldest packet from the outbound queue
|
|
|
|
if req.body[0,4] == "RECV"
|
|
|
|
rpkt = send_queue.pop
|
|
|
|
resp.body = rpkt || ''
|
|
|
|
begin
|
|
|
|
cli.send_response(resp)
|
|
|
|
rescue ::Exception => e
|
|
|
|
send_queue.unshift(rpkt) if rpkt
|
|
|
|
elog("Exception sending a reply to the reader request: #{cli.inspect} #{e.class} #{e} #{e.backtrace}")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
resp.body = ""
|
|
|
|
if req.body and req.body.length > 0
|
|
|
|
packet = Packet.new(0)
|
|
|
|
packet.from_r(req.body)
|
|
|
|
dispatch_inbound_packet(packet)
|
|
|
|
end
|
|
|
|
cli.send_response(resp)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Force a closure for older WinInet implementations
|
|
|
|
self.passive_service.close_client( cli )
|
|
|
|
|
|
|
|
rescue ::Exception => e
|
|
|
|
elog("Exception handling request: #{cli.inspect} #{req.inspect} #{e.class} #{e} #{e.backtrace}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Transmission
|
|
|
|
#
|
|
|
|
##
|
2005-04-10 02:05:43 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Sends a packet without waiting for a response.
|
|
|
|
#
|
2005-04-10 02:05:43 +00:00
|
|
|
def send_packet(packet, completion_routine = nil, completion_param = nil)
|
|
|
|
if (completion_routine)
|
|
|
|
add_response_waiter(packet, completion_routine, completion_param)
|
|
|
|
end
|
|
|
|
|
|
|
|
bytes = 0
|
|
|
|
raw = packet.to_r
|
2011-05-16 02:00:47 +00:00
|
|
|
err = nil
|
2005-04-10 02:05:43 +00:00
|
|
|
|
2011-06-28 21:26:43 +00:00
|
|
|
# Short-circuit send when using a passive dispatcher
|
|
|
|
if self.passive_service
|
|
|
|
send_queue.push(raw)
|
|
|
|
return raw.size # Lie!
|
|
|
|
end
|
|
|
|
|
2005-04-10 02:05:43 +00:00
|
|
|
if (raw)
|
2011-06-28 21:26:43 +00:00
|
|
|
|
|
|
|
# This mutex is used to lock out new commands during an
|
|
|
|
# active migration.
|
|
|
|
|
|
|
|
self.comm_mutex.synchronize do
|
|
|
|
begin
|
|
|
|
bytes = self.sock.write(raw)
|
|
|
|
rescue ::Exception => e
|
|
|
|
err = e
|
|
|
|
end
|
2011-05-16 02:00:47 +00:00
|
|
|
end
|
2011-06-28 21:26:43 +00:00
|
|
|
|
2011-05-16 02:00:47 +00:00
|
|
|
if bytes.to_i == 0
|
2010-09-25 03:57:30 +00:00
|
|
|
# Mark the session itself as dead
|
|
|
|
self.alive = false
|
2011-06-28 21:26:43 +00:00
|
|
|
|
2010-09-25 03:57:30 +00:00
|
|
|
# Indicate that the dispatcher should shut down too
|
|
|
|
@finish = true
|
2011-06-28 21:26:43 +00:00
|
|
|
|
2010-09-25 03:57:30 +00:00
|
|
|
# Reraise the error to the top-level caller
|
2011-06-28 21:26:43 +00:00
|
|
|
raise err if err
|
2010-09-25 03:57:30 +00:00
|
|
|
end
|
2009-12-04 21:08:34 +00:00
|
|
|
end
|
2005-04-10 02:05:43 +00:00
|
|
|
|
|
|
|
return bytes
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Sends a packet and waits for a timeout for the given time interval.
|
|
|
|
#
|
2005-07-08 00:28:52 +00:00
|
|
|
def send_request(packet, t = self.response_timeout)
|
2011-06-28 21:26:43 +00:00
|
|
|
|
2010-11-03 03:03:29 +00:00
|
|
|
if not t
|
|
|
|
send_packet(packet)
|
|
|
|
return nil
|
|
|
|
end
|
2011-06-28 21:26:43 +00:00
|
|
|
|
2005-04-10 16:21:53 +00:00
|
|
|
response = send_packet_wait_response(packet, t)
|
|
|
|
|
|
|
|
if (response == nil)
|
2010-05-11 07:37:57 +00:00
|
|
|
raise TimeoutError.new("Send timed out")
|
2005-04-10 16:21:53 +00:00
|
|
|
elsif (response.result != 0)
|
2010-10-20 01:05:50 +00:00
|
|
|
einfo = lookup_error(response.result)
|
|
|
|
e = RequestError.new(packet.method, einfo, response.result)
|
2005-07-19 04:21:15 +00:00
|
|
|
|
|
|
|
e.set_backtrace(caller)
|
|
|
|
|
|
|
|
raise e
|
2005-04-10 16:21:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return response
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Transmits a packet and waits for a response.
|
|
|
|
#
|
2005-04-10 02:05:43 +00:00
|
|
|
def send_packet_wait_response(packet, t)
|
|
|
|
# First, add the waiter association for the supplied packet
|
|
|
|
waiter = add_response_waiter(packet)
|
|
|
|
|
|
|
|
# Transmit the packet
|
2011-05-16 02:00:47 +00:00
|
|
|
if (send_packet(packet).to_i <= 0)
|
2009-12-04 21:08:34 +00:00
|
|
|
# Remove the waiter if we failed to send the packet.
|
2009-12-04 17:32:56 +00:00
|
|
|
remove_response_waiter(waiter)
|
2009-12-04 21:08:34 +00:00
|
|
|
return nil
|
2005-04-10 02:05:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Wait for the supplied time interval
|
|
|
|
waiter.wait(t)
|
|
|
|
|
2009-12-04 21:08:34 +00:00
|
|
|
# Remove the waiter from the list of waiters in case it wasn't
|
2005-04-10 02:05:43 +00:00
|
|
|
# removed
|
|
|
|
remove_response_waiter(waiter)
|
|
|
|
|
|
|
|
# Return the response packet, if any
|
|
|
|
return waiter.response
|
|
|
|
end
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Reception
|
|
|
|
#
|
|
|
|
##
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
# Monitors the PacketDispatcher's sock for data in its own
|
2005-11-15 05:22:13 +00:00
|
|
|
# thread context and parsers all inbound packets.
|
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
def monitor_socket
|
2011-06-28 21:26:43 +00:00
|
|
|
|
|
|
|
# Skip if we are using a passive dispatcher
|
|
|
|
return if self.passive_service
|
|
|
|
|
|
|
|
self.comm_mutex = ::Mutex.new
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
self.waiters = []
|
|
|
|
|
2010-01-28 23:53:15 +00:00
|
|
|
@pqueue = []
|
|
|
|
@finish = false
|
|
|
|
@last_recvd = Time.now
|
|
|
|
@ping_sent = false
|
|
|
|
|
2010-01-29 18:52:44 +00:00
|
|
|
self.alive = true
|
|
|
|
|
2010-01-28 23:53:15 +00:00
|
|
|
# Spawn a thread for receiving packets
|
2010-11-12 06:19:49 +00:00
|
|
|
self.receiver_thread = Rex::ThreadFactory.spawn("MeterpreterReceiver", false) do
|
2010-09-25 03:57:30 +00:00
|
|
|
while (self.alive)
|
2010-01-28 23:53:15 +00:00
|
|
|
begin
|
|
|
|
rv = Rex::ThreadSafe.select([ self.sock.fd ], nil, nil, 0.25)
|
|
|
|
ping_time = 60
|
|
|
|
# If there's nothing to read, and it's been awhile since we
|
|
|
|
# saw a packet, we need to send a ping. We wait
|
|
|
|
# ping_time*2 seconds before deciding a session is dead.
|
|
|
|
if (not rv and self.send_keepalives and Time.now - @last_recvd > ping_time)
|
|
|
|
# If the queue is empty and we've already sent a
|
|
|
|
# keepalive without getting a reply, then this
|
|
|
|
# session is hosed, and we should give up on it.
|
|
|
|
if @ping_sent and @pqueue.empty? and (Time.now - @last_recvd > ping_time * 2)
|
|
|
|
dlog("No response to ping, session #{self.sid} is dead", LEV_3)
|
|
|
|
self.alive = false
|
|
|
|
@finish = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
# Let the packet queue processor finish up before
|
|
|
|
# we send a ping.
|
|
|
|
if not @ping_sent and @pqueue.empty?
|
|
|
|
# Our 'ping' is actually just a check for eof on
|
|
|
|
# channel id 0. This method has no side effects
|
|
|
|
# and always returns an answer (regardless of the
|
|
|
|
# existence of chan 0), which is all that's
|
|
|
|
# needed for a liveness check. The answer itself
|
|
|
|
# is unimportant and is ignored.
|
|
|
|
pkt = Packet.create_request('core_channel_eof')
|
|
|
|
pkt.add_tlv(TLV_TYPE_CHANNEL_ID, 0)
|
|
|
|
waiter = Proc.new { |response, param|
|
|
|
|
@ping_sent = false
|
|
|
|
@last_recvd = Time.now
|
|
|
|
}
|
|
|
|
send_packet(pkt, waiter)
|
|
|
|
@ping_sent = true
|
|
|
|
end
|
|
|
|
next
|
2009-12-10 05:40:26 +00:00
|
|
|
end
|
2010-01-28 23:53:15 +00:00
|
|
|
next if not rv
|
|
|
|
packet = receive_packet
|
|
|
|
@pqueue << packet if packet
|
|
|
|
@last_recvd = Time.now
|
|
|
|
rescue ::Exception
|
|
|
|
dlog("Exception caught in monitor_socket: #{$!}", 'meterpreter', LEV_1)
|
|
|
|
@finish = true
|
|
|
|
self.alive = false
|
|
|
|
break
|
2009-12-10 05:40:26 +00:00
|
|
|
end
|
|
|
|
end
|
2010-01-28 23:53:15 +00:00
|
|
|
end
|
2009-12-10 05:40:26 +00:00
|
|
|
|
2010-01-28 23:53:15 +00:00
|
|
|
# Spawn a new thread that monitors the socket
|
2010-11-12 06:19:49 +00:00
|
|
|
self.dispatcher_thread = Rex::ThreadFactory.spawn("MeterpreterDispatcher", false) do
|
2009-12-08 18:32:26 +00:00
|
|
|
begin
|
2010-01-28 23:53:15 +00:00
|
|
|
# Whether we're finished or not is determined by the receiver
|
|
|
|
# thread above.
|
2009-12-10 05:40:26 +00:00
|
|
|
while(not @finish)
|
|
|
|
if(@pqueue.empty?)
|
2010-05-20 20:42:17 +00:00
|
|
|
::IO.select(nil, nil, nil, 0.10)
|
2009-12-10 05:40:26 +00:00
|
|
|
next
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2009-12-10 05:40:26 +00:00
|
|
|
incomplete = []
|
|
|
|
backlog = []
|
|
|
|
|
|
|
|
while(@pqueue.length > 0)
|
|
|
|
backlog << @pqueue.shift
|
2005-07-18 05:59:27 +00:00
|
|
|
end
|
2005-04-12 05:37:11 +00:00
|
|
|
|
2009-12-10 05:40:26 +00:00
|
|
|
#
|
|
|
|
# Prioritize message processing here
|
|
|
|
# 1. Close should always be processed at the end
|
|
|
|
# 2. Command responses always before channel data
|
|
|
|
#
|
|
|
|
|
|
|
|
tmp_command = []
|
|
|
|
tmp_channel = []
|
|
|
|
tmp_close = []
|
|
|
|
backlog.each do |pkt|
|
|
|
|
if(pkt.response?)
|
|
|
|
tmp_command << pkt
|
|
|
|
next
|
2009-12-04 21:08:34 +00:00
|
|
|
end
|
2009-12-10 05:40:26 +00:00
|
|
|
if(pkt.method == "core_channel_close")
|
|
|
|
tmp_close << pkt
|
|
|
|
next
|
|
|
|
end
|
|
|
|
tmp_channel << pkt
|
2005-04-12 05:37:11 +00:00
|
|
|
end
|
|
|
|
|
2009-12-10 05:40:26 +00:00
|
|
|
backlog = []
|
|
|
|
backlog.push(*tmp_command)
|
|
|
|
backlog.push(*tmp_channel)
|
|
|
|
backlog.push(*tmp_close)
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Process the message queue
|
|
|
|
#
|
|
|
|
|
2009-12-04 21:08:34 +00:00
|
|
|
backlog.each do |pkt|
|
2009-12-10 05:40:26 +00:00
|
|
|
|
|
|
|
begin
|
2009-12-04 21:08:34 +00:00
|
|
|
if ! dispatch_inbound_packet(pkt)
|
2009-12-08 18:32:26 +00:00
|
|
|
# Only requeue packets newer than the timeout
|
|
|
|
if (::Time.now.to_i - pkt.created_at.to_i < PacketTimeout)
|
|
|
|
incomplete << pkt
|
|
|
|
end
|
2009-12-04 21:08:34 +00:00
|
|
|
end
|
2009-12-10 05:40:26 +00:00
|
|
|
|
|
|
|
rescue ::Exception => e
|
|
|
|
dlog("Dispatching exception with packet #{pkt}: #{e} #{e.backtrace}", 'meterpreter', LEV_1)
|
|
|
|
end
|
2009-12-04 21:08:34 +00:00
|
|
|
end
|
|
|
|
|
2009-12-10 05:40:26 +00:00
|
|
|
@pqueue.unshift(*incomplete)
|
2009-12-04 21:08:34 +00:00
|
|
|
|
2009-12-10 05:40:26 +00:00
|
|
|
if(@pqueue.length > 100)
|
|
|
|
dlog("Backlog has grown to over 100 in monitor_socket, dropping older packets: #{@pqueue[0 .. 25].map{|x| x.inspect}.join(" - ")}", 'meterpreter', LEV_1)
|
|
|
|
@pqueue = @pqueue[25 .. 100]
|
2005-04-12 05:37:11 +00:00
|
|
|
end
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
rescue ::Exception => e
|
|
|
|
dlog("Exception caught in monitor_socket dispatcher: #{e.class} #{e} #{e.backtrace}", 'meterpreter', LEV_1)
|
2009-12-10 05:40:26 +00:00
|
|
|
ensure
|
2010-02-23 05:59:30 +00:00
|
|
|
self.receiver_thread.kill if self.receiver_thread
|
2009-12-08 18:32:26 +00:00
|
|
|
end
|
2009-12-10 05:40:26 +00:00
|
|
|
end
|
2005-04-12 05:37:11 +00:00
|
|
|
end
|
|
|
|
|
2009-12-10 05:40:26 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
# Parses data from the dispatcher's sock and returns a Packet context
|
2005-11-15 05:22:13 +00:00
|
|
|
# once a full packet has been received.
|
|
|
|
#
|
2005-04-10 02:05:43 +00:00
|
|
|
def receive_packet
|
|
|
|
return parser.recv(self.sock)
|
|
|
|
end
|
|
|
|
|
2010-01-29 18:52:44 +00:00
|
|
|
#
|
|
|
|
# Stop the monitor
|
|
|
|
#
|
|
|
|
def monitor_stop
|
|
|
|
if(self.receiver_thread)
|
|
|
|
self.receiver_thread.kill
|
|
|
|
self.receiver_thread = nil
|
|
|
|
end
|
2011-06-28 21:26:43 +00:00
|
|
|
|
2010-01-29 18:52:44 +00:00
|
|
|
if(self.dispatcher_thread)
|
|
|
|
self.dispatcher_thread.kill
|
|
|
|
self.dispatcher_thread = nil
|
|
|
|
end
|
|
|
|
end
|
2005-04-12 05:37:11 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Waiter registration
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Adds a waiter association with the supplied request packet.
|
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
def add_response_waiter(request, completion_routine = nil, completion_param = nil)
|
|
|
|
waiter = PacketResponseWaiter.new(request.rid, completion_routine, completion_param)
|
|
|
|
|
|
|
|
self.waiters << waiter
|
|
|
|
|
|
|
|
return waiter
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
# Notifies a whomever is waiting for a the supplied response,
|
2005-11-15 05:22:13 +00:00
|
|
|
# if anyone.
|
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
def notify_response_waiter(response)
|
|
|
|
self.waiters.each() { |waiter|
|
|
|
|
if (waiter.waiting_for?(response))
|
|
|
|
waiter.notify(response)
|
|
|
|
|
|
|
|
remove_response_waiter(waiter)
|
|
|
|
|
|
|
|
break
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Removes a waiter from the list of waiters.
|
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
def remove_response_waiter(waiter)
|
|
|
|
self.waiters.delete(waiter)
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Dispatching
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Initializes the inbound handlers.
|
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
def initialize_inbound_handlers
|
|
|
|
@inbound_handlers = []
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
# Dispatches and processes an inbound packet. If the packet is a
|
|
|
|
# response that has an associated waiter, the waiter is notified.
|
|
|
|
# Otherwise, the packet is passed onto any registered dispatch
|
|
|
|
# handlers until one returns success.
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
def dispatch_inbound_packet(packet, client = nil)
|
|
|
|
handled = false
|
|
|
|
|
|
|
|
# If no client context was provided, return self as PacketDispatcher
|
|
|
|
# is a mixin for the Client instance
|
|
|
|
if (client == nil)
|
|
|
|
client = self
|
|
|
|
end
|
|
|
|
|
|
|
|
# If the packet is a response, try to notify any potential
|
|
|
|
# waiters
|
|
|
|
if ((resp = packet.response?))
|
|
|
|
if (notify_response_waiter(packet))
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Enumerate all of the inbound packet handlers until one handles
|
|
|
|
# the packet
|
|
|
|
@inbound_handlers.each { |handler|
|
|
|
|
|
2009-12-10 05:40:26 +00:00
|
|
|
handled = nil
|
|
|
|
begin
|
|
|
|
|
|
|
|
if ! resp
|
2005-04-12 05:37:11 +00:00
|
|
|
handled = handler.request_handler(client, packet)
|
|
|
|
else
|
|
|
|
handled = handler.response_handler(client, packet)
|
|
|
|
end
|
|
|
|
|
2009-12-10 05:40:26 +00:00
|
|
|
rescue ::Exception => e
|
|
|
|
dlog("Exception caught in dispatch_inbound_packet: handler=#{handler} #{e.class} #{e} #{e.backtrace}", 'meterpreter', LEV_1)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
if (handled)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
}
|
|
|
|
return handled
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
# Registers an inbound packet handler that implements the
|
|
|
|
# InboundPacketHandler interface.
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
def register_inbound_handler(handler)
|
|
|
|
@inbound_handlers << handler
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Deregisters a previously registered inbound packet handler.
|
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
def deregister_inbound_handler(handler)
|
|
|
|
@inbound_handlers.delete(handler)
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2010-01-29 18:52:44 +00:00
|
|
|
attr_accessor :receiver_thread # :nodoc:
|
2006-09-17 23:12:46 +00:00
|
|
|
attr_accessor :dispatcher_thread # :nodoc:
|
2005-11-15 05:22:13 +00:00
|
|
|
attr_accessor :waiters # :nodoc:
|
2005-04-10 02:05:43 +00:00
|
|
|
end
|
|
|
|
|
2009-06-14 21:30:56 +00:00
|
|
|
end; end; end
|
2009-12-04 21:08:34 +00:00
|
|
|
|