2005-05-25 05:07:22 +00:00
|
|
|
module Msf
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# The purpose of the session manager is to keep track of sessions that are
|
|
|
|
# created during the course of a framework instance's lifetime. When
|
|
|
|
# exploits succeed, the payloads they use will create a session object,
|
|
|
|
# where applicable, there will implement zero or more of the core
|
|
|
|
# supplied interfaces for interacting with that session. For instance,
|
|
|
|
# if the payload supports reading and writing from an executed process,
|
|
|
|
# the session would implement SimpleCommandShell in a method that is
|
2010-03-08 22:58:43 +00:00
|
|
|
# applicable to the way that the command interpreter is communicated
|
2005-05-25 05:07:22 +00:00
|
|
|
# with.
|
|
|
|
#
|
|
|
|
###
|
2005-07-16 08:12:58 +00:00
|
|
|
class SessionManager < Hash
|
2005-05-25 05:07:22 +00:00
|
|
|
|
2005-07-16 08:12:58 +00:00
|
|
|
include Framework::Offspring
|
2005-05-26 06:35:37 +00:00
|
|
|
|
2011-04-26 05:10:14 +00:00
|
|
|
LAST_SEEN_INTERVAL = 2.5 * 60
|
2011-04-25 16:11:02 +00:00
|
|
|
|
2005-05-25 05:07:22 +00:00
|
|
|
def initialize(framework)
|
|
|
|
self.framework = framework
|
|
|
|
self.sid_pool = 0
|
2010-11-12 06:19:49 +00:00
|
|
|
self.reaper_thread = framework.threads.spawn("SessionManager", true, self) do |manager|
|
2011-04-26 05:10:14 +00:00
|
|
|
last_seen_timer = Time.now.utc
|
2011-04-07 21:59:32 +00:00
|
|
|
begin
|
2011-04-26 05:10:14 +00:00
|
|
|
|
2010-09-25 03:57:30 +00:00
|
|
|
while true
|
2011-04-07 21:59:32 +00:00
|
|
|
|
|
|
|
rings = values.select{|s| s.respond_to?(:ring) and s.ring and s.rstream }
|
|
|
|
ready = ::IO.select(rings.map{|s| s.rstream}, nil, nil, 0.5) || [[],[],[]]
|
|
|
|
|
|
|
|
ready[0].each do |fd|
|
|
|
|
s = rings.select{|s| s.rstream == fd}.first
|
|
|
|
next if not s
|
|
|
|
|
|
|
|
begin
|
|
|
|
buff = fd.get_once(-1)
|
|
|
|
if buff
|
|
|
|
# Store the data in the associated ring
|
|
|
|
s.ring.store_data(buff)
|
|
|
|
|
|
|
|
# Store the session event into the database.
|
|
|
|
# Rescue anything the event handlers raise so they
|
|
|
|
# don't break our session.
|
|
|
|
framework.events.on_session_output(s, buff) rescue nil
|
|
|
|
end
|
|
|
|
rescue ::Exception => e
|
|
|
|
wlog("Exception reading from Session #{s.sid}: #{e.class} #{e}")
|
|
|
|
unless e.kind_of? EOFError
|
|
|
|
# Don't bother with a call stack if it's just a
|
|
|
|
# normal EOF
|
|
|
|
dlog("Call Stack\n#{e.backtrace.join("\n")}", 'core', LEV_3)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Flush any ring data in the queue
|
|
|
|
s.ring.clear_data rescue nil
|
|
|
|
|
|
|
|
# Shut down the socket itself
|
|
|
|
s.rstream.close rescue nil
|
|
|
|
|
|
|
|
# Deregister the session
|
|
|
|
manager.deregister(s, "Died from #{e.class}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Check for closed / dead / terminated sessions
|
2010-11-12 06:19:49 +00:00
|
|
|
manager.each_value do |s|
|
2010-02-23 05:59:30 +00:00
|
|
|
if not s.alive?
|
2010-11-12 06:19:49 +00:00
|
|
|
manager.deregister(s, "Died")
|
2010-09-25 03:57:30 +00:00
|
|
|
wlog("Session #{s.sid} has died")
|
2010-03-08 22:58:43 +00:00
|
|
|
next
|
2010-02-23 05:59:30 +00:00
|
|
|
end
|
2011-04-26 05:10:14 +00:00
|
|
|
|
|
|
|
next if ((Time.now.utc - last_seen_timer) < LAST_SEEN_INTERVAL)
|
2011-04-25 16:11:02 +00:00
|
|
|
# Update the database entry for this session every 5
|
|
|
|
# minutes, give or take. This notifies other framework
|
|
|
|
# instances that this session is being maintained.
|
2011-04-26 05:10:14 +00:00
|
|
|
last_seen_timer = Time.now.utc
|
|
|
|
if framework.db.active and s.db_record
|
2011-04-25 16:11:02 +00:00
|
|
|
s.db_record.last_seen = Time.now.utc
|
|
|
|
s.db_record.save
|
|
|
|
end
|
2010-02-23 05:59:30 +00:00
|
|
|
end
|
2011-04-26 05:10:14 +00:00
|
|
|
|
|
|
|
# Skip the database cleanup code below if there is no database
|
|
|
|
next if not (framework.db and framework.db.active)
|
|
|
|
|
|
|
|
# Clean out any stale sessions that have been orphaned by a dead
|
|
|
|
# framewort instance.
|
|
|
|
Msf::DBManager::Session.find_all_by_closed_at(nil).each do |db_session|
|
|
|
|
if db_session.last_seen.nil? or ((Time.now.utc - db_session.last_seen) > (2*LAST_SEEN_INTERVAL))
|
|
|
|
db_session.closed_at = db_session.last_seen || Time.now.utc
|
2011-04-26 16:19:59 +00:00
|
|
|
db_session.close_reason = "Orphaned"
|
2011-04-26 05:10:14 +00:00
|
|
|
db_session.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-02-23 05:59:30 +00:00
|
|
|
end
|
2011-04-07 21:59:32 +00:00
|
|
|
|
|
|
|
rescue ::Exception => e
|
|
|
|
wlog("Exception in reaper thread #{e.class} #{e}")
|
|
|
|
wlog("Call Stack\n#{e.backtrace.join("\n")}", 'core', LEV_3)
|
|
|
|
end
|
2011-04-26 05:10:14 +00:00
|
|
|
|
2010-02-23 05:59:30 +00:00
|
|
|
end
|
2005-05-25 05:07:22 +00:00
|
|
|
end
|
|
|
|
|
2005-07-17 02:04:39 +00:00
|
|
|
#
|
|
|
|
# Enumerates the sorted list of keys.
|
|
|
|
#
|
|
|
|
def each_sorted(&block)
|
|
|
|
self.keys.sort.each(&block)
|
|
|
|
end
|
|
|
|
|
2005-05-25 05:07:22 +00:00
|
|
|
#
|
|
|
|
# Registers the supplied session object with the framework and returns
|
|
|
|
# a unique session identifier to the caller.
|
|
|
|
#
|
|
|
|
def register(session)
|
|
|
|
if (session.sid)
|
|
|
|
wlog("registered session passed to register again (sid #{session.sid}).")
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2005-05-26 06:35:37 +00:00
|
|
|
next_sid = (self.sid_pool += 1)
|
2011-04-18 20:07:53 +00:00
|
|
|
|
2005-05-25 05:07:22 +00:00
|
|
|
# Initialize the session's sid and framework instance pointer
|
|
|
|
session.sid = next_sid
|
|
|
|
session.framework = framework
|
2010-03-08 22:58:43 +00:00
|
|
|
|
2011-04-18 20:07:53 +00:00
|
|
|
# Only register if the session allows for it
|
|
|
|
if session.register?
|
|
|
|
# Insert the session into the session hash table
|
|
|
|
self[next_sid.to_i] = session
|
|
|
|
|
|
|
|
# Notify the framework that we have a new session opening up...
|
|
|
|
# Don't let errant event handlers kill our session
|
|
|
|
begin
|
2005-05-25 05:07:22 +00:00
|
|
|
|
2011-04-18 20:07:53 +00:00
|
|
|
framework.events.on_session_open(session)
|
|
|
|
rescue ::Exception => e
|
|
|
|
wlog("Exception in on_session_open event handler: #{e.class}: #{e}")
|
|
|
|
wlog("Call Stack\n#{e.backtrace.join("\n")}", 'core', LEV_3)
|
|
|
|
end
|
|
|
|
|
|
|
|
if session.respond_to?("console")
|
|
|
|
session.console.on_command_proc = Proc.new { |command, error| framework.events.on_session_command(session, command) }
|
|
|
|
session.console.on_print_proc = Proc.new { |output| framework.events.on_session_output(session, output) }
|
|
|
|
end
|
2010-01-06 05:06:12 +00:00
|
|
|
end
|
|
|
|
|
2005-05-25 05:07:22 +00:00
|
|
|
return next_sid
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 15:11:43 +00:00
|
|
|
# Deregisters the supplied session object with the framework.
|
2005-05-25 05:07:22 +00:00
|
|
|
#
|
2010-02-23 05:59:30 +00:00
|
|
|
def deregister(session, reason='')
|
2011-04-18 20:07:53 +00:00
|
|
|
return if not session.register?
|
2010-03-21 04:24:27 +00:00
|
|
|
|
|
|
|
if (session.dead? and not self[session.sid.to_i])
|
2010-03-19 03:12:36 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2005-05-25 05:07:22 +00:00
|
|
|
# Tell the framework that we have a parting session
|
2011-04-07 21:59:32 +00:00
|
|
|
framework.events.on_session_close(session, reason) rescue nil
|
2005-05-25 05:07:22 +00:00
|
|
|
|
2005-11-08 18:00:17 +00:00
|
|
|
# If this session implements the comm interface, remove any routes
|
|
|
|
# that have been created for it.
|
|
|
|
if (session.kind_of?(Msf::Session::Comm))
|
|
|
|
Rex::Socket::SwitchBoard.remove_by_comm(session)
|
|
|
|
end
|
|
|
|
|
2005-05-25 05:07:22 +00:00
|
|
|
# Remove it from the hash
|
2005-07-17 02:04:39 +00:00
|
|
|
self.delete(session.sid.to_i)
|
2005-05-25 05:07:22 +00:00
|
|
|
|
2010-03-21 04:24:27 +00:00
|
|
|
# Mark the session as dead
|
|
|
|
session.alive = false
|
|
|
|
|
2005-05-25 05:07:22 +00:00
|
|
|
# Close it down
|
2005-05-26 06:35:37 +00:00
|
|
|
session.cleanup
|
2005-05-25 05:07:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 15:11:43 +00:00
|
|
|
# Returns the session associated with the supplied sid, if any.
|
2005-05-25 05:07:22 +00:00
|
|
|
#
|
|
|
|
def get(sid)
|
2005-07-17 02:04:39 +00:00
|
|
|
return self[sid.to_i]
|
2005-05-25 05:07:22 +00:00
|
|
|
end
|
|
|
|
|
2005-05-26 06:35:37 +00:00
|
|
|
protected
|
2010-03-08 22:58:43 +00:00
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
attr_accessor :sid_pool, :sessions # :nodoc:
|
2010-02-23 05:59:30 +00:00
|
|
|
attr_accessor :reaper_thread # :nodoc:
|
2005-05-25 05:07:22 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2010-01-06 05:06:12 +00:00
|
|
|
end
|
2010-03-08 22:58:43 +00:00
|
|
|
|