Module: Msf::ModuleManager::ModulePaths

Extended by:
ActiveSupport::Concern
Included in:
Msf::ModuleManager
Defined in:
lib/msf/core/module_manager/module_paths.rb

Overview

Deals with module paths in the Msf::ModuleManager

Instance Method Summary collapse

Instance Method Details

#add_module_path(path, opts = {}) ⇒ Hash{String => Integer}

Adds a path to be searched for new modules.

Parameters:

  • path (String)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • whitelist (Array)

    An array of regex patterns to search for specific modules

Returns:

  • (Hash{String => Integer})

    Maps module type to number of modules loaded


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/msf/core/module_manager/module_paths.rb', line 18

def add_module_path(path, opts={})
  nested_paths = []

  # remove trailing file separator
  path_without_trailing_file_separator = path.sub(/#{File::SEPARATOR}$/, '')

  # Make the path completely canonical
  pathname = Pathname.new(path_without_trailing_file_separator).expand_path
  extension = pathname.extname

  if extension == Msf::Modules::Loader::Archive::ARCHIVE_EXTENSION
    unless pathname.exist?
      raise ArgumentError, "The path supplied does not exist", caller
    end

    nested_paths << pathname.to_s
  else
    # Make sure the path is a valid directory
    unless pathname.directory?
      raise ArgumentError, "The path supplied is not a valid directory.", caller
    end

    nested_paths << pathname.to_s

    # Identify any fastlib archives inside of this path
    fastlib_glob = pathname.join('**', "*#{Msf::Modules::Loader::Archive::ARCHIVE_EXTENSION}")

    Dir.glob(fastlib_glob).each do |fastlib_path|
      nested_paths << fastlib_path
    end
  end

  # Update the module paths appropriately
  self.module_paths = (module_paths + nested_paths).flatten.uniq

  # Load all of the modules from the nested paths
  count_by_type = {}
  nested_paths.each { |path|
    path_count_by_type = load_modules(path, opts.merge({:force => false}))

    # merge hashes
    path_count_by_type.each do |type, path_count|
      accumulated_count = count_by_type.fetch(type, 0)
      count_by_type[type] = accumulated_count + path_count
    end
  }

  return count_by_type
end

#remove_module_path(path) ⇒ Object

Removes a path from which to search for modules.


71
72
73
74
# File 'lib/msf/core/module_manager/module_paths.rb', line 71

def remove_module_path(path)
  module_paths.delete(path)
  module_paths.delete(::File.expand_path(path))
end