Module: Msf::ModuleManager::Cache

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

Overview

Concerns the module cache maintained by the Msf::ModuleManager.

Instance Method Summary collapse

Instance Method Details

#cache_empty?true, false

Returns whether the cache is empty

Returns:

  • (true)

    if the cache has no entries.

  • (false)

    if the cache has any entries.


14
15
16
# File 'lib/msf/core/module_manager/cache.rb', line 14

def cache_empty?
  module_info_by_path.empty?
end

#cache_in_memory(class_or_module, options = {}) ⇒ void

Note:

path, reference_name, and type must be passed as options because when class_or_module is a payload Module, those attributes will either not be set or not exist on the module.

This method returns an undefined value.

Updates the in-memory cache so that Loading#file_changed? will report false if the module is loaded again.

Parameters:

  • class_or_module (Class<Msf::Module>, ::Module)

    either a module Class or a payload Module.

  • options (Hash{Symbol => String}) (defaults to: {})

Options Hash (options):

  • :path (String)

    the path to the file from which class_or_module was loaded.

  • :reference_name (String)

    the reference name for class_or_module.

  • :type (String)

    the module type

Raises:

  • (KeyError)

    unless :path is given.

  • (KeyError)

    unless :reference_name is given.

  • (KeyError)

    unless :type is given.


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
# File 'lib/msf/core/module_manager/cache.rb', line 36

def cache_in_memory(class_or_module, options={})
  options.assert_valid_keys(:path, :reference_name, :type)

  path = options.fetch(:path)

  begin
    modification_time = File.mtime(path)
  rescue Errno::ENOENT => error
    log_lines = []
    log_lines << "Could not find the modification of time of #{path}:"
    log_lines << error.class.to_s
    log_lines << error.to_s
    log_lines << "Call stack:"
    log_lines += error.backtrace

    log_message = log_lines.join("\n")
    elog(log_message)
  else
    parent_path = class_or_module.parent.parent_path
    reference_name = options.fetch(:reference_name)
    type = options.fetch(:type)

    module_info_by_path[path] = {
        :modification_time => modification_time,
        :parent_path => parent_path,
        :reference_name => reference_name,
        :type => type
    }
  end
end

#load_cached_module(type, reference_name) ⇒ false, true

Forces loading of the module with the given type and module reference name from the cache.

Parameters:

  • type (String)

    the type of the module.

  • reference_name (String)

    the module reference name.

Returns:

  • (false)

    if a module with the given type and reference name does not exist in the cache.

  • (false)

    if :force is false and parent_path has not changed.

  • (false)

    if exception encountered while parsing module content

  • (false)

    if the module is incompatible with the Core or API version.

  • (false)

    if the module does not implement a Metasploit(d+) class.

  • (false)

    if the module's is_usable method returns false.

  • (true)

    if all those condition pass and the module is successfully loaded.


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/msf/core/module_manager/cache.rb', line 73

def load_cached_module(type, reference_name)
  loaded = false

  module_info = self.module_info_by_path.values.find { |inner_info|
    inner_info[:type] == type and inner_info[:reference_name] == reference_name
  }

  if module_info
    parent_path = module_info[:parent_path]

    loaders.each do |loader|
      if loader.loadable?(parent_path)
        type = module_info[:type]
        reference_name = module_info[:reference_name]

        loaded = loader.load_module(parent_path, type, reference_name, :force => true)

        break
      end
    end
  end

  loaded
end

#refresh_cache_from_databasevoid

This method returns an undefined value.

Refreshes the in-memory cache from the database cache.


122
123
124
# File 'lib/msf/core/module_manager/cache.rb', line 122

def refresh_cache_from_database
  self.module_info_by_path_from_database!
end

#refresh_cache_from_module_filesvoid #refresh_cache_from_module_files(module_class_or_instance) ⇒ void

Overloads:

  • #refresh_cache_from_module_filesvoid

    This method returns an undefined value.

    Rebuilds database and in-memory cache for all modules.

  • #refresh_cache_from_module_files(module_class_or_instance) ⇒ void

    This method returns an undefined value.

    Rebuilds database and in-memory cache for given module_class_or_instance.

    Parameters:

    • module_instance (Msf::Module)

      a metasploit module instance.


107
108
109
110
111
112
113
114
115
116
117
# File 'lib/msf/core/module_manager/cache.rb', line 107

def refresh_cache_from_module_files(module_class_or_instance = nil)
  if framework_migrated?
    if module_class_or_instance
      framework.db.update_module_details(module_class_or_instance)
    else
      framework.db.update_all_module_details
    end

    refresh_cache_from_database
  end
end