Class: Msf::Modules::Loader::Base
- Inherits:
-
Object
- Object
- Msf::Modules::Loader::Base
- Defined in:
- lib/msf/core/modules/loader/base.rb
Overview
Direct Known Subclasses
Constant Summary collapse
- DIRECTORY_BY_TYPE =
Not all types are pluralized when a directory name, so here’s the mapping that currently exists
{ Msf::MODULE_AUX => 'auxiliary', Msf::MODULE_ENCODER => 'encoders', Msf::MODULE_EXPLOIT => 'exploits', Msf::MODULE_NOP => 'nops', Msf::MODULE_PAYLOAD => 'payloads', Msf::MODULE_POST => 'post', Msf::MODULE_EVASION => 'evasion' }
- TYPE_BY_DIRECTORY =
DIRECTORY_BY_TYPE.invert
- NAMESPACE_MODULE_LINE =
This must calculate the first line of the NAMESPACE_MODULE_CONTENT string so that errors are reported correctly
__LINE__ + 4
- NAMESPACE_MODULE_CONTENT =
By calling module_eval from inside the module definition, the lexical scope is captured and available to the code in module_content.
<<-EOS class << self # The loader that originally loaded this module # # @return [Msf::Modules::Loader::Base] the loader that loaded this namespace module and can reload it. attr_accessor :loader # @return [String] The path under which the module of the given type was found. attr_accessor :parent_path end # Calls module_eval on the module_content, but the lexical scope of the namespace_module is passed through # module_eval, so that module_content can act like it was written inline in the namespace_module. # # @param [String] module_content The content of the {Msf::Module}. # @param [String] module_path The path to the module, so that error messages in evaluating the module_content can # be reported correctly. def self.module_eval_with_lexical_scope(module_content, module_path) # By calling module_eval from inside the module definition, the lexical scope is captured and available to the # code in module_content. module_eval(module_content, module_path) end EOS
- MODULE_EXTENSION =
The extension for metasploit modules.
'.rb'
- MODULE_SEPARATOR =
String used to separate module names in a qualified module name.
'::'
- NAMESPACE_MODULE_NAMES =
The base namespace name under which namespace modules are created.
['Msf', 'Modules']
- UNIT_TEST_REGEX =
Regex that can distinguish regular ruby source from unit test source.
/rb\.(ut|ts)\.rb$/
Instance Attribute Summary collapse
-
#module_manager ⇒ Msf::ModuleManager
readonly
protected
The module manager for which this loader is loading modules.
Class Method Summary collapse
-
.reverse_relative_name(relative_name) ⇒ String
protected
This reverses a namespace module’s relative name to a module full name.
-
.typed_path(type, module_reference_name) ⇒ String
protected
The path to the module qualified by the type directory.
Instance Method Summary collapse
-
#create_namespace_module(namespace_module_names) ⇒ Module
protected
Returns a nested module to wrap the MetasploitModule class so that it doesn’t overwrite other (metasploit) module’s classes.
-
#current_module(module_names) ⇒ Module?
protected
Returns the module with ‘module_names` if it exists.
-
#each_module_reference_name(path) {|parent_path, type, module_reference_name| ... } ⇒ void
protected
abstract
Yields module reference names under path.
-
#initialize(module_manager) ⇒ Base
constructor
A new instance of Base.
-
#load_error(module_path, error) ⇒ void
protected
Records the load error to Msf::ModuleManager::Loading#module_load_error_by_path and the log.
-
#load_module(parent_path, type, module_reference_name, options = {}) ⇒ false, true
Loads a module from the supplied path and module_reference_name.
-
#load_modules(path, options = {}) ⇒ Hash{String => Integer}
Loads all of the modules from the supplied path.
-
#load_warning(module_path, error) ⇒ void
protected
Records the load warning to Msf::ModuleManager::Loading#module_load_warnings and the log.
-
#loadable?(path) ⇒ Boolean
abstract
Returns whether the path can be loaded this module loader.
-
#module_path(parent_path, type, module_reference_name) ⇒ String
protected
abstract
Returns path to module that can be used for reporting errors in evaluating the module_content.
-
#module_path?(path) ⇒ true, false
protected
Returns whether the path could refer to a module.
-
#module_reference_name_from_path(path) ⇒ String
protected
Changes a file name path to a canonical module reference name.
-
#namespace_module_name(module_full_name) ⇒ String
protected
Returns the fully-qualified name to the #create_namespace_module that wraps the module with the given module reference name.
-
#namespace_module_names(module_full_name) ⇒ Array<String>
protected
Returns an Array of names to make a fully qualified module name to wrap the MetasploitModule class so that it doesn’t overwrite other (metasploit) module’s classes.
- #namespace_module_transaction(module_full_name, options = {}, &block) ⇒ Object protected
-
#read_module_content(parent_path, type, module_reference_name) ⇒ String
protected
abstract
Read the content of the module from under path.
-
#read_module_content_from_path(full_path) ⇒ String
protected
abstract
Read the content of a module.
-
#reload_module(original_metasploit_class_or_instance) ⇒ Class, Msf::Module
Reloads the specified module.
-
#restore_namespace_module(parent_module, relative_name, namespace_module) ⇒ void
protected
Restores the namespace module to its original name under its original parent Module if there was a previous namespace module.
-
#script_path?(path) ⇒ Boolean
protected
Tries to determine if a file might be executable,.
-
#typed_path(type, module_reference_name) ⇒ String
protected
The path to the module qualified by the type directory.
Constructor Details
#initialize(module_manager) ⇒ Base
Returns a new instance of Base.
65 66 67 |
# File 'lib/msf/core/modules/loader/base.rb', line 65 def initialize(module_manager) @module_manager = module_manager end |
Instance Attribute Details
#module_manager ⇒ Msf::ModuleManager (readonly, protected)
Returns The module manager for which this loader is loading modules.
451 452 453 |
# File 'lib/msf/core/modules/loader/base.rb', line 451 def module_manager @module_manager end |
Class Method Details
.reverse_relative_name(relative_name) ⇒ String (protected)
This reverses a namespace module’s relative name to a module full name
533 534 535 |
# File 'lib/msf/core/modules/loader/base.rb', line 533 def self.reverse_relative_name(relative_name) relative_name.split('__').map(&:downcase).join('/') end |
.typed_path(type, module_reference_name) ⇒ String (protected)
The path to the module qualified by the type directory.
639 640 641 642 643 644 645 |
# File 'lib/msf/core/modules/loader/base.rb', line 639 def self.typed_path(type, module_reference_name) file_name = module_reference_name + MODULE_EXTENSION type_directory = DIRECTORY_BY_TYPE[type] typed_path = File.join(type_directory, file_name) typed_path end |
Instance Method Details
#create_namespace_module(namespace_module_names) ⇒ Module (protected)
Returns a nested module to wrap the MetasploitModule class so that it doesn’t overwrite other (metasploit) module’s classes. The wrapper module must be named so that active_support’s autoloading code doesn’t break when searching constants from inside the Metasploit class.
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/msf/core/modules/loader/base.rb', line 354 def create_namespace_module(namespace_module_names) # In order to have constants defined in Msf resolve without the Msf qualifier in the module_content, the # Module.nesting must resolve for the entire nesting. Module.nesting is strictly lexical, and can't be faked with # module_eval(&block). (There's actually code in ruby's implementation to stop module_eval from being added to # Module.nesting when using the block syntax.) All this means is the modules have to be declared as a string that # gets module_eval'd. nested_module_names = namespace_module_names.reverse namespace_module_content = nested_module_names.inject(NAMESPACE_MODULE_CONTENT) { |wrapped_content, module_name| lines = [] lines << "module #{module_name}" lines << wrapped_content lines << "end" lines.join("\n") } # - because the added wrap lines have to act like they were written before NAMESPACE_MODULE_CONTENT line_with_wrapping = NAMESPACE_MODULE_LINE - nested_module_names.length Object.module_eval(namespace_module_content, __FILE__, line_with_wrapping) # The namespace_module exists now, so no need to use constantize to do const_missing namespace_module = current_module(namespace_module_names) # record the loader, so that the namespace module and its metasploit_class can be reloaded namespace_module.loader = self namespace_module end |
#current_module(module_names) ⇒ Module? (protected)
Returns the module with ‘module_names` if it exists.
389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/msf/core/modules/loader/base.rb', line 389 def current_module(module_names) # Don't want to trigger ActiveSupport's const_missing, so can't use constantize. named_module = module_names.reduce(Object) do |parent, module_name| # Since we're searching parent namespaces first anyway, this is # semantically equivalent to providing false for the 1.9-only # "inherit" parameter to const_defined?. If we ever drop 1.8 # support, we can save a few cycles here by adding it back. return unless parent.const_defined?(module_name) parent.const_get(module_name) end named_module end |
#each_module_reference_name(path) {|parent_path, type, module_reference_name| ... } ⇒ void (protected)
Override and search the path for modules.
This method returns an undefined value.
Yields module reference names under path.
414 415 416 |
# File 'lib/msf/core/modules/loader/base.rb', line 414 def each_module_reference_name(path) raise ::NotImplementedError end |
#load_error(module_path, error) ⇒ void (protected)
This method returns an undefined value.
Records the load error to Msf::ModuleManager::Loading#module_load_error_by_path and the log.
425 426 427 428 429 430 431 |
# File 'lib/msf/core/modules/loader/base.rb', line 425 def load_error(module_path, error) # module_load_error_by_path does not get the backtrace because the value is echoed to the msfconsole where # backtraces should not appear. module_manager.module_load_error_by_path[module_path] = "#{error.class} #{error}" elog("#{module_path} failed to load", error: error) end |
#load_module(parent_path, type, module_reference_name, options = {}) ⇒ false, true
Loads a module from the supplied path and module_reference_name.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/msf/core/modules/loader/base.rb', line 110 def load_module(parent_path, type, module_reference_name, = {}) .assert_valid_keys(:count_by_type, :force, :recalculate_by_type, :reload, :cached_metadata) force = [:force] || false reload = [:reload] || false if [:cached_metadata] module_path = [:cached_metadata].path else module_path = self.module_path(parent_path, type, module_reference_name) end file_changed = module_manager.file_changed?(module_path) unless force or file_changed dlog("Cached module from #{module_path} has not changed.", 'core', LEV_2) return false end reload ||= force || file_changed module_content = read_module_content_from_path(module_path) if module_content.empty? # read_module_content is responsible for calling {#load_error}, so just return here. return false end klass = nil try_eval_module = lambda { |namespace_module| # set the parent_path so that the module can be reloaded with #load_module namespace_module.parent_path = parent_path begin namespace_module.module_eval_with_lexical_scope(module_content, module_path) # handle interrupts as pass-throughs unlike other Exceptions so users can bail with Ctrl+C rescue ::Interrupt raise rescue ::Exception => error load_error(module_path, error) return false end if namespace_module.const_defined?('Metasploit3', false) klass = namespace_module.const_get('Metasploit3', false) load_warning(module_path, "Please change the module's class name from Metasploit3 to MetasploitModule") elsif namespace_module.const_defined?('Metasploit4', false) klass = namespace_module.const_get('Metasploit4', false) load_warning(module_path, "Please change the module's class name from Metasploit4 to MetasploitModule") elsif namespace_module.const_defined?('MetasploitModule', false) klass = namespace_module.const_get('MetasploitModule', false) else load_error(module_path, Msf::Modules::Error.new( module_path: module_path, module_reference_name: module_reference_name, causal_message: 'invalid module class name (must be MetasploitModule)' )) return false end if reload ilog("Reloading #{type} module #{module_reference_name}. Ambiguous module warnings are safe to ignore", 'core', LEV_2) else ilog("Loaded #{type} module #{module_reference_name} under #{parent_path}", 'core', LEV_2) end module_manager.module_load_error_by_path.delete(module_path) true } begin loaded = namespace_module_transaction("#{type}/#{module_reference_name}", reload: reload, &try_eval_module) return false unless loaded rescue NameError load_error(module_path, Msf::Modules::Error.new( module_path: module_path, module_reference_name: module_reference_name, causal_message: 'invalid module filename (must be lowercase alphanumeric snake case)' )) return false rescue => e load_error(module_path, Msf::Modules::Error.new( module_path: module_path, module_reference_name: module_reference_name, causal_message: "unknown error #{e.}" )) return false end # Do some processing on the loaded module to get it into the right associations module_manager.on_module_load( klass, type, module_reference_name, { # files[0] is stored in the {Msf::Module#file_path} and is used to reload the module, so it needs to be a # full path 'files' => [ module_path ], 'paths' => [ module_reference_name ], 'type' => type, 'cached_metadata' => [:cached_metadata] } ) # Set this module type as needing recalculation recalculate_by_type = [:recalculate_by_type] if recalculate_by_type recalculate_by_type[type] = true end # The number of loaded modules this round count_by_type = [:count_by_type] if count_by_type count_by_type[type] ||= 0 count_by_type[type] += 1 end return true end |
#load_modules(path, options = {}) ⇒ Hash{String => Integer}
Only paths where #loadable? returns true should be passed to this method.
Loads all of the modules from the supplied path.
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/msf/core/modules/loader/base.rb', line 250 def load_modules(path, ={}) .assert_valid_keys(:force, :recalculate) force = [:force] count_by_type = {} recalculate_by_type = {} each_module_reference_name(path, ) do |parent_path, type, module_reference_name| load_module( parent_path, type, module_reference_name, :recalculate_by_type => recalculate_by_type, :count_by_type => count_by_type, :force => force ) end if [:recalculate] recalculate_by_type.each do |type, recalculate| if recalculate module_set = module_manager.module_set(type) module_set.recalculate end end end count_by_type end |
#load_warning(module_path, error) ⇒ void (protected)
This method returns an undefined value.
Records the load warning to Msf::ModuleManager::Loading#module_load_warnings and the log.
440 441 442 443 444 445 446 447 448 |
# File 'lib/msf/core/modules/loader/base.rb', line 440 def load_warning(module_path, error) module_manager.module_load_warnings[module_path] = error.to_s log_lines = [] log_lines << "#{module_path} generated a warning during load:" log_lines << error.to_s = log_lines.join(' ') wlog() end |
#loadable?(path) ⇒ Boolean
Override and determine from properties of the path or the file to which the path points whether it is loadable using #load_modules for the subclass.
Returns whether the path can be loaded this module loader.
77 78 79 |
# File 'lib/msf/core/modules/loader/base.rb', line 77 def loadable?(path) raise ::NotImplementedError end |
#module_path(parent_path, type, module_reference_name) ⇒ String (protected)
Override to return the path to the module on the file system so that errors can be reported correctly.
Returns path to module that can be used for reporting errors in evaluating the module_content.
462 463 464 |
# File 'lib/msf/core/modules/loader/base.rb', line 462 def module_path(parent_path, type, module_reference_name) raise ::NotImplementedError end |
#module_path?(path) ⇒ true, false (protected)
Returns whether the path could refer to a module. The path would still need to be loaded in order to check if it actually is a valid module.
474 475 476 477 478 479 480 |
# File 'lib/msf/core/modules/loader/base.rb', line 474 def module_path?(path) path.ends_with?(MODULE_EXTENSION) && File.file?(path) && !path.starts_with?(".") && !path.match?(UNIT_TEST_REGEX) && !script_path?(path) end |
#module_reference_name_from_path(path) ⇒ String (protected)
Changes a file name path to a canonical module reference name.
493 494 495 |
# File 'lib/msf/core/modules/loader/base.rb', line 493 def module_reference_name_from_path(path) path.gsub(/#{MODULE_EXTENSION}$/, '') end |
#namespace_module_name(module_full_name) ⇒ String (protected)
Returns the fully-qualified name to the #create_namespace_module that wraps the module with the given module reference name.
506 507 508 509 510 511 |
# File 'lib/msf/core/modules/loader/base.rb', line 506 def namespace_module_name(module_full_name) namespace_module_names = self.namespace_module_names(module_full_name) namespace_module_name = namespace_module_names.join(MODULE_SEPARATOR) namespace_module_name end |
#namespace_module_names(module_full_name) ⇒ Array<String> (protected)
Returns an Array of names to make a fully qualified module name to wrap the MetasploitModule class so that it doesn’t overwrite other (metasploit) module’s classes.
522 523 524 525 |
# File 'lib/msf/core/modules/loader/base.rb', line 522 def namespace_module_names(module_full_name) relative_name = module_full_name.split('/').map(&:capitalize).join('__') NAMESPACE_MODULE_NAMES + [relative_name] end |
#namespace_module_transaction(module_full_name, options = {}, &block) ⇒ Object (protected)
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 |
# File 'lib/msf/core/modules/loader/base.rb', line 537 def namespace_module_transaction(module_full_name, ={}, &block) .assert_valid_keys(:reload) reload = [:reload] || false namespace_module_names = self.namespace_module_names(module_full_name) previous_namespace_module = current_module(namespace_module_names) if previous_namespace_module and not reload elog("Reloading namespace_module #{previous_namespace_module} when :reload => false") end relative_name = namespace_module_names.last if previous_namespace_module parent_module = previous_namespace_module.module_parent # remove_const is private, so use send to bypass parent_module.send(:remove_const, relative_name) end namespace_module = create_namespace_module(namespace_module_names) # Get the parent module from the created module so that # restore_namespace_module can remove namespace_module's constant if # needed. parent_module = namespace_module.module_parent begin loaded = block.call(namespace_module) rescue Exception restore_namespace_module(parent_module, relative_name, previous_namespace_module) # re-raise the original exception in the original context raise else unless loaded restore_namespace_module(parent_module, relative_name, previous_namespace_module) end loaded end end |
#read_module_content(parent_path, type, module_reference_name) ⇒ String (protected)
Override to read the module content based on the method of the loader subclass and return a string.
Read the content of the module from under path.
587 588 589 |
# File 'lib/msf/core/modules/loader/base.rb', line 587 def read_module_content(parent_path, type, module_reference_name) raise ::NotImplementedError end |
#read_module_content_from_path(full_path) ⇒ String (protected)
Override to read the module content based on the method of the loader subclass and return a string.
Read the content of a module
597 598 599 |
# File 'lib/msf/core/modules/loader/base.rb', line 597 def read_module_content_from_path(full_path) raise ::NotImplementedError end |
#reload_module(original_metasploit_class_or_instance) ⇒ Class, Msf::Module
Reloads the specified module.
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/msf/core/modules/loader/base.rb', line 287 def reload_module() if .is_a? Msf::Module = = .class else = nil = end namespace_module = .module_parent parent_path = namespace_module.parent_path type = .type module_reference_name = .refname module_fullname = .fullname module_used_name = .fullname if dlog("Reloading module #{module_fullname}...", 'core') if load_module(parent_path, type, module_reference_name, :force => true, :reload => true) # Create a new instance of the module, using the alias if one was used reloaded_module_instance = module_manager.create(module_used_name || module_fullname) if !reloaded_module_instance && module_fullname != module_used_name reloaded_module_instance = module_manager.create(module_fullname) reloaded_module_instance&.add_warning "Alias #{module_used_name} no longer available after reloading, using #{module_fullname}" end if reloaded_module_instance if # copy over datastore reloaded_module_instance.datastore.update(.datastore) end else elog("Failed to create instance of #{.refname} after reload.") # Return the old module instance to avoid an strace trace return end else elog("Failed to reload #{module_fullname}") return nil end # Let the specific module sets have an opportunity to handle the fact # that this module was reloaded. module_set = module_manager.module_set(type) module_set.on_module_reload(reloaded_module_instance) # Rebuild the cache for just this module module_manager.refresh_cache_from_module_files(reloaded_module_instance) reloaded_module_instance end |
#restore_namespace_module(parent_module, relative_name, namespace_module) ⇒ void (protected)
This method returns an undefined value.
Restores the namespace module to its original name under its original parent Module if there was a previous namespace module.
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 |
# File 'lib/msf/core/modules/loader/base.rb', line 609 def restore_namespace_module(parent_module, relative_name, namespace_module) if parent_module # If there is a current module with relative_name if parent_module.const_defined?(relative_name) # if the current value isn't the value to be restored. if parent_module.const_get(relative_name) != namespace_module # remove_const is private, so use send to bypass parent_module.send(:remove_const, relative_name) # if there was a previous module, not set it to the name if namespace_module parent_module.const_set(relative_name, namespace_module) end end else # if there was a previous module, but there isn't a current module, then restore the previous module if namespace_module parent_module.const_set(relative_name, namespace_module) end end end end |
#script_path?(path) ⇒ Boolean (protected)
Tries to determine if a file might be executable,
483 484 485 486 487 |
# File 'lib/msf/core/modules/loader/base.rb', line 483 def script_path?(path) File.file?(path) && File.executable?(path) && ['#!', '//'].include?(File.read(path, 2)) end |
#typed_path(type, module_reference_name) ⇒ String (protected)
To get the full path to the module, use #module_path.
The path to the module qualified by the type directory.
653 654 655 |
# File 'lib/msf/core/modules/loader/base.rb', line 653 def typed_path(type, module_reference_name) self.class.typed_path(type, module_reference_name) end |