From 1bf8d8b5ac9f6e70bca0c3e0ad7916132be96e18 Mon Sep 17 00:00:00 2001 From: Matt Miller Date: Wed, 25 May 2005 05:07:22 +0000 Subject: [PATCH] starting to work on session stuff, tired git-svn-id: file:///home/svn/incoming/trunk@2519 4d416f70-5f16-0410-b530-b9f4589650da --- lib/msf/core/UnitTestSuite.rb | 26 ---------- lib/msf/core/session.rb | 59 +++++++--------------- lib/msf/core/session_manager.rb | 86 +++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 68 deletions(-) delete mode 100644 lib/msf/core/UnitTestSuite.rb create mode 100644 lib/msf/core/session_manager.rb diff --git a/lib/msf/core/UnitTestSuite.rb b/lib/msf/core/UnitTestSuite.rb deleted file mode 100644 index addcffda3b..0000000000 --- a/lib/msf/core/UnitTestSuite.rb +++ /dev/null @@ -1,26 +0,0 @@ -require 'Msf/Core' - -module Msf -module Test - -### -# -# FrameworkCoreTestSuite -# ---------------------- -# -# This test suite is used to test the various core components of -# framework-core. -# -### -class FrameworkCoreTestSuite - def self.suite - suite = ::Test::Unit::TestSuite.new - - suite << Msf::Test::OptionContainerTestCase.suite - - return suite; - end -end - -end -end diff --git a/lib/msf/core/session.rb b/lib/msf/core/session.rb index 3e3f3161d4..05930ab550 100644 --- a/lib/msf/core/session.rb +++ b/lib/msf/core/session.rb @@ -41,62 +41,37 @@ end ### class Session - def initialize(framework, sid, stream = nil) - self.framework = framework - self.stream = stream - self.sid = sid - - # Call the framework event dispatcher to let it know that we've - # opened a new session - framework.events.on_session_open(self) + def initialize(conn = nil) + self.conn = conn end - # Read length supplied bytes from the stream + # + # Read length supplied bytes from the conn + # def read(length = nil) - return stream.read(length) + return conn.read(length) end - # Write the supplied buffer to the stream + # + # Write the supplied buffer to the conn + # def write(buf) - return stream.write(buf) + return conn.write(buf) end - # Close the session's stream and perform cleanup as necessary + # + # Close the session's conn and perform cleanup as necessary + # def close - # Call the framework event dispatcher to let it know that we've - # closed a session - framework.events.on_session_close(self) - - return stream.close + return conn.close end - attr_reader :sid, :stream + attr_reader :conn + attr_accessor :framework, :sid protected - attr_writer :sid, :stream - attr_accessor :framework -end - -# -# -# Built-in session classes -# -# - -### -# -# ShellSession -# ------------ -# -# This class represents a session that is associated with a command -# interpreter. Its read and write operations interact with whatever -# command interpreter it is backed against, whether it be local or -# otherwise. -# -### -class ShellSession < Session - + attr_writer :conn end end diff --git a/lib/msf/core/session_manager.rb b/lib/msf/core/session_manager.rb new file mode 100644 index 0000000000..ddca7139c1 --- /dev/null +++ b/lib/msf/core/session_manager.rb @@ -0,0 +1,86 @@ +module Msf + +### +# +# SessionManager +# -------------- +# +# 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. +# +### +class SessionManager + + def initialize(framework) + self.framework = framework + self.sessions = {} + self.sid_pool = 0 + end + + # + # Returns the session object that is associated with the supplied sid + # + def [](sid) + return get(sid) + end + + # + # 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 + + next_sid = (sid_pool += 1) + + # Insert the session into the session hash table + sessions[next_sid] = session + + # 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 + + # + # Deregisters the supplied session object with the framework + # + def deregister(session) + # Tell the framework that we have a parting session + framework.events.on_session_close(session) + + # Remove it from the hash + sessions.delete(session.sid) + + # Close it down + session.close + end + + # + # Returns the session associated with the supplied sid, if any + # + def get(sid) + return sessions[sid] + end + +protected: + + attr_accessor :sid_pool, :sessions + +end + +end