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

460 lines
11 KiB
Ruby
Raw Normal View History

# -*- coding: binary -*-
2005-07-09 21:18:49 +00:00
require 'msf/core'
require 'msf/util'
2005-05-21 17:57:00 +00:00
module Msf
###
#
# This class is the primary context that modules, scripts, and user
# interfaces interact with. It ties everything together.
#
###
class Framework
#
# Versioning information
#
2011-07-21 18:35:09 +00:00
Major = 4
2013-04-10 08:59:56 -05:00
Minor = 7
Point = 0
2012-12-24 08:40:29 -06:00
Release = "-dev"
if(Point)
Version = "#{Major}.#{Minor}.#{Point}#{Release}"
else
Version = "#{Major}.#{Minor}#{Release}"
end
2009-11-09 03:22:24 +00:00
Revision = "$Revision$"
# Repository information
RepoRevision = ::Msf::Util::SVN.revision
RepoUpdated = ::Msf::Util::SVN.updated
RepoUpdatedDays = ::Msf::Util::SVN.days_since_update
RepoUpdatedDaysNote = ::Msf::Util::SVN.last_updated_friendly
2009-11-13 22:50:12 +00:00
RepoUpdatedDate = ::Msf::Util::SVN.last_updated_date
RepoRoot = ::Msf::Util::SVN.root
# EICAR canary
EICARCorrupted = ::Msf::Util::EXE.is_eicar_corrupted?
# API Version
APIMajor = 1
APIMinor = 0
# Base/API Version
VersionCore = Major + (Minor / 10.0)
VersionAPI = APIMajor + (APIMinor / 10.0)
#
# Mixin meant to be included into all classes that can have instances that
# should be tied to the framework, such as modules.
#
module Offspring
2005-10-19 03:37:22 +00:00
#
# A reference to the framework instance from which this offspring was
# derived.
#
attr_accessor :framework
end
require 'msf/core/thread_manager'
require 'msf/core/module_manager'
2005-07-16 08:12:58 +00:00
require 'msf/core/session_manager'
2011-05-02 03:40:03 +00:00
require 'msf/core/plugin_manager'
2006-03-21 04:37:48 +00:00
require 'msf/core/db_manager'
2010-01-15 00:32:48 +00:00
require 'msf/core/event_dispatcher'
2005-05-21 17:57:00 +00:00
2005-10-19 03:37:22 +00:00
#
# Creates an instance of the framework context.
#
def initialize(opts={})
# Allow specific module types to be loaded
types = opts[:module_types] || MODULE_TYPES
self.threads = ThreadManager.new(self)
2006-03-21 04:37:48 +00:00
self.events = EventDispatcher.new(self)
self.modules = ModuleManager.new(self,types)
2005-07-16 08:12:58 +00:00
self.sessions = SessionManager.new(self)
self.datastore = DataStore.new
self.jobs = Rex::JobContainer.new
2005-11-19 16:25:26 +00:00
self.plugins = PluginManager.new(self)
self.db = DBManager.new(self, opts)
# Configure the thread factory
Rex::ThreadFactory.provider = self.threads
2010-01-15 00:32:48 +00:00
subscriber = FrameworkEventSubscriber.new(self)
events.add_exploit_subscriber(subscriber)
events.add_session_subscriber(subscriber)
events.add_general_subscriber(subscriber)
events.add_db_subscriber(subscriber)
events.add_ui_subscriber(subscriber)
end
def inspect
"#<Framework (#{sessions.length} sessions, #{jobs.length} jobs, #{plugins.length} plugins#{db.active ? ", #{db.driver} database active" : ""})>"
end
2005-07-12 14:32:44 +00:00
#
2005-10-19 03:37:22 +00:00
# Returns the module set for encoders.
2005-07-12 14:32:44 +00:00
#
def encoders
return modules.encoders
end
2005-07-12 14:32:44 +00:00
#
2005-10-19 03:37:22 +00:00
# Returns the module set for exploits.
2005-07-12 14:32:44 +00:00
#
2005-05-22 07:25:15 +00:00
def exploits
return modules.exploits
end
2005-07-12 14:32:44 +00:00
#
# Returns the module set for nops
#
def nops
return modules.nops
end
2005-07-12 14:32:44 +00:00
#
# Returns the module set for payloads
#
2005-05-22 07:25:15 +00:00
def payloads
return modules.payloads
end
2005-05-21 17:57:00 +00:00
2005-07-12 14:32:44 +00:00
#
# Returns the module set for auxiliary modules
2005-07-12 14:32:44 +00:00
#
def auxiliary
return modules.auxiliary
2005-05-21 17:57:00 +00:00
end
#
# Returns the module set for post modules
#
def post
return modules.post
end
2005-11-24 03:31:23 +00:00
#
# Returns the framework version in Major.Minor format.
#
def version
Version
2005-11-24 03:31:23 +00:00
end
2005-10-19 03:37:22 +00:00
#
# Event management interface for registering event handler subscribers and
# for interacting with the correlation engine.
#
2005-05-21 17:57:00 +00:00
attr_reader :events
2005-10-19 03:37:22 +00:00
#
# Module manager that contains information about all loaded modules,
# regardless of type.
#
attr_reader :modules
2005-10-19 03:37:22 +00:00
#
# Session manager that tracks sessions associated with this framework
# instance over the course of their lifetime.
#
2005-07-16 08:12:58 +00:00
attr_reader :sessions
2005-10-19 03:37:22 +00:00
#
# The global framework datastore that can be used by modules.
#
attr_reader :datastore
2005-10-19 03:37:22 +00:00
#
# The framework instance's aux manager. The aux manager is responsible
# for collecting and catalogging all aux information that comes in from
# aux modules.
#
attr_reader :auxmgr
#
2005-10-19 03:37:22 +00:00
# Background job management specific to things spawned from this instance
# of the framework.
#
attr_reader :jobs
2005-11-19 16:25:26 +00:00
#
# The framework instance's plugin manager. The plugin manager is
# responsible for exposing an interface that allows for the loading and
# unloading of plugins.
#
attr_reader :plugins
2006-03-21 04:37:48 +00:00
#
# The framework instance's db manager. The db manager
# maintains the database db and handles db events
#
attr_reader :db
#
# The framework instance's thread manager. The thread manager
# provides a cleaner way to manage spawned threads
#
attr_reader :threads
2005-05-21 17:57:00 +00:00
protected
2005-10-19 03:37:22 +00:00
attr_writer :events # :nodoc:
attr_writer :modules # :nodoc:
attr_writer :sessions # :nodoc:
attr_writer :datastore # :nodoc:
attr_writer :auxmgr # :nodoc:
2005-10-19 03:37:22 +00:00
attr_writer :jobs # :nodoc:
2005-11-19 16:25:26 +00:00
attr_writer :plugins # :nodoc:
2006-03-21 04:37:48 +00:00
attr_writer :db # :nodoc:
attr_writer :threads # :nodoc:
2005-05-21 17:57:00 +00:00
end
2010-01-15 00:32:48 +00:00
class FrameworkEventSubscriber
include Framework::Offspring
def initialize(framework)
self.framework = framework
end
def report_event(data)
2010-01-15 04:34:12 +00:00
if framework.db.active
framework.db.report_event(data)
end
2010-01-15 00:32:48 +00:00
end
include GeneralEventSubscriber
2010-03-17 14:07:45 +00:00
#
# Generic handler for module events
#
def module_event(name, instance, opts={})
2010-01-15 04:34:12 +00:00
if framework.db.active
2010-02-26 18:45:24 +00:00
event = {
2010-02-26 18:52:22 +00:00
:workspace => framework.db.find_workspace(instance.workspace),
2010-03-17 14:07:45 +00:00
:name => name,
:username => instance.owner,
2010-02-26 18:45:24 +00:00
:info => {
:module_name => instance.fullname,
:module_uuid => instance.uuid
}.merge(opts)
2010-02-26 18:45:24 +00:00
}
2010-03-17 14:07:45 +00:00
2010-02-26 18:45:24 +00:00
report_event(event)
2010-01-15 04:34:12 +00:00
end
2010-01-15 00:32:48 +00:00
end
2012-04-16 19:34:21 -06:00
##
# :category: ::Msf::GeneralEventSubscriber implementors
def on_module_run(instance)
opts = { :datastore => instance.datastore.to_h }
module_event('module_run', instance, opts)
end
2012-04-16 19:34:21 -06:00
##
# :category: ::Msf::GeneralEventSubscriber implementors
def on_module_complete(instance)
module_event('module_complete', instance)
end
2012-04-16 19:34:21 -06:00
##
# :category: ::Msf::GeneralEventSubscriber implementors
def on_module_error(instance, exception=nil)
module_event('module_error', instance, :exception => exception.to_s)
end
2010-01-15 00:32:48 +00:00
include ::Msf::UiEventSubscriber
2012-04-16 19:34:21 -06:00
##
# :category: ::Msf::UiEventSubscriber implementors
2010-01-15 00:32:48 +00:00
def on_ui_command(command)
2010-01-15 04:34:12 +00:00
if framework.db.active
report_event(:name => "ui_command", :info => {:command => command})
end
2010-01-15 00:32:48 +00:00
end
2012-04-16 19:34:21 -06:00
##
# :category: ::Msf::UiEventSubscriber implementors
2010-01-15 00:32:48 +00:00
def on_ui_stop()
2010-01-15 04:34:12 +00:00
if framework.db.active
report_event(:name => "ui_stop")
end
2010-01-15 00:32:48 +00:00
end
2012-04-16 19:34:21 -06:00
##
# :category: ::Msf::UiEventSubscriber implementors
2010-01-15 00:32:48 +00:00
def on_ui_start(rev)
#
# The database is not active at startup time unless msfconsole was
# started with a database.yml, so this event won't always be saved to
# the db. Not great, but best we can do.
2010-01-15 00:32:48 +00:00
#
info = { :revision => rev }
report_event(:name => "ui_start", :info => info)
2010-01-15 00:32:48 +00:00
end
require 'msf/core/session'
2010-01-15 00:32:48 +00:00
include ::Msf::SessionEvent
#
# Generic handler for session events
#
def session_event(name, session, opts={})
address = session.session_host
2013-03-07 18:20:08 -06:00
if not (address and address.length > 0)
elog("Session with no session_host/target_host/tunnel_peer")
dlog("#{session.inspect}", LEV_3)
return
end
if framework.db.active
ws = framework.db.find_workspace(session.workspace)
event = {
:workspace => ws,
:username => session.username,
:name => name,
:host => address,
:info => {
:session_id => session.sid,
:session_info => session.info,
:session_uuid => session.uuid,
:session_type => session.type,
:username => session.username,
:target_host => address,
:via_exploit => session.via_exploit,
:via_payload => session.via_payload,
:tunnel_peer => session.tunnel_peer,
:exploit_uuid => session.exploit_uuid
}.merge(opts)
}
report_event(event)
end
end
2012-04-16 19:34:21 -06:00
##
# :category: ::Msf::SessionEvent implementors
2010-01-15 00:32:48 +00:00
def on_session_open(session)
opts = { :datastore => session.exploit_datastore.to_h, :critical => true }
session_event('session_open', session, opts)
framework.db.report_session(:session => session)
2010-01-15 00:32:48 +00:00
end
2012-04-16 19:34:21 -06:00
##
# :category: ::Msf::SessionEvent implementors
def on_session_upload(session, lpath, rpath)
session_event('session_upload', session, :local_path => lpath, :remote_path => rpath)
framework.db.report_session_event({
:etype => 'upload',
:session => session,
:local_path => lpath,
:remote_path => rpath
})
end
2012-04-16 19:34:21 -06:00
##
# :category: ::Msf::SessionEvent implementors
def on_session_download(session, rpath, lpath)
session_event('session_download', session, :local_path => lpath, :remote_path => rpath)
framework.db.report_session_event({
:etype => 'download',
:session => session,
:local_path => lpath,
:remote_path => rpath
})
end
2012-04-16 19:34:21 -06:00
##
# :category: ::Msf::SessionEvent implementors
def on_session_close(session, reason='')
session_event('session_close', session)
if session.db_record
# Don't bother saving here, the session's cleanup method will take
# care of that later.
session.db_record.close_reason = reason
session.db_record.closed_at = Time.now.utc
end
2010-01-15 00:32:48 +00:00
end
#def on_session_interact(session)
# $stdout.puts('session_interact', session.inspect)
#end
2010-01-15 00:32:48 +00:00
2012-04-16 19:34:21 -06:00
##
# :category: ::Msf::SessionEvent implementors
2010-01-15 00:32:48 +00:00
def on_session_command(session, command)
session_event('session_command', session, :command => command)
framework.db.report_session_event({
:etype => 'command',
:session => session,
:command => command
})
2010-01-15 00:32:48 +00:00
end
2012-04-16 19:34:21 -06:00
##
# :category: ::Msf::SessionEvent implementors
def on_session_output(session, output)
# Break up the output into chunks that will fit into the database.
buff = output.dup
chunks = []
if buff.length > 1024
while buff.length > 0
chunks << buff.slice!(0,1024)
end
else
chunks << buff
end
chunks.each { |chunk|
session_event('session_output', session, :output => chunk)
framework.db.report_session_event({
:etype => 'output',
:session => session,
:output => chunk
})
}
end
2012-04-16 19:34:21 -06:00
##
# :category: ::Msf::SessionEvent implementors
def on_session_route(session, route)
framework.db.report_session_route(session, route)
end
2012-04-16 19:34:21 -06:00
##
# :category: ::Msf::SessionEvent implementors
def on_session_route_remove(session, route)
framework.db.report_session_route_remove(session, route)
end
2012-04-16 19:34:21 -06:00
##
# :category: ::Msf::SessionEvent implementors
def on_session_script_run(session, script)
framework.db.report_session_event({
:etype => 'script_run',
:session => session,
:local_path => script
})
end
2012-04-16 19:34:21 -06:00
##
# :category: ::Msf::SessionEvent implementors
def on_session_module_run(session, mod)
framework.db.report_session_event({
:etype => 'module_run',
:session => session,
:local_path => mod.fullname
})
end
2010-01-15 00:32:48 +00:00
#
2010-01-15 00:32:48 +00:00
# This is covered by on_module_run and on_session_open, so don't bother
#
#require 'msf/core/exploit'
#include ExploitEvent
#def on_exploit_success(exploit, session)
#end
end
2008-11-10 22:15:23 +00:00
end