Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/plugins/fzuse.rb
Views: 11705
require 'socket'12# this is the main routine that's executed in the grandchild process (msfconsole -> fzf -> this)3if $PROGRAM_NAME == __FILE__4exit 64 unless ARGV.length == 256UNIXSocket.open(ARGV[0]) do |sock|7sock.write ARGV[1] + "\n"8sock.flush910puts sock.read11end12exit 013end1415module Msf16class Plugin::FuzzyUse < Msf::Plugin17class ConsoleCommandDispatcher18include Msf::Ui::Console::CommandDispatcher1920FZF_THEME = {21'fg' => '-1',22'fg+' => 'white:regular:bold',23'bg' => '-1',24'bg+' => '-1',25'hl' => '-1',26'hl+' => 'red:regular:bold',27'info' => '-1',28'marker' => '-1',29'prompt' => '-1',30'spinner' => '-1',31'pointer' => 'blue:bold',32'header' => '-1',33'border' => '-1',34'label' => '-1',35'query' => '-1'36}.freeze3738def initialize(driver)39super4041@module_dispatcher = Msf::Ui::Console::CommandDispatcher::Modules.new(driver)42end4344def name45'FuzzyUse'46end4748#49# Returns the hash of commands supported by this dispatcher.50#51def commands52{53'fzuse' => 'A fuzzy use command added by the FuzzyUse plugin'54}55end5657def pipe_server(socket_path)58server = UNIXServer.new(socket_path)59File.chmod(0600, socket_path)60loop do61client = server.accept62begin63unless (input_string = client.gets&.chomp).blank?64if (mod = framework.modules.create(input_string))65client.puts(Serializer::ReadableText.dump_module(mod))66end67end68rescue StandardError69end70client.close71end72rescue EOFError73ensure74server.close if server75File.delete(socket_path) if File.exist?(socket_path)76end7778#79# This method handles the fzuse command.80#81def cmd_fzuse(*args)82selection = nil8384Dir.mktmpdir('msf-fzuse-') do |dir|85socket_path = File.join(dir, "msf-fzuse.sock")86server_thread = Thread.new { pipe_server(socket_path) }8788query = args.empty? ? '' : args.first89ruby = RbConfig::CONFIG['bindir'] + '/' + RbConfig::CONFIG['ruby_install_name'] + RbConfig::CONFIG['EXEEXT']9091color = "--color=#{FZF_THEME.map { |key, value| "#{key}:#{value}" }.join(',')}"92Open3.popen3('fzf', '--select-1', '--query', query, '--pointer=->', color, '--preview', "'#{ruby}' '#{__FILE__}' '#{socket_path}' '{1}'", '--preview-label', "Module Information") do |stdin, stdout, stderr, wait_thr|93framework.modules.module_types.each do |module_type|94framework.modules.module_names(module_type).each do |module_name|95stdin.puts "#{module_type}/#{module_name}"96end97end98stdin.close99selection = stdout.read100end101102server_thread.kill103server_thread.join104end105106return if selection.blank?107108selection.strip!109@module_dispatcher.cmd_use(selection)110end111end112113def initialize(framework, opts)114super115116unless defined?(UNIXSocket)117# This isn't a requirement that can be fixed by installing something118print_error("The FuzzyUse plugin has loaded but the Ruby environment does not support UNIX sockets.")119return120end121122missing_requirements = []123missing_requirements << 'fzf' unless Msf::Util::Helper.which('fzf')124125unless missing_requirements.empty?126print_error("The FuzzyUse plugin has loaded but the following requirements are missing: #{missing_requirements.join(', ')}")127print_error("Please install the missing requirements, then reload the plugin by running: `unload fzuse` and `load fzuse`.")128return129end130131add_console_dispatcher(ConsoleCommandDispatcher)132133print_status('FuzzyUse plugin loaded.')134end135136def cleanup137remove_console_dispatcher('FuzzyUse')138end139140def name141'fuzzy_use'142end143144def desc145'A plugin offering a fuzzy use command'146end147148end149end150151152