Skip to content

Commit 6468eb5

Browse files
committed
Do changes to have into account powershell sesions are not cmd sessions
1 parent 4cbe35e commit 6468eb5

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

lib/msf/base/sessions/powershell.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,14 @@ def desc
4444
#
4545
# Takes over the shell_command of the parent
4646
#
47-
def shell_command(cmd)
47+
def shell_command(cmd, timeout = 1800)
4848
# insert random marker
4949
strm = Rex::Text.rand_text_alpha(15)
5050
endm = Rex::Text.rand_text_alpha(15)
5151

5252
# Send the shell channel's stdin.
5353
shell_write(";'#{strm}'\n" + cmd + "\n'#{endm}';\n")
5454

55-
timeout = 1800 # 30 minute timeout
5655
etime = ::Time.now.to_f + timeout
5756

5857
buff = ""

lib/msf/core/exploit/powershell.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def generate_psh_args(opts)
147147
# @param ps_code [String] Powershell code
148148
# @param payload_arch [String] The payload architecture 'x86'/'x86_64'
149149
# @param encoded [Boolean] Indicates whether ps_code is encoded or not
150-
#
150+
#ex
151151
# @return [String] Wrapped powershell code
152152
def run_hidden_psh(ps_code, payload_arch, encoded)
153153
arg_opts = {

lib/msf/core/post/common.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@ def cmd_exec(cmd, args=nil, time_out=15)
198198
end
199199

200200
process.close
201+
when /powershell/
202+
if args.nil? || args.empty?
203+
o = session.shell_command("#{cmd}", time_out)
204+
else
205+
o = session.shell_command("#{cmd} #{args}", time_out)
206+
end
207+
o.chomp! if o
201208
when /shell/
202209
if args.nil? || args.empty?
203210
o = session.shell_command_token("#{cmd}", time_out)

modules/post/multi/manage/shell_to_meterpreter.rb

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,37 @@ def run
128128

129129
case platform
130130
when 'win'
131-
if (have_powershell?) && (datastore['WIN_TRANSFER'] != 'VBS')
132-
vprint_status("Transfer method: Powershell")
133-
psh_opts = { :prepend_sleep => 1, :encode_inner_payload => true, :persist => false }
134-
cmd_exec(cmd_psh_payload(payload_data, psh_arch, psh_opts))
135-
else
136-
print_error('Powershell is not installed on the target.') if datastore['WIN_TRANSFER'] == 'POWERSHELL'
137-
vprint_status("Transfer method: VBS [fallback]")
138-
exe = Msf::Util::EXE.to_executable(framework, larch, lplat, payload_data)
139-
aborted = transmit_payload(exe)
131+
if session.type == 'powershell'
132+
template_path = File.join(Msf::Config.data_directory, 'templates', 'scripts')
133+
psh_payload = case datastore['Powershell::method']
134+
when 'net'
135+
Rex::Powershell::Payload.to_win32pe_psh_net(template_path, payload_data)
136+
when 'reflection'
137+
Rex::Powershell::Payload.to_win32pe_psh_reflection(template_path, payload_data)
138+
when 'old'
139+
Rex::Powershell::Payload.to_win32pe_psh(template_path, payload_data)
140+
when 'msil'
141+
fail RuntimeError, 'MSIL Powershell method no longer exists'
142+
else
143+
fail RuntimeError, 'No Powershell method specified'
144+
end
145+
146+
# prepend_sleep => 1
147+
psh_payload = 'Start-Sleep -s 1;' << psh_payload
148+
149+
encoded_psh_payload = encode_script(psh_payload)
150+
cmd_exec(run_hidden_psh(encoded_psh_payload, psh_arch, true))
151+
else # shell
152+
if (have_powershell?) && (datastore['WIN_TRANSFER'] != 'VBS')
153+
vprint_status("Transfer method: Powershell")
154+
psh_opts = { :prepend_sleep => 1, :encode_inner_payload => true, :persist => false }
155+
cmd_exec(cmd_psh_payload(payload_data, psh_arch, psh_opts))
156+
else
157+
print_error('Powershell is not installed on the target.') if datastore['WIN_TRANSFER'] == 'POWERSHELL'
158+
vprint_status("Transfer method: VBS [fallback]")
159+
exe = Msf::Util::EXE.to_executable(framework, larch, lplat, payload_data)
160+
aborted = transmit_payload(exe)
161+
end
140162
end
141163
when 'python'
142164
vprint_status("Transfer method: Python")

0 commit comments

Comments
 (0)