Class: Msf::Config

Inherits:
Hash
  • Object
show all
Defined in:
lib/msf/base/config.rb

Overview

This class wraps interaction with global configuration that can be used as a persistent storage point for configuration, logs, and other such fun things.

Constant Summary collapse

InstallRoot =

The installation's root directory for the distribution

File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..'))
FileSep =

Default system file separator.

File::SEPARATOR
Defaults =

Default configuration locations.

{
  'ConfigDirectory'     => get_config_root,
  'ConfigFile'          => "config",
  'ModuleDirectory'     => "modules",
  'ScriptDirectory'     => "scripts",
  'LogDirectory'        => "logs",
  'SessionLogDirectory' => "logs/sessions",
  'PluginDirectory'     => "plugins",
  'DataDirectory'       => "data",
  'LootDirectory'       => "loot",
  'LocalDirectory'      => "local"
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHash

Updates the config class' self with the default hash.


211
212
213
# File 'lib/msf/base/config.rb', line 211

def initialize
  update(Defaults)
end

Class Method Details

.config_directoryString

Returns the configuration directory default.

Returns:

  • (String)

    the root configuration directory.


79
80
81
# File 'lib/msf/base/config.rb', line 79

def self.config_directory
  self.new.config_directory
end

.config_fileString

Returns the full path to the configuration file.

Returns:

  • (String)

    path to the configuration file.


163
164
165
# File 'lib/msf/base/config.rb', line 163

def self.config_file
  self.new.config_file
end

.data_directoryString

Returns the data directory

Returns:

  • (String)

    path to data directory.


156
157
158
# File 'lib/msf/base/config.rb', line 156

def self.data_directory
  self.new.data_directory
end

.get_config_rootString

Determines the base configuration directory.

Returns:

  • (String)

    the base configuration directory


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/msf/base/config.rb', line 16

def self.get_config_root

  # Use MSFCFGDIR environment variable first. See feature request #5797
  val = Rex::Compat.getenv('MSF_CFGROOT_CONFIG')
  if (val and File.directory?(val))
    return val
  end

  # Windows-specific environment variables
  ['HOME', 'LOCALAPPDATA', 'APPDATA', 'USERPROFILE'].each do |dir|
    val = Rex::Compat.getenv(dir)
    if (val and File.directory?(val))
      return File.join(val, ".msf#{Msf::Framework::Major}")
    end
  end

  begin
    # First we try $HOME/.msfx
    File.expand_path("~#{FileSep}.msf#{Msf::Framework::Major}")
  rescue ::ArgumentError
    # Give up and install root + ".msfx"
    InstallRoot + ".msf#{Msf::Framework::Major}"
  end
end

.history_fileString

Returns the full path to the history file.

Returns:

  • (String)

    path the history file.


170
171
172
# File 'lib/msf/base/config.rb', line 170

def self.history_file
  self.new.history_file
end

.initvoid

This method returns an undefined value.

Initializes configuration, creating directories as necessary.


177
178
179
# File 'lib/msf/base/config.rb', line 177

def self.init
  self.new.init
end

.install_rootString

Returns the framework installation root.

Returns:

  • (String)

    the framework installation root InstallRoot.


72
73
74
# File 'lib/msf/base/config.rb', line 72

def self.install_root
  InstallRoot
end

.load(path = nil) ⇒ Rex::Parser::Ini

Loads configuration from the supplied file path, or the default one if none is specified.

Parameters:

  • path (String) (defaults to: nil)

    the path to the configuration file.

Returns:


186
187
188
# File 'lib/msf/base/config.rb', line 186

def self.load(path = nil)
  self.new.load(path)
end

.local_directoryString

Returns the directory in which locally-generated data will reside.

Returns:

  • (String)

    path to locally-generated data directory.


135
136
137
# File 'lib/msf/base/config.rb', line 135

def self.local_directory
  self.new.local_directory
end

.log_directoryString

Returns the directory that log files should be stored in.

Returns:

  • (String)

    path to log directory.


100
101
102
# File 'lib/msf/base/config.rb', line 100

def self.log_directory
  self.new.log_directory
end

.loot_directoryString

Returns the directory in which captured data will reside.

Returns:

  • (String)

    path to loot directory.


128
129
130
# File 'lib/msf/base/config.rb', line 128

def self.loot_directory
  self.new.loot_directory
end

.module_directoryString

Returns the global module directory.

Returns:

  • (String)

    path to global module directory.


86
87
88
# File 'lib/msf/base/config.rb', line 86

def self.module_directory
  self.new.module_directory
end

.plugin_directoryString

Returns the directory that plugins are stored in.

Returns:

  • (String)

    path to plugin directory.


107
108
109
# File 'lib/msf/base/config.rb', line 107

def self.plugin_directory
  self.new.plugin_directory
end

.save(opts) ⇒ void

This method returns an undefined value.

Saves configuration to the path specified in the ConfigFile hash key or the default path if one isn't specified. The options should be group references that have named value pairs.

Examples:

Save 'Cat' => 'Foo' in group 'ExampleGroup'

save(
  'ExampleGroup' =>
     {
        'Foo' => 'Cat'
     })

Parameters:

  • opts (Hash)

    Hash containing configuration options.

Options Hash (opts):

  • configuration ('ConfigFile'Hash)

    file these options apply to.


204
205
206
# File 'lib/msf/base/config.rb', line 204

def self.save(opts)
  self.new.save(opts)
end

.script_directoryString

Returns the path that scripts can be loaded from.

Returns:

  • (String)

    path to script directory.


93
94
95
# File 'lib/msf/base/config.rb', line 93

def self.script_directory
  self.new.script_directory
end

.session_log_directoryString

Returns the directory in which session log files are to reside.

Returns:

  • (String)

    path to session log directory.


121
122
123
# File 'lib/msf/base/config.rb', line 121

def self.session_log_directory
  self.new.session_log_directory
end

.user_module_directoryString

Returns the user-specific module base path

Returns:

  • (String)

    path to user-specific modules directory.


142
143
144
# File 'lib/msf/base/config.rb', line 142

def self.user_module_directory
  self.new.user_module_directory
end

.user_plugin_directoryString

Returns the user-specific plugin base path

Returns:

  • (String)

    path to user-specific plugin directory.


114
115
116
# File 'lib/msf/base/config.rb', line 114

def self.user_plugin_directory
  self.new.user_plugin_directory
end

.user_script_directoryString

Returns the user-specific script base path

Returns:

  • (String)

    path to user-specific script directory.


149
150
151
# File 'lib/msf/base/config.rb', line 149

def self.user_script_directory
  self.new.user_script_directory
end

Instance Method Details

#config_directoryString

Returns the configuration directory default.

Returns:

  • (String)

    the root configuration directory.


225
226
227
# File 'lib/msf/base/config.rb', line 225

def config_directory
  self['ConfigDirectory']
end

#config_fileString

Returns the full path to the configuration file.

Returns:

  • (String)

    path to the configuration file.


232
233
234
# File 'lib/msf/base/config.rb', line 232

def config_file
  config_directory + FileSep + self['ConfigFile']
end

#data_directoryString

Returns the data directory

Returns:

  • (String)

    path to data directory.


316
317
318
# File 'lib/msf/base/config.rb', line 316

def data_directory
  install_root + FileSep + self['DataDirectory']
end

#history_fileString

Returns the full path to the history file.

Returns:

  • (String)

    path the history file.


239
240
241
# File 'lib/msf/base/config.rb', line 239

def history_file
  config_directory + FileSep + "history"
end

#initvoid

This method returns an undefined value.

Initializes configuration, creating directories as necessary.


323
324
325
326
327
328
329
330
331
332
# File 'lib/msf/base/config.rb', line 323

def init
  FileUtils.mkdir_p(module_directory)
  FileUtils.mkdir_p(config_directory)
  FileUtils.mkdir_p(log_directory)
  FileUtils.mkdir_p(session_log_directory)
  FileUtils.mkdir_p(loot_directory)
  FileUtils.mkdir_p(local_directory)
  FileUtils.mkdir_p(user_module_directory)
  FileUtils.mkdir_p(user_plugin_directory)
end

#install_rootString

Returns the installation root directory

Returns:

  • (String)

    the installation root directory InstallRoot.


218
219
220
# File 'lib/msf/base/config.rb', line 218

def install_root
  InstallRoot
end

#load(path = nil) ⇒ Rex::Parser::Ini

Loads configuration from the supplied file path, or the default one if none is specified.

Parameters:

  • path (String) (defaults to: nil)

    the path to the configuration file.

Returns:


339
340
341
342
343
# File 'lib/msf/base/config.rb', line 339

def load(path = nil)
  path = config_file if (!path)

  return Rex::Parser::Ini.new(path)
end

#local_directoryString

Returns the directory in which locally-generated data will reside.

Returns:

  • (String)

    path to locally-generated data directory.


288
289
290
# File 'lib/msf/base/config.rb', line 288

def local_directory
  config_directory + FileSep + self['LocalDirectory']
end

#log_directoryString

Returns the directory that log files should be stored in.

Returns:

  • (String)

    path to log directory.


260
261
262
# File 'lib/msf/base/config.rb', line 260

def log_directory
  config_directory + FileSep + self['LogDirectory']
end

#loot_directoryString

Returns the directory in which captured data will reside.

Returns:

  • (String)

    path to loot directory.


281
282
283
# File 'lib/msf/base/config.rb', line 281

def loot_directory
  config_directory + FileSep + self['LootDirectory']
end

#module_directoryString

Returns the global module directory.

Returns:

  • (String)

    path to global module directory.


246
247
248
# File 'lib/msf/base/config.rb', line 246

def module_directory
  install_root + FileSep + self['ModuleDirectory']
end

#plugin_directoryString

Returns the directory that plugins are stored in.

Returns:

  • (String)

    path to plugin directory.


267
268
269
# File 'lib/msf/base/config.rb', line 267

def plugin_directory
  install_root + FileSep + self['PluginDirectory']
end

#save(opts) ⇒ void

This method returns an undefined value.

Saves configuration to the path specified in the ConfigFile hash key or the default path if one isn't specified. The options should be group references that have named value pairs.

Examples:

Save 'Cat' => 'Foo' in group 'ExampleGroup'

save(
  'ExampleGroup' =>
     {
        'Foo' => 'Cat'
     })

Parameters:

  • opts (Hash)

    Hash containing configuration options.

Options Hash (opts):

  • configuration ('ConfigFile'Hash)

    file these options apply to.


359
360
361
362
363
364
365
# File 'lib/msf/base/config.rb', line 359

def save(opts)
  ini = Rex::Parser::Ini.new(opts['ConfigFile'] || config_file)

  ini.update(opts)

  ini.to_file
end

#script_directoryString

Returns the path that scripts can be loaded from.

Returns:

  • (String)

    path to script directory.


253
254
255
# File 'lib/msf/base/config.rb', line 253

def script_directory
  install_root + FileSep + self['ScriptDirectory']
end

#session_log_directoryString

Returns the directory in which session log files are to reside.

Returns:

  • (String)

    path to session log directory.


274
275
276
# File 'lib/msf/base/config.rb', line 274

def session_log_directory
  config_directory + FileSep + self['SessionLogDirectory']
end

#user_module_directoryString

Returns the user-specific module base path

Returns:

  • (String)

    path to user-specific modules directory.


295
296
297
# File 'lib/msf/base/config.rb', line 295

def user_module_directory
  config_directory + FileSep + "modules"
end

#user_plugin_directoryString

Returns the user-specific plugin base path

Returns:

  • (String)

    path to user-specific plugin directory.


302
303
304
# File 'lib/msf/base/config.rb', line 302

def user_plugin_directory
  config_directory + FileSep + "plugins"
end

#user_script_directoryString

Returns the user-specific script base path

Returns:

  • (String)

    path to user-specific script directory.


309
310
311
# File 'lib/msf/base/config.rb', line 309

def user_script_directory
  config_directory + FileSep + "scripts"
end