Class: Msf::PluginManager

Inherits:
Array
  • Object
show all
Includes:
Framework::Offspring
Defined in:
lib/msf/core/plugin_manager.rb

Overview

This class manages the loading and unloading plugins. All plugins must implement the Plugin base class interface.

Constant Summary collapse

@@path_hash =

The hash of path names to classes that is used during load.

{}

Instance Attribute Summary

Attributes included from Framework::Offspring

#framework

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(framework) ⇒ PluginManager

Initializes the plugin manager.



38
39
40
# File 'lib/msf/core/plugin_manager.rb', line 38

def initialize(framework)
  self.framework = framework
end

Class Method Details

.check_path_hash(path) ⇒ Object

Check the hash using the supplied path name to see if there is already a class association.



24
25
26
# File 'lib/msf/core/plugin_manager.rb', line 24

def self.check_path_hash(path)
  @@path_hash[path]
end

.set_path_hash(path, klass) ⇒ Object

Set the class that’s associated with the supplied hash.



31
32
33
# File 'lib/msf/core/plugin_manager.rb', line 31

def self.set_path_hash(path, klass)
  @@path_hash[path] = klass
end

Instance Method Details

#load(path, opts = {}) ⇒ Object

Loads a plugin from the supplied path and returns the instance that is created as a result.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/msf/core/plugin_manager.rb', line 46

def load(path, opts = {})
  # Check to see if a plugin from this path has already been loaded
  # before.
  if ((klass = self.class.check_path_hash(path)) == nil)
    old = Msf::Plugin.constants
    require(path)
    new = Msf::Plugin.constants

    # No new classes added?
    if ((diff = new - old).empty?)
      raise RuntimeError, "No classes were loaded from #{path} in the Msf::Plugin namespace."
    end

    # Grab the class
    klass = Msf::Plugin.const_get(diff[0])

    # Cache the path to class association for future reference
    self.class.set_path_hash(path, klass)
  # If it's already been loaded, go ahead and try to re-load it in case
  # the contents have changed.
  else
    Kernel.load(path + ".rb")
  end

  # Force unloading if already loaded
  plugin = self.find { |p| p.class == klass }
  unload(plugin) if plugin

  # Create an instance of the plugin and let it initialize
  instance = klass.create(framework, opts)

  # Add it to the list of plugins
  if (self.member?(instance) == false)
    self.unshift(instance)
  end

  # Return the instance to the caller
  instance
end

#unload(inst) ⇒ Object

Unloads a plugin using the instance that was returned from a previous call to load.



90
91
92
93
94
95
96
97
# File 'lib/msf/core/plugin_manager.rb', line 90

def unload(inst)
  # If the reference count drops to zero, remove it from the list of
  # loaded plugins.  This will indirectly call the cleanup method on the
  # plugin.
  if (inst.deref == true)
    delete(inst)
  end
end