Skip to content

Commit f696a5a

Browse files
committed
msfconsole --defer-module-loads
MSP-11671 Add command line option --defer-module-loads to msfconsole. It will stop `Msf::Ui::Console::Driver` from calling `framework.modules.init_module_paths` AND `framework.modules.refresh_cache_from_database`. This flag is only meant to speed up msfconsole boot when modules do not need to accessed, such as during cucumber testing of command help or command line options.
1 parent 35ff82c commit f696a5a

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

lib/metasploit/framework/command/console.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def driver_options
6161
driver_options['DatabaseEnv'] = options.environment
6262
driver_options['DatabaseMigrationPaths'] = options.database.migrations_paths
6363
driver_options['DatabaseYAML'] = options.database.config
64+
driver_options['DeferModuleLoads'] = options.modules.defer_loads
6465
driver_options['Defanged'] = options.console.defanged
6566
driver_options['DisableBanner'] = options.console.quiet
6667
driver_options['DisableDatabase'] = options.database.disable

lib/metasploit/framework/parsed_options/base.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def options
8585
options.framework.config = nil
8686

8787
options.modules = ActiveSupport::OrderedOptions.new
88+
options.modules.defer_loads = false
8889
options.modules.path = nil
8990

9091
@options = options
@@ -155,6 +156,13 @@ def option_parser
155156
option_parser.separator ''
156157
option_parser.separator 'Module options'
157158

159+
option_parser.on(
160+
'--defer-module-loads',
161+
'Defer module loading unless explicitly asked.'
162+
) do
163+
options.modules.defer_loads = true
164+
end
165+
158166
option_parser.on(
159167
'-m',
160168
'--module-path DIRECTORY',

lib/msf/ui/console/driver.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def initialize(prompt = DefaultPrompt, prompt_char = DefaultPromptChar, opts = {
6161
# Initialize attributes
6262

6363
# Defer loading of modules until paths from opts can be added below
64-
framework_create_options = {'DeferModuleLoads' => true}.merge(opts)
64+
framework_create_options = opts.merge('DeferModuleLoads' => true)
6565
self.framework = opts['Framework'] || Msf::Simple::Framework.create(framework_create_options)
6666

6767
if self.framework.datastore['Prompt']
@@ -187,10 +187,12 @@ def initialize(prompt = DefaultPrompt, prompt_char = DefaultPromptChar, opts = {
187187
# framework.db.active will be true if after_establish_connection ran directly when connection_established? was
188188
# already true or if framework.db.connect called after_establish_connection.
189189
if framework.db.active
190-
self.framework.modules.refresh_cache_from_database
190+
unless opts['DeferModuleLoads']
191+
self.framework.modules.refresh_cache_from_database
191192

192-
if self.framework.modules.cache_empty?
193-
print_status("The initial module cache will be built in the background, this can take 2-5 minutes...")
193+
if self.framework.modules.cache_empty?
194+
print_status("The initial module cache will be built in the background, this can take 2-5 minutes...")
195+
end
194196
end
195197
elsif !framework.db.error.nil?
196198
if framework.db.error.to_s =~ /RubyGem version.*pg.*0\.11/i
@@ -212,8 +214,8 @@ def initialize(prompt = DefaultPrompt, prompt_char = DefaultPromptChar, opts = {
212214
end
213215
end
214216

215-
# Initialize the module paths only if we didn't get passed a Framework instance
216-
unless opts['Framework']
217+
# Initialize the module paths only if we didn't get passed a Framework instance and 'DeferModuleLoads' is false
218+
unless opts['Framework'] || opts['DeferModuleLoads']
217219
# Configure the framework module paths
218220
self.framework.init_module_paths
219221
self.framework.modules.add_module_path(opts['ModulePath']) if opts['ModulePath']

0 commit comments

Comments
 (0)