Skip to content

Commit 964be3b

Browse files
committed
Fix problem with stale module cache
1 parent dfcbbfe commit 964be3b

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

lib/msf/core/modules/metadata/cache.rb

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Cache
2424
# Refreshes cached module metadata as well as updating the store
2525
#
2626
def refresh_metadata_instance(module_instance)
27+
dlog "Refreshing #{module_instance.refname} of type: #{module_instance.type}"
2728
refresh_metadata_instance_internal(module_instance)
2829
update_store
2930
end
@@ -48,8 +49,22 @@ def refresh_metadata(module_sets)
4849

4950
mt[1].keys.sort.each do |mn|
5051
next if unchanged_reference_name_set.include? mn
51-
module_instance = mt[1].create(mn)
52-
next if not module_instance
52+
53+
begin
54+
module_instance = mt[1].create(mn)
55+
rescue Exception => e
56+
elog "Unable to create module: #{mn}"
57+
end
58+
59+
unless module_instance
60+
wlog "Removing invalid module reference from cache: #{mn}"
61+
existed = remove_from_cache(mn)
62+
if existed
63+
has_changes = true
64+
end
65+
next
66+
end
67+
5368
begin
5469
refresh_metadata_instance_internal(module_instance)
5570
has_changes = true
@@ -72,7 +87,7 @@ def get_unchanged_module_references
7287

7388
@module_metadata_cache.each_value do |module_metadata|
7489

75-
unless module_metadata.path and ::File.exist?(module_metadata.path)
90+
if !module_metadata.path || !::File.exist?(module_metadata.path)
7691
next
7792
end
7893

@@ -91,15 +106,41 @@ def get_unchanged_module_references
91106
private
92107
#######
93108

109+
def remove_from_cache(module_name)
110+
@module_metadata_cache.each do |cache_key, module_metadata|
111+
112+
if module_metadata.ref_name.eql? module_name
113+
@module_metadata_cache.delete cache_key
114+
return true
115+
end
116+
end
117+
118+
return false
119+
end
120+
94121
def wait_for_load
95122
@load_thread.join unless @store_loaded
96123
end
97124

98125
def refresh_metadata_instance_internal(module_instance)
99126
metadata_obj = Obj.new(module_instance)
127+
purge_duplicate(metadata_obj)
100128
@module_metadata_cache[get_cache_key(module_instance)] = metadata_obj
101129
end
102130

131+
#
132+
# Remove all instances of modules pointing to the same path. This prevents stale data hanging
133+
# around when modules are incorrectly typed (eg: Auxilary that should be Exploit)
134+
#
135+
def purge_duplicate(metadata_obj)
136+
@module_metadata_cache.each do |cache_key, module_metadata|
137+
138+
if module_metadata.path.eql? metadata_obj.path
139+
@module_metadata_cache.delete cache_key
140+
end
141+
end
142+
end
143+
103144
def get_cache_key(module_instance)
104145
key = ''
105146
key << (module_instance.type.nil? ? '' : module_instance.type)

0 commit comments

Comments
 (0)