Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/msf/core/event_dispatcher.rb
Views: 11779
# -*- coding: binary -*-12module Msf3456###7#8# This class manages subscriber registration and is the entry point9# for dispatching various events that occur for modules, such as10# exploit results and auxiliary module data. The framework11# and external modules can register themselves as subscribers to12# various events such that they can perform custom actions when13# a specific event or events occur.14#15###16class EventDispatcher1718include Framework::Offspring1920def initialize(framework)21self.framework = framework22self.general_event_subscribers = []23self.custom_event_subscribers = []24self.exploit_event_subscribers = []25self.session_event_subscribers = []26self.db_event_subscribers = []27self.ui_event_subscribers = []28end2930##31#32# Subscriber registration33#34##3536#37# This method adds a general subscriber. General subscribers receive38# notifications when all events occur.39#40def add_general_subscriber(subscriber)41add_event_subscriber(general_event_subscribers, subscriber)42end4344#45# Removes a general subscriber.46#47def remove_general_subscriber(subscriber)48remove_event_subscriber(general_event_subscribers, subscriber)49end5051#52# This method adds a db event subscriber. db event subscribers53# receive notifications when events occur that pertain to db changes.54# The subscriber provided must implement the DatabaseEvent module methods55# in some form.56#57def add_db_subscriber(subscriber)58add_event_subscriber(db_event_subscribers, subscriber)59end6061#62# Removes a db event subscriber.63#64def remove_db_subscriber(subscriber)65remove_event_subscriber(db_event_subscribers, subscriber)66end676869#70# This method adds an exploit event subscriber. Exploit event subscribers71# receive notifications when events occur that pertain to exploits, such as72# the success or failure of an exploitation attempt. The subscriber73# provided must implement the ExploitEvent module methods in some form.74#75def add_exploit_subscriber(subscriber)76add_event_subscriber(exploit_event_subscribers, subscriber)77end7879#80# Removes an exploit event subscriber.81#82def remove_exploit_subscriber(subscriber)83remove_event_subscriber(exploit_event_subscribers, subscriber)84end8586#87# This method adds a session event subscriber. Session event subscribers88# receive notifications when sessions are opened and closed. The89# subscriber provided must implement the SessionEvent module methods in90# some form.91#92def add_session_subscriber(subscriber)93add_event_subscriber(session_event_subscribers, subscriber)94end9596#97# Removes a session event subscriber.98#99def remove_session_subscriber(subscriber)100remove_event_subscriber(session_event_subscribers, subscriber)101end102103##104#105# General events106#107##108109#110# Called when a module is loaded into the framework. This, in turn,111# notifies all registered general event subscribers.112#113# This is covered by the method_missing logic, but defining it manually114# reduces startup time by about 10%.115#116def on_module_load(name, mod)117general_event_subscribers.each { |subscriber|118subscriber.on_module_load(name, mod)119}120end121122#123# Capture incoming events and pass them off to the subscribers124#125# When receiving an on_* event, look for a subscriber type matching the126# type of the event. If one exists, send the event on to each subscriber127# of that type. Otherwise, try to send the event each of the general128# subscribers.129#130# Event method names should be like "on_<type>_<event>", e.g.:131# on_exploit_success.132#133def method_missing(name, *args)134135event,type,rest = name.to_s.split("_", 3)136subscribers = "#{type}_event_subscribers"137found = false138case event139when "on"140if respond_to?(subscribers, true)141found = true142self.send(subscribers).each do |sub|143next if not sub.respond_to?(name, true)144sub.send(name, *args)145end146else147(general_event_subscribers + custom_event_subscribers).each do |sub|148next if not sub.respond_to?(name, true)149sub.send(name, *args)150found = true151end152end153when "add"154if respond_to?(subscribers, true)155found = true156add_event_subscriber(self.send(subscribers), *args)157end158when "remove"159if respond_to?(subscribers, true)160found = true161remove_event_subscriber(self.send(subscribers), *args)162end163end164165return found166end167168169protected170171#172# Adds an event subscriber to the supplied subscriber array.173#174def add_event_subscriber(array, subscriber) # :nodoc:175array << subscriber176end177178#179# Removes an event subscriber from the supplied subscriber array.180#181def remove_event_subscriber(array, subscriber) # :nodoc:182array.delete(subscriber)183end184185attr_accessor :custom_event_subscribers # :nodoc:186attr_accessor :db_event_subscribers # :nodoc:187attr_accessor :exploit_event_subscribers # :nodoc:188attr_accessor :general_event_subscribers # :nodoc:189attr_accessor :session_event_subscribers # :nodoc:190attr_accessor :ui_event_subscribers # :nodoc:191192end193194end195196197