Skip to content

Commit 8ac5d2d

Browse files
author
Brent Cook
committed
tidy up a bit while we're in here
1 parent cf29a51 commit 8ac5d2d

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

lib/msf/core/exploit/powershell.rb

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,23 @@ def initialize(info = {})
1818
OptBool.new('Powershell::encode_inner_payload', [true, 'Encode inner payload for -EncodedCommand', false]),
1919
OptBool.new('Powershell::use_single_quotes', [true, 'Wraps the -Command argument in single quotes', false]),
2020
OptBool.new('Powershell::no_equals', [true, 'Pad base64 until no "=" remains', false]),
21-
OptEnum.new('Powershell::method', [true, 'Payload delivery method', 'reflection', %w(net reflection old msil)]),
22-
], self.class)
21+
OptEnum.new('Powershell::method', [true, 'Payload delivery method', 'reflection', %w[net reflection old msil]])
22+
]
23+
)
2324
end
2425

2526
#
2627
# Return a script from path or string
2728
#
2829
def read_script(script_path)
29-
return Rex::Powershell::Script.new(script_path)
30+
Rex::Powershell::Script.new(script_path)
3031
end
3132

3233
#
3334
# Return an array of substitutions for use in make_subs
3435
#
3536
def process_subs(subs)
36-
return [] if subs.nil? or subs.empty?
37+
return [] if subs.nil? || subs.empty?
3738
new_subs = []
3839
subs.split(';').each do |set|
3940
new_subs << set.split(',', 2)
@@ -49,7 +50,7 @@ def process_subs(subs)
4950
#
5051
def make_subs(script, subs)
5152
subs.each do |set|
52-
script.gsub!(set[0],set[1])
53+
script.gsub!(set[0], set[1])
5354
end
5455

5556
script
@@ -79,12 +80,11 @@ def encode_script(script_in, eof = nil)
7980
#
8081
# @return [String] Decoded script
8182
def decode_script(script_in)
82-
if script_in.to_s.match( /[A-Za-z0-9+\/]+={0,3}/)[0] == script_in.to_s and
83-
script_in.to_s.length % 4 == 0
84-
return Rex::Powershell::Command.decode_script(script_in)
85-
else
86-
return script_in
87-
end
83+
return script_in unless
84+
script_in.to_s.match(%r{[A-Za-z0-9+/]+={0,3}})[0] == script_in.to_s &&
85+
(script_in.to_s.length % 4).zero?
86+
87+
Rex::Powershell::Command.decode_script(script_in)
8888
end
8989

9090
#
@@ -95,7 +95,7 @@ def decode_script(script_in)
9595
# @param eof [String] Marker to indicate the end of file appended to script
9696
#
9797
# @return [String] Compressed script with decompression stub
98-
def compress_script(script_in, eof=nil)
98+
def compress_script(script_in, eof = nil)
9999
opts = {}
100100
datastore.select { |k, v| k =~ /^Powershell::(strip|sub)/ && v }.keys.map do |k|
101101
mod_method = k.split('::').last.intern
@@ -112,7 +112,8 @@ def compress_script(script_in, eof=nil)
112112
#
113113
# @return [String] Decompressed script
114114
def decompress_script(script_in)
115-
return script_in if script_in.match(/FromBase64String/).nil?
115+
return script_in unless script_in.match?(/FromBase64String/)
116+
116117
Rex::Powershell::Command.decompress_script(script_in)
117118
end
118119

@@ -182,8 +183,8 @@ def generate_psh_args(opts)
182183
# @return [String] Wrapped powershell code
183184
def run_hidden_psh(ps_code, payload_arch, encoded)
184185
arg_opts = {
185-
noprofile: true,
186-
windowstyle: 'hidden',
186+
noprofile: true,
187+
windowstyle: 'hidden'
187188
}
188189

189190
# Old technique fails if powershell exits..
@@ -221,26 +222,21 @@ def run_hidden_psh(ps_code, payload_arch, encoded)
221222
def cmd_psh_payload(pay, payload_arch, opts = {})
222223
options.validate(datastore)
223224

224-
[ :persist, :prepend_sleep, :exec_in_place, :encode_final_payload,
225-
:encode_inner_payload, :use_single_quotes, :no_equals, :method ].map { |opt|
225+
%i[persist prepend_sleep exec_in_place encode_final_payload encode_inner_payload use_single_quotes no_equals method].map do |opt|
226226
opts[opt] ||= datastore["Powershell::#{opt}"]
227-
}
227+
end
228228

229229
unless opts.key? :shorten
230230
opts[:shorten] = (datastore['Powershell::method'] != 'old')
231231
end
232-
template_path = Rex::Powershell::Templates::TEMPLATE_DIR
233232

234-
command = Rex::Powershell::Command.cmd_psh_payload(pay,
235-
payload_arch,
236-
template_path,
237-
opts)
233+
template_path = Rex::Powershell::Templates::TEMPLATE_DIR
234+
command = Rex::Powershell::Command.cmd_psh_payload(pay, payload_arch, template_path, opts)
238235
vprint_status("Powershell command length: #{command.length}")
239236

240237
command
241238
end
242239

243-
244240
#
245241
# Useful method cache
246242
#

0 commit comments

Comments
 (0)