Files
metasploit-gs/lib/msf/core/session_manager.rb
T

90 lines
2.1 KiB
Ruby
Raw Normal View History

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
# applicable to the way that the command interpreter is communicated
# 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
2005-05-25 05:07:22 +00:00
def initialize(framework)
self.framework = framework
self.sid_pool = 0
end
#
# 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)
2005-05-25 05:07:22 +00:00
# Insert the session into the session hash table
self[next_sid.to_i] = session
2005-05-25 05:07:22 +00:00
# Initialize the session's sid and framework instance pointer
session.sid = next_sid
session.framework = framework
# Notify the framework that we have a new session opening up...
framework.events.on_session_open(session)
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
#
def deregister(session)
# Tell the framework that we have a parting session
framework.events.on_session_close(session)
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
self.delete(session.sid.to_i)
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)
return self[sid.to_i]
2005-05-25 05:07:22 +00:00
end
2005-05-26 06:35:37 +00:00
protected
2005-05-25 05:07:22 +00:00
2005-11-15 15:11:43 +00:00
attr_accessor :sid_pool, :sessions # :nodoc:
2005-05-25 05:07:22 +00:00
end
2008-10-19 21:03:39 +00:00
end