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

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

Overview

Concerns loading Ruby modules 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::TYPE_BY_DIRECTORY, 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.



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

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)
        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



11
12
13
# File 'lib/msf/core/modules/loader/directory.rb', line 11

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

#loadable_module?(parent_path, type, module_reference_name) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
# File 'lib/msf/core/modules/loader/directory.rb', line 15

def loadable_module?(parent_path, type, module_reference_name)
  full_path = module_path(parent_path, type, module_reference_name)
  module_path?(full_path)
end

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

Returns the full path to the module file on disk.

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:

  • (String)

    Path to module file on disk.



62
63
64
65
66
67
# File 'lib/msf/core/modules/loader/directory.rb', line 62

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:



73
74
75
76
77
# File 'lib/msf/core/modules/loader/directory.rb', line 73

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

  read_module_content_from_path(full_path)
end

#read_module_content_from_path(full_path) ⇒ String (protected)

Loads the module content from the on disk file.

Parameters:

  • full_path

    Path to the module to be read

Returns:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/msf/core/modules/loader/directory.rb', line 83

def read_module_content_from_path(full_path)
  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