Skip to content

Commit

Permalink
Add feature flag tracking close method calls
Browse files Browse the repository at this point in the history
  • Loading branch information
adfoster-r7 committed Nov 14, 2023
1 parent 04361e1 commit d84f75e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/msf/core/feature_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class FeatureManager
MANAGER_COMMANDS = 'manager_commands'
METASPLOIT_PAYLOAD_WARNINGS = 'metasploit_payload_warnings'
DEFER_MODULE_LOADS = 'defer_module_loads'
LOG_ALL_CLOSE_METHOD_CALLS = 'log_all_close_method_calls'
DEFAULTS = [
{
name: WRAPPED_TABLES,
Expand Down Expand Up @@ -53,6 +54,12 @@ class FeatureManager
description: 'When enabled will not eagerly load all modules',
requires_restart: true,
default_value: false
}.freeze,
{
name: LOG_ALL_CLOSE_METHOD_CALLS,
description: 'Log all close method calls - used to debug crashes reported in https://github.com/rapid7/metasploit-framework/issues/18462',
requires_restart: true,
default_value: false
}.freeze
].freeze

Expand Down
22 changes: 22 additions & 0 deletions lib/msf/core/framework.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,28 @@ def initialize(options={})
self.features = FeatureManager.instance
self.features.load_config

if self.features.enabled?(Msf::FeatureManager::LOG_ALL_CLOSE_METHOD_CALLS)
log_directory = File.join(Msf::Config.log_directory, "close")
FileUtils.mkdir_p(log_directory)
close_logger = Logger.new(File.join(log_directory, "#{Time.now.to_s.gsub(' ', '-')}.log"))
TracePoint.new(:c_call) do |tp|
if tp.method_id == :close
event = {
lineno: tp.lineno,
defined_class: tp.defined_class,
tp: tp.method_id,
event: tp.event
}
details = <<~EOF
[DEBUG] Close called:
#{(caller || []).join("\n")}
#{JSON.pretty_generate(event)}
EOF
close_logger.info details
end
end.enable
end

self.events = EventDispatcher.new(self)
self.modules = ModuleManager.new(self,types)
self.datastore = DataStore.new
Expand Down

0 comments on commit d84f75e

Please sign in to comment.