2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2005-07-15 22:30:04 +00:00
|
|
|
require 'fileutils'
|
|
|
|
|
|
|
|
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 root directory for the distribution
|
|
|
|
#
|
|
|
|
InstallRoot = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..'))
|
|
|
|
|
2005-11-04 00:55:46 +00:00
|
|
|
#
|
|
|
|
# Determines the base configuration directory.
|
|
|
|
#
|
|
|
|
def self.get_config_root
|
2011-06-25 01:40:16 +00:00
|
|
|
|
2011-10-31 16:53:45 +00:00
|
|
|
# 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
|
|
|
|
|
2007-12-29 23:05:15 +00:00
|
|
|
# Windows-specific environment variables
|
2009-06-21 14:16:39 +00:00
|
|
|
['HOME', 'LOCALAPPDATA', 'APPDATA', 'USERPROFILE'].each do |dir|
|
2008-11-10 21:44:58 +00:00
|
|
|
val = Rex::Compat.getenv(dir)
|
|
|
|
if (val and File.directory?(val))
|
2011-06-25 01:40:16 +00:00
|
|
|
return File.join(val, ".msf#{Msf::Framework::Major}")
|
2007-12-29 23:05:15 +00:00
|
|
|
end
|
|
|
|
end
|
2011-06-25 01:40:16 +00:00
|
|
|
|
2005-11-04 00:55:46 +00:00
|
|
|
begin
|
2011-06-25 01:40:16 +00:00
|
|
|
# First we try $HOME/.msfx
|
|
|
|
File.expand_path("~#{FileSep}.msf#{Msf::Framework::Major}")
|
2006-10-29 17:41:19 +00:00
|
|
|
rescue ::ArgumentError
|
2011-06-25 01:40:16 +00:00
|
|
|
# Give up and install root + ".msfx"
|
|
|
|
InstallRoot + ".msf#{Msf::Framework::Major}"
|
2005-11-04 00:55:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-07-15 22:30:04 +00:00
|
|
|
#
|
|
|
|
# Default values
|
|
|
|
#
|
|
|
|
FileSep = File::SEPARATOR
|
|
|
|
Defaults =
|
|
|
|
{
|
2005-11-04 00:55:46 +00:00
|
|
|
'ConfigDirectory' => get_config_root,
|
2005-10-01 06:35:16 +00:00
|
|
|
'ConfigFile' => "config",
|
2005-10-01 09:10:41 +00:00
|
|
|
'ModuleDirectory' => "modules",
|
2006-09-19 03:15:25 +00:00
|
|
|
'ScriptDirectory' => "scripts",
|
2005-10-01 06:35:16 +00:00
|
|
|
'LogDirectory' => "logs",
|
|
|
|
'SessionLogDirectory' => "logs/sessions",
|
2005-11-24 03:31:23 +00:00
|
|
|
'PluginDirectory' => "plugins",
|
2010-12-10 05:47:33 +00:00
|
|
|
'DataDirectory' => "data",
|
2011-10-27 17:01:51 +00:00
|
|
|
'LootDirectory' => "loot",
|
|
|
|
'LocalDirectory' => "local"
|
2005-07-15 22:30:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Class methods
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# Returns the framework installation root.
|
|
|
|
#
|
2005-07-15 22:30:04 +00:00
|
|
|
def self.install_root
|
|
|
|
InstallRoot
|
|
|
|
end
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# Calls the instance method.
|
|
|
|
#
|
2005-07-15 22:30:04 +00:00
|
|
|
def self.config_directory
|
|
|
|
self.new.config_directory
|
|
|
|
end
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# Calls the instance method.
|
|
|
|
#
|
2005-10-01 09:10:41 +00:00
|
|
|
def self.module_directory
|
|
|
|
self.new.module_directory
|
|
|
|
end
|
2013-03-08 00:20:08 +00:00
|
|
|
|
2006-09-19 03:15:25 +00:00
|
|
|
#
|
|
|
|
# Calls the instance method.
|
|
|
|
#
|
|
|
|
def self.script_directory
|
|
|
|
self.new.script_directory
|
|
|
|
end
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# Calls the instance method.
|
|
|
|
#
|
2005-07-15 22:30:04 +00:00
|
|
|
def self.log_directory
|
|
|
|
self.new.log_directory
|
|
|
|
end
|
|
|
|
|
2005-11-19 16:25:26 +00:00
|
|
|
#
|
|
|
|
# Calls the instance method.
|
|
|
|
#
|
|
|
|
def self.plugin_directory
|
|
|
|
self.new.plugin_directory
|
|
|
|
end
|
|
|
|
|
2009-08-01 14:07:05 +00:00
|
|
|
#
|
|
|
|
# Calls the instance method.
|
|
|
|
#
|
|
|
|
def self.user_plugin_directory
|
|
|
|
self.new.user_plugin_directory
|
|
|
|
end
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# Calls the instance method.
|
|
|
|
#
|
2005-10-01 06:35:16 +00:00
|
|
|
def self.session_log_directory
|
|
|
|
self.new.session_log_directory
|
|
|
|
end
|
2010-12-10 05:47:33 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Calls the instance method.
|
|
|
|
#
|
|
|
|
def self.loot_directory
|
|
|
|
self.new.loot_directory
|
|
|
|
end
|
2011-06-25 01:40:16 +00:00
|
|
|
|
2011-10-27 17:01:51 +00:00
|
|
|
#
|
|
|
|
# Calls the instance method.
|
|
|
|
#
|
|
|
|
def self.local_directory
|
|
|
|
self.new.local_directory
|
|
|
|
end
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# Calls the instance method.
|
|
|
|
#
|
2005-10-01 06:35:16 +00:00
|
|
|
def self.user_module_directory
|
|
|
|
self.new.user_module_directory
|
|
|
|
end
|
2013-03-08 00:20:08 +00:00
|
|
|
|
2006-09-19 03:15:25 +00:00
|
|
|
#
|
|
|
|
# Calls the instance method.
|
|
|
|
#
|
|
|
|
def self.user_script_directory
|
|
|
|
self.new.user_script_directory
|
|
|
|
end
|
2011-06-25 01:40:16 +00:00
|
|
|
|
2005-11-24 03:31:23 +00:00
|
|
|
#
|
|
|
|
# Calls the instance method.
|
|
|
|
#
|
|
|
|
def self.data_directory
|
|
|
|
self.new.data_directory
|
|
|
|
end
|
2005-10-01 06:35:16 +00:00
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# Calls the instance method.
|
|
|
|
#
|
2005-07-15 22:30:04 +00:00
|
|
|
def self.config_file
|
|
|
|
self.new.config_file
|
|
|
|
end
|
|
|
|
|
2009-11-26 01:01:27 +00:00
|
|
|
#
|
|
|
|
# Calls the instance method.
|
|
|
|
#
|
|
|
|
def self.history_file
|
|
|
|
self.new.history_file
|
|
|
|
end
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# Calls the instance method.
|
|
|
|
#
|
2005-07-15 22:30:04 +00:00
|
|
|
def self.init
|
|
|
|
self.new.init
|
|
|
|
end
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# Calls the instance method.
|
|
|
|
#
|
2005-07-15 22:30:04 +00:00
|
|
|
def self.load(path = nil)
|
|
|
|
self.new.load(path)
|
|
|
|
end
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# Calls the instance method.
|
|
|
|
#
|
2005-07-15 22:30:04 +00:00
|
|
|
def self.save(opts)
|
|
|
|
self.new.save(opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Updates the config class' self with the default hash.
|
|
|
|
#
|
|
|
|
def initialize
|
|
|
|
update(Defaults)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the installation root directory
|
|
|
|
#
|
|
|
|
def install_root
|
|
|
|
InstallRoot
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the configuration directory default.
|
|
|
|
#
|
|
|
|
def config_directory
|
|
|
|
self['ConfigDirectory']
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the full path to the configuration file.
|
|
|
|
#
|
|
|
|
def config_file
|
|
|
|
config_directory + FileSep + self['ConfigFile']
|
|
|
|
end
|
|
|
|
|
2009-11-26 01:01:27 +00:00
|
|
|
#
|
|
|
|
# Returns the full path to the configuration file.
|
|
|
|
#
|
|
|
|
def history_file
|
|
|
|
config_directory + FileSep + "history"
|
|
|
|
end
|
|
|
|
|
2005-10-01 09:10:41 +00:00
|
|
|
#
|
|
|
|
# Returns the global module directory.
|
|
|
|
#
|
|
|
|
def module_directory
|
|
|
|
install_root + FileSep + self['ModuleDirectory']
|
|
|
|
end
|
2013-03-08 00:20:08 +00:00
|
|
|
|
2006-09-19 03:15:25 +00:00
|
|
|
#
|
|
|
|
# Returns the path that scripts can be loaded from.
|
|
|
|
#
|
|
|
|
def script_directory
|
|
|
|
install_root + FileSep + self['ScriptDirectory']
|
|
|
|
end
|
|
|
|
|
2005-07-15 22:30:04 +00:00
|
|
|
#
|
|
|
|
# Returns the directory that log files should be stored in.
|
|
|
|
#
|
|
|
|
def log_directory
|
2005-10-01 06:35:16 +00:00
|
|
|
config_directory + FileSep + self['LogDirectory']
|
|
|
|
end
|
|
|
|
|
2005-11-19 16:25:26 +00:00
|
|
|
#
|
|
|
|
# Returns the directory that plugins are stored in.
|
|
|
|
#
|
|
|
|
def plugin_directory
|
|
|
|
install_root + FileSep + self['PluginDirectory']
|
|
|
|
end
|
|
|
|
|
2005-10-01 06:35:16 +00:00
|
|
|
#
|
|
|
|
# Returns the directory in which session log files are to reside.
|
|
|
|
#
|
|
|
|
def session_log_directory
|
|
|
|
config_directory + FileSep + self['SessionLogDirectory']
|
|
|
|
end
|
2011-06-25 01:40:16 +00:00
|
|
|
|
2010-12-10 05:47:33 +00:00
|
|
|
#
|
|
|
|
# Returns the directory in which captured data will reside.
|
|
|
|
#
|
|
|
|
def loot_directory
|
|
|
|
config_directory + FileSep + self['LootDirectory']
|
|
|
|
end
|
2011-06-25 01:40:16 +00:00
|
|
|
|
2011-10-27 17:01:51 +00:00
|
|
|
#
|
|
|
|
# Returns the directory in which locally-generated data will reside.
|
|
|
|
#
|
|
|
|
def local_directory
|
|
|
|
config_directory + FileSep + self['LocalDirectory']
|
|
|
|
end
|
|
|
|
|
2005-10-01 06:35:16 +00:00
|
|
|
#
|
|
|
|
# Returns the user-specific module base path
|
|
|
|
#
|
|
|
|
def user_module_directory
|
|
|
|
config_directory + FileSep + "modules"
|
2005-07-15 22:30:04 +00:00
|
|
|
end
|
2013-03-08 00:20:08 +00:00
|
|
|
|
2009-08-01 14:07:05 +00:00
|
|
|
#
|
|
|
|
# Returns the user-specific plugin base path
|
|
|
|
#
|
|
|
|
def user_plugin_directory
|
|
|
|
config_directory + FileSep + "plugins"
|
|
|
|
end
|
|
|
|
|
2006-09-19 03:15:25 +00:00
|
|
|
#
|
|
|
|
# Returns the user-specific script base path
|
|
|
|
#
|
|
|
|
def user_script_directory
|
|
|
|
config_directory + FileSep + "scripts"
|
|
|
|
end
|
|
|
|
|
2005-11-24 03:31:23 +00:00
|
|
|
#
|
|
|
|
# Returns the data directory
|
|
|
|
#
|
|
|
|
def data_directory
|
|
|
|
install_root + FileSep + self['DataDirectory']
|
|
|
|
end
|
|
|
|
|
2005-07-15 22:30:04 +00:00
|
|
|
#
|
|
|
|
# Initializes configuration, creating directories as necessary.
|
|
|
|
#
|
|
|
|
def init
|
2011-11-21 21:58:19 +00:00
|
|
|
FileUtils.mkdir_p(module_directory)
|
2005-07-15 22:30:04 +00:00
|
|
|
FileUtils.mkdir_p(config_directory)
|
|
|
|
FileUtils.mkdir_p(log_directory)
|
2005-10-01 06:35:16 +00:00
|
|
|
FileUtils.mkdir_p(session_log_directory)
|
2011-06-25 01:40:16 +00:00
|
|
|
FileUtils.mkdir_p(loot_directory)
|
2011-10-27 17:01:51 +00:00
|
|
|
FileUtils.mkdir_p(local_directory)
|
2005-10-01 06:35:16 +00:00
|
|
|
FileUtils.mkdir_p(user_module_directory)
|
2009-08-01 14:07:05 +00:00
|
|
|
FileUtils.mkdir_p(user_plugin_directory)
|
2005-07-15 22:30:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Loads configuration from the supplied file path, or the default one if
|
|
|
|
# none is specified.
|
|
|
|
#
|
|
|
|
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
|
|
|
|
# the default path is one isn't specified. The options should be group
|
|
|
|
# references that have named value pairs. Example:
|
|
|
|
#
|
|
|
|
# save(
|
|
|
|
# 'ExampleGroup' =>
|
|
|
|
# {
|
|
|
|
# 'Foo' => 'Cat'
|
|
|
|
# })
|
|
|
|
#
|
|
|
|
def save(opts)
|
|
|
|
ini = Rex::Parser::Ini.new(opts['ConfigFile'] || config_file)
|
|
|
|
|
|
|
|
ini.update(opts)
|
|
|
|
|
|
|
|
ini.to_file
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2008-11-10 21:44:58 +00:00
|
|
|
end
|
2011-06-25 01:40:16 +00:00
|
|
|
|