@@ -18,22 +18,23 @@ def initialize(info = {})
18
18
OptBool . new ( 'Powershell::encode_inner_payload' , [ true , 'Encode inner payload for -EncodedCommand' , false ] ) ,
19
19
OptBool . new ( 'Powershell::use_single_quotes' , [ true , 'Wraps the -Command argument in single quotes' , false ] ) ,
20
20
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
+ )
23
24
end
24
25
25
26
#
26
27
# Return a script from path or string
27
28
#
28
29
def read_script ( script_path )
29
- return Rex ::Powershell ::Script . new ( script_path )
30
+ Rex ::Powershell ::Script . new ( script_path )
30
31
end
31
32
32
33
#
33
34
# Return an array of substitutions for use in make_subs
34
35
#
35
36
def process_subs ( subs )
36
- return [ ] if subs . nil? or subs . empty?
37
+ return [ ] if subs . nil? || subs . empty?
37
38
new_subs = [ ]
38
39
subs . split ( ';' ) . each do |set |
39
40
new_subs << set . split ( ',' , 2 )
@@ -49,11 +50,12 @@ def process_subs(subs)
49
50
#
50
51
def make_subs ( script , subs )
51
52
subs . each do |set |
52
- script . gsub! ( set [ 0 ] , set [ 1 ] )
53
+ script . gsub! ( set [ 0 ] , set [ 1 ] )
53
54
end
54
55
55
56
script
56
57
end
58
+
57
59
#
58
60
# Return an encoded powershell script
59
61
# Will invoke PSH modifiers as enabled
@@ -71,6 +73,20 @@ def encode_script(script_in, eof = nil)
71
73
Rex ::Powershell ::Command . encode_script ( script_in , eof , opts )
72
74
end
73
75
76
+ #
77
+ # Return an decoded powershell script
78
+ #
79
+ # @param script_in [String] Encoded contents
80
+ #
81
+ # @return [String] Decoded script
82
+ def decode_script ( script_in )
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 )
88
+ end
89
+
74
90
#
75
91
# Return a gzip compressed powershell script
76
92
# Will invoke PSH modifiers as enabled
@@ -79,7 +95,7 @@ def encode_script(script_in, eof = nil)
79
95
# @param eof [String] Marker to indicate the end of file appended to script
80
96
#
81
97
# @return [String] Compressed script with decompression stub
82
- def compress_script ( script_in , eof = nil )
98
+ def compress_script ( script_in , eof = nil )
83
99
opts = { }
84
100
datastore . select { |k , v | k =~ /^Powershell::(strip|sub)/ && v } . keys . map do |k |
85
101
mod_method = k . split ( '::' ) . last . intern
@@ -89,6 +105,18 @@ def compress_script(script_in, eof=nil)
89
105
Rex ::Powershell ::Command . compress_script ( script_in , eof , opts )
90
106
end
91
107
108
+ #
109
+ # Return a decompressed powershell sript
110
+ #
111
+ # @param script_in [String] Compressed contents with decompression stub
112
+ #
113
+ # @return [String] Decompressed script
114
+ def decompress_script ( script_in )
115
+ return script_in unless script_in . match? ( /FromBase64String/ )
116
+
117
+ Rex ::Powershell ::Command . decompress_script ( script_in )
118
+ end
119
+
92
120
#
93
121
# Generate a powershell command line, options are passed on to
94
122
# generate_psh_args
@@ -155,8 +183,8 @@ def generate_psh_args(opts)
155
183
# @return [String] Wrapped powershell code
156
184
def run_hidden_psh ( ps_code , payload_arch , encoded )
157
185
arg_opts = {
158
- noprofile : true ,
159
- windowstyle : 'hidden' ,
186
+ noprofile : true ,
187
+ windowstyle : 'hidden'
160
188
}
161
189
162
190
# Old technique fails if powershell exits..
@@ -194,26 +222,21 @@ def run_hidden_psh(ps_code, payload_arch, encoded)
194
222
def cmd_psh_payload ( pay , payload_arch , opts = { } )
195
223
options . validate ( datastore )
196
224
197
- [ :persist , :prepend_sleep , :exec_in_place , :encode_final_payload ,
198
- :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 |
199
226
opts [ opt ] ||= datastore [ "Powershell::#{ opt } " ]
200
- }
227
+ end
201
228
202
229
unless opts . key? :shorten
203
230
opts [ :shorten ] = ( datastore [ 'Powershell::method' ] != 'old' )
204
231
end
205
- template_path = Rex ::Powershell ::Templates ::TEMPLATE_DIR
206
232
207
- command = Rex ::Powershell ::Command . cmd_psh_payload ( pay ,
208
- payload_arch ,
209
- template_path ,
210
- opts )
233
+ template_path = Rex ::Powershell ::Templates ::TEMPLATE_DIR
234
+ command = Rex ::Powershell ::Command . cmd_psh_payload ( pay , payload_arch , template_path , opts )
211
235
vprint_status ( "Powershell command length: #{ command . length } " )
212
236
213
237
command
214
238
end
215
239
216
-
217
240
#
218
241
# Useful method cache
219
242
#
0 commit comments