Skip to content

Commit 706e304

Browse files
committed
Land 5299, implement shell_command for PS sessions
2 parents d2e1fdb + 98d531e commit 706e304

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

lib/msf/base/sessions/powershell.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,37 @@ def self.type
3333
def desc
3434
"Powershell session"
3535
end
36+
37+
#
38+
# Takes over the shell_command of the parent
39+
#
40+
def shell_command(cmd)
41+
# insert random marker
42+
strm = Rex::Text.rand_text_alpha(15)
43+
endm = Rex::Text.rand_text_alpha(15)
44+
45+
# Send the shell channel's stdin.
46+
shell_write(";'#{strm}'\n" + cmd + "\n'#{endm}';\n")
47+
48+
timeout = 1800 # 30 minute timeout
49+
etime = ::Time.now.to_f + timeout
50+
51+
buff = ""
52+
# Keep reading data until the marker has been received or the 30 minture timeout has occured
53+
while (::Time.now.to_f < etime)
54+
res = shell_read(-1, timeout)
55+
break unless res
56+
timeout = etime - ::Time.now.to_f
57+
58+
buff << res
59+
if buff.match(/#{endm}/)
60+
# if you see the end marker, read the buffer from the start marker to the end and then display back to screen
61+
buff = buff.split(/#{strm}/)[-1]
62+
buff.gsub!(/PS .*>/, '')
63+
buff.gsub!(/#{endm}/, '')
64+
return buff
65+
end
66+
end
67+
buff
68+
end
3669
end

lib/msf/ui/console/command_dispatcher/core.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,13 +1772,15 @@ def cmd_sessions(*args)
17721772
rescue Rex::TimeoutError
17731773
print_error("Operation timed out")
17741774
end
1775-
elsif session.type == 'shell'
1775+
elsif session.type == 'shell' || session.type == 'powershell'
17761776
output = session.shell_command(cmd)
17771777
print_line(output) if output
17781778
end
17791779
ensure
17801780
# Restore timeout for each session
1781-
session.response_timeout = last_known_timeout if last_known_timeout
1781+
if session.respond_to?(:response_timeout)
1782+
session.response_timeout = last_known_timeout if last_known_timeout
1783+
end
17821784
end
17831785
# If the session isn't a meterpreter or shell type, it
17841786
# could be a VNC session (which can't run commands) or

0 commit comments

Comments
 (0)