diff --git a/lib/msf/ui/console/command_dispatcher/core.rb b/lib/msf/ui/console/command_dispatcher/core.rb index ce08f3cb5d48..117b9d0cdb1f 100644 --- a/lib/msf/ui/console/command_dispatcher/core.rb +++ b/lib/msf/ui/console/command_dispatcher/core.rb @@ -1617,7 +1617,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) @@ -1653,20 +1653,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' @@ -1722,16 +1730,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 @@ -1756,12 +1767,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