Class: Msf::Modules::Loader::Directory

Inherits:
Base
  • Object
show all
Defined in:
lib/msf/core/modules/loader/directory.rb

Overview

Concerns loading module from a directory

Constant Summary

Constants inherited from Base

Base::DIRECTORY_BY_TYPE, Base::MODULE_EXTENSION, Base::MODULE_SEPARATOR, Base::NAMESPACE_MODULE_CONTENT, Base::NAMESPACE_MODULE_LINE, Base::NAMESPACE_MODULE_NAMES, Base::UNIT_TEST_REGEX

Instance Attribute Summary

Attributes inherited from Base

#module_manager

Instance Method Summary collapse

Methods inherited from Base

#create_namespace_module, #current_module, #initialize, #load_error, #load_module, #load_modules, #load_warning, #module_path?, #module_reference_name_from_path, #namespace_module_name, #namespace_module_names, #namespace_module_transaction, #reload_module, #restore_namespace_module, reverse_relative_name, #script_path?, typed_path, #typed_path

Constructor Details

This class inherits a constructor from Msf::Modules::Loader::Base

Instance Method Details

#each_module_reference_name(path, opts = {}) {|parent_path, type, module_reference_name| ... } ⇒ void (protected)

This method returns an undefined value.

Yields the module_reference_name for each module file found under the directory path.

Parameters:

  • path (String)

    The path to the directory.

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

    Input Hash.

Yields:

  • (parent_path, type, module_reference_name)

    Gives the path and the module_reference_name of the module found under the path.

Yield Parameters:

  • path (String)

    The path to the directory.

  • type (String)

    The type correlated with the directory under path.

  • module_reference_name (String)

    The canonical name for referencing the module.


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
# File 'lib/msf/core/modules/loader/directory.rb', line 28

def each_module_reference_name(path, opts={})
  whitelist = opts[:whitelist] || []
  ::Dir.foreach(path) do |entry|

    full_entry_path = ::File.join(path, entry)
    type = entry.singularize

    next unless ::File.directory?(full_entry_path) && module_manager.type_enabled?(type)

    full_entry_pathname = Pathname.new(full_entry_path)

    # Try to load modules from all the files in the supplied path
    Rex::Find.find(full_entry_path) do |entry_descendant_path|
      if module_path?(entry_descendant_path) && !script_path?(entry_descendant_path)
        entry_descendant_pathname = Pathname.new(entry_descendant_path)
        relative_entry_descendant_pathname = entry_descendant_pathname.relative_path_from(full_entry_pathname)
        relative_entry_descendant_path = relative_entry_descendant_pathname.to_s
        next if File::basename(relative_entry_descendant_path).start_with?('example')
        # The module_reference_name doesn't have a file extension
        module_reference_name = module_reference_name_from_path(relative_entry_descendant_path)

        yield path, type, module_reference_name
      end
    end
  end
end

#loadable?(path) ⇒ true, false

Returns true if the path is a directory

Parameters:

  • path (String)

    Path under which there are modules

Returns:

  • (true)

    if path is a directory

  • (false)

    otherwise


13
14
15
# File 'lib/msf/core/modules/loader/directory.rb', line 13

def loadable?(path)
  File.directory?(path)
end

#module_path(parent_path, type, module_reference_name) ⇒ String (protected)

Returns the full path to the module file on disk.

Parameters:

  • type (String)

    The type of module.

  • module_reference_name (String)

    The canonical name for referring to the module.

Returns:

  • (String)

    Path to module file on disk.


59
60
61
62
63
64
# File 'lib/msf/core/modules/loader/directory.rb', line 59

def module_path(parent_path, type, module_reference_name)
  typed_path = self.typed_path(type, module_reference_name)
  full_path = File.join(parent_path, typed_path)

  full_path
end

#read_module_content(parent_path, type, module_reference_name) ⇒ String (protected)

Loads the module content from the on disk file.

Parameters:

  • parent_path (String)

    The path under which the module exists. This is not necessarily the same path as passed to Base#load_modules: it may just be derived from that path.

  • type (String)

    The type of module.

  • module_reference_name (String)

    The canonical name for referring to the module.

Returns:


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/msf/core/modules/loader/directory.rb', line 70

def read_module_content(parent_path, type, module_reference_name)
  full_path = module_path(parent_path, type, module_reference_name)

  module_content = ''

  begin
    # force to read in binary mode so Pro modules won't be truncated on Windows
    File.open(full_path, 'rb') do |f|
      # Pass the size of the file as it leads to faster reads due to fewer buffer resizes. Greatest effect on Windows.
      # @see http://www.ruby-forum.com/topic/209005
      # @see https://github.com/ruby/ruby/blob/ruby_1_8_7/io.c#L1205
      # @see https://github.com/ruby/ruby/blob/ruby_1_9_3/io.c#L2038
      module_content = f.read(f.stat.size)
    end
  rescue Errno::ENOENT => error
    load_error(full_path, error)
  end

  module_content
end