Class: Metasploit::Framework::Command::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/metasploit/framework/command/base.rb

Overview

Based on pattern used for lib/rails/commands in the railties gem.

Direct Known Subclasses

Console

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.

Parameters:

  • attributes (Hash{Symbol => ActiveSupport::OrderedOptions,Rails::Application}) (defaults to: {})

Options Hash (attributes):

  • :application (Rails::Application)
  • :parsed_options (ActiveSupport::OrderedOptions)

Raises:

  • (KeyError) —

    if :application is not given

  • (KeyError) —

    if :parsed_options is not given



94
95
96
97
# File 'lib/metasploit/framework/command/base.rb', line 94

def initialize(attributes={})
  @application = attributes.fetch(:application)
  @parsed_options = attributes.fetch(:parsed_options)
end

Instance Attribute Details

#application ⇒ Object (readonly)

Returns the value of attribute application.



25
26
27
# File 'lib/metasploit/framework/command/base.rb', line 25

def application
  @application
end

#parsed_options ⇒ Object (readonly)

Returns the value of attribute parsed_options.



31
32
33
# File 'lib/metasploit/framework/command/base.rb', line 31

def parsed_options
  @parsed_options
end

Class Method Details

.parsed_options ⇒ Object



68
69
70
# File 'lib/metasploit/framework/command/base.rb', line 68

def self.parsed_options
  parsed_options_class.new
end

.parsed_options_class ⇒ Object



72
73
74
# File 'lib/metasploit/framework/command/base.rb', line 72

def self.parsed_options_class
  @parsed_options_class ||= parsed_options_class_name.constantize
end

.parsed_options_class_name ⇒ Object



76
77
78
# File 'lib/metasploit/framework/command/base.rb', line 76

def self.parsed_options_class_name
  @parsed_options_class_name ||= "#{module_parent.module_parent}::ParsedOptions::#{name.demodulize}"
end

.require_environment! ⇒ Object

Note:

require_environment! should be called to load config/application.rb to so that the RAILS_ENV can be set from the command line options in ARGV prior to Rails.env being set.

Note:

After returning, Rails.application will be defined and configured.

Parses ARGV for command line arguments to configure the Rails.application.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/metasploit/framework/command/base.rb', line 46

def self.require_environment!
  parsed_options = self.parsed_options
  # RAILS_ENV must be set before requiring 'config/application.rb'
  parsed_options.environment!
  ARGV.replace(parsed_options.positional)

  # allow other Rails::Applications to use this command
  if !defined?(Rails) || Rails.application.nil?
    # @see https://github.com/rails/rails/blob/v3.2.17/railties/lib/rails/commands.rb#L39-L40
    require Pathname.new(__FILE__).parent.parent.parent.parent.parent.join('config', 'application')
  end

  # have to configure before requiring environment because
  # config/environment.rb calls initialize! and the initializers will use
  # the configuration from the parsed options.
  parsed_options.configure(Rails.application)

  Rails.application.require_environment!

  parsed_options
end

.start ⇒ Object



80
81
82
83
# File 'lib/metasploit/framework/command/base.rb', line 80

def self.start
  parsed_options = require_environment!
  new(application: Rails.application, parsed_options: parsed_options).start
end

Instance Method Details

#start ⇒ void

This method is abstract.

Use #application to start this command.

This method returns an undefined value.

Starts this command.

Raises:

  • (NotImplementedError)


105
106
107
# File 'lib/metasploit/framework/command/base.rb', line 105

def start
  raise NotImplementedError
end