Skip to content

Commit a16cd5a

Browse files
committed
Clean up metadata store logic
1 parent 0e642bd commit a16cd5a

File tree

3 files changed

+37
-39
lines changed

3 files changed

+37
-39
lines changed

lib/msf/core/framework.rb

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ module Offspring
6161
require 'msf/core/db_manager'
6262
require 'msf/core/event_dispatcher'
6363
require 'rex/json_hash_file'
64-
require 'msf/core/modules/metadata/cache'
6564

6665
#
6766
# Creates an instance of the framework context.
@@ -230,24 +229,8 @@ def threads?
230229
}
231230
end
232231

232+
# TODO: Anything still using this should be ported to use metadata::cache search
233233
def search(match, logger: nil)
234-
# Check if the database is usable
235-
use_db = true
236-
if self.db
237-
if !(self.db.migrated && self.db.modules_cached)
238-
logger.print_warning("Module database cache not built yet, using slow search") if logger
239-
use_db = false
240-
end
241-
else
242-
logger.print_warning("Database not connected, using slow search") if logger
243-
use_db = false
244-
end
245-
246-
# Used the database for search
247-
if use_db
248-
return self.db.search_modules(match)
249-
end
250-
251234
# Do an in-place search
252235
matches = []
253236
[ self.exploits, self.auxiliary, self.post, self.payloads, self.nops, self.encoders ].each do |mset|

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

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module Msf::Modules::Metadata::Store
1313
UserMetaDataFile = 'modules_metadata.pstore'
1414

1515
#
16-
# Initializes from user store (under ~/.msf4) if it exists. else base file (under $INSTALL_ROOT/db) is copied and loaded.
16+
# Initializes from user store (under ~/store/.msf4) if it exists. else base file (under $INSTALL_ROOT/db) is copied and loaded.
1717
#
1818
def init_store
1919
load_metadata
@@ -23,8 +23,12 @@ def init_store
2323
# Update the module meta cache disk store
2424
#
2525
def update_store
26-
@store.transaction do
27-
@store[:module_metadata] = @module_metadata_cache
26+
begin
27+
@store.transaction do
28+
@store[:module_metadata] = @module_metadata_cache
29+
end
30+
rescue Excepion => e
31+
elog("Unable to update metadata store: #{e.message}")
2832
end
2933
end
3034

@@ -45,10 +49,12 @@ def load_metadata
4549

4650
# Try to handle the scenario where the file is corrupted
4751
if (retries < 2 && ::File.exist?(@path_to_user_metadata))
48-
FileUtils.remove(@path_to_user_metadata, true)
52+
elog('Possible corrupt user metadata store, attempting restore')
53+
FileUtils.remove(@path_to_user_metadata)
4954
retry
5055
else
51-
@console.print_warning("Unable to load module metadata: #{e.message}")
56+
@console.print_warning('Unable to load module metadata from disk see error log')
57+
elog("Unable to load module metadata: #{e.message}")
5258
end
5359
end
5460

@@ -69,28 +75,38 @@ def validate_data(copied)
6975

7076
def configure_user_store
7177
copied = false
72-
@path_to_user_metadata = ::File.join(Msf::Config.config_directory, UserMetaDataFile)
78+
@path_to_user_metadata = get_user_store
7379
path_to_base_metadata = ::File.join(Msf::Config.install_root, "db", BaseMetaDataFile)
80+
user_file_exists = ::File.exist?(@path_to_user_metadata)
81+
base_file_exists = ::File.exist?(path_to_base_metadata)
7482

75-
if (!::File.exist?(path_to_base_metadata))
83+
if (!base_file_exists)
7684
wlog("Missing base module metadata file: #{path_to_base_metadata}")
77-
else
78-
if (!::File.exist?(@path_to_user_metadata))
79-
FileUtils.cp(path_to_base_metadata, @path_to_user_metadata)
80-
copied = true
81-
dlog('Created user based module store')
82-
83-
# Update the user based module store if an updated base file is created/pushed
84-
elsif (::File.mtime(path_to_base_metadata).to_i > ::File.mtime(@path_to_user_metadata).to_i)
85-
FileUtils.remove(@path_to_user_metadata, true)
86-
FileUtils.cp(path_to_base_metadata, @path_to_user_metadata)
87-
copied = true
88-
dlog('Updated user based module store')
89-
end
85+
return copied if !user_file_exists
86+
end
87+
88+
if (!user_file_exists)
89+
FileUtils.cp(path_to_base_metadata, @path_to_user_metadata)
90+
copied = true
91+
92+
dlog('Created user based module store')
93+
94+
# Update the user based module store if an updated base file is created/pushed
95+
elsif (::File.mtime(path_to_base_metadata).to_i > ::File.mtime(@path_to_user_metadata).to_i)
96+
FileUtils.remove(@path_to_user_metadata)
97+
FileUtils.cp(path_to_base_metadata, @path_to_user_metadata)
98+
copied = true
99+
dlog('Updated user based module store')
90100
end
91101

92102
return copied
93103
end
94104

105+
def get_user_store
106+
store_dir = ::File.join(Msf::Config.config_directory, "store")
107+
FileUtils.mkdir(store_dir) if !::File.exist?(store_dir)
108+
return ::File.join(store_dir, UserMetaDataFile)
109+
end
110+
95111
end
96112

spec/support/shared/examples/msf/module_manager/cache.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
end
55

66
let(:metadata_cache) do
7-
# Msf::Modules::Metadata::Cache.instance.init(framework)
87
Msf::Modules::Metadata::Cache.instance
98
end
109

0 commit comments

Comments
 (0)