Files
metasploit-gs/lib/msf/core/modules/error.rb
T
Luke Imhoff 16407f91c8 Rescue Errno::ENOENT from File.open in read_module_content
[Fixes #38426061, #38097411]

Msf::Modules::Loader::Directory#read_module_content may calculate a non-existent
module_path that gets passed to File.open causing an Errno::ENOENT exception
to be raised when using the module cache with a module that has been
moved to a new path (as is the case that originally found this bug) or
deleted.  Now, the exception is rescued and read_module_content returns
an empty string (''), which load_module detects with
module_content.empty? and returns earlier without attempting to module
eval the (empty) content.

As having Msf::Modules::Loader::Directory#read_module_content rescue the
exception, meant there was another place that needed to log and error
and store an error in Msf::ModuleManager#module_load_error_by_path, I
refactored the error reporting to call
Msf::Modules::Loader::Base#load_error, which handles writing to the log
and setting the Hash, so the error reporting is consistent across the
loaders.

The exception hierarchy was also refactored so that
namespace_module.metasploit_class now has an error raising counter-part:
namespace_module.metasploit_class! that can be used with
Msf::Modules::Loader::Base#load_error as it requires an exception, and
not just a string so the exception class, message, and backtrace can be
logged.
2012-11-06 17:38:38 -06:00

38 lines
830 B
Ruby

# Base error class for all error under {Msf::Modules}
class Msf::Modules::Error < StandardError
def initialize(attributes={})
@module_path = attributes[:module_path]
@module_reference_name = attributes[:module_reference_name]
message_parts = []
message_parts << "Failed to load module"
if module_reference_name or module_path
clause_parts = []
if module_reference_name
clause_parts << module_reference_name
end
if module_path
clause_parts << "from #{module_path}"
end
clause = clause_parts.join(' ')
message_parts << "(#{clause})"
end
causal_message = attributes[:causal_message]
if causal_message
message_parts << "due to #{causal_message}"
end
message = message_parts.join(' ')
super(message)
end
attr_reader :module_reference_name
attr_reader :module_path
end