diff --git a/Gemfile.lock b/Gemfile.lock index 30e984d96771b..4fe22aa28347c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -402,7 +402,7 @@ GEM nokogiri redcarpet (3.6.0) regexp_parser (2.9.2) - reline (0.5.8) + reline (0.5.10) io-console (~> 0.5) require_all (3.0.0) rex-arch (0.1.16) diff --git a/lib/metasploit/framework/command/console.rb b/lib/metasploit/framework/command/console.rb index 3c476574289c9..d104a042d33be 100644 --- a/lib/metasploit/framework/command/console.rb +++ b/lib/metasploit/framework/command/console.rb @@ -92,7 +92,6 @@ def driver_options driver_options['ModulePath'] = options.modules.path driver_options['Plugins'] = options.console.plugins driver_options['Readline'] = options.console.readline - driver_options['RealReadline'] = options.console.real_readline driver_options['Resource'] = options.console.resources driver_options['XCommands'] = options.console.commands diff --git a/lib/metasploit/framework/parsed_options/console.rb b/lib/metasploit/framework/parsed_options/console.rb index 34aa126633aa3..1252f1c45648a 100644 --- a/lib/metasploit/framework/parsed_options/console.rb +++ b/lib/metasploit/framework/parsed_options/console.rb @@ -16,7 +16,6 @@ def options options.console.plugins = [] options.console.quiet = false options.console.readline = true - options.console.real_readline = false options.console.resources = [] options.console.subcommand = :run } @@ -54,7 +53,12 @@ def option_parser end option_parser.on('-L', '--real-readline', 'Use the system Readline library instead of RbReadline') do - options.console.real_readline = true + message = "The RealReadline option has been marked as deprecated, and is currently a noop.\n" + message << "Metasploit Framework now uses RbReadline exclusively as the input handling library, with the option to use Reline.\n" + message << "Reline is planned to be the only UI library used in the future.\n" + message << "If you require this functionality, please use the following link to tell us:\n" + message << ' https://github.com/rapid7/metasploit-framework/issues/19399' + warn message end option_parser.on('-o', '--output FILE', 'Output to the specified file') do |file| diff --git a/lib/metasploit/framework/parsed_options/remote_db.rb b/lib/metasploit/framework/parsed_options/remote_db.rb index 49dd9c466644d..5344539bfd42c 100644 --- a/lib/metasploit/framework/parsed_options/remote_db.rb +++ b/lib/metasploit/framework/parsed_options/remote_db.rb @@ -13,7 +13,6 @@ def options options.console.local_output = nil options.console.plugins = [] options.console.quiet = false - options.console.real_readline = false options.console.resources = [] options.console.subcommand = :run } diff --git a/lib/msf/core/feature_manager.rb b/lib/msf/core/feature_manager.rb index 3ab3865ae771e..836c371db3345 100644 --- a/lib/msf/core/feature_manager.rb +++ b/lib/msf/core/feature_manager.rb @@ -28,6 +28,7 @@ class FeatureManager MSSQL_SESSION_TYPE = 'mssql_session_type' LDAP_SESSION_TYPE = 'ldap_session_type' SHOW_SUCCESSFUL_LOGINS = 'show_successful_logins' + USE_RELINE = 'use_reline' DEFAULTS = [ { @@ -124,6 +125,13 @@ class FeatureManager requires_restart: false, default_value: true, developer_notes: 'Enabled in Metasploit 6.4.x' + }.freeze, + { + name: USE_RELINE, + description: 'When enabled, the new Reline library will be used instead of the legacy Readline library for input/output.', + requires_restart: true, + default_value: false, + developer_notes: 'To be enabled by default after sufficient testing and Reline fixes the issues raised here: https://github.com/ruby/reline/issues/created_by/sjanusz-r7' }.freeze ].freeze diff --git a/lib/msf/ui/console/command_dispatcher/core.rb b/lib/msf/ui/console/command_dispatcher/core.rb index 5599879afad6a..4f3f5accdafc2 100644 --- a/lib/msf/ui/console/command_dispatcher/core.rb +++ b/lib/msf/ui/console/command_dispatcher/core.rb @@ -760,7 +760,8 @@ def cmd_features_tabs(_str, words) end def cmd_history(*args) - length = Readline::HISTORY.length + history = Msf::Ui::Console::MsfReadline.instance.history + length = history.length if length < @history_limit limit = length @@ -780,10 +781,12 @@ def cmd_history(*args) limit = val.to_i end when '-c' - if Readline::HISTORY.respond_to?(:clear) - Readline::HISTORY.clear - elsif defined?(RbReadline) - RbReadline.clear_history + if history.respond_to?(:clear) + history.clear + elsif history.respond_to?(:clear_history) + history.clear_history + elsif history.respond_to?(:pop) && history.respond_to?(:length) + history.length.times { |_i| history.pop } else print_error('Could not clear history, skipping file') return false @@ -808,7 +811,7 @@ def cmd_history(*args) (start..length-1).each do |pos| cmd_num = (pos + 1).to_s - print_line "#{cmd_num.ljust(pad_len)} #{Readline::HISTORY[pos]}" + print_line "#{cmd_num.ljust(pad_len)} #{history[pos]}" end end diff --git a/lib/msf/ui/console/command_dispatcher/developer.rb b/lib/msf/ui/console/command_dispatcher/developer.rb index 33c2c39a3f1f6..5ad376822a237 100644 --- a/lib/msf/ui/console/command_dispatcher/developer.rb +++ b/lib/msf/ui/console/command_dispatcher/developer.rb @@ -131,6 +131,7 @@ def cmd_irb(*args) framework.history_manager.with_context(name: :irb) do begin + reline_autocomplete = Reline.autocompletion if defined?(Reline) if active_module print_status("You are in #{active_module.fullname}\n") Rex::Ui::Text::IrbShell.new(active_module).run @@ -140,6 +141,8 @@ def cmd_irb(*args) end rescue print_error("Error during IRB: #{$!}\n\n#{$@.join("\n")}") + ensure + Reline.autocompletion = reline_autocomplete if defined?(Reline) end end @@ -515,6 +518,10 @@ def cmd_time_help private def modified_files + # Temporary work-around until Open3 gets fixed on Windows 11: + # https://github.com/ruby/open3/issues/9 + return [] if Rex::Compat.is_cygwin || Rex::Compat.is_windows + # Using an array avoids shelling out, so we avoid escaping/quoting changed_files = %w[git diff --name-only] begin diff --git a/lib/msf/ui/console/driver.rb b/lib/msf/ui/console/driver.rb index 199980028499c..e7486809b919b 100644 --- a/lib/msf/ui/console/driver.rb +++ b/lib/msf/ui/console/driver.rb @@ -53,8 +53,6 @@ class Driver < Msf::Ui::Driver # @option opts [Boolean] 'AllowCommandPassthru' (true) Whether to allow # unrecognized commands to be executed by the system shell # @option opts [Boolean] 'Readline' (true) Whether to use the readline or not - # @option opts [Boolean] 'RealReadline' (false) Whether to use the system's - # readline library instead of RBReadline # @option opts [String] 'HistFile' (Msf::Config.history_file) Path to a file # where we can store command history # @option opts [Array] 'Resources' ([]) A list of resource files to @@ -64,7 +62,7 @@ class Driver < Msf::Ui::Driver # @option opts [Boolean] 'SkipDatabaseInit' (false) Whether to skip # connecting to the database and running migrations def initialize(prompt = DefaultPrompt, prompt_char = DefaultPromptChar, opts = {}) - choose_readline(opts) + setup_readline if opts['Readline'] histfile = opts['HistFile'] || Msf::Config.history_file @@ -132,14 +130,6 @@ def initialize(prompt = DefaultPrompt, prompt_char = DefaultPromptChar, opts = { # stack enstack_dispatcher(CommandDispatcher::Core) - # Report readline error if there was one.. - if !@rl_err.nil? - print_error("***") - print_error("* Unable to load readline: #{@rl_err}") - print_error("* Falling back to RbReadLine") - print_error("***") - end - # Load the other "core" command dispatchers CommandDispatchers.each do |dispatcher_class| dispatcher = enstack_dispatcher(dispatcher_class) @@ -323,12 +313,9 @@ def save_config # Saves the recent history to the specified file # def save_recent_history(path) - num = Readline::HISTORY.length - hist_last_saved - 1 - - tmprc = "" - num.times { |x| - tmprc << Readline::HISTORY[hist_last_saved + x] + "\n" - } + history = Msf::Ui::Console::MsfReadline.instance.history + num = history.length - hist_last_saved - 1 + tmprc = history.entries[hist_last_saved..].join("\n") if tmprc.length > 0 print_status("Saving last #{num} commands to #{path} ...") @@ -339,7 +326,7 @@ def save_recent_history(path) # Always update this, even if we didn't save anything. We do this # so that we don't end up saving the "makerc" command itself. - self.hist_last_saved = Readline::HISTORY.length + self.hist_last_saved = history.length end # @@ -706,77 +693,46 @@ def handle_session_tlv_logging(val) # Require the appropriate readline library based on the user's preference. # # @return [void] - def choose_readline(opts) - # Choose a readline library before calling the parent - @rl_err = nil - if opts['RealReadline'] - # Remove the gem version from load path to be sure we're getting the - # stdlib readline. - gem_dir = Gem::Specification.find_all_by_name('rb-readline').first.gem_dir - rb_readline_path = File.join(gem_dir, "lib") - index = $LOAD_PATH.index(rb_readline_path) - # Bundler guarantees that the gem will be there, so it should be safe to - # assume we found it in the load path, but check to be on the safe side. - if index - $LOAD_PATH.delete_at(index) - end - end + def setup_readline + require 'readline' + + # Only Windows requires a monkey-patched RbReadline + return unless Rex::Compat.is_windows + + if defined?(::RbReadline) && !defined?(RbReadline.refresh_console_handle) + ::RbReadline.instance_eval do + class << self + alias_method :old_rl_move_cursor_relative, :_rl_move_cursor_relative + alias_method :old_rl_get_screen_size, :_rl_get_screen_size + alias_method :old_space_to_eol, :space_to_eol + alias_method :old_insert_some_chars, :insert_some_chars + end - begin - require 'readline' - - # Only Windows requires a monkey-patched RbReadline - return unless Rex::Compat.is_windows - - if defined?(::RbReadline) && !defined?(RbReadline.refresh_console_handle) - ::RbReadline.instance_eval do - class << self - alias_method :old_rl_move_cursor_relative, :_rl_move_cursor_relative - alias_method :old_rl_get_screen_size, :_rl_get_screen_size - alias_method :old_space_to_eol, :space_to_eol - alias_method :old_insert_some_chars, :insert_some_chars - end - - def self.refresh_console_handle - # hConsoleHandle gets set only when RbReadline detects it is running on Windows. - # Therefore, we don't need to check Rex::Compat.is_windows, we can simply check if hConsoleHandle is nil or not. - @hConsoleHandle = @GetStdHandle.Call(::Readline::STD_OUTPUT_HANDLE) if @hConsoleHandle - end - - def self._rl_move_cursor_relative(*args) - refresh_console_handle - old_rl_move_cursor_relative(*args) - end - - def self._rl_get_screen_size(*args) - refresh_console_handle - old_rl_get_screen_size(*args) - end - - def self.space_to_eol(*args) - refresh_console_handle - old_space_to_eol(*args) - end - - def self.insert_some_chars(*args) - refresh_console_handle - old_insert_some_chars(*args) - end + def self.refresh_console_handle + # hConsoleHandle gets set only when RbReadline detects it is running on Windows. + # Therefore, we don't need to check Rex::Compat.is_windows, we can simply check if hConsoleHandle is nil or not. + @hConsoleHandle = @GetStdHandle.Call(::Readline::STD_OUTPUT_HANDLE) if @hConsoleHandle + end + + def self._rl_move_cursor_relative(*args) + refresh_console_handle + old_rl_move_cursor_relative(*args) + end + + def self._rl_get_screen_size(*args) + refresh_console_handle + old_rl_get_screen_size(*args) + end + + def self.space_to_eol(*args) + refresh_console_handle + old_space_to_eol(*args) + end + + def self.insert_some_chars(*args) + refresh_console_handle + old_insert_some_chars(*args) end - end - rescue ::LoadError => e - if @rl_err.nil? && index - # Then this is the first time the require failed and we have an index - # for the gem version as a fallback. - @rl_err = e - # Put the gem back and see if that works - $LOAD_PATH.insert(index, rb_readline_path) - index = rb_readline_path = nil - retry - else - # Either we didn't have the gem to fall back on, or we failed twice. - # Nothing more we can do here. - raise e end end end diff --git a/lib/msf/ui/console/module_option_tab_completion.rb b/lib/msf/ui/console/module_option_tab_completion.rb index f8ecd07216e3d..919a5caa7a832 100644 --- a/lib/msf/ui/console/module_option_tab_completion.rb +++ b/lib/msf/ui/console/module_option_tab_completion.rb @@ -53,18 +53,18 @@ def tab_complete_option(mod, str, words) option_name = str.chop option_value = '' - ::Readline.completion_append_character = ' ' + Msf::Ui::Console::MsfReadline.instance.completion_append_character = ' ' return tab_complete_option_values(mod, option_value, words, opt: option_name).map { |value| "#{str}#{value}" } elsif str.include?('=') str_split = str.split('=') option_name = str_split[0].strip option_value = str_split[1].strip - ::Readline.completion_append_character = ' ' + Msf::Ui::Console::MsfReadline.instance.completion_append_character = ' ' return tab_complete_option_values(mod, option_value, words, opt: option_name).map { |value| "#{option_name}=#{value}" } end - ::Readline.completion_append_character = '' + Msf::Ui::Console::MsfReadline.instance.completion_append_character = '' tab_complete_option_names(mod, str, words).map { |name| "#{name}=" } end diff --git a/lib/msf/ui/console/msf_readline.rb b/lib/msf/ui/console/msf_readline.rb new file mode 100644 index 0000000000000..2152172cc5600 --- /dev/null +++ b/lib/msf/ui/console/msf_readline.rb @@ -0,0 +1,104 @@ +# +# This class is responsible for handling Readline/Reline-agnostic user input. +# +class Msf::Ui::Console::MsfReadline + require 'singleton' + include Singleton + + # Required to check the Reline flag. + require 'msf/core/feature_manager' + + attr_accessor :history + + def initialize + @backend = using_reline? ? ::Reline : ::Readline + @history = @backend::HISTORY + end + + def method_missing(sym, *args, &block) + if @backend.respond_to?(sym) + @backend.send(sym, *args, &block) + else + msg = "Method '#{sym}' not found in #{@backend.class}" + elog(msg) + raise NoMethodError, msg + end + end + + # Read a line from the user, and return it. + # @param prompt [String] The prompt to show to the user. + # @param add_history [Boolean] True if the user's input should be saved to the history. + # @param opts [Hash] Options + # @return String The line that the user has entered. + def readline(prompt, add_history = false, opts: {}) + input(prompt, add_history, opts: opts) + end + + def using_reline? + Msf::FeatureManager.instance.enabled?(Msf::FeatureManager::USE_RELINE) + end + + private + + attr_accessor :backend + + def input(*args, opts: {}) + using_reline? ? input_reline(*args, opts: opts) : input_rbreadline(*args, opts: opts) + end + + def input_rbreadline(prompt, add_history = false, opts: {}) + # rb-readlines's Readline.readline hardcodes the input and output to + # $stdin and $stdout, which means setting `Readline.input` or + # `Readline.output` has no effect when running `Readline.readline` with + # rb-readline, so need to reimplement + # []`Readline.readline`](https://github.com/luislavena/rb-readline/blob/ce4908dae45dbcae90a6e42e3710b8c3a1f2cd64/lib/readline.rb#L36-L58) + # for rb-readline to support setting input and output. Output needs to + # be set so that colorization works for the prompt on Windows. + + input_on_entry = RbReadline.rl_instream + output_on_entry = RbReadline.rl_outstream + + begin + RbReadline.rl_instream = opts[:fd] + RbReadline.rl_outstream = opts[:output] + line = RbReadline.readline(prompt.to_s) + rescue ::StandardError => e + RbReadline.rl_instream = input_on_entry + RbReadline.rl_outstream = output_on_entry + RbReadline.rl_cleanup_after_signal + RbReadline.rl_deprep_terminal + + raise e + end + + if add_history && line && !line.start_with?(' ') + # Don't add duplicate lines to history + if ::Readline::HISTORY.empty? || line.strip != ::Readline::HISTORY[-1] + RbReadline.add_history(line.strip) + end + end + + line.dup + end + + def input_reline(prompt, add_history = false, opts: {}) + input_on_entry = Reline::IOGate.instance_variable_get(:@input) + output_on_entry = Reline::IOGate.instance_variable_get(:@output) + + begin + Reline.input = opts[:fd] + Reline.output = opts[:output] + line = Reline.readline(prompt.to_s, add_history) + ensure + Reline.input = input_on_entry + Reline.output = output_on_entry + end + + # Don't add duplicate lines to history + if Reline::HISTORY.length > 1 && line == Reline::HISTORY[-2] + Reline::HISTORY.pop + end + + line.dup + end +end diff --git a/lib/msf/ui/debug.rb b/lib/msf/ui/debug.rb index 6bf8f8e3b1385..42067acff3457 100644 --- a/lib/msf/ui/debug.rb +++ b/lib/msf/ui/debug.rb @@ -220,13 +220,14 @@ def self.framework_config(framework) end def self.history(driver) - end_pos = Readline::HISTORY.length - 1 + history = Msf::Ui::Console::MsfReadline.instance.history + end_pos = history.length - 1 start_pos = end_pos - COMMAND_HISTORY_TOTAL > driver.hist_last_saved ? end_pos - (COMMAND_HISTORY_TOTAL - 1) : driver.hist_last_saved commands = '' while start_pos <= end_pos # Formats command position in history to 6 characters in length - commands += "#{'%-6.6s' % start_pos.to_s} #{Readline::HISTORY[start_pos]}\n" + commands += "#{'%-6.6s' % start_pos.to_s} #{history[start_pos]}\n" start_pos += 1 end diff --git a/lib/msf_autoload.rb b/lib/msf_autoload.rb index c546aa8dc1957..83e652c183be9 100644 --- a/lib/msf_autoload.rb +++ b/lib/msf_autoload.rb @@ -347,3 +347,17 @@ def finalize_loader(loader) # XXX: Should be removed once the `lib/metasploit` folder is loaded by Zeitwerk require 'metasploit/framework/hashes' + +# TODO: Remove this monkey-patch once the following issue is addressed over on Reline's repository here: +# https://github.com/ruby/reline/issues/756 +require 'reline' +class ::Reline::Core + alias old_completion_append_character= completion_append_character= + alias old_completion_append_character completion_append_character + + def completion_append_character=(v) + self.old_completion_append_character = v + # Additionally keep the line_editor in sync + line_editor.completion_append_character = self.old_completion_append_character + end +end diff --git a/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/fs.rb b/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/fs.rb index 676be4ace17e8..8183175cc161f 100644 --- a/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/fs.rb +++ b/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/fs.rb @@ -902,7 +902,7 @@ def tab_complete_cdirectory(str, words) def tab_complete_path(str, words, dir_only) if client.platform == 'windows' - ::Readline.completion_case_fold = true + Msf::Ui::Console::MsfReadline.instance.completion_case_fold = true end if client.commands.include?(COMMAND_ID_STDAPI_FS_LS) expanded = str @@ -915,7 +915,7 @@ def tab_complete_path(str, words, dir_only) # This is annoying if we're recursively tab-traversing our way through subdirectories - # we may want to continue traversing, but MSF will add a space, requiring us to back up to continue # tab-completing our way through successive subdirectories. - ::Readline.completion_append_character = nil + Msf::Ui::Console::MsfReadline.instance.completion_append_character = nil end results else diff --git a/lib/rex/ui/text/dispatcher_shell.rb b/lib/rex/ui/text/dispatcher_shell.rb index 2131cc363292d..58c26c44fb51f 100644 --- a/lib/rex/ui/text/dispatcher_shell.rb +++ b/lib/rex/ui/text/dispatcher_shell.rb @@ -311,7 +311,7 @@ def tab_complete_directory(str, words) # This is annoying if we're recursively tab-traversing our way through subdirectories - # we may want to continue traversing, but MSF will add a space, requiring us to back up to continue # tab-completing our way through successive subdirectories. - ::Readline.completion_append_character = nil + Msf::Ui::Console::MsfReadline.instance.completion_append_character = nil end if dirs.length == 0 && File.directory?(str) @@ -408,9 +408,13 @@ def initialize(prompt, prompt_char = '>', histfile = nil, framework = nil, name # a design problem in the Readline module and depends on the # Readline.basic_word_break_characters variable being set to \x00 # - def tab_complete(str) - ::Readline.completion_append_character = ' ' - ::Readline.completion_case_fold = false + def tab_complete(str, opts: {}) + Msf::Ui::Console::MsfReadline.instance.completion_append_character = ' ' + Msf::Ui::Console::MsfReadline.instance.completion_case_fold = false + + if opts[:preposing] && Msf::Ui::Console::MsfReadline.instance.using_reline? + str = "#{opts[:preposing]}#{str}" + end # Check trailing whitespace so we can tell 'x' from 'x ' str_match = str.match(/[^\\]([\\]{2})*\s+$/) @@ -424,11 +428,7 @@ def tab_complete(str) # Pop the last word and pass it to the real method result = tab_complete_stub(str, split_str) - if result - result.uniq - else - result - end + result&.uniq end # Performs tab completion of a command, if supported diff --git a/lib/rex/ui/text/input/readline.rb b/lib/rex/ui/text/input/readline.rb index 85ca9d1218056..529a39e34e7ed 100644 --- a/lib/rex/ui/text/input/readline.rb +++ b/lib/rex/ui/text/input/readline.rb @@ -4,30 +4,32 @@ module Rex module Ui module Text -begin - - ### # # This class implements standard input using readline against # standard input. It supports tab completion. # - ### class Input::Readline < Rex::Ui::Text::Input # - # Initializes the readline-aware Input instance for text. + # The prompt that is to be displayed. # - def initialize(tab_complete_proc = nil) - if(not Object.const_defined?('Readline')) - require 'readline' - end + attr_accessor :prompt - self.extend(::Readline) + # + # The output handle to use when displaying the prompt. + # + attr_accessor :output + # + # Initializes the readline-aware Input instance for text. + # + def initialize(tab_complete_proc = nil) + super() if tab_complete_proc - ::Readline.basic_word_break_characters = "" - @rl_saved_proc = with_error_handling(tab_complete_proc) - ::Readline.completion_proc = @rl_saved_proc + Msf::Ui::Console::MsfReadline.instance.basic_word_break_characters = '' + # Cache the value so that we can use it when resetting the proc. + @completion_proc = tab_complete_proc + Msf::Ui::Console::MsfReadline.instance.completion_proc = with_error_handling(@completion_proc) end end @@ -35,24 +37,11 @@ def initialize(tab_complete_proc = nil) # Reattach the original completion proc # def reset_tab_completion(tab_complete_proc = nil) - ::Readline.basic_word_break_characters = "\x00" - ::Readline.completion_proc = tab_complete_proc ? with_error_handling(tab_complete_proc) : @rl_saved_proc + Msf::Ui::Console::MsfReadline.instance.basic_word_break_characters = "\x00" + @completion_proc = with_error_handling(tab_complete_proc) if tab_complete_proc + Msf::Ui::Console::MsfReadline.instance.completion_proc = @completion_proc end - - # - # Retrieve the line buffer - # - def line_buffer - if defined? RbReadline - RbReadline.rl_line_buffer - else - ::Readline.line_buffer - end - end - - attr_accessor :prompt - # # Whether or not the input medium supports readline. # @@ -95,11 +84,11 @@ def pgets begin Thread.current.priority = -20 - output.prompting - line = readline_with_output(prompt, true) - ::Readline::HISTORY.pop if (line and line.empty?) + output.prompting(true) + line = Msf::Ui::Console::MsfReadline.instance.readline(prompt, true, opts: { fd: fd, output: output }) ensure Thread.current.priority = orig || 0 + output.prompting(false) end line @@ -120,88 +109,21 @@ def intrinsic_shell? true end - # - # The prompt that is to be displayed. - # - attr_accessor :prompt - # - # The output handle to use when displaying the prompt. - # - attr_accessor :output - private - def readline_with_output(prompt, add_history=false) - # rb-readlines's Readline.readline hardcodes the input and output to - # $stdin and $stdout, which means setting `Readline.input` or - # `Readline.ouput` has no effect when running `Readline.readline` with - # rb-readline, so need to reimplement - # []`Readline.readline`](https://github.com/luislavena/rb-readline/blob/ce4908dae45dbcae90a6e42e3710b8c3a1f2cd64/lib/readline.rb#L36-L58) - # for rb-readline to support setting input and output. Output needs to - # be set so that colorization works for the prompt on Windows. - self.prompt = prompt - - # TODO: there are unhandled quirks in async output buffering that - # we have not solved yet, for instance when loading meterpreter - # extensions, supporting Windows, printing output from commands, etc. - # Remove this guard when issues are resolved. -=begin - reset_sequence = "\n\001\r\033[K\002" - if (/mingw/ =~ RUBY_PLATFORM) - reset_sequence = "" - end -=end - reset_sequence = "" - - if defined? RbReadline - RbReadline.rl_instream = fd - RbReadline.rl_outstream = output - - begin - line = RbReadline.readline(reset_sequence + prompt) - rescue ::Exception => exception - RbReadline.rl_cleanup_after_signal() - RbReadline.rl_deprep_terminal() - - raise exception - end - - if add_history && line && !line.start_with?(' ') - # Don't add duplicate lines to history - if ::Readline::HISTORY.empty? || line.strip != ::Readline::HISTORY[-1] - RbReadline.add_history(line.strip) - end - end - - line.try(:dup) - else - # The line that's read is immediately added to history - line = ::Readline.readline(reset_sequence + prompt, true) - - # Don't add duplicate lines to history - if ::Readline::HISTORY.length > 1 && line == ::Readline::HISTORY[-2] - ::Readline::HISTORY.pop - end - - line - end - end - - private + attr_accessor :completion_proc def with_error_handling(proc) proc do |*args| proc.call(*args) - rescue StandardError => e + rescue ::StandardError => e elog("tab_complete_proc has failed with args #{args}", error: e) [] end end end -rescue LoadError end end end -end diff --git a/lib/rex/ui/text/irb_shell.rb b/lib/rex/ui/text/irb_shell.rb index 956523cb8c799..95aabb69401f3 100644 --- a/lib/rex/ui/text/irb_shell.rb +++ b/lib/rex/ui/text/irb_shell.rb @@ -39,21 +39,18 @@ def run # commands will work. IRB.conf[:MAIN_CONTEXT] = irb.context - # Trap interrupt - old_sigint = trap("SIGINT") do - begin + begin + old_sigint = trap("SIGINT") do irb.signal_handle - rescue RubyLex::TerminateLineInput - irb.eval_input end - end - # Keep processing input until the cows come home... - catch(:IRB_EXIT) do - irb.eval_input + # Keep processing input until the cows come home... + catch(:IRB_EXIT) do + irb.eval_input + end + ensure + trap("SIGINT", old_sigint) if old_sigint end - - trap("SIGINT", old_sigint) end end diff --git a/lib/rex/ui/text/output.rb b/lib/rex/ui/text/output.rb index ef8f8fd030c6c..7877e81d61eaa 100644 --- a/lib/rex/ui/text/output.rb +++ b/lib/rex/ui/text/output.rb @@ -86,6 +86,11 @@ def puts(*args) nil end + + # For Reline interop + def tty? + true + end end end diff --git a/lib/rex/ui/text/output/stdio.rb b/lib/rex/ui/text/output/stdio.rb index 085c912a60335..7d53fa0af8088 100644 --- a/lib/rex/ui/text/output/stdio.rb +++ b/lib/rex/ui/text/output/stdio.rb @@ -94,6 +94,7 @@ def print_raw(msg = '') msg end alias_method :write, :print_raw + alias_method :<<, :write def supports_color? case config[:color] diff --git a/lib/rex/ui/text/output/tee.rb b/lib/rex/ui/text/output/tee.rb index ebfbb4034a4b7..c729590ed05dc 100644 --- a/lib/rex/ui/text/output/tee.rb +++ b/lib/rex/ui/text/output/tee.rb @@ -44,6 +44,7 @@ def print_raw(msg = '') end alias :write :print_raw + alias_method :<<, :write def close self.fd.close if self.fd diff --git a/lib/rex/ui/text/shell.rb b/lib/rex/ui/text/shell.rb index 56a323c5bfacd..8cf0452ff6f09 100644 --- a/lib/rex/ui/text/shell.rb +++ b/lib/rex/ui/text/shell.rb @@ -63,10 +63,21 @@ def initialize(prompt, prompt_char = '>', histfile = nil, framework = nil, name self.framework = framework end + def create_tab_complete_proc + # Unless cont_flag because there's no tab complete for continuation lines + proc do |str, preposing = nil, postposting = nil| + next nil if cont_flag + + result = tab_complete(str, opts: { preposing: preposing, postposting: postposting }) + result.map! { |val| val[preposing.to_s.length..] } if result && Msf::Ui::Console::MsfReadline.instance.using_reline? + + next result + end + end + def init_tab_complete if (self.input and self.input.supports_readline) - # Unless cont_flag because there's no tab complete for continuation lines - self.input = Input::Readline.new(lambda { |str| tab_complete(str) unless cont_flag }) + self.input = Input::Readline.new(create_tab_complete_proc) self.input.output = self.output end end @@ -114,8 +125,8 @@ def unset_log_source # # Performs tab completion on the supplied string. # - def tab_complete(str) - return tab_complete_proc(str) if (tab_complete_proc) + def tab_complete(str, opts: {}) + return tab_complete_proc(str, opts: opts) if (tab_complete_proc) end # @@ -304,13 +315,13 @@ def with_history_manager_context begin history_manager.with_context(history_file: histfile, name: name) do - self.hist_last_saved = Readline::HISTORY.length + self.hist_last_saved = Msf::Ui::Console::MsfReadline.instance.history.length yield end ensure history_manager.flush - self.hist_last_saved = Readline::HISTORY.length + self.hist_last_saved = Msf::Ui::Console::MsfReadline.instance.history.length end end diff --git a/lib/rex/ui/text/shell/history_manager.rb b/lib/rex/ui/text/shell/history_manager.rb index cde3495fc9e8e..d69dfad345e64 100644 --- a/lib/rex/ui/text/shell/history_manager.rb +++ b/lib/rex/ui/text/shell/history_manager.rb @@ -1,6 +1,7 @@ # -*- coding: binary -*- -require 'singleton' +require 'reline' +require 'readline' module Rex module Ui @@ -124,12 +125,7 @@ def readline_available? end def reline_available? - begin - require 'reline' - defined?(::Reline) - rescue ::LoadError => _e - false - end + defined?(::Reline) end def clear_readline diff --git a/modules/post/linux/manage/pseudo_shell.rb b/modules/post/linux/manage/pseudo_shell.rb index 6cdac56f8e230..2dbdb9a7ff3ae 100644 --- a/modules/post/linux/manage/pseudo_shell.rb +++ b/modules/post/linux/manage/pseudo_shell.rb @@ -4,6 +4,7 @@ ## require 'readline' +require 'reline' class MetasploitModule < Msf::Post include Msf::Post::File @@ -104,9 +105,9 @@ def help def prompt_show promptshell = "#{@vusername}@#{@vhostname}:#{pwd.strip}#{@vpromptchar} " comp = proc { |s| LIST.grep(/^#{Regexp.escape(s)}/) } - Readline.completion_append_character = ' ' - Readline.completion_proc = comp - input = Readline.readline(promptshell, true) + Msf::Ui::Console::MsfReadline.instance.completion_append_character = ' ' + Msf::Ui::Console::MsfReadline.instance.completion_proc = comp + input = Msf::Ui::Console::MsfReadline.instance.readline(promptshell, true) return nil if input.nil? input diff --git a/spec/lib/msf/ui/console/command_dispatcher/core_spec.rb b/spec/lib/msf/ui/console/command_dispatcher/core_spec.rb index 79de59e7c79fb..f0088811f4c7b 100644 --- a/spec/lib/msf/ui/console/command_dispatcher/core_spec.rb +++ b/spec/lib/msf/ui/console/command_dispatcher/core_spec.rb @@ -1,6 +1,7 @@ require 'spec_helper' require 'readline' +require 'reline' RSpec.describe Msf::Ui::Console::CommandDispatcher::Core do include_context 'Msf::DBManager' diff --git a/spec/lib/msf/ui/text/dispatcher_shell_spec.rb b/spec/lib/msf/ui/text/dispatcher_shell_spec.rb index a9dbea33842d4..15941b04f67e0 100644 --- a/spec/lib/msf/ui/text/dispatcher_shell_spec.rb +++ b/spec/lib/msf/ui/text/dispatcher_shell_spec.rb @@ -1,5 +1,6 @@ require 'spec_helper' require 'readline' +require 'reline' RSpec.describe Rex::Ui::Text::DispatcherShell do let(:prompt) { '%undmsf6%clr' } diff --git a/tools/exploit/metasm_shell.rb b/tools/exploit/metasm_shell.rb index b2b24234083cf..b0c8c4187bab7 100755 --- a/tools/exploit/metasm_shell.rb +++ b/tools/exploit/metasm_shell.rb @@ -32,6 +32,7 @@ require 'rex' require 'readline' +require 'reline' require 'metasm' #PowerPC, seems broken for now in metasm diff --git a/tools/exploit/nasm_shell.rb b/tools/exploit/nasm_shell.rb index 9915274b37d1f..000a82e09b713 100755 --- a/tools/exploit/nasm_shell.rb +++ b/tools/exploit/nasm_shell.rb @@ -22,6 +22,7 @@ require 'msfenv' require 'rex' require 'readline' +require 'reline' # Check to make sure nasm is installed and reachable through the user's PATH. begin