Clean up metadata store logic

MS-2855/keylogger-mettle-extension
christopher lee 2017-11-17 12:42:19 -06:00
parent 0e642bd9cd
commit a16cd5aade
3 changed files with 36 additions and 38 deletions

View File

@ -61,7 +61,6 @@ class Framework
require 'msf/core/db_manager' require 'msf/core/db_manager'
require 'msf/core/event_dispatcher' require 'msf/core/event_dispatcher'
require 'rex/json_hash_file' require 'rex/json_hash_file'
require 'msf/core/modules/metadata/cache'
# #
# Creates an instance of the framework context. # Creates an instance of the framework context.
@ -230,24 +229,8 @@ class Framework
} }
end end
# TODO: Anything still using this should be ported to use metadata::cache search
def search(match, logger: nil) def search(match, logger: nil)
# Check if the database is usable
use_db = true
if self.db
if !(self.db.migrated && self.db.modules_cached)
logger.print_warning("Module database cache not built yet, using slow search") if logger
use_db = false
end
else
logger.print_warning("Database not connected, using slow search") if logger
use_db = false
end
# Used the database for search
if use_db
return self.db.search_modules(match)
end
# Do an in-place search # Do an in-place search
matches = [] matches = []
[ self.exploits, self.auxiliary, self.post, self.payloads, self.nops, self.encoders ].each do |mset| [ self.exploits, self.auxiliary, self.post, self.payloads, self.nops, self.encoders ].each do |mset|

View File

@ -13,7 +13,7 @@ module Msf::Modules::Metadata::Store
UserMetaDataFile = 'modules_metadata.pstore' UserMetaDataFile = 'modules_metadata.pstore'
# #
# Initializes from user store (under ~/.msf4) if it exists. else base file (under $INSTALL_ROOT/db) is copied and loaded. # Initializes from user store (under ~/store/.msf4) if it exists. else base file (under $INSTALL_ROOT/db) is copied and loaded.
# #
def init_store def init_store
load_metadata load_metadata
@ -23,8 +23,12 @@ module Msf::Modules::Metadata::Store
# Update the module meta cache disk store # Update the module meta cache disk store
# #
def update_store def update_store
@store.transaction do begin
@store[:module_metadata] = @module_metadata_cache @store.transaction do
@store[:module_metadata] = @module_metadata_cache
end
rescue Excepion => e
elog("Unable to update metadata store: #{e.message}")
end end
end end
@ -45,10 +49,12 @@ module Msf::Modules::Metadata::Store
# Try to handle the scenario where the file is corrupted # Try to handle the scenario where the file is corrupted
if (retries < 2 && ::File.exist?(@path_to_user_metadata)) if (retries < 2 && ::File.exist?(@path_to_user_metadata))
FileUtils.remove(@path_to_user_metadata, true) elog('Possible corrupt user metadata store, attempting restore')
FileUtils.remove(@path_to_user_metadata)
retry retry
else else
@console.print_warning("Unable to load module metadata: #{e.message}") @console.print_warning('Unable to load module metadata from disk see error log')
elog("Unable to load module metadata: #{e.message}")
end end
end end
@ -69,28 +75,38 @@ module Msf::Modules::Metadata::Store
def configure_user_store def configure_user_store
copied = false copied = false
@path_to_user_metadata = ::File.join(Msf::Config.config_directory, UserMetaDataFile) @path_to_user_metadata = get_user_store
path_to_base_metadata = ::File.join(Msf::Config.install_root, "db", BaseMetaDataFile) path_to_base_metadata = ::File.join(Msf::Config.install_root, "db", BaseMetaDataFile)
user_file_exists = ::File.exist?(@path_to_user_metadata)
base_file_exists = ::File.exist?(path_to_base_metadata)
if (!::File.exist?(path_to_base_metadata)) if (!base_file_exists)
wlog("Missing base module metadata file: #{path_to_base_metadata}") wlog("Missing base module metadata file: #{path_to_base_metadata}")
else return copied if !user_file_exists
if (!::File.exist?(@path_to_user_metadata)) end
FileUtils.cp(path_to_base_metadata, @path_to_user_metadata)
copied = true
dlog('Created user based module store')
# Update the user based module store if an updated base file is created/pushed if (!user_file_exists)
elsif (::File.mtime(path_to_base_metadata).to_i > ::File.mtime(@path_to_user_metadata).to_i) FileUtils.cp(path_to_base_metadata, @path_to_user_metadata)
FileUtils.remove(@path_to_user_metadata, true) copied = true
FileUtils.cp(path_to_base_metadata, @path_to_user_metadata)
copied = true dlog('Created user based module store')
dlog('Updated user based module store')
end # Update the user based module store if an updated base file is created/pushed
elsif (::File.mtime(path_to_base_metadata).to_i > ::File.mtime(@path_to_user_metadata).to_i)
FileUtils.remove(@path_to_user_metadata)
FileUtils.cp(path_to_base_metadata, @path_to_user_metadata)
copied = true
dlog('Updated user based module store')
end end
return copied return copied
end end
def get_user_store
store_dir = ::File.join(Msf::Config.config_directory, "store")
FileUtils.mkdir(store_dir) if !::File.exist?(store_dir)
return ::File.join(store_dir, UserMetaDataFile)
end
end end

View File

@ -4,7 +4,6 @@ RSpec.shared_examples_for 'Msf::ModuleManager::Cache' do
end end
let(:metadata_cache) do let(:metadata_cache) do
# Msf::Modules::Metadata::Cache.instance.init(framework)
Msf::Modules::Metadata::Cache.instance Msf::Modules::Metadata::Cache.instance
end end