Module: Metasploit::Framework::Require

Included in:
Msf::Auxiliary::Report, Msf::DBManager
Defined in:
lib/metasploit/framework/require.rb

Overview

Extension to `Kernel#require` behavior.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.optionally(name, without_warning) { ... } ⇒ void

This method returns an undefined value.

Tries to require `name`. If a `LoadError` occurs, then `without_warning` is printed to standard error using `Kernel#warn`, along with instructions for reinstalling the bundle. If a `LoadError` does not occur, then `with_block` is called.

Parameters:

  • name (String)

    the name of the library to `Kernel#require`.

  • without_warning (String)

    warning to print if `name` cannot be required.

Yields:

  • block to run when `name` requires successfully

Yield Returns:

  • (void)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/metasploit/framework/require.rb', line 20

def self.optionally(name, without_warning)
  begin
    require name
  rescue LoadError
    warn without_warning
    warn "Bundle installed '--without #{Bundler.settings.without.join(' ')}'"
    warn "To clear the without option do `bundle install --without ''` " \
     "(the --without flag with an empty string) or " \
     "`rm -rf .bundle` to remove the .bundle/config manually and " \
     "then `bundle install`"
  else
    if block_given?
      yield
    end
  end
end

.optionally_active_record_railtievoid

This method returns an undefined value.

Tries to `require 'active_record/railtie'` to define the activerecord Rails initializers and rake tasks.

Examples:

Optionally requiring 'active_record/railtie'

require 'metasploit/framework/require'

class MyClass
  def setup
    if database_enabled
      Metasploit::Framework::Require.optionally_active_record_railtie
    end
  end
end


51
52
53
54
55
56
57
58
59
60
# File 'lib/metasploit/framework/require.rb', line 51

def self.optionally_active_record_railtie
  if ::Rails.application.config.paths['config/database'].any?
    optionally(
      'active_record/railtie',
      'activerecord not in the bundle, so database support will be disabled.'
    )
  else
    warn 'Could not find database.yml, so database support will be disabled.'
  end
end

.optionally_include_metasploit_credential_creation(including_module) ⇒ void

This method returns an undefined value.

Tries to `require 'metasploit/credential'` and include `Metasploit::Credential::Creation` in the `including_module`.

Parameters:

  • including_module (Module)

    `Class` or `Module` that wants to `include Metasploit::Credential::Creation`.



67
68
69
70
71
72
73
74
# File 'lib/metasploit/framework/require.rb', line 67

def self.optionally_include_metasploit_credential_creation(including_module)
  optionally(
      'metasploit/credential',
      "metasploit-credential not in the bundle, so Metasploit::Credential creation will fail for #{including_module.name}"
  ) do
    including_module.send(:include, Metasploit::Credential::Creation)
  end
end

.optionally_require_metasploit_db_gem_enginesvoid

This method returns an undefined value.

Tries to require gems necessary for using a database with the framework.

Examples:

Metasploit::Framework::Require.optionally_require_metasploit_db_gems


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/metasploit/framework/require.rb', line 82

def self.optionally_require_metasploit_db_gem_engines
  optionally(
      'metasploit/credential',
      'metasploit-credential not in the bundle',
  ) do
    require 'metasploit/credential/engine'
  end

  optionally(
    'metasploit_data_models',
    'metasploit_data_models not in the bundle'
  ) do
    require 'metasploit_data_models/engine'
  end
end

Instance Method Details

#optionally_include_metasploit_credential_creationvoid

This method returns an undefined value.

Tries to `require 'metasploit/credential/creation'` and include it in this `Class` or `Module`.

Examples:

Using in a `Module`

require 'metasploit/framework/require'

module MyModule
  extend Metasploit::Framework::Require

  optionally_include_metasploit_credential_creation
end


114
115
116
# File 'lib/metasploit/framework/require.rb', line 114

def optionally_include_metasploit_credential_creation
  Metasploit::Framework::Require.optionally_include_metasploit_credential_creation(self)
end