-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpayload.diff
More file actions
355 lines (328 loc) · 11.9 KB
/
payload.diff
File metadata and controls
355 lines (328 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
Index: lib/msf/core/rpc/module.rb
===================================================================
--- lib/msf/core/rpc/module.rb (revision 9913)
+++ lib/msf/core/rpc/module.rb (working copy)
@@ -174,14 +174,29 @@
def _run_payload(mod, opts)
badchars = [opts['BadChars'] || ''].pack("H*")
+ fmt = opts['Format'] || 'raw'
+ force = opts['ForceEncode'] || false
+ template = opts['Template'] || nil
+ plat = opts['Platform'] || nil
+ keep = opts['KeepTemplateWorking'] || false
+ force = opts['ForceEncode'] || false
+ sled_size = opts['NopSledSize'].to_i || 0
+ iter = opts['Iterations'].to_i || 0
+
begin
res = Msf::Simple::Payload.generate_simple(mod, {
- 'BadChars' => badchars,
+ 'BadChars' => badchars,
'Encoder' => opts['Encoder'],
+ 'Format' => fmt,
'NoComment' => true,
- 'Format' => 'raw',
- 'Options' => opts
+ 'NopSledSize' => sled_size,
+ 'Options' => opts,
+ 'ForceEncode' => force,
+ 'Template' => template,
+ 'Platform' => plat,
+ 'KeepTemplateWorking' => keep,
+ 'Iterations' => iter
})
{"result" => "success", "payload" => res.unpack("H*")[0]}
Index: lib/msf/core/encoded_payload.rb
===================================================================
--- lib/msf/core/encoded_payload.rb (revision 9913)
+++ lib/msf/core/encoded_payload.rb (working copy)
@@ -45,6 +45,7 @@
self.nop_sled = nil
self.encoder = nil
self.nop = nil
+ self.iterations = (reqs['Iterations'].to_i || 1)
# Increase thread priority as necessary. This is done
# to ensure that the encoding and sled generation get
@@ -152,35 +153,40 @@
next
end
-
- # Try encoding with the current encoder
- begin
- self.encoded = self.encoder.encode(self.raw, reqs['BadChars'], nil, pinst.platform)
- rescue EncodingError
- wlog("#{pinst.refname}: Encoder #{encoder.refname} failed: #{$!}", 'core', LEV_1)
- dlog("#{pinst.refname}: Call stack\n#{$@.join("\n")}", 'core', LEV_3)
- next
- rescue ::Exception
- elog("#{pinst.refname}: Broken encoder #{encoder.refname}: #{$!}", 'core', LEV_0)
- dlog("#{pinst.refname}: Call stack\n#{$@.join("\n")}", 'core', LEV_1)
- next
- end
-
- # Get the minimum number of nops to use
- min = (reqs['MinNops'] || 0).to_i
- min = 0 if reqs['DisableNops']
- # Check to see if we have enough room for the minimum requirements
- if ((reqs['Space']) and (reqs['Space'] < self.encoded.length + min))
- wlog("#{pinst.refname}: Encoded payload version is too large with encoder #{encoder.refname}",
- 'core', LEV_1)
+ eout = self.raw.dup
- next
+ # Try encoding with the current encoder
+ 1.upto(self.iterations) do |iter|
+ begin
+ eout = self.encoder.encode(eout, reqs['BadChars'], nil, pinst.platform)
+ rescue EncodingError
+ wlog("#{pinst.refname}: Encoder #{encoder.refname} failed: #{$!}", 'core', LEV_1)
+ dlog("#{pinst.refname}: Call stack\n#{$@.join("\n")}", 'core', LEV_3)
+ next
+ rescue ::Exception
+ elog("#{pinst.refname}: Broken encoder #{encoder.refname}: #{$!}", 'core', LEV_0)
+ dlog("#{pinst.refname}: Call stack\n#{$@.join("\n")}", 'core', LEV_1)
+ next
+ end
+
+ # Get the minimum number of nops to use
+ min = (reqs['MinNops'] || 0).to_i
+ min = 0 if reqs['DisableNops']
+
+ # Check to see if we have enough room for the minimum requirements
+ if ((reqs['Space']) and (reqs['Space'] < eout.length + min))
+ wlog("#{pinst.refname}: Encoded payload version is too large with encoder #{encoder.refname}",
+ 'core', LEV_1)
+
+ next
+ end
+
+ ilog("#{pinst.refname}: Successfully encoded with encoder #{encoder.refname} (size is #{eout.length} on iteration #{iter})",
+ 'core', LEV_0)
+
end
-
- ilog("#{pinst.refname}: Successfully encoded with encoder #{encoder.refname} (size is #{self.encoded.length})",
- 'core', LEV_0)
-
+ self.encoded = eout
break
}
@@ -305,6 +311,10 @@
# The NOP generator that was used
#
attr_reader :nop
+ #
+ # The number of encoding iteartions used
+ #
+ attr_reader :iterations
protected
@@ -315,6 +325,7 @@
attr_writer :payload # :nodoc:
attr_writer :encoder # :nodoc:
attr_writer :nop # :nodoc:
+ attr_writer :iterations # :nodoc:
#
# The payload instance used to generate the payload
Index: lib/msf/base/simple/payload.rb
===================================================================
--- lib/msf/base/simple/payload.rb (revision 9913)
+++ lib/msf/base/simple/payload.rb (working copy)
@@ -27,6 +27,8 @@
# NoComment => Disables prepention of a comment
# NopSledSize => The number of NOPs to use
# MaxSize => The maximum size of the payload.
+ # Iterations => Number of times to encode.
+ # Force => Force encoding.
#
# raises:
#
@@ -38,48 +40,94 @@
# Import any options we may need
payload._import_extra_options(opts)
+ framework = payload.framework
# Generate the payload
e = EncodedPayload.create(payload,
'BadChars' => opts['BadChars'],
'MinNops' => opts['NopSledSize'],
'Encoder' => opts['Encoder'],
+ 'Iterations' => opts['Iterations'],
+ 'ForceEncode' => opts['ForceEncode'],
'Space' => opts['MaxSize'])
fmt = opts['Format'] || 'raw'
+ inject = opts['KeepTemplateWorking'] || false
+ altexe = opts['Template'] || nil
+ arch = payload.arch
+
# Save off the original payload length
len = e.encoded.length
- # Serialize the generated payload to some sort of format
- buf = Buffer.transform(e.encoded, fmt)
- # Prepend a comment
- if (fmt != 'raw' and opts['NoComment'] != true)
- ((ou = payload.options.options_used_to_s(payload.datastore)) and ou.length > 0) ? ou += "\n" : ou = ''
- buf = Buffer.comment(
- "#{payload.refname} - #{len} bytes#{payload.staged? ? " (stage 1)" : ""}\n" +
- "http://www.metasploit.com\n" +
- ((e.encoder) ? "Encoder: #{e.encoder.refname}\n" : '') +
- ((e.nop) ? "NOP gen: #{e.nop.refname}\n" : '') +
- "#{ou}",
- fmt) + buf
+
+ case fmt
+ when 'exe'
+ buf = nil
+ if(not arch or (arch.index(ARCH_X86)))
+ buf = Msf::Util::EXE.to_win32pe(framework, e.encoded , {:insert => inject, :template => altexe})
+ end
- # If it's multistage, include the second stage too
- if payload.staged?
- stage = payload.generate_stage
+ if(arch and (arch.index( ARCH_X86_64 ) or arch.index( ARCH_X64 )))
+ buf = Msf::Util::EXE.to_win64pe(framework, e.encoded, {:insert => inject, :template => altexe})
+ end
- # If a stage was generated, then display it
- if stage and stage.length > 0
- buf +=
- "\n" +
- Buffer.comment(
- "#{payload.refname} - #{stage.length} bytes (stage 2)\n" +
- "http://www.metasploit.com\n",
- fmt) + Buffer.transform(stage, fmt)
- end
+ when 'exe-small'
+ buf = nil
+ if(not arch or (arch.index(ARCH_X86)))
+ buf = Msf::Util::EXE.to_win32pe_old(framework, e.encoded)
end
+ when 'elf'
+ buf = Msf::Util::EXE.to_linux_x86_elf(framework, e.encoded)
+ when 'macho'
+ buf = Msf::Util::EXE.to_osx_x86_macho(framework, e.encoded)
+ when 'vba'
+ exe = nil
+ exe = Msf::Util::EXE.to_win32pe(framework, e.encoded , {:insert => inject, :template => altexe})
+ buf = Msf::Util::EXE.to_exe_vba(exe)
+ when 'vbs'
+ buf = Msf::Util::EXE.to_win32pe_vbs(framework, e.encoded, {:insert => inject, :persist => false, :template => altexe})
+ when 'loop-vbs'
+ buf = Msf::Util::EXE.to_win32pe_vbs(framework, e.encoded, {:insert => inject, :persist => true, :template => altexe})
+ when 'asp'
+ buf = Msf::Util::EXE.to_win32pe_asp(framework, e.encoded , {:insert => inject, :persist => false, :template => altexe})
+ when 'war'
+ plat = Msf::Module::PlatformList.transform(opts['Platform'])
+
+ tmp_plat = plat.platforms
+ buf = Msf::Util::EXE.to_jsp_war(framework, arch, tmp_plat, e.encoded, {:persist => false, :template => altexe})
+ else
+ # Serialize the generated payload to some sort of format
+ buf = Buffer.transform(e.encoded, fmt)
+
+ # Prepend a comment
+ if (fmt != 'raw' and opts['NoComment'] != true)
+ ((ou = payload.options.options_used_to_s(payload.datastore)) and ou.length > 0) ? ou += "\n" : ou = ''
+ buf = Buffer.comment(
+ "#{payload.refname} - #{len} bytes#{payload.staged? ? " (stage 1)" : ""}\n" +
+ "http://www.metasploit.com\n" +
+ ((e.encoder) ? "Encoder: #{e.encoder.refname}\n" : '') +
+ ((e.nop) ? "NOP gen: #{e.nop.refname}\n" : '') +
+ "#{ou}",
+ fmt) + buf
+
+ # If it's multistage, include the second stage too
+ if payload.staged?
+ stage = payload.generate_stage
+
+ # If a stage was generated, then display it
+ if stage and stage.length > 0
+ buf +=
+ "\n" +
+ Buffer.comment(
+ "#{payload.refname} - #{stage.length} bytes (stage 2)\n" +
+ "http://www.metasploit.com\n",
+ fmt) + Buffer.transform(stage, fmt)
+ end
+ end
+ end
end
return buf
@@ -95,4 +143,4 @@
end
end
-end
\ No newline at end of file
+end
Index: lib/msf/ui/console/command_dispatcher/payload.rb
===================================================================
--- lib/msf/ui/console/command_dispatcher/payload.rb (revision 9913)
+++ lib/msf/ui/console/command_dispatcher/payload.rb (working copy)
@@ -16,12 +16,18 @@
@@generate_opts = Rex::Parser::Arguments.new(
"-b" => [ true, "The list of characters to avoid: '\\x00\\xff'" ],
+ "-E" => [ false, "Force encoding." ],
"-e" => [ true, "The name of the encoder module to use." ],
"-h" => [ false, "Help banner." ],
"-o" => [ true, "A comma separated list of options in VAR=VAL format." ],
"-s" => [ true, "NOP sled length." ],
"-f" => [ true, "The output file name (otherwise stdout)" ],
- "-t" => [ true, "The output type: ruby, perl, c, or raw." ])
+ "-t" => [ true, "The output type: c, elf, exe, java, js_le, js_be, perl, raw,
+ ruby, vba, vbs, loop-vbs, asp, war, macho." ],
+ "-p" => [ true, "The Platform for output." ],
+ "-k" => [ false, "Keep the template executable functional" ],
+ "-x" => [ true, "The executable template to use" ],
+ "-i" => [ true, "the number of encoding iterations." ])
#
# Returns the hash of commands specific to payload modules.
@@ -51,6 +57,11 @@
badchars = nil
type = "ruby"
ofile = nil
+ iter = 1
+ force = nil
+ template = nil
+ plat = nil
+ keep = false
@@generate_opts.parse(args) { |opt, idx, val|
case opt
@@ -58,6 +69,8 @@
badchars = Rex::Text.hex_to_raw(val)
when '-e'
encoder_name = val
+ when '-E'
+ force = true
when '-o'
option_str = val
when '-s'
@@ -66,6 +79,14 @@
type = val
when '-f'
ofile = val
+ when '-i'
+ iter = val
+ when '-k'
+ keep = true
+ when '-p'
+ plat = val
+ when '-x'
+ template = val
when '-h'
print(
"Usage: generate [options]\n\n" +
@@ -86,7 +107,12 @@
'Encoder' => encoder_name,
'Format' => type,
'NopSledSize' => sled_size,
- 'OptionStr' => option_str)
+ 'OptionStr' => option_str,
+ 'ForceEncode' => force,
+ 'Template' => template,
+ 'Platform' => plat,
+ 'KeepTemplateWorking' => keep,
+ 'Iterations' => iter)
rescue
log_error("Payload generation failed: #{$!}")
return false
@@ -108,4 +134,4 @@
end
-end end end end
\ No newline at end of file
+end end end end