2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2014-05-12 15:16:10 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Standard Library
|
|
|
|
#
|
|
|
|
|
2005-07-15 22:30:04 +00:00
|
|
|
require 'fileutils'
|
|
|
|
|
2014-05-12 15:16:10 +00:00
|
|
|
#
|
|
|
|
# Project
|
|
|
|
#
|
|
|
|
|
2014-08-04 23:23:25 +00:00
|
|
|
require 'metasploit/framework/version'
|
2014-05-12 15:16:10 +00:00
|
|
|
require 'rex/compat'
|
|
|
|
|
2005-07-15 22:30:04 +00:00
|
|
|
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
|
|
|
|
|
2013-12-24 09:23:19 +00:00
|
|
|
# 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
|
|
|
#
|
2013-12-24 09:23:19 +00:00
|
|
|
# @return [String] the base configuration directory
|
2013-08-30 21:28:33 +00:00
|
|
|
def self.get_config_root
|
|
|
|
|
2016-03-23 21:23:30 +00:00
|
|
|
# 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))
|
2014-07-29 19:04:15 +00:00
|
|
|
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
|
2014-07-29 19:04:15 +00:00
|
|
|
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"
|
2014-07-29 19:04:15 +00:00
|
|
|
InstallRoot + ".msf#{Metasploit::Framework::Version::MAJOR}"
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Default values
|
|
|
|
#
|
2013-12-24 09:23:19 +00:00
|
|
|
|
|
|
|
# Default system file separator.
|
2013-08-30 21:28:33 +00:00
|
|
|
FileSep = File::SEPARATOR
|
2013-12-24 09:23:19 +00:00
|
|
|
|
|
|
|
# Default configuration locations.
|
2013-08-30 21:28:33 +00:00
|
|
|
Defaults =
|
|
|
|
{
|
|
|
|
'ConfigDirectory' => get_config_root,
|
|
|
|
'ConfigFile' => "config",
|
|
|
|
'ModuleDirectory' => "modules",
|
|
|
|
'ScriptDirectory' => "scripts",
|
|
|
|
'LogDirectory' => "logs",
|
2014-10-24 14:52:05 +00:00
|
|
|
'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
|
|
|
#
|
2013-12-24 09:23:19 +00:00
|
|
|
# @return [String] the framework installation root {InstallRoot}.
|
2013-08-30 21:28:33 +00:00
|
|
|
def self.install_root
|
|
|
|
InstallRoot
|
|
|
|
end
|
|
|
|
|
2013-12-24 09:23:19 +00:00
|
|
|
# Returns the configuration directory default.
|
2013-12-26 18:14:41 +00:00
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
|
|
2014-10-24 14:52:05 +00:00
|
|
|
# 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
|
|
|
|
|
2013-12-24 09:23:19 +00:00
|
|
|
# Returns the global module directory.
|
2013-12-26 18:14:41 +00:00
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
|
|
2013-12-24 09:23:19 +00:00
|
|
|
# Returns the path that scripts can be loaded from.
|
2013-12-26 18:14:41 +00:00
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
|
|
2013-12-24 09:23:19 +00:00
|
|
|
# Returns the directory that log files should be stored in.
|
2013-12-26 18:14:41 +00:00
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
|
|
2013-12-24 09:23:19 +00:00
|
|
|
# Returns the directory that plugins are stored in.
|
2013-12-26 18:14:41 +00:00
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
|
|
2013-12-24 09:23:19 +00:00
|
|
|
# Returns the user-specific plugin base path
|
2013-12-26 18:14:41 +00:00
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
|
|
2013-12-24 09:23:19 +00:00
|
|
|
# Returns the directory in which session log files are to reside.
|
2013-12-26 18:14:41 +00:00
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
|
|
2013-12-24 09:23:19 +00:00
|
|
|
# Returns the directory in which captured data will reside.
|
2013-12-26 18:14:41 +00:00
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
|
|
2013-12-24 09:23:19 +00:00
|
|
|
# Returns the directory in which locally-generated data will reside.
|
2013-12-26 18:14:41 +00:00
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
|
|
2014-10-24 14:52:05 +00:00
|
|
|
# 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
|
|
|
|
|
2013-12-24 09:23:19 +00:00
|
|
|
# Returns the user-specific module base path
|
2013-12-26 18:14:41 +00:00
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
|
|
2013-12-24 09:23:19 +00:00
|
|
|
# Returns the user-specific script base path
|
2013-12-26 18:14:41 +00:00
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
|
|
2013-12-24 09:23:19 +00:00
|
|
|
# Returns the data directory
|
2013-12-26 18:14:41 +00:00
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
|
|
2013-12-24 09:23:19 +00:00
|
|
|
# Returns the full path to the configuration file.
|
2013-12-26 18:14:41 +00:00
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
|
|
2013-12-24 09:23:19 +00:00
|
|
|
# Returns the full path to the history file.
|
2013-12-26 18:14:41 +00:00
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
|
|
2013-12-24 09:23:19 +00:00
|
|
|
# Initializes configuration, creating directories as necessary.
|
2013-12-26 18:14:41 +00:00
|
|
|
#
|
2013-12-24 09:23:19 +00:00
|
|
|
# @return [void]
|
2013-08-30 21:28:33 +00:00
|
|
|
def self.init
|
|
|
|
self.new.init
|
|
|
|
end
|
|
|
|
|
2013-12-24 09:23:19 +00:00
|
|
|
# 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.
|
2013-12-24 09:23:19 +00:00
|
|
|
# @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
|
|
|
|
|
2013-12-24 09:23:19 +00:00
|
|
|
# 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
|
2013-12-24 09:23:19 +00:00
|
|
|
# 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.
|
2013-12-24 09:23:19 +00:00
|
|
|
# @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
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
#
|
2013-12-24 09:23:19 +00:00
|
|
|
# @return [String] the installation root directory {InstallRoot}.
|
2013-08-30 21:28:33 +00:00
|
|
|
def install_root
|
|
|
|
InstallRoot
|
|
|
|
end
|
|
|
|
|
2014-10-24 14:52:05 +00:00
|
|
|
# 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
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
|
|
2013-12-24 09:23:19 +00:00
|
|
|
# Returns the full path to the history file.
|
2013-12-26 18:14:41 +00:00
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
|
|
2014-10-24 14:52:05 +00:00
|
|
|
# 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
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
#
|
2013-12-24 09:23:19 +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
|
|
|
#
|
2013-12-24 09:23:19 +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)
|
2014-10-24 14:52:05 +00:00
|
|
|
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.
|
2013-12-24 09:23:19 +00:00
|
|
|
# @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
|
2013-12-24 09:23:19 +00:00
|
|
|
# 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.
|
2013-12-24 09:23:19 +00:00
|
|
|
# @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
|
2005-07-15 22:30:04 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2008-11-10 21:44:58 +00:00
|
|
|
end
|
2011-06-25 01:40:16 +00:00
|
|
|
|