From 862e738015792986b59767350755bf2f2f35acdd Mon Sep 17 00:00:00 2001 From: Zach Goldman Date: Fri, 18 Aug 2023 10:45:34 -0500 Subject: [PATCH] extend error message for timeouts to include more detail to user initial functionality, testing/cleanup still needed script and command functionality remove unnecessary accessor switch puts to print_error in proc ensure proc is reset, run on every error, add yard docs fix yard, refactor/remove dead code rename on_error_proc --- lib/msf/ui/console/command_dispatcher/core.rb | 55 ++++++++++++++----- lib/rex/post/meterpreter/ui/console.rb | 27 +++++---- lib/rex/ui/interactive.rb | 6 ++ 3 files changed, 64 insertions(+), 24 deletions(-) diff --git a/lib/msf/ui/console/command_dispatcher/core.rb b/lib/msf/ui/console/command_dispatcher/core.rb index 7eee0b163dc3..999d478bae21 100644 --- a/lib/msf/ui/console/command_dispatcher/core.rb +++ b/lib/msf/ui/console/command_dispatcher/core.rb @@ -1576,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) @@ -1612,20 +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 - 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_run_command_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_run_command_error_proc = nil + end + end end end when 'kill' @@ -1674,16 +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_run_command_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 + 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_run_command_error_proc = nil end end else @@ -1708,12 +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_run_command_error_proc = log_on_timeout_error("Send timed out. Timeout currently #{session.response_timeout} seconds, you can configure this with %grnsessions --timeout --script