metasploit-framework/lib/msf/base/config.rb

413 lines
10 KiB
Ruby
Raw Normal View History

# -*- coding: binary -*-
#
# Standard Library
#
require 'fileutils'
#
# Project
#
require 'metasploit/framework/version'
require 'rex/compat'
module Msf
# This class wraps interaction with global configuration that can be used as a
# persistent storage point for configuration, logs, and other such fun things.
class Config < Hash
# The installation's root directory for the distribution
2013-08-30 21:28:33 +00:00
InstallRoot = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..'))
# Determines the base configuration directory.
2013-12-26 18:14:41 +00:00
#
# @return [String] the base configuration directory
2013-08-30 21:28:33 +00:00
def self.get_config_root
# Use MSF_CFGROOT_CONFIG environment variable first.
2013-08-30 21:28:33 +00:00
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#{Metasploit::Framework::Version::MAJOR}")
2013-08-30 21:28:33 +00:00
end
end
begin
# First we try $HOME/.msfx
File.expand_path("~#{FileSep}.msf#{Metasploit::Framework::Version::MAJOR}")
2013-08-30 21:28:33 +00:00
rescue ::ArgumentError
# Give up and install root + ".msfx"
InstallRoot + ".msf#{Metasploit::Framework::Version::MAJOR}"
2013-08-30 21:28:33 +00:00
end
end
#
# Default values
#
# Default system file separator.
2013-08-30 21:28:33 +00:00
FileSep = File::SEPARATOR
# Default configuration locations.
2013-08-30 21:28:33 +00:00
Defaults =
{
'ConfigDirectory' => get_config_root,
'ConfigFile' => "config",
'ModuleDirectory' => "modules",
'ScriptDirectory' => "scripts",
'LogDirectory' => "logs",
'LogosDirectory' => "logos",
2013-08-30 21:28:33 +00:00
'SessionLogDirectory' => "logs/sessions",
'PluginDirectory' => "plugins",
'DataDirectory' => "data",
'LootDirectory' => "loot",
'LocalDirectory' => "local"
}
##
#
# Class methods
#
##
# Returns the framework installation root.
2013-12-26 18:14:41 +00:00
#
# @return [String] the framework installation root {InstallRoot}.
2013-08-30 21:28:33 +00:00
def self.install_root
InstallRoot
end
# Returns the configuration directory default.
2013-12-26 18:14:41 +00:00
#
# @return [String] the root configuration directory.
2013-08-30 21:28:33 +00:00
def self.config_directory
self.new.config_directory
end
# Return the directory that logo files should be loaded from.
#
# @return [String] path to the logos directory.
def self.logos_directory
self.new.logos_directory
end
# Returns the global module directory.
2013-12-26 18:14:41 +00:00
#
# @return [String] path to global module directory.
2013-08-30 21:28:33 +00:00
def self.module_directory
self.new.module_directory
end
# Returns the path that scripts can be loaded from.
2013-12-26 18:14:41 +00:00
#
# @return [String] path to script directory.
2013-08-30 21:28:33 +00:00
def self.script_directory
self.new.script_directory
end
# Returns the directory that log files should be stored in.
2013-12-26 18:14:41 +00:00
#
# @return [String] path to log directory.
2013-08-30 21:28:33 +00:00
def self.log_directory
self.new.log_directory
end
# Returns the directory that plugins are stored in.
2013-12-26 18:14:41 +00:00
#
# @return [String] path to plugin directory.
2013-08-30 21:28:33 +00:00
def self.plugin_directory
self.new.plugin_directory
end
# Returns the user-specific plugin base path
2013-12-26 18:14:41 +00:00
#
# @return [String] path to user-specific plugin directory.
2013-08-30 21:28:33 +00:00
def self.user_plugin_directory
self.new.user_plugin_directory
end
# Returns the directory in which session log files are to reside.
2013-12-26 18:14:41 +00:00
#
# @return [String] path to session log directory.
2013-08-30 21:28:33 +00:00
def self.session_log_directory
self.new.session_log_directory
end
# Returns the directory in which captured data will reside.
2013-12-26 18:14:41 +00:00
#
# @return [String] path to loot directory.
2013-08-30 21:28:33 +00:00
def self.loot_directory
self.new.loot_directory
end
# Returns the directory in which locally-generated data will reside.
2013-12-26 18:14:41 +00:00
#
# @return [String] path to locally-generated data directory.
2013-08-30 21:28:33 +00:00
def self.local_directory
self.new.local_directory
end
# Return the user-specific directory that logo files should be loaded from.
#
# @return [String] path to the logos directory.
def self.user_logos_directory
self.new.user_logos_directory
end
# Returns the user-specific module base path
2013-12-26 18:14:41 +00:00
#
# @return [String] path to user-specific modules directory.
2013-08-30 21:28:33 +00:00
def self.user_module_directory
self.new.user_module_directory
end
# Returns the user-specific script base path
2013-12-26 18:14:41 +00:00
#
# @return [String] path to user-specific script directory.
2013-08-30 21:28:33 +00:00
def self.user_script_directory
self.new.user_script_directory
end
# Returns the data directory
2013-12-26 18:14:41 +00:00
#
# @return [String] path to data directory.
2013-08-30 21:28:33 +00:00
def self.data_directory
self.new.data_directory
end
# Returns the full path to the configuration file.
2013-12-26 18:14:41 +00:00
#
# @return [String] path to the configuration file.
2013-08-30 21:28:33 +00:00
def self.config_file
self.new.config_file
end
# Returns the full path to the history file.
2013-12-26 18:14:41 +00:00
#
# @return [String] path the history file.
2013-08-30 21:28:33 +00:00
def self.history_file
self.new.history_file
end
# Initializes configuration, creating directories as necessary.
2013-12-26 18:14:41 +00:00
#
# @return [void]
2013-08-30 21:28:33 +00:00
def self.init
self.new.init
end
# Loads configuration from the supplied file path, or the default one if
# none is specified.
2013-12-26 18:14:41 +00:00
#
# @param path [String] the path to the configuration file.
# @return [Rex::Parser::Ini] INI file parser.
2013-08-30 21:28:33 +00:00
def self.load(path = nil)
self.new.load(path)
end
# Saves configuration to the path specified in the ConfigFile hash key or
2014-01-01 00:02:18 +00:00
# the default path if one isn't specified. The options should be group
# references that have named value pairs.
2013-12-26 18:14:41 +00:00
#
# @param opts [Hash] Hash containing configuration options.
# @option opts 'ConfigFile' [Hash] configuration file these options apply
# to.
# @return [void]
# @example Save 'Cat' => 'Foo' in group 'ExampleGroup'
# save(
# 'ExampleGroup' =>
# {
# 'Foo' => 'Cat'
# })
2013-08-30 21:28:33 +00:00
def self.save(opts)
self.new.save(opts)
end
# Updates the config class' self with the default hash.
2013-12-26 18:14:41 +00:00
#
# @return [Hash] the updated Hash.
2013-08-30 21:28:33 +00:00
def initialize
update(Defaults)
end
# Returns the installation root directory
2013-12-26 18:14:41 +00:00
#
# @return [String] the installation root directory {InstallRoot}.
2013-08-30 21:28:33 +00:00
def install_root
InstallRoot
end
# Return the directory that logo files should be loaded from.
#
# @return [String] path to the logos directory.
def logos_directory
data_directory + FileSep + self['LogosDirectory']
end
2013-08-30 21:28:33 +00:00
# Returns the configuration directory default.
2013-12-26 18:14:41 +00:00
#
# @return [String] the root configuration directory.
2013-08-30 21:28:33 +00:00
def config_directory
self['ConfigDirectory']
end
# Returns the full path to the configuration file.
2013-12-26 18:14:41 +00:00
#
# @return [String] path to the configuration file.
2013-08-30 21:28:33 +00:00
def config_file
config_directory + FileSep + self['ConfigFile']
end
# Returns the full path to the history file.
2013-12-26 18:14:41 +00:00
#
# @return [String] path the history file.
2013-08-30 21:28:33 +00:00
def history_file
config_directory + FileSep + "history"
end
# Returns the global module directory.
2013-12-26 18:14:41 +00:00
#
# @return [String] path to global module directory.
2013-08-30 21:28:33 +00:00
def module_directory
install_root + FileSep + self['ModuleDirectory']
end
# Returns the path that scripts can be loaded from.
2013-12-26 18:14:41 +00:00
#
# @return [String] path to script directory.
2013-08-30 21:28:33 +00:00
def script_directory
install_root + FileSep + self['ScriptDirectory']
end
# Returns the directory that log files should be stored in.
2013-12-26 18:14:41 +00:00
#
# @return [String] path to log directory.
2013-08-30 21:28:33 +00:00
def log_directory
config_directory + FileSep + self['LogDirectory']
end
# Returns the directory that plugins are stored in.
2013-12-26 18:14:41 +00:00
#
# @return [String] path to plugin directory.
2013-08-30 21:28:33 +00:00
def plugin_directory
install_root + FileSep + self['PluginDirectory']
end
# Returns the directory in which session log files are to reside.
2013-12-26 18:14:41 +00:00
#
# @return [String] path to session log directory.
2013-08-30 21:28:33 +00:00
def session_log_directory
config_directory + FileSep + self['SessionLogDirectory']
end
# Returns the directory in which captured data will reside.
2013-12-26 18:14:41 +00:00
#
# @return [String] path to loot directory.
2013-08-30 21:28:33 +00:00
def loot_directory
config_directory + FileSep + self['LootDirectory']
end
# Returns the directory in which locally-generated data will reside.
2013-12-26 18:14:41 +00:00
#
# @return [String] path to locally-generated data directory.
2013-08-30 21:28:33 +00:00
def local_directory
config_directory + FileSep + self['LocalDirectory']
end
# Return the user-specific directory that logo files should be loaded from.
#
# @return [String] path to the logos directory.
def user_logos_directory
config_directory + FileSep + self['LogosDirectory']
end
2013-08-30 21:28:33 +00:00
# Returns the user-specific module base path
2013-12-26 18:14:41 +00:00
#
# @return [String] path to user-specific modules directory.
2013-08-30 21:28:33 +00:00
def user_module_directory
config_directory + FileSep + "modules"
end
# Returns the user-specific plugin base path
2013-12-26 18:14:41 +00:00
#
# @return [String] path to user-specific plugin directory.
2013-08-30 21:28:33 +00:00
def user_plugin_directory
config_directory + FileSep + "plugins"
end
# Returns the user-specific script base path
2013-12-26 18:14:41 +00:00
#
# @return [String] path to user-specific script directory.
2013-08-30 21:28:33 +00:00
def user_script_directory
config_directory + FileSep + "scripts"
end
# Returns the data directory
2013-12-26 18:14:41 +00:00
#
# @return [String] path to data directory.
2013-08-30 21:28:33 +00:00
def data_directory
install_root + FileSep + self['DataDirectory']
end
# Initializes configuration, creating directories as necessary.
2013-12-26 18:14:41 +00:00
#
# @return [void]
2013-08-30 21:28:33 +00:00
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_logos_directory)
2013-08-30 21:28:33 +00:00
FileUtils.mkdir_p(user_module_directory)
FileUtils.mkdir_p(user_plugin_directory)
end
# Loads configuration from the supplied file path, or the default one if
# none is specified.
2013-12-26 18:14:41 +00:00
#
# @param path [String] the path to the configuration file.
# @return [Rex::Parser::Ini] INI file parser.
2013-08-30 21:28:33 +00:00
def load(path = nil)
path = config_file if (!path)
return Rex::Parser::Ini.new(path)
end
# Saves configuration to the path specified in the ConfigFile hash key or
2014-01-01 00:02:18 +00:00
# the default path if one isn't specified. The options should be group
# references that have named value pairs.
2013-12-26 18:14:41 +00:00
#
# @param opts [Hash] Hash containing configuration options.
# @option opts 'ConfigFile' [Hash] configuration file these options apply
# to.
# @return [void]
# @example Save 'Cat' => 'Foo' in group 'ExampleGroup'
# save(
# 'ExampleGroup' =>
# {
# 'Foo' => 'Cat'
# })
2013-08-30 21:28:33 +00:00
def save(opts)
ini = Rex::Parser::Ini.new(opts['ConfigFile'] || config_file)
ini.update(opts)
ini.to_file
end
end
end