Skip to content

Commit

Permalink
Place Reline behind a feature flag, used by default on new Windows hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanusz-r7 committed Oct 21, 2024
1 parent 27fa707 commit 6fb1316
Show file tree
Hide file tree
Showing 26 changed files with 275 additions and 242 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion lib/metasploit/framework/command/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 6 additions & 2 deletions lib/metasploit/framework/parsed_options/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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|
Expand Down
1 change: 0 additions & 1 deletion lib/metasploit/framework/parsed_options/remote_db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 8 additions & 0 deletions lib/msf/core/feature_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
{
Expand Down Expand Up @@ -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

Expand Down
15 changes: 9 additions & 6 deletions lib/msf/ui/console/command_dispatcher/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
7 changes: 7 additions & 0 deletions lib/msf/ui/console/command_dispatcher/developer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
130 changes: 43 additions & 87 deletions lib/msf/ui/console/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>] 'Resources' ([]) A list of resource files to
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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} ...")
Expand All @@ -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

#
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/msf/ui/console/module_option_tab_completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading

0 comments on commit 6fb1316

Please sign in to comment.