Class: Msf::FeatureManager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/msf/core/feature_manager.rb

Overview

The feature manager is responsible for managing feature flags that can change characteristics of framework. Each feature will have a default value. The user can choose to override this default value if they wish.

Constant Summary collapse

CONFIG_KEY =
'framework/features'
WRAPPED_TABLES =
'wrapped_tables'
DEFAULTS =
[
  {
    name: WRAPPED_TABLES,
    description: 'When enabled Metasploit will wordwrap all tables to fit into the available terminal width',
    default_value: true
  }.freeze
].freeze

Instance Method Summary collapse

Constructor Details

#initializeFeatureManager

Initializes the feature manager.


28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/msf/core/feature_manager.rb', line 28

def initialize
  @flag_lookup = DEFAULTS.each_with_object({}) do |feature, acc|
    if feature[:name] == WRAPPED_TABLES
      if feature[:default_value] == true
        Rex::Text::Table.wrap_tables!
      else
        Rex::Text::Table.unwrap_tables!
      end
    end

    key = feature[:name]
    acc[key] = feature.dup
  end
end

Instance Method Details

#allObject


43
44
45
46
47
# File 'lib/msf/core/feature_manager.rb', line 43

def all
  @flag_lookup.values.map do |feature|
    feature.slice(:name, :description).merge(enabled: enabled?(feature[:name]))
  end
end

#enabled?(name) ⇒ Boolean

Returns:

  • (Boolean)

49
50
51
52
53
54
# File 'lib/msf/core/feature_manager.rb', line 49

def enabled?(name)
  return false unless @flag_lookup[name]

  feature = @flag_lookup[name]
  feature.key?(:user_preference) ? feature[:user_preference] : feature[:default_value]
end

#exists?(name) ⇒ Boolean

Returns:

  • (Boolean)

56
57
58
# File 'lib/msf/core/feature_manager.rb', line 56

def exists?(name)
  @flag_lookup.key?(name)
end

#load_configObject


78
79
80
81
82
83
# File 'lib/msf/core/feature_manager.rb', line 78

def load_config
  conf = Msf::Config.load
  conf.fetch(CONFIG_KEY, {}).each do |name, value|
    set(name, value == 'true')
  end
end

#namesObject


60
61
62
# File 'lib/msf/core/feature_manager.rb', line 60

def names
  all.map { |feature| feature[:name] }
end

#save_configObject


85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/msf/core/feature_manager.rb', line 85

def save_config
  # Note, we intentionally omit features that have not explicitly been set by the user.
  config = Msf::Config.load
  old_config = config.fetch(CONFIG_KEY, {})
  new_config = @flag_lookup.values.each_with_object(old_config) do |feature, config|
    next unless feature.key?(:user_preference)

    config.merge!(feature[:name] => feature[:user_preference].to_s)
  end

  Msf::Config.save(CONFIG_KEY => new_config)
end

#set(name, value) ⇒ Object


64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/msf/core/feature_manager.rb', line 64

def set(name, value)
  return false unless @flag_lookup[name]

  @flag_lookup[name][:user_preference] = value

  if name == WRAPPED_TABLES
    if value
      Rex::Text::Table.wrap_tables!
    else
      Rex::Text::Table.unwrap_tables!
    end
  end
end