2015-04-23 23:12:02 +01:00
|
|
|
# -*- coding: binary -*-
|
2015-04-26 18:50:42 +01:00
|
|
|
require 'msf/base/sessions/command_shell'
|
2015-04-23 23:12:02 +01:00
|
|
|
|
2015-04-26 18:50:42 +01:00
|
|
|
class Msf::Sessions::PowerShell < Msf::Sessions::CommandShell
|
2015-04-26 20:13:18 +01:00
|
|
|
#
|
|
|
|
|
# Execute any specified auto-run scripts for this session
|
|
|
|
|
#
|
|
|
|
|
def process_autoruns(datastore)
|
2015-04-26 15:59:36 -05:00
|
|
|
|
|
|
|
|
# Read the username and hostname from the initial banner
|
2015-04-26 20:13:18 +01:00
|
|
|
initial_output = shell_read(-1, 0.01)
|
2015-04-26 21:47:49 +01:00
|
|
|
if initial_output =~ /running as user ([^\s]+) on ([^\s]+)/
|
|
|
|
|
username = $1
|
|
|
|
|
hostname = $2
|
2015-04-26 15:59:36 -05:00
|
|
|
self.info = "#{username} @ #{hostname}"
|
|
|
|
|
else
|
|
|
|
|
self.info = initial_output.gsub(/[\r\n]/, ' ')
|
2015-04-26 21:47:49 +01:00
|
|
|
end
|
2015-04-26 15:59:36 -05:00
|
|
|
|
2015-04-26 20:13:18 +01:00
|
|
|
# Call our parent class's autoruns processing method
|
|
|
|
|
super
|
|
|
|
|
end
|
2015-04-23 23:12:02 +01:00
|
|
|
#
|
|
|
|
|
# Returns the type of session.
|
|
|
|
|
#
|
|
|
|
|
def self.type
|
|
|
|
|
"powershell"
|
|
|
|
|
end
|
|
|
|
|
|
2015-09-08 15:37:42 +02:00
|
|
|
#
|
|
|
|
|
# Returns the session platform.
|
|
|
|
|
#
|
|
|
|
|
def platform
|
|
|
|
|
"win"
|
|
|
|
|
end
|
|
|
|
|
|
2015-04-23 23:12:02 +01:00
|
|
|
#
|
|
|
|
|
# Returns the session description.
|
|
|
|
|
#
|
|
|
|
|
def desc
|
|
|
|
|
"Powershell session"
|
|
|
|
|
end
|
2015-05-04 22:07:22 +01:00
|
|
|
|
|
|
|
|
#
|
2015-05-04 22:17:08 +01:00
|
|
|
# Takes over the shell_command of the parent
|
2015-05-04 22:07:22 +01:00
|
|
|
#
|
2015-10-02 15:26:42 -05:00
|
|
|
def shell_command(cmd, timeout = 1800)
|
2015-05-07 19:06:36 +01:00
|
|
|
# insert random marker
|
2015-05-07 22:50:09 +01:00
|
|
|
strm = Rex::Text.rand_text_alpha(15)
|
|
|
|
|
endm = Rex::Text.rand_text_alpha(15)
|
2015-05-05 19:20:03 +01:00
|
|
|
|
|
|
|
|
# Send the shell channel's stdin.
|
2015-05-07 22:50:09 +01:00
|
|
|
shell_write(";'#{strm}'\n" + cmd + "\n'#{endm}';\n")
|
2015-05-04 22:07:22 +01:00
|
|
|
|
2015-05-05 19:20:03 +01:00
|
|
|
etime = ::Time.now.to_f + timeout
|
2015-05-04 22:07:22 +01:00
|
|
|
|
2015-05-05 19:20:03 +01:00
|
|
|
buff = ""
|
|
|
|
|
# Keep reading data until the marker has been received or the 30 minture timeout has occured
|
|
|
|
|
while (::Time.now.to_f < etime)
|
|
|
|
|
res = shell_read(-1, timeout)
|
|
|
|
|
break unless res
|
|
|
|
|
timeout = etime - ::Time.now.to_f
|
2015-05-07 19:06:36 +01:00
|
|
|
|
2015-05-05 19:20:03 +01:00
|
|
|
buff << res
|
2019-10-23 13:47:46 -05:00
|
|
|
if buff.include?(endm)
|
2015-05-07 19:06:36 +01:00
|
|
|
# if you see the end marker, read the buffer from the start marker to the end and then display back to screen
|
2015-05-25 20:10:29 +01:00
|
|
|
buff = buff.split(/#{strm}\r\n/)[-1]
|
2019-10-23 13:47:46 -05:00
|
|
|
buff = buff.split(endm)[0]
|
2019-10-16 20:06:50 -05:00
|
|
|
buff.gsub!(/(?<=\r\n)PS [^>]*>/, '')
|
2019-10-22 11:51:00 -05:00
|
|
|
return buff
|
2015-05-05 19:20:03 +01:00
|
|
|
end
|
2015-05-04 22:07:22 +01:00
|
|
|
end
|
|
|
|
|
buff
|
|
|
|
|
end
|
2015-04-23 23:12:02 +01:00
|
|
|
end
|