From af60ee2f19e3b3a61b3edbc375857c3779d08bfd Mon Sep 17 00:00:00 2001 From: Zach Goldman Date: Wed, 18 Oct 2023 16:38:06 -0500 Subject: [PATCH] ensure proc is reset, run on every error, add yard docs --- lib/msf/ui/console/command_dispatcher/core.rb | 68 ++++++++++++------- lib/rex/post/meterpreter/ui/console.rb | 32 ++++----- lib/rex/ui/interactive.rb | 5 ++ 3 files changed, 63 insertions(+), 42 deletions(-) diff --git a/lib/msf/ui/console/command_dispatcher/core.rb b/lib/msf/ui/console/command_dispatcher/core.rb index 67c03702d73f4..9e543c75097d8 100644 --- a/lib/msf/ui/console/command_dispatcher/core.rb +++ b/lib/msf/ui/console/command_dispatcher/core.rb @@ -957,14 +957,6 @@ def cmd_load_help print_line end - def log_on_timeout_error(message) - proc do |e| - next unless e.is_a?(Rex::TimeoutError) - elog(e) - print_error(message) - end - end - def list_plugins plugin_directories = { 'Framework' => Msf::Config.plugin_directory, @@ -1584,7 +1576,7 @@ def cmd_sessions(*args) rescue ::Rex::Post::Meterpreter::RequestError print_error("Failed: #{$!.class} #{$!}") rescue Rex::TimeoutError - print_error("Operation timed out") + print_error("Operation timed out. Timeout currently #{session.response_timeout} seconds, you can configure this with %grnsessions -c --timeout %clr") end elsif session.type == 'shell' || session.type == 'powershell' output = session.shell_command(cmd) @@ -1620,21 +1612,28 @@ def cmd_sessions(*args) cmds.each do |cmd| sessions.each do |session| - session = verify_session(session) - unless session.type == 'meterpreter' - print_error "Session ##{session.sid} is not a Meterpreter shell. Skipping..." - next - end + begin + session = verify_session(session) + unless session.type == 'meterpreter' + print_error "Session ##{session.sid} is not a Meterpreter shell. Skipping..." + next + end - next unless session - print_status("Running '#{cmd}' on #{session.type} session #{session.sid} (#{session.session_host})") - if session.respond_to?(:response_timeout) - last_known_timeout = session.response_timeout - session.response_timeout = response_timeout - session.on_error_proc = log_on_timeout_error("Send timed out. Timeout currently #{session.response_timeout} seconds, you can configure this with sessions -C --timeout ") - end + next unless session + print_status("Running '#{cmd}' on #{session.type} session #{session.sid} (#{session.session_host})") + if session.respond_to?(:response_timeout) + last_known_timeout = session.response_timeout + session.response_timeout = response_timeout + session.on_error_proc = log_on_timeout_error("Send timed out. Timeout currently #{session.response_timeout} seconds, you can configure this with %grnsessions -C --timeout %clr") + end - output = session.run_cmd(cmd, driver.output) + output = session.run_cmd(cmd, driver.output) + ensure + if session.respond_to?(:response_timeout) && last_known_timeout + session.response_timeout = last_known_timeout + session.on_error_proc = nil + end + end end end when 'kill' @@ -1683,19 +1682,19 @@ def cmd_sessions(*args) if session.respond_to?(:response_timeout) last_known_timeout = session.response_timeout session.response_timeout = response_timeout + session.on_error_proc = log_on_timeout_error("Send timed out. Timeout currently #{session.response_timeout} seconds, you can configure this with %grnsessions --interact --timeout %clr") end print_status("Starting interaction with #{session.name}...\n") unless quiet begin self.active_session = session - session.on_error_proc = log_on_timeout_error("Send timed out. Timeout currently #{session.response_timeout} seconds, you can configure this with sessions --interact --timeout ") - sid = session.interact(driver.input.dup, driver.output) self.active_session = nil driver.input.reset_tab_completion if driver.input.supports_readline ensure if session.respond_to?(:response_timeout) && last_known_timeout session.response_timeout = last_known_timeout + session.on_error_proc = nil end end else @@ -1720,13 +1719,14 @@ def cmd_sessions(*args) if session.respond_to?(:response_timeout) last_known_timeout = session.response_timeout session.response_timeout = response_timeout - session.on_error_proc = log_on_timeout_error("Send timed out. Timeout currently #{session.response_timeout} seconds, you can configure this with sessions --timeout --script