Skip to content

Commit

Permalink
consolidate the config directory lookups
Browse files Browse the repository at this point in the history
The user configuration directory can be overridden via environment
variables or configuration files.

In the current implementation `Msf::Config.config_directory` should be
utilized for consistent location reporting. `Msf::Config.get_config_root`
is reserved to generation of a default location and should be considered
`private` as it ignores some injected configuration options. Currently
autoloading does not allow application of the `private` keyword to this method,
requiring guidance during development that module writers should access the
full configured `user` value of `Msf::Config.config_directory`.
  • Loading branch information
jmartin-tech committed Jul 25, 2022
1 parent a97f884 commit f779f0f
Show file tree
Hide file tree
Showing 14 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/metasploit/framework/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def self.project_configurations_pathname
# @return [Pathname] if the user has a `database.yml` in their config directory (`~/.msf4` by default).
# @return [nil] if the user does not have a `database.yml` in their config directory.
def self.user_configurations_pathname
Pathname.new(Msf::Config.get_config_root).join('database.yml')
Pathname.new(Msf::Config.config_directory).join('database.yml')
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/msf/base/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Config < Hash
# The installation's root directory for the distribution
InstallRoot = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..'))

# Determines the base configuration directory.
# Determines the base configuration directory. This method should be considered `private`.
#
# @return [String] the base configuration directory
def self.get_config_root
Expand Down
2 changes: 1 addition & 1 deletion lib/msf/core/db_connector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def self.db_disconnect(framework)
# Connect to a database via the supplied yaml file
#
def self.db_connect_yaml(framework, opts)
file = opts[:yaml_file] || ::File.join(Msf::Config.get_config_root, 'database.yml')
file = opts[:yaml_file] || ::File.join(Msf::Config.config_directory, 'database.yml')
file = ::File.expand_path(file)
unless ::File.exist?(file)
return { error: 'File not found' }
Expand Down
2 changes: 1 addition & 1 deletion modules/auxiliary/gather/ldap_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def initialize(info = {})
end

def initialize_actions
user_config_file_path = File.join(::Msf::Config.get_config_root, 'ldap_queries.yaml')
user_config_file_path = File.join(::Msf::Config.config_directory, 'ldap_queries.yaml')
default_config_file_path = File.join(::Msf::Config.data_directory, 'auxiliary', 'gather', 'ldap_query', 'ldap_queries_default.yaml')

@loaded_queries = safe_load_queries(default_config_file_path) || []
Expand Down
4 changes: 2 additions & 2 deletions modules/auxiliary/scanner/telephony/wardial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ def run
raise RuntimeError, "Telephony not available"
end

@confdir = File.join(Msf::Config.get_config_root, 'wardial')
@datadir = File.join(Msf::Config.get_config_root, 'logs', 'wardial')
@confdir = File.join(Msf::Config.config_directory, 'wardial')
@datadir = File.join(Msf::Config.config_directory, 'logs', 'wardial')

# make sure working dirs exist
FileUtils.mkdir_p(@confdir)
Expand Down
2 changes: 1 addition & 1 deletion modules/post/windows/gather/screen_spy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def record?

def run
host = session.session_host
screenshot = Msf::Config.get_config_root + '/logs/' + host + '.jpg'
screenshot = Msf::Config.config_directory + '/logs/' + host + '.jpg'

# If no PID is specified, don't migrate.
if datastore['PID'] != ''
Expand Down
2 changes: 1 addition & 1 deletion msfdb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ require 'msfenv'
@script_name = File.basename(__FILE__)
@framework = File.expand_path(File.dirname(__FILE__))

@localconf = Msf::Config.get_config_root
@localconf = Msf::Config.config_directory
@db = "#{@localconf}/db"
@db_conf = "#{@localconf}/database.yml"
@pg_cluster_conf_root = "#{@localconf}/.local/etc/postgresql"
Expand Down
8 changes: 4 additions & 4 deletions msfrpcd
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ if $PROGRAM_NAME == __FILE__

require 'rex/parser/arguments'

ws_ssl_key_default = File.join(Msf::Config.get_config_root, "#{WS_TAG}-key.pem")
ws_ssl_cert_default = File.join(Msf::Config.get_config_root, "#{WS_TAG}-cert.pem")
ws_log = File.join(Msf::Config.get_config_root, 'logs', "#{WS_RPC_TAG}.log")
ws_rpc_pid = File.join(Msf::Config.get_config_root, "#{WS_RPC_TAG}.pid")
ws_ssl_key_default = File.join(Msf::Config.config_directory, "#{WS_TAG}-key.pem")
ws_ssl_cert_default = File.join(Msf::Config.config_directory, "#{WS_TAG}-cert.pem")
ws_log = File.join(Msf::Config.config_directory, 'logs', "#{WS_RPC_TAG}.log")
ws_rpc_pid = File.join(Msf::Config.config_directory, "#{WS_RPC_TAG}.pid")
ws_ssl_key = ws_ssl_key_default
ws_ssl_cert = ws_ssl_cert_default
ssl_enable_verify = false
Expand Down
2 changes: 1 addition & 1 deletion plugins/aggregator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#

module Msf
Aggregator_yaml = "#{Msf::Config.get_config_root}/aggregator.yaml" # location of the aggregator.yml containing saved aggregator creds
Aggregator_yaml = "#{Msf::Config.config_directory}/aggregator.yaml" # location of the aggregator.yml containing saved aggregator creds

class Plugin::Aggregator < Msf::Plugin
class AggregatorCommandDispatcher
Expand Down
2 changes: 1 addition & 1 deletion plugins/beholder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class BeholderCommandDispatcher
webcam: false,
keystrokes: true,
automigrate: true,
base: ::File.join(Msf::Config.get_config_root, "beholder", Time.now.strftime("%Y-%m-%d.%s")),
base: ::File.join(Msf::Config.config_directory, "beholder", Time.now.strftime("%Y-%m-%d.%s")),
freq: 30,
# TODO: Only capture when the idle threshold has been reached
idle: 0,
Expand Down
4 changes: 2 additions & 2 deletions plugins/capture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ def parse_stop_args(args)
end

def parse_start_args(args)
config_file = File.join(Msf::Config.get_config_root, 'capture_config.yaml')
config_file = File.join(Msf::Config.config_directory, 'capture_config.yaml')
# See if there was a config file set
@start_opt_parser.parse(args) do |opt, _idx, val|
case opt
Expand Down Expand Up @@ -623,7 +623,7 @@ def initialize(framework, opts)
super
add_console_dispatcher(ConsoleCommandDispatcher)
filename = 'capture_config.yaml'
user_config_file = File.join(Msf::Config.get_config_root, filename)
user_config_file = File.join(Msf::Config.config_directory, filename)
unless File.exist?(user_config_file)
# Initialise user config file with the installed one
base_config_file = File.join(Msf::Config.data_directory, filename)
Expand Down
6 changes: 3 additions & 3 deletions plugins/nessus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ def name
end

def xindex
"#{Msf::Config.get_config_root}/nessus_index"
"#{Msf::Config.config_directory}/nessus_index"
end

def nessus_yaml
"#{Msf::Config.get_config_root}/nessus.yaml"
"#{Msf::Config.config_directory}/nessus.yaml"
end

def msf_local
Expand Down Expand Up @@ -102,7 +102,7 @@ def create_xindex
start = Time.now
print_status("Creating Exploit Search Index - (#{xindex}) - this won't take long.")
count = 0
#Use Msf::Config.get_config_root as the location.
#Use Msf::Config.config_directory as the location.
File.open("#{xindex}", "w+") do |f|
#need to add version line.
f.puts(Msf::Framework::Version)
Expand Down
2 changes: 1 addition & 1 deletion plugins/nexpose.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
require 'nexpose'

module Msf
Nexpose_yaml = "#{Msf::Config.get_config_root}/nexpose.yaml" #location of the nexpose.yml containing saved nexpose creds
Nexpose_yaml = "#{Msf::Config.config_directory}/nexpose.yaml" #location of the nexpose.yml containing saved nexpose creds

class Plugin::Nexpose < Msf::Plugin
class NexposeCommandDispatcher
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/metasploit/framework/database_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@
end

before(:example) do
allow(Msf::Config).to receive(:get_config_root).and_return(config_root)
allow(Msf::Config).to receive(:config_directory).and_return(config_root)
end

it 'is database.yml under the user config root' do
Expand Down

0 comments on commit f779f0f

Please sign in to comment.