From 658958d8e7cd26f435a18ddf55b30e2a7b0669cd Mon Sep 17 00:00:00 2001 From: benpturner Date: Mon, 4 May 2015 22:07:22 +0100 Subject: [PATCH] Allow sessions -c command on powershell --- lib/msf/base/sessions/powershell.rb | 23 +++++++++++++++++++ lib/msf/ui/console/command_dispatcher/core.rb | 3 +++ 2 files changed, 26 insertions(+) diff --git a/lib/msf/base/sessions/powershell.rb b/lib/msf/base/sessions/powershell.rb index b718e940c6..b221b25d58 100644 --- a/lib/msf/base/sessions/powershell.rb +++ b/lib/msf/base/sessions/powershell.rb @@ -33,4 +33,27 @@ class Msf::Sessions::PowerShell < Msf::Sessions::CommandShell def desc "Powershell session" end + + # + # Takes over the shell_command of the parent + # + def shell_command(cmd) + # Send the command to the session's stdin. + shell_write(cmd + "\n") + + timeo = 5 + etime = ::Time.now.to_f + timeo + buff = "" + + # Keep reading data until no more data is available or the timeout is + # reached. + while (::Time.now.to_f < etime and (self.respond_to?(:ring) or ::IO.select([rstream], nil, nil, timeo))) + res = shell_read(-1, 0.01) + res.gsub!(/PS .*>/, '') + buff << res if res + timeo = etime - ::Time.now.to_f + end + + buff + end end diff --git a/lib/msf/ui/console/command_dispatcher/core.rb b/lib/msf/ui/console/command_dispatcher/core.rb index 641b77ceae..173c2c59f0 100644 --- a/lib/msf/ui/console/command_dispatcher/core.rb +++ b/lib/msf/ui/console/command_dispatcher/core.rb @@ -1775,6 +1775,9 @@ class Core elsif session.type == 'shell' output = session.shell_command(cmd) print_line(output) if output + elsif session.type == 'powershell' + output = session.shell_command(cmd) + print_line(output) if output end ensure # Restore timeout for each session