Skip to content

Commit 84060bb

Browse files
author
Brent Cook
committed
Land rapid7#5370, support specifying maximum encoder space with msfvenom
2 parents 5d085a3 + fb43ef4 commit 84060bb

File tree

2 files changed

+39
-29
lines changed

2 files changed

+39
-29
lines changed

lib/msf/core/payload_generator.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class PayloadGenerator
6464
# @!attribute space
6565
# @return [Fixnum] The maximum size in bytes of the payload
6666
attr_accessor :space
67+
# @!attribute encoder_space
68+
# @return [Fixnum] The maximum size in bytes of the encoded payload
69+
attr_accessor :encoder_space
6770
# @!attribute stdin
6871
# @return [String] The raw bytes of a payload taken from STDIN
6972
attr_accessor :stdin
@@ -85,6 +88,7 @@ class PayloadGenerator
8588
# @option opts [String] :badchars (see #badchars)
8689
# @option opts [String] :template (see #template)
8790
# @option opts [Fixnum] :space (see #space)
91+
# @option opts [Fixnum] :encoder_space (see #encoder_space)
8892
# @option opts [Fixnum] :nops (see #nops)
8993
# @option opts [String] :add_code (see #add_code)
9094
# @option opts [Boolean] :keep (see #keep)
@@ -109,6 +113,7 @@ def initialize(opts={})
109113
@stdin = opts.fetch(:stdin, nil)
110114
@template = opts.fetch(:template, '')
111115
@var_name = opts.fetch(:var_name, 'buf')
116+
@encoder_space = opts.fetch(:encoder_space, @space)
112117

113118
@framework = opts.fetch(:framework)
114119

@@ -200,7 +205,7 @@ def encode_payload(shellcode)
200205
encoder_list.each do |encoder_mod|
201206
cli_print "Attempting to encode payload with #{iterations} iterations of #{encoder_mod.refname}"
202207
begin
203-
encoder_mod.available_space = @space
208+
encoder_mod.available_space = @encoder_space
204209
return run_encoder(encoder_mod, shellcode.dup)
205210
rescue ::Msf::EncoderSpaceViolation => e
206211
cli_print "#{encoder_mod.refname} failed with #{e.message}"
@@ -395,7 +400,7 @@ def run_encoder(encoder_module, shellcode)
395400
iterations.times do |x|
396401
shellcode = encoder_module.encode(shellcode.dup, badchars, nil, platform_list)
397402
cli_print "#{encoder_module.refname} succeeded with size #{shellcode.length} (iteration=#{x})"
398-
if shellcode.length > space
403+
if shellcode.length > encoder_space
399404
raise EncoderSpaceViolation, "encoder has made a buffer that is too big"
400405
end
401406
end

msfvenom

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,90 +58,95 @@ require 'msf/core/payload_generator'
5858
opt.separator('')
5959
opt.separator('Options:')
6060

61-
opt.on('-p', '--payload <payload>', String, 'Payload to use. Specify a \'-\' or stdin to use custom payloads') do |p|
61+
opt.on('-p', '--payload <payload>', String,
62+
'Payload to use. Specify a \'-\' or stdin to use custom payloads') do |p|
6263
if p == '-'
6364
opts[:payload] = 'stdin'
6465
else
6566
opts[:payload] = p
6667
end
6768
end
6869

69-
opt.on('-l', '--list [module_type]', Array, 'List a module type. Options are: payloads, encoders, nops, all') do |l|
70+
opt.on('--payload-options', "List the payload's standard options") do
71+
opts[:list_options] = true
72+
end
73+
74+
opt.on('-l', '--list [type]', Array, 'List a module type. Options are: payloads, encoders, nops, all') do |l|
7075
if l.nil? or l.empty?
7176
l = ["all"]
7277
end
7378
opts[:list] = l
7479
end
7580

76-
opt.on('-n', '--nopsled <length>', Integer, 'Prepend a nopsled of [length] size on to the payload') do |n|
81+
opt.on('-n', '--nopsled <length>', Integer, 'Prepend a nopsled of [length] size on to the payload') do |n|
7782
opts[:nops] = n.to_i
7883
end
7984

80-
opt.on('-f', '--format <format>', String, "Output format (use --help-formats for a list)") do |f|
85+
opt.on('-f', '--format <format>', String, "Output format (use --help-formats for a list)") do |f|
8186
opts[:format] = f
8287
end
8388

84-
opt.on('-e', '--encoder [encoder]', String, 'The encoder to use') do |e|
89+
opt.on('--help-formats', String, "List available formats") do
90+
init_framework(:module_types => [])
91+
msg = "Executable formats\n" +
92+
"\t" + ::Msf::Util::EXE.to_executable_fmt_formats.join(", ") + "\n" +
93+
"Transform formats\n" +
94+
"\t" + ::Msf::Simple::Buffer.transform_formats.join(", ")
95+
raise UsageError, msg
96+
end
97+
98+
opt.on('-e', '--encoder <encoder>', String, 'The encoder to use') do |e|
8599
opts[:encoder] = e
86100
end
87101

88-
opt.on('-a', '--arch <architecture>', String, 'The architecture to use') do |a|
102+
opt.on('-a', '--arch <arch>', String, 'The architecture to use') do |a|
89103
opts[:arch] = a
90104
end
91105

92-
opt.on('--platform <platform>', String, 'The platform of the payload') do |l|
106+
opt.on('--platform <platform>', String, 'The platform of the payload') do |l|
93107
opts[:platform] = l
94108
end
95109

96-
opt.on('-s', '--space <length>', Integer, 'The maximum size of the resulting payload') do |s|
110+
opt.on('-s', '--space <length>', Integer, 'The maximum size of the resulting payload') do |s|
97111
opts[:space] = s
98112
end
99113

100-
opt.on('-b', '--bad-chars <list>', String, 'The list of characters to avoid example: \'\x00\xff\'') do |b|
114+
opt.on('--encoder-space <length>', Integer, 'The maximum size of the encoded payload (defaults to the -s value)') do |s|
115+
opts[:encoder_space] = s
116+
end
117+
118+
opt.on('-b', '--bad-chars <list>', String, 'The list of characters to avoid example: \'\x00\xff\'') do |b|
101119
opts[:badchars] = Rex::Text.hex_to_raw(b)
102120
end
103121

104-
opt.on('-i', '--iterations <count>', Integer, 'The number of times to encode the payload') do |i|
122+
opt.on('-i', '--iterations <count>', Integer, 'The number of times to encode the payload') do |i|
105123
opts[:iterations] = i
106124
end
107125

108-
opt.on('-c', '--add-code <path>', String, 'Specify an additional win32 shellcode file to include') do |x|
126+
opt.on('-c', '--add-code <path>', String, 'Specify an additional win32 shellcode file to include') do |x|
109127
opts[:add_code] = x
110128
end
111129

112-
opt.on('-x', '--template <path>', String, 'Specify a custom executable file to use as a template') do |x|
130+
opt.on('-x', '--template <path>', String, 'Specify a custom executable file to use as a template') do |x|
113131
opts[:template] = x
114132
end
115133

116134
opt.on('-k', '--keep', 'Preserve the template behavior and inject the payload as a new thread') do
117135
opts[:keep] = true
118136
end
119137

120-
opt.on('--payload-options', "List the payload's standard options") do
121-
opts[:list_options] = true
122-
end
123-
124-
opt.on('-o', '--out <path>', 'Save the payload') do |x|
138+
opt.on('-o', '--out <path>', 'Save the payload') do |x|
125139
opts[:out] = x
126140
end
127141

128-
opt.on('-v', '--var-name <name>', String, 'Specify a custom variable name to use for certain output formats') do |x|
142+
opt.on('-v', '--var-name <name>', String, 'Specify a custom variable name to use for certain output formats') do |x|
129143
opts[:var_name] = x
130144
end
131145

132146
opt.on_tail('-h', '--help', 'Show this message') do
133147
raise UsageError, "#{opt}"
134148
end
135149

136-
opt.on_tail('--help-formats', String, "List available formats") do
137-
init_framework(:module_types => [])
138-
msg = "Executable formats\n" +
139-
"\t" + ::Msf::Util::EXE.to_executable_fmt_formats.join(", ") + "\n" +
140-
"Transform formats\n" +
141-
"\t" + ::Msf::Simple::Buffer.transform_formats.join(", ")
142-
raise UsageError, msg
143-
end
144-
145150
begin
146151
opt.parse!(args)
147152
rescue OptionParser::InvalidOption => e

0 commit comments

Comments
 (0)