Skip to content

Commit

Permalink
Load from the user's module store
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroSteiner committed Aug 5, 2024
1 parent 233cd61 commit a1a59cf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
13 changes: 7 additions & 6 deletions lib/msf/core/modules/metadata/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ def init_store
load_metadata
end

def get_user_store
store_dir = ::File.join(Msf::Config.config_directory, "store")
FileUtils.makedirs(store_dir) if !::File.exist?(store_dir)
return ::File.join(store_dir, UserMetaDataFile)
end


#######
private
#######
Expand Down Expand Up @@ -107,12 +114,6 @@ def configure_user_store
return copied
end

def get_user_store
store_dir = ::File.join(Msf::Config.config_directory, "store")
FileUtils.makedirs(store_dir) if !::File.exist?(store_dir)
return ::File.join(store_dir, UserMetaDataFile)
end

def load_cache_from_file_store
cache_map = JSON.parse(File.read(@path_to_user_metadata))
cache_map.each {|k,v|
Expand Down
3 changes: 2 additions & 1 deletion plugins/fzuse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def commands
#
def cmd_fzuse(*args)
previewer = File.join(Msf::Config.install_root, 'tools', 'modules', 'print.py')
metadata_path = Msf::Modules::Metadata::Cache.instance.get_user_store

module_types = framework.modules.module_types

Expand All @@ -52,7 +53,7 @@ def cmd_fzuse(*args)
selection = nil
# alternative preview:
# jq \'to_entries[] | select(.value.fullname == "{1}") | .value\' db/modules_metadata_base.json | bat --language=json --color=always
stdin, stdout, stderr, wait_thr = Open3.popen3('fzf', '--select-1', '--query', query, '--preview', "#{previewer} {1}", '--preview-label', "Module Information") do |stdin, stdout, stderr, wait_thr|
stdin, stdout, stderr, wait_thr = Open3.popen3('fzf', '--select-1', '--query', query, '--preview', "#{previewer} --metadata-path '#{metadata_path}' '{1}'", '--preview-label', "Module Information") do |stdin, stdout, stderr, wait_thr|
module_types
module_types.each do |module_type|
framework.modules.module_names(module_type).each do |module_name|
Expand Down
15 changes: 11 additions & 4 deletions tools/modules/print.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
}

framework_root = pathlib.Path(__file__).parent.parent.parent
default_metadata_path = (framework_root / 'db' / 'modules_metadata_base.json')

def get_notes(module_metadata):
tree = Tree('Notes', hide_root=True)
Expand Down Expand Up @@ -64,10 +65,11 @@ def get_bulleted_list(items):
def main():
parser = argparse.ArgumentParser(description='fzuse helper', conflict_handler='resolve')
parser.add_argument('module_name', help='module name to display')
parser.add_argument('--metadata-path', default=default_metadata_path, type=pathlib.Path, help='the path to the module metadata')
parser.add_argument('-v', '--version', action='version', version='%(prog)s Version: ' + __version__)
arguments = parser.parse_args()

with (framework_root / 'db' / 'modules_metadata_base.json').open('r') as file_h:
with arguments.metadata_path.open('r') as file_h:
all_metadata = json.load(file_h)
module_metadata = next((metadata for metadata in all_metadata.values() if metadata['fullname'] == arguments.module_name), None)
if not module_metadata:
Expand All @@ -84,7 +86,7 @@ def main():
table.add_row('[bold]Rank[/bold]', RANKS[module_metadata['rank']])
table.add_row('[bold]Disclosed[/bold]', module_metadata['disclosure_date'])

console = Console()
console = Console(color_system='256')
console.print(table)

panel_title = lambda v: f"[bold]{v}[/bold]"
Expand All @@ -96,8 +98,13 @@ def main():
if module_metadata.get('references'):
console.print(Panel(get_references(module_metadata), title=panel_title('References'), title_align='left'))
if module_metadata.get('path', ''):
syntax = Syntax.from_path(framework_root / module_metadata['path'][1:], background_color='default', line_numbers=True)
console.print(Panel(syntax, title=panel_title('Source code'), title_align='left'))
if pathlib.Path(module_metadata['path']).is_file():
module_path = pathlib.Path(module_metadata['path'])
elif pathlib.Path(framework_root / module_metadata['path'][1:]).is_file():
module_path = pathlib.Path(framework_root / module_metadata['path'][1:])
if module_path:
syntax = Syntax.from_path(module_path, background_color='default', line_numbers=True)
console.print(Panel(syntax, title=panel_title('Source code'), title_align='left'))

if __name__ == '__main__':
main()

0 comments on commit a1a59cf

Please sign in to comment.