2014-08-17 17:31:53 -05:00
# -*- coding: binary -*-
2025-07-10 11:09:10 +02:00
2025-07-10 13:18:40 +02:00
# Msf::ModuleManager::Reloading
#
# Provides methods for reloading Metasploit modules (including payloads,
# stagers, adapters, stages, etc.), clearing out old aliases, and
# refreshing the module cache.
2012-10-01 13:09:30 -05:00
module Msf::ModuleManager::Reloading
# Reloads the module specified in mod. This can either be an instance of a module or a module class.
#
# @param [Msf::Module, Class] mod either an instance of a module or a module class
2012-10-03 16:48:55 -05:00
# @return (see Msf::Modules::Loader::Base#reload_module)
2012-10-01 13:09:30 -05:00
def reload_module ( mod )
2025-06-29 12:00:58 +02:00
# if it's an instance, then get its class
2012-10-01 13:09:30 -05:00
if mod . is_a? Msf :: Module
metasploit_class = mod . class
else
metasploit_class = mod
end
2025-07-10 13:18:40 +02:00
if ( aliased_as = inv_aliases [ metasploit_class . fullname ] )
2019-06-03 13:40:27 -05:00
aliased_as . each do | a |
2025-07-10 11:09:10 +02:00
aliases . delete a
2019-06-03 13:40:27 -05:00
end
2025-07-10 11:09:10 +02:00
inv_aliases . delete metasploit_class . fullname
2025-06-29 12:00:58 +02:00
end
2025-07-03 11:07:30 +02:00
if mod . payload?
return reload_payload_module ( mod )
2019-06-03 13:40:27 -05:00
end
2025-07-10 11:09:10 +02:00
2025-07-10 13:18:40 +02:00
if ( aliased_as = inv_aliases [ metasploit_class . fullname ] )
2025-07-10 11:09:10 +02:00
aliased_as . each do | a |
aliases . delete a
end
inv_aliases . delete metasploit_class . fullname
end
2021-05-27 15:15:31 +01:00
namespace_module = metasploit_class . module_parent
2025-07-10 11:09:10 +02:00
2025-06-29 12:00:58 +02:00
# Check if the namespace module has a loader
unless namespace_module . respond_to? ( :loader )
2025-07-10 11:09:10 +02:00
elog ( 'Module does not have loader' )
2025-07-03 11:07:30 +02:00
return mod
2025-06-29 12:00:58 +02:00
end
2012-10-01 13:09:30 -05:00
loader = namespace_module . loader
2025-07-01 08:37:27 +02:00
loader . reload_module ( mod )
2012-10-01 13:09:30 -05:00
end
2025-07-10 11:09:10 +02:00
def manual_reload ( parent_path , type , ref_name )
loaders . each { | loader | loader . load_module ( parent_path , type , ref_name , { force : true } ) }
end
# Reload payload module, separately from other categories. This is due to complexity of payload module and due to the fact they don't follow class structure as rest of the modules.
2025-07-03 11:07:30 +02:00
# @param [Msf::Module, Class] mod either an instance of a module or a module class
# @return (see Msf::Modules::Loader::Base#reload_module)
def reload_payload_module ( mod )
if mod . is_a? Msf :: Module
metasploit_class = mod . class
original_instance = mod
else
metasploit_class = mod
original_instance = nil
2025-07-10 11:09:10 +02:00
end
if ( module_set = module_set_by_type . fetch ( metasploit_class . type , nil ) )
2025-07-03 11:07:30 +02:00
module_set . delete ( metasploit_class . refname )
2025-07-03 08:12:54 +02:00
end
2025-07-10 11:09:10 +02:00
module_info = module_info_by_path [ metasploit_class . file_path ]
2025-07-03 08:12:54 +02:00
unless module_info && ( parent_path = module_info [ :parent_path ] )
2025-07-10 11:09:10 +02:00
elog ( 'Failed to get parent_path from module object' )
2025-07-03 11:07:30 +02:00
return mod
end
2025-07-10 11:09:10 +02:00
# reload adapters if any
manual_reload ( parent_path , module_info [ :type ] , File . join ( 'adapters' , mod . adapter_refname ) ) if mod . adapter_refname
# reload stagers if any
manual_reload ( parent_path , module_info [ :type ] , File . join ( 'stagers' , mod . stager_refname ) ) if mod . stager_refname
2025-06-29 12:00:58 +02:00
2025-07-10 11:09:10 +02:00
# reload stages if any
manual_reload ( parent_path , module_info [ :type ] , File . join ( 'stages' , mod . stage_refname ) ) if mod . stage_refname
2025-07-03 11:07:30 +02:00
2025-07-10 11:09:10 +02:00
# reload single if any
manual_reload ( parent_path , module_info [ :type ] , File . join ( 'singles' , module_info [ :reference_name ] ) ) if original_instance . payload_type == Msf :: Payload :: Type :: Single
2025-06-29 12:00:58 +02:00
2025-07-03 11:07:30 +02:00
# Get reloaded module
new_instance = framework . modules . create ( metasploit_class . fullname )
2025-06-29 12:00:58 +02:00
2025-07-03 08:12:54 +02:00
if new_instance . blank?
2025-07-10 11:09:10 +02:00
elog ( 'Failed create new instance' )
2025-07-03 11:07:30 +02:00
return mod
2025-06-29 12:00:58 +02:00
end
2025-07-10 11:09:10 +02:00
# Restore the datastore
2025-07-03 11:07:30 +02:00
new_instance . datastore . merge! ( original_instance . datastore )
2025-07-03 08:12:54 +02:00
# Return the new instance, which the framework will make the active module.
2025-06-29 12:00:58 +02:00
return new_instance
rescue StandardError = > e
2025-07-03 11:07:30 +02:00
elog ( " Failed to reload payload #{ fullname } : #{ e . message } " )
return mod
2025-06-29 12:00:58 +02:00
end
2012-10-01 13:09:30 -05:00
# Reloads modules from all module paths
#
2012-10-03 16:48:55 -05:00
# @return (see Msf::ModuleManager::Loading#load_modules)
2012-10-01 13:09:30 -05:00
def reload_modules
2025-07-10 11:09:10 +02:00
enablement_by_type . each_key do | type |
2012-10-01 13:09:30 -05:00
module_set_by_type [ type ] . clear
init_module_set ( type )
end
2025-07-10 11:09:10 +02:00
aliases . clear
inv_aliases . clear
2012-10-01 13:09:30 -05:00
2012-10-03 16:48:55 -05:00
# default the count to zero the first time a type is accessed
count_by_type = Hash . new ( 0 )
2025-07-03 11:07:30 +02:00
framework . init_module_paths unless framework . module_paths_inited
2012-10-01 13:09:30 -05:00
module_paths . each do | path |
2024-01-03 15:49:37 +00:00
path_count_by_type = load_modules ( path , force : true )
2012-10-03 16:48:55 -05:00
# merge count with count from other paths
path_count_by_type . each do | type , count |
count_by_type [ type ] += count
end
2012-10-01 13:09:30 -05:00
end
2012-10-04 11:14:08 -05:00
refresh_cache_from_module_files
2025-07-03 11:07:30 +02:00
2012-10-03 16:48:55 -05:00
count_by_type
2012-10-01 13:09:30 -05:00
end
2012-12-13 11:23:49 -06:00
end