@@ -46,13 +46,19 @@ def load_cached_module(type, reference_name)
46
46
loaded
47
47
end
48
48
49
- # Rebuild the cache for the module set
49
+ # @overload refresh_cache_from_module_files
50
+ # Rebuilds database and in-memory cache for all modules.
50
51
#
51
- # @return [void]
52
- def refresh_cache_from_module_files ( mod = nil )
52
+ # @return [void]
53
+ # @overload refresh_cache_from_module_files(module_class_or_instance)
54
+ # Rebuilds database and in-memory cache for given module_class_or_instance.
55
+ #
56
+ # @param (see Msf::DBManager#update_module_details)
57
+ # @return [void]
58
+ def refresh_cache_from_module_files ( module_class_or_instance = nil )
53
59
if framework_migrated?
54
- if mod
55
- framework . db . update_module_details ( mod )
60
+ if module_class_or_instance
61
+ framework . db . update_module_details ( module_class_or_instance )
56
62
else
57
63
framework . db . update_all_module_details
58
64
end
@@ -61,7 +67,7 @@ def refresh_cache_from_module_files(mod = nil)
61
67
end
62
68
end
63
69
64
- # Reset the module cache
70
+ # Refreshes the in-memory cache from the database cache.
65
71
#
66
72
# @return [void]
67
73
def refresh_cache_from_database
@@ -97,32 +103,38 @@ def module_info_by_path_from_database!
97
103
self . module_info_by_path = { }
98
104
99
105
if framework_migrated?
100
- # TODO record module parent_path in {Mdm::ModuleDetail} so it does not need to be derived from file.
101
- ::Mdm ::ModuleDetail . find ( :all ) . each do |module_detail |
102
- path = module_detail . file
103
- type = module_detail . mtype
104
- reference_name = module_detail . refname
105
-
106
- typed_path = Msf ::Modules ::Loader ::Base . typed_path ( type , reference_name )
107
- escaped_typed_path = Regexp . escape ( typed_path )
108
- parent_path = path . gsub ( /#{ escaped_typed_path } $/ , '' )
109
-
110
- module_info_by_path [ path ] = {
111
- :reference_name => reference_name ,
112
- :type => type ,
113
- :parent_path => parent_path ,
114
- :modification_time => module_detail . mtime
115
- }
116
-
117
- typed_module_set = module_set ( type )
118
-
119
- # Don't want to trigger as {Msf::ModuleSet#create} so check for
120
- # key instead of using ||= which would call {Msf::ModuleSet#[]}
121
- # which would potentially call {Msf::ModuleSet#create}.
122
- unless typed_module_set . has_key? reference_name
123
- typed_module_set [ reference_name ] = Msf ::SymbolicModule
124
- end
125
- end
106
+ ActiveRecord ::Base . connection_pool . with_connection do
107
+ # TODO record module parent_path in {Mdm::ModuleDetail} so it does not need to be derived from file.
108
+ # Use find_each so Mdm::ModuleDetails are returned in batches, which will
109
+ # handle the growing number of modules better than all.each.
110
+ Mdm ::ModuleDetail . find_each do |module_detail |
111
+ path = module_detail . file
112
+ type = module_detail . mtype
113
+ reference_name = module_detail . refname
114
+
115
+ typed_path = Msf ::Modules ::Loader ::Base . typed_path ( type , reference_name )
116
+ # join to '' so that typed_path_prefix starts with file separator
117
+ typed_path_suffix = File . join ( '' , typed_path )
118
+ escaped_typed_path = Regexp . escape ( typed_path_suffix )
119
+ parent_path = path . gsub ( /#{ escaped_typed_path } $/ , '' )
120
+
121
+ module_info_by_path [ path ] = {
122
+ :reference_name => reference_name ,
123
+ :type => type ,
124
+ :parent_path => parent_path ,
125
+ :modification_time => module_detail . mtime
126
+ }
127
+
128
+ typed_module_set = module_set ( type )
129
+
130
+ # Don't want to trigger as {Msf::ModuleSet#create} so check for
131
+ # key instead of using ||= which would call {Msf::ModuleSet#[]}
132
+ # which would potentially call {Msf::ModuleSet#create}.
133
+ unless typed_module_set . has_key? reference_name
134
+ typed_module_set [ reference_name ] = Msf ::SymbolicModule
135
+ end
136
+ end
137
+ end
126
138
end
127
139
128
140
self . module_info_by_path
0 commit comments