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

160 lines
2.9 KiB
Ruby
Raw Normal View History

2005-11-24 03:31:23 +00:00
require 'rex/proto/http'
2005-11-22 03:20:09 +00:00
require 'msf/core'
require 'msf/base'
require 'msf/ui'
module Msf
module Ui
module Web
2007-01-19 08:47:20 +00:00
require 'rex/io/bidirectional_pipe'
2007-02-10 18:07:08 +00:00
require 'msf/ui/web/console'
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:
2007-01-19 08:46:06 +00:00
ConfigCore = "framework/core"
ConfigGroup = "framework/ui/web"
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()
# Set the passed options hash for referencing later on.
self.opts = opts
2007-01-19 08:46:06 +00:00
self.consoles = {}
2007-02-10 18:07:08 +00:00
self.sessions = {}
2007-01-19 08:46:06 +00:00
2005-12-21 02:05:46 +00:00
# Initialize configuration
Msf::Config.init
2007-02-10 05:01:48 +00:00
2005-11-22 03:20:09 +00:00
# Initialize logging
initialize_logging
2005-12-21 02:05:46 +00:00
2005-11-22 03:20:09 +00:00
# Initialize attributes
self.framework = Msf::Simple::Framework.create
2007-02-10 05:01:48 +00: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
2007-01-19 08:46:06 +00:00
def create_console
# Destroy any unused consoles
clean_consoles
2007-02-10 05:01:48 +00:00
2007-01-19 08:46:06 +00:00
console = WebConsole.new(self.framework, self.last_console)
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
2007-02-10 05:01:48 +00:00
def destroy_console(cid)
con = self.consoles[cid]
if(con)
con.shutdown
self.consoles.delete(cid)
end
end
2007-01-19 08:46:06 +00:00
def write_console(id, buf)
self.consoles[id] ? self.consoles.write(buf) : nil
2005-11-24 02:02:10 +00:00
end
2007-02-10 05:01:48 +00:00
2007-01-19 08:46:06 +00:00
def read_console(id)
self.consoles[id] ? self.consoles.read() : nil
2005-11-24 03:31:23 +00:00
end
2007-02-10 05:01:48 +00: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
2007-02-10 05:01:48 +00: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
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
# Detach the session from an existing input/output pair
def connect_session(id)
# Ignore invalid sessions
ses = self.framework.sessions[id]
return if not ses
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
2007-02-10 18:07:08 +00:00
# Create a new pipe
spipe = WebConsole::WebConsolePipe.new
spipe.input = spipe.pipe_input
# Create a read subscriber
spipe.create_subscriber('session_reader')
Thread.new do
2007-02-18 05:07:56 +00:00
ses.interact(spipe.input, spipe)
2007-02-10 18:07:08 +00:00
end
end
def sessions
self.framework.sessions
end
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
2008-10-19 21:03:39 +00:00
end