2005-07-09 21:18:49 +00:00
|
|
|
require 'msf/core'
|
2005-05-21 17:57:00 +00:00
|
|
|
|
|
|
|
module Msf
|
|
|
|
|
2005-07-14 06:34:58 +00:00
|
|
|
###
|
|
|
|
#
|
2005-11-02 16:49:45 +00:00
|
|
|
# This event subscriber class exposes methods that are called when internal
|
|
|
|
# framework events occur, such as the loading and creation of a module.
|
2005-07-14 06:34:58 +00:00
|
|
|
#
|
|
|
|
###
|
|
|
|
module GeneralEventSubscriber
|
|
|
|
#
|
|
|
|
# Called when a module is loaded
|
|
|
|
#
|
2007-02-02 03:45:37 +00:00
|
|
|
def on_module_load(refname, klass)
|
|
|
|
end
|
|
|
|
|
2005-07-14 06:34:58 +00:00
|
|
|
#
|
|
|
|
# Called when a new module instance is created
|
|
|
|
#
|
2007-02-02 03:45:37 +00:00
|
|
|
def on_module_created(instance)
|
|
|
|
end
|
2005-07-14 06:34:58 +00:00
|
|
|
end
|
|
|
|
|
2005-05-21 17:57:00 +00:00
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class manages subscriber registration and is the entry point
|
|
|
|
# for dispatching various events that occur for modules, such as
|
2006-01-24 03:59:44 +00:00
|
|
|
# exploit results and auxiliary module data. The framework
|
2005-05-21 17:57:00 +00:00
|
|
|
# and external modules can register themselves as subscribers to
|
|
|
|
# various events such that they can perform custom actions when
|
|
|
|
# a specific event or events occur.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class EventDispatcher
|
|
|
|
|
2006-03-21 04:37:48 +00:00
|
|
|
include Framework::Offspring
|
|
|
|
|
|
|
|
def initialize(framework)
|
|
|
|
self.framework = framework
|
2005-07-14 06:34:58 +00:00
|
|
|
self.general_event_subscribers = []
|
2005-05-21 17:57:00 +00:00
|
|
|
self.exploit_event_subscribers = []
|
|
|
|
self.session_event_subscribers = []
|
2006-03-21 05:29:26 +00:00
|
|
|
self.db_event_subscribers = []
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
##
|
2005-05-21 17:57:00 +00:00
|
|
|
#
|
|
|
|
# Subscriber registration
|
|
|
|
#
|
2005-10-19 03:20:20 +00:00
|
|
|
##
|
|
|
|
|
|
|
|
#
|
|
|
|
# This method adds a general subscriber. General subscribers receive
|
|
|
|
# notifications when all events occur.
|
|
|
|
#
|
2005-07-14 06:34:58 +00:00
|
|
|
def add_general_subscriber(subscriber)
|
|
|
|
add_event_subscriber(general_event_subscribers, subscriber)
|
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Removes a general subscriber.
|
|
|
|
#
|
2005-07-14 06:34:58 +00:00
|
|
|
def remove_general_subscriber(subscriber)
|
|
|
|
remove_event_subscriber(general_event_subscribers, subscriber)
|
|
|
|
end
|
2005-05-21 17:57:00 +00:00
|
|
|
|
2006-03-21 04:37:48 +00:00
|
|
|
#
|
|
|
|
# This method adds a db event subscriber. db event subscribers
|
|
|
|
# receive notifications when events occur that pertain to db changes.
|
2007-02-25 21:25:41 +00:00
|
|
|
# The subscriber provided must implement the DatabaseEvent module methods
|
|
|
|
# in some form.
|
2006-03-21 04:37:48 +00:00
|
|
|
#
|
|
|
|
def add_db_subscriber(subscriber)
|
|
|
|
add_event_subscriber(db_event_subscribers, subscriber)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Removes a db event subscriber.
|
|
|
|
#
|
|
|
|
def remove_db_subscriber(subscriber)
|
|
|
|
remove_event_subscriber(db_event_subscribers, subscriber)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# This method adds an exploit event subscriber. Exploit event subscribers
|
|
|
|
# receive notifications when events occur that pertain to exploits, such as
|
|
|
|
# the success or failure of an exploitation attempt. The subscriber
|
|
|
|
# provided must implement the ExploitEvents module methods in some form.
|
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def add_exploit_subscriber(subscriber)
|
|
|
|
add_event_subscriber(exploit_event_subscribers, subscriber)
|
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Removes an exploit event subscriber.
|
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def remove_exploit_subscriber(subscriber)
|
|
|
|
remove_event_subscriber(exploit_event_subscribers, subscriber)
|
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# This method adds a session event subscriber. Session event subscribers
|
|
|
|
# receive notifications when sessions are opened and closed. The
|
|
|
|
# subscriber provided must implement the SessionEvents module methods in
|
|
|
|
# some form.
|
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def add_session_subscriber(subscriber)
|
|
|
|
add_event_subscriber(session_event_subscribers, subscriber)
|
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Removes a session event subscriber.
|
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def remove_session_subscriber(subscriber)
|
|
|
|
remove_event_subscriber(session_event_subscribers, subscriber)
|
|
|
|
end
|
|
|
|
|
2005-07-14 06:34:58 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# General events
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Called when a module is loaded into the framework. This, in turn,
|
|
|
|
# notifies all registered general event subscribers.
|
|
|
|
#
|
2005-07-14 06:34:58 +00:00
|
|
|
def on_module_load(name, mod)
|
2009-03-23 20:57:53 +00:00
|
|
|
general_event_subscribers.each { |subscriber|
|
|
|
|
subscriber.on_module_load(name, mod)
|
2005-07-14 06:34:58 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Called when a module is unloaded from the framework. This, in turn,
|
|
|
|
# notifies all registered general event subscribers.
|
|
|
|
#
|
2005-07-14 06:34:58 +00:00
|
|
|
def on_module_created(instance)
|
2009-03-23 20:57:53 +00:00
|
|
|
general_event_subscribers.each { |subscriber|
|
|
|
|
subscriber.on_module_created(instance)
|
2005-07-14 06:34:58 +00:00
|
|
|
}
|
|
|
|
end
|
2006-03-21 04:37:48 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Capture incoming events and pass them off to the subscribers
|
|
|
|
#
|
|
|
|
def method_missing(name, *args)
|
|
|
|
|
2009-03-23 20:57:53 +00:00
|
|
|
case name.to_s
|
|
|
|
|
|
|
|
# Exploit events
|
|
|
|
when /^on_exploit/
|
|
|
|
exploit_event_subscribers.each do |subscriber|
|
|
|
|
next if not subscriber.respond_to?(name)
|
|
|
|
subscriber.send(name, *args)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Session events
|
|
|
|
when /^on_session/
|
|
|
|
session_event_subscribers.each do |subscriber|
|
|
|
|
next if not subscriber.respond_to?(name)
|
|
|
|
subscriber.send(name, *args)
|
|
|
|
end
|
|
|
|
|
|
|
|
# db events
|
|
|
|
when /^on_db/
|
|
|
|
# Only process these events if the db is active
|
|
|
|
if (framework.db.active)
|
|
|
|
db_event_subscribers.each do |subscriber|
|
2006-03-21 04:37:48 +00:00
|
|
|
next if not subscriber.respond_to?(name)
|
|
|
|
subscriber.send(name, *args)
|
|
|
|
end
|
2009-03-23 20:57:53 +00:00
|
|
|
end
|
|
|
|
# Everything else
|
|
|
|
else
|
|
|
|
elog("Event dispatcher received an unhandled event: #{name}")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2006-03-21 05:29:26 +00:00
|
|
|
return true
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
2006-03-21 04:37:48 +00:00
|
|
|
|
|
|
|
|
2005-05-21 17:57:00 +00:00
|
|
|
protected
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Adds an event subscriber to the supplied subscriber array.
|
|
|
|
#
|
|
|
|
def add_event_subscriber(array, subscriber) # :nodoc:
|
2009-03-23 20:57:53 +00:00
|
|
|
array << subscriber
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Removes an event subscriber from the supplied subscriber array.
|
|
|
|
#
|
|
|
|
def remove_event_subscriber(array, subscriber) # :nodoc:
|
2009-03-23 20:57:53 +00:00
|
|
|
array.delete(subscriber)
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
attr_accessor :general_event_subscribers # :nodoc:
|
|
|
|
attr_accessor :exploit_event_subscribers # :nodoc:
|
|
|
|
attr_accessor :session_event_subscribers # :nodoc:
|
2006-03-21 05:29:26 +00:00
|
|
|
attr_accessor :db_event_subscribers # :nodoc:
|
2005-05-21 17:57:00 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2009-01-02 07:29:56 +00:00
|
|
|
end
|