Files
metasploit-gs/lib/msf/ui/web/driver.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

162 lines
3.1 KiB
Ruby
Raw Normal View History

# -*- coding: binary -*-
2021-01-28 10:35:25 +00:00
2005-11-22 03:20:09 +00:00
module Msf
module Ui
module Web
2007-02-10 05:01:48 +00:00
2007-01-19 08:46:06 +00:00
###
#
# This class implements a user interface driver on a web interface.
#
###
class Driver < Msf::Ui::Driver
2005-11-24 03:31:23 +00:00
2007-01-19 08:46:06 +00:00
attr_accessor :framework # :nodoc:
attr_accessor :consoles # :nodoc:
2007-02-10 18:07:08 +00:00
attr_accessor :sessions # :nodoc:
2007-02-10 05:01:48 +00:00
attr_accessor :last_console # :nodoc:
2013-08-30 16:28:33 -05:00
2007-01-19 08:46:06 +00:00
ConfigCore = "framework/core"
ConfigGroup = "framework/ui/web"
2013-08-30 16:28:33 -05:00
2005-11-22 03:20:09 +00:00
#
# Initializes a web driver instance and prepares it for listening to HTTP
# requests. The constructor takes a hash of options that can control how
# the web server will operate.
#
def initialize(opts = {})
# Call the parent
super()
2013-08-30 16:28:33 -05:00
2005-11-22 03:20:09 +00:00
# Set the passed options hash for referencing later on.
self.opts = opts
2013-08-30 16:28:33 -05:00
2007-01-19 08:46:06 +00:00
self.consoles = {}
2007-02-10 18:07:08 +00:00
self.sessions = {}
2013-08-30 16:28:33 -05:00
if(opts[:framework])
self.framework = opts[:framework]
else
# Initialize configuration
Msf::Config.init
2013-08-30 16:28:33 -05:00
# Initialize logging
initialize_logging
2013-08-30 16:28:33 -05:00
# Initialize attributes
self.framework = Msf::Simple::Framework.create
end
2013-08-30 16:28:33 -05:00
2007-01-19 08:46:06 +00:00
# Initialize the console count
self.last_console = 0
2005-11-22 03:20:09 +00:00
end
2013-08-30 16:28:33 -05:00
def create_console(opts={})
2007-01-19 08:46:06 +00:00
# Destroy any unused consoles
clean_consoles
2013-08-30 16:28:33 -05:00
console = WebConsole.new(self.framework, self.last_console, opts)
2007-01-19 08:46:06 +00:00
self.last_console += 1
2007-02-10 05:01:48 +00:00
self.consoles[console.console_id.to_s] = console
2007-01-19 08:46:06 +00:00
console.console_id.to_s
2005-11-22 03:20:09 +00:00
end
2013-08-30 16:28:33 -05:00
def destroy_console(cid)
con = self.consoles[cid]
if(con)
con.shutdown
self.consoles.delete(cid)
end
end
2013-08-30 16:28:33 -05:00
2007-01-19 08:46:06 +00:00
def write_console(id, buf)
self.consoles[id] ? self.consoles[id].write(buf) : nil
2005-11-24 02:02:10 +00:00
end
2013-08-30 16:28:33 -05:00
2007-01-19 08:46:06 +00:00
def read_console(id)
self.consoles[id] ? self.consoles[id].read() : nil
2005-11-24 03:31:23 +00:00
end
2013-08-30 16:28:33 -05:00
2007-01-19 08:46:06 +00:00
def clean_consoles(timeout=300)
self.consoles.each_pair do |id, con|
if (con.last_access + timeout < Time.now)
con.shutdown
self.consoles.delete(id)
end
end
2005-11-24 03:31:23 +00:00
end
2013-08-30 16:28:33 -05:00
2007-02-10 18:07:08 +00:00
def write_session(id, buf)
ses = self.framework.sessions[id]
return if not ses
2007-02-18 05:07:56 +00:00
return if not ses.user_input
2007-02-10 18:07:08 +00:00
ses.user_input.put(buf)
end
2013-08-30 16:28:33 -05:00
2007-02-10 18:07:08 +00:00
def read_session(id)
ses = self.framework.sessions[id]
2007-02-18 05:07:56 +00:00
return if not ses
return if not ses.user_output
2007-02-10 18:07:08 +00:00
ses.user_output.read_subscriber('session_reader')
end
2013-08-30 16:28:33 -05:00
2007-02-10 18:07:08 +00:00
# Detach the session from an existing input/output pair
def connect_session(id)
2013-08-30 16:28:33 -05:00
2007-02-10 18:07:08 +00:00
# Ignore invalid sessions
ses = self.framework.sessions[id]
return if not ses
2013-08-30 16:28:33 -05:00
2007-02-10 18:07:08 +00:00
# Has this session already been detached?
2007-02-18 05:07:56 +00:00
if (ses.user_output)
return if ses.user_output.has_subscriber?('session_reader')
end
2013-08-30 16:28:33 -05:00
2007-02-10 18:07:08 +00:00
# Create a new pipe
spipe = WebConsole::WebConsolePipe.new
spipe.input = spipe.pipe_input
2013-08-30 16:28:33 -05:00
2007-02-10 18:07:08 +00:00
# Create a read subscriber
spipe.create_subscriber('session_reader')
2013-08-30 16:28:33 -05:00
framework.threads.spawn("ConnectSessionInteraction", false) do
2007-02-18 05:07:56 +00:00
ses.interact(spipe.input, spipe)
2007-02-10 18:07:08 +00:00
end
end
2013-08-30 16:28:33 -05:00
2007-02-10 18:07:08 +00:00
def sessions
self.framework.sessions
end
2013-08-30 16:28:33 -05:00
2005-11-22 03:20:09 +00:00
#
2007-01-19 08:46:06 +00:00
# Stub
2005-11-22 03:20:09 +00:00
#
2007-01-19 08:46:06 +00:00
def run
true
end
2005-11-22 03:20:09 +00:00
protected
attr_accessor :opts # :nodoc:
#
2007-01-19 08:46:06 +00:00
# Initializes logging for the web interface
2005-11-22 03:20:09 +00:00
#
def initialize_logging
level = (opts['LogLevel'] || 0).to_i
Msf::Logging.enable_log_source(LogSource, level)
end
end
end
end
end