2005-05-22 07:14:16 +00:00
|
|
|
require 'find'
|
2005-07-09 21:18:49 +00:00
|
|
|
require 'msf/core'
|
2005-05-22 07:14:16 +00:00
|
|
|
|
|
|
|
module Msf
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# ModuleSet
|
|
|
|
# ---------
|
|
|
|
#
|
|
|
|
# A module set contains zero or more named module classes of an arbitrary
|
|
|
|
# type.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class ModuleSet < Hash
|
2005-05-22 07:58:02 +00:00
|
|
|
def initialize(type = nil)
|
2005-05-22 07:46:41 +00:00
|
|
|
self.module_type = type
|
|
|
|
|
|
|
|
# Hashes that convey the supported architectures and platforms for a
|
|
|
|
# given module
|
|
|
|
self.mod_arch_hash = {}
|
|
|
|
self.mod_platform_hash = {}
|
2005-07-11 05:15:30 +00:00
|
|
|
self.mod_sorted = nil
|
2005-05-22 07:14:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Create an instance of the supplied module by its name
|
|
|
|
def create(name)
|
2005-07-09 19:35:29 +00:00
|
|
|
klass = self[name]
|
2005-05-22 07:17:43 +00:00
|
|
|
|
|
|
|
return (klass) ? klass.new : nil
|
2005-05-22 07:14:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Enumerates each module class in the set
|
2005-05-22 07:46:41 +00:00
|
|
|
def each_module(opts = {}, &block)
|
2005-07-11 05:15:30 +00:00
|
|
|
# Re-sort if the cached copy is out of date
|
|
|
|
mod_sorted = self.sort if (mod_sorted == nil)
|
|
|
|
|
|
|
|
mod_sorted.each { |entry|
|
|
|
|
name, mod = entry
|
|
|
|
|
2005-05-22 07:46:41 +00:00
|
|
|
# Filter out incompatible architectures
|
|
|
|
if (opts['arch'])
|
|
|
|
if (!mod_arch_hash[mod])
|
|
|
|
mod_arch_hash[mod] = mod.new.arch
|
|
|
|
end
|
|
|
|
|
|
|
|
next if (mod_arch_hash[mod].include?(opts['arch']) == false)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Filter out incompatible platforms
|
|
|
|
if (opts['platform'])
|
|
|
|
if (!mod_platform_hash[mod])
|
|
|
|
mod_platform_hash[mod] = mod.new.platform
|
|
|
|
end
|
|
|
|
|
|
|
|
next if (mod_platform_hash[mod].include?(opts['platform']) == false)
|
|
|
|
end
|
|
|
|
|
2005-07-12 05:39:44 +00:00
|
|
|
# Custom filtering
|
|
|
|
next if (each_module_filter(opts, name, entry) == true)
|
|
|
|
|
2005-07-10 00:49:12 +00:00
|
|
|
block.call(name, mod)
|
2005-05-22 07:46:41 +00:00
|
|
|
}
|
2005-05-22 07:14:16 +00:00
|
|
|
end
|
|
|
|
|
2005-07-12 05:39:44 +00:00
|
|
|
#
|
|
|
|
# Custom each_module filtering if an advanced set supports doing extended
|
|
|
|
# filtering. Returns true if the entry should be filtered.
|
|
|
|
#
|
|
|
|
def each_module_filter(opts, name, entry)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-07-09 00:24:02 +00:00
|
|
|
# Dummy placeholder to relcalculate aliases and other fun things
|
2005-07-12 05:39:44 +00:00
|
|
|
#
|
2005-07-09 00:24:02 +00:00
|
|
|
def recalculate
|
|
|
|
end
|
|
|
|
|
2005-07-09 19:35:29 +00:00
|
|
|
attr_reader :module_type
|
2005-05-22 07:58:02 +00:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
2005-07-10 00:16:48 +00:00
|
|
|
# Adds a module with a the supplied name
|
|
|
|
def add_module(module_class, name)
|
2005-07-10 19:21:40 +00:00
|
|
|
# Set the module's name so that it can be referenced when
|
|
|
|
# instances are created.
|
|
|
|
module_class.refname = name
|
|
|
|
|
2005-07-10 00:16:48 +00:00
|
|
|
self[name] = module_class
|
2005-07-11 05:15:30 +00:00
|
|
|
|
|
|
|
# Invalidate the sorted array
|
|
|
|
mod_sorted = nil
|
2005-05-22 07:14:16 +00:00
|
|
|
end
|
|
|
|
|
2005-07-09 19:35:29 +00:00
|
|
|
attr_writer :module_type
|
2005-05-22 07:46:41 +00:00
|
|
|
attr_accessor :mod_arch_hash, :mod_platform_hash
|
2005-07-11 05:15:30 +00:00
|
|
|
attr_accessor :mod_sorted
|
2005-05-22 07:14:16 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# ModuleManager
|
|
|
|
# -------------
|
|
|
|
#
|
|
|
|
# Upper management decided to throw in some middle management
|
|
|
|
# because the modules were getting out of hand. This bad boy
|
|
|
|
# takes care of the work of managing the interaction with
|
|
|
|
# modules in terms of loading and instantiation.
|
|
|
|
#
|
2005-05-22 07:23:25 +00:00
|
|
|
# TODO:
|
|
|
|
#
|
|
|
|
# - add reload support
|
|
|
|
# - add unload support
|
|
|
|
#
|
2005-05-22 07:14:16 +00:00
|
|
|
###
|
2005-05-22 07:58:02 +00:00
|
|
|
class ModuleManager < ModuleSet
|
2005-05-22 07:14:16 +00:00
|
|
|
|
2005-07-09 21:18:49 +00:00
|
|
|
require 'msf/core/payload_set'
|
2005-07-09 00:24:02 +00:00
|
|
|
|
2005-05-22 07:14:16 +00:00
|
|
|
def initialize()
|
|
|
|
self.module_paths = []
|
|
|
|
self.module_history = {}
|
|
|
|
self.module_history_mtime = {}
|
2005-05-22 07:58:02 +00:00
|
|
|
self.module_sets = {}
|
2005-05-22 07:14:16 +00:00
|
|
|
|
|
|
|
MODULE_TYPES.each { |type|
|
2005-07-09 00:24:02 +00:00
|
|
|
case type
|
|
|
|
when MODULE_PAYLOAD
|
|
|
|
instance = PayloadSet.new(self)
|
|
|
|
else
|
|
|
|
instance = ModuleSet.new(type)
|
|
|
|
end
|
|
|
|
|
|
|
|
self.module_sets[type] = instance
|
2005-05-22 07:14:16 +00:00
|
|
|
}
|
2005-05-22 07:58:02 +00:00
|
|
|
|
|
|
|
super
|
2005-05-22 07:14:16 +00:00
|
|
|
end
|
|
|
|
|
2005-05-22 07:23:25 +00:00
|
|
|
#
|
|
|
|
# Accessors by module type
|
|
|
|
#
|
|
|
|
|
|
|
|
# Returns the set of loaded encoder module classes
|
2005-05-22 07:14:16 +00:00
|
|
|
def encoders
|
2005-05-22 07:58:02 +00:00
|
|
|
return module_sets[MODULE_ENCODER]
|
2005-05-22 07:14:16 +00:00
|
|
|
end
|
|
|
|
|
2005-05-22 07:23:25 +00:00
|
|
|
# Returns the set of loaded exploit module classes
|
2005-05-22 07:14:16 +00:00
|
|
|
def exploits
|
2005-05-22 07:58:02 +00:00
|
|
|
return module_sets[MODULE_EXPLOIT]
|
2005-05-22 07:14:16 +00:00
|
|
|
end
|
|
|
|
|
2005-05-22 07:25:15 +00:00
|
|
|
# Returns the set of loaded nop module classes
|
|
|
|
def nops
|
2005-05-22 20:28:21 +00:00
|
|
|
return module_sets[MODULE_NOP]
|
2005-05-22 07:25:15 +00:00
|
|
|
end
|
|
|
|
|
2005-05-22 07:23:25 +00:00
|
|
|
# Returns the set of loaded payload module classes
|
|
|
|
def payloads
|
2005-05-22 07:58:02 +00:00
|
|
|
return module_sets[MODULE_PAYLOAD]
|
2005-05-22 07:23:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the set of loaded recon module classes
|
2005-05-22 07:14:16 +00:00
|
|
|
def recon
|
2005-05-22 07:58:02 +00:00
|
|
|
return module_sets[MODULE_RECON]
|
2005-05-22 07:14:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Module path management
|
|
|
|
#
|
|
|
|
|
|
|
|
# Adds a path to be searched for new modules
|
|
|
|
def add_module_path(path)
|
2005-06-04 08:23:16 +00:00
|
|
|
path.sub!(/#{File::SEPARATOR}$/, '')
|
|
|
|
|
2005-05-22 07:14:16 +00:00
|
|
|
module_paths << path
|
|
|
|
|
|
|
|
load_modules(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Removes a path from which to search for modules
|
|
|
|
def remove_module_path(path)
|
|
|
|
module_paths.delete(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
# Load all of the modules from the supplied module path (independent of
|
|
|
|
# module type)
|
|
|
|
def load_modules(path)
|
|
|
|
loaded = {}
|
2005-07-09 00:24:02 +00:00
|
|
|
recalc = {}
|
2005-05-22 07:14:16 +00:00
|
|
|
|
|
|
|
Find.find(path) { |file|
|
|
|
|
|
|
|
|
# If the file doesn't end in the expected extension...
|
|
|
|
next if (!file.match(/\.rb$/))
|
|
|
|
|
|
|
|
# If the file on disk hasn't changed with what we have stored in the
|
|
|
|
# cache, then there's no sense in loading it
|
|
|
|
if (!has_module_file_changed?(file))
|
|
|
|
dlog("Cached module from file #{file} has not changed.", 'core',
|
|
|
|
LEV_1)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Substitute the base path
|
|
|
|
path_base = file.sub(path + File::SEPARATOR, '')
|
|
|
|
|
2005-07-10 00:49:12 +00:00
|
|
|
# Derive the name from the path with the exclusion of the .rb
|
|
|
|
name = path_base.match(/^(.+?)#{File::SEPARATOR}(.*)(.rb?)$/)[2]
|
|
|
|
|
2005-07-09 23:48:41 +00:00
|
|
|
# Chop off the file name
|
2005-07-10 00:49:12 +00:00
|
|
|
path_base.sub!(/(.+)(#{File::SEPARATOR}.+)(.rb?)$/, '\1')
|
2005-05-22 07:14:16 +00:00
|
|
|
|
2005-07-09 23:48:41 +00:00
|
|
|
# Extract the module's namespace from its path
|
|
|
|
mod = mod_from_name(path_base)
|
|
|
|
type = path_base.match(/^(.+?)#{File::SEPARATOR}+?/)[1].sub(/s$/, '')
|
2005-05-22 07:14:16 +00:00
|
|
|
|
|
|
|
# Get the module and grab the current number of constants
|
2005-07-09 23:48:41 +00:00
|
|
|
old_constants = mod.constants
|
2005-05-22 07:14:16 +00:00
|
|
|
|
|
|
|
# Load the file
|
|
|
|
begin
|
|
|
|
if (!load(file))
|
|
|
|
elog("Failed to load from file #{file}.")
|
|
|
|
next
|
|
|
|
end
|
|
|
|
rescue LoadError
|
|
|
|
elog("LoadError: #{$!}.")
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
added = mod.constants - old_constants
|
|
|
|
|
|
|
|
if (added.length > 1)
|
|
|
|
elog("Loaded file contained more than one class (#{file}).")
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
# If nothing was added, check to see if there's anything
|
|
|
|
# in the cache
|
|
|
|
if (added.empty?)
|
|
|
|
if (module_history[file])
|
|
|
|
added = module_history[file]
|
|
|
|
else
|
|
|
|
elog("Loaded #{file} but no classes were added.")
|
|
|
|
next
|
|
|
|
end
|
|
|
|
else
|
|
|
|
added = mod.const_get(added[0])
|
|
|
|
end
|
|
|
|
|
|
|
|
ilog("Loaded #{type} module #{added} from #{file}.", 'core', LEV_1)
|
|
|
|
|
|
|
|
# Do some processing on the loaded module to get it into the
|
|
|
|
# right associations
|
2005-07-10 00:16:48 +00:00
|
|
|
on_module_load(added, type, name)
|
2005-05-22 07:14:16 +00:00
|
|
|
|
2005-07-09 00:24:02 +00:00
|
|
|
# Set this module type as needing recalculation
|
|
|
|
recalc[type] = true
|
|
|
|
|
2005-05-22 07:14:16 +00:00
|
|
|
# Append the added module to the hash of file->module
|
|
|
|
loaded[file] = added
|
|
|
|
}
|
|
|
|
|
|
|
|
# Cache the loaded file mtimes
|
|
|
|
loaded.each_key {|file|
|
|
|
|
module_history_mtime[file] = File.new(file).mtime
|
|
|
|
}
|
|
|
|
|
|
|
|
# Cache the loaded file module associations
|
|
|
|
module_history.update(loaded)
|
|
|
|
|
2005-07-09 00:24:02 +00:00
|
|
|
# Perform any required recalculations for the individual module types
|
|
|
|
# that actually had load changes
|
|
|
|
recalc.each_key { |key|
|
|
|
|
module_sets[key].recalculate
|
|
|
|
}
|
|
|
|
|
2005-05-22 07:14:16 +00:00
|
|
|
return loaded.values
|
|
|
|
end
|
|
|
|
|
|
|
|
# Checks to see if the supplied file has changed (if it's even in the
|
|
|
|
# cache)
|
|
|
|
def has_module_file_changed?(file)
|
|
|
|
return (module_history_mtime[file] != File.new(file).mtime)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the module object that is associated with the supplied module
|
|
|
|
# name
|
|
|
|
def mod_from_name(name)
|
2005-07-09 23:48:41 +00:00
|
|
|
obj = Msf
|
|
|
|
|
|
|
|
name.split(File::SEPARATOR).each { |m|
|
|
|
|
# Up-case the first letter and any prefixed by _
|
|
|
|
m.gsub!(/^[a-z]/) { |s| s.upcase }
|
|
|
|
m.gsub!(/(_[a-z])/) { |s| s[1..1].upcase }
|
2005-05-22 07:14:16 +00:00
|
|
|
|
|
|
|
begin
|
|
|
|
obj = obj.const_get(m)
|
|
|
|
rescue NameError
|
2005-07-09 23:48:41 +00:00
|
|
|
obj = obj.const_set(m, ::Module.new)
|
2005-05-22 07:14:16 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj
|
|
|
|
end
|
|
|
|
|
|
|
|
# Called when a module is initially loaded such that it can be
|
|
|
|
# categorized accordingly
|
2005-07-10 00:16:48 +00:00
|
|
|
def on_module_load(mod, type, name)
|
2005-07-09 00:24:02 +00:00
|
|
|
# Payload modules require custom loading as the individual files
|
|
|
|
# may not directly contain a logical payload that a user would
|
|
|
|
# reference, such as would be the case with a payload stager or
|
|
|
|
# stage. As such, when payload modules are loaded they are handed
|
|
|
|
# off to a special payload set. The payload set, in turn, will
|
|
|
|
# automatically create all the permutations after all the payload
|
|
|
|
# modules have been loaded.
|
|
|
|
if (type != MODULE_PAYLOAD)
|
|
|
|
# Add the module class to the list of modules and add it to the
|
|
|
|
# type separated set of module classes
|
2005-07-10 00:16:48 +00:00
|
|
|
add_module(mod, name)
|
2005-07-09 00:24:02 +00:00
|
|
|
end
|
2005-07-11 05:15:30 +00:00
|
|
|
|
2005-07-10 00:16:48 +00:00
|
|
|
module_sets[type].add_module(mod, name)
|
2005-05-22 07:14:16 +00:00
|
|
|
end
|
|
|
|
|
2005-05-22 07:58:02 +00:00
|
|
|
attr_accessor :modules, :module_sets
|
2005-05-22 07:14:16 +00:00
|
|
|
attr_accessor :module_paths
|
|
|
|
attr_accessor :module_history, :module_history_mtime
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|