From 10cfadaf984b251a2dde2ec2002675abefd62b8c Mon Sep 17 00:00:00 2001 From: David Maloney Date: Thu, 5 Jan 2017 12:10:53 -0600 Subject: [PATCH] add optional output to merterp run_cmd the run_cmd method on meterpreter sessions can now take an optiona output IO to redirect output. This allows backgrounded sessions to also run commands and still output to the console --- lib/msf/base/sessions/meterpreter.rb | 19 +++++++++++++++++-- lib/msf/ui/console/command_dispatcher/core.rb | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/msf/base/sessions/meterpreter.rb b/lib/msf/base/sessions/meterpreter.rb index c97300168e..bcb592c97f 100644 --- a/lib/msf/base/sessions/meterpreter.rb +++ b/lib/msf/base/sessions/meterpreter.rb @@ -277,8 +277,23 @@ class Meterpreter < Rex::Post::Meterpreter::Client # # Explicitly runs a command in the meterpreter console. # - def run_cmd(cmd) - console.run_single(cmd) + def run_cmd(cmd,output_object=nil) + stored_output_state = nil + # If the user supplied an Output IO object, then we tell + # the console to use that, while saving it's previous output/ + if output_object + stored_output_state = console.output + console.send(:output=, output_object) + end + success = console.run_single(cmd) + # If we stored the previous output object of the channel + # we restore it here to put everything back the way we found it + # We re-use the conditional above, because we expect in many cases for + # the stored state to actually be nil here. + if output_object + console.send(:output=,stored_output_state) + end + success end # diff --git a/lib/msf/ui/console/command_dispatcher/core.rb b/lib/msf/ui/console/command_dispatcher/core.rb index 8509dbb80e..20c9e2b589 100644 --- a/lib/msf/ui/console/command_dispatcher/core.rb +++ b/lib/msf/ui/console/command_dispatcher/core.rb @@ -1236,7 +1236,7 @@ class Core session.response_timeout = response_timeout end - output = session.run_cmd cmd + output = session.run_cmd(cmd, driver.output) end end when 'kill'