Skip to content

Commit c786852

Browse files
committed
Land rapid7#4403, msfvenom configurable variable name
2 parents 2604746 + 513fd12 commit c786852

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

lib/msf/base/simple/buffer.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,39 @@ module Buffer
1818
# Serializes a buffer to a provided format. The formats supported are raw,
1919
# num, dword, ruby, python, perl, bash, c, js_be, js_le, java and psh
2020
#
21-
def self.transform(buf, fmt = "ruby")
21+
def self.transform(buf, fmt = "ruby", var_name = 'buf')
22+
default_wrap = 60
23+
2224
case fmt
2325
when 'raw'
2426
when 'num'
2527
buf = Rex::Text.to_num(buf)
2628
when 'dword', 'dw'
2729
buf = Rex::Text.to_dword(buf)
2830
when 'python', 'py'
29-
buf = Rex::Text.to_python(buf)
31+
buf = Rex::Text.to_python(buf, default_wrap, var_name)
3032
when 'ruby', 'rb'
31-
buf = Rex::Text.to_ruby(buf)
33+
buf = Rex::Text.to_ruby(buf, default_wrap, var_name)
3234
when 'perl', 'pl'
33-
buf = Rex::Text.to_perl(buf)
35+
buf = Rex::Text.to_perl(buf, default_wrap, var_name)
3436
when 'bash', 'sh'
35-
buf = Rex::Text.to_bash(buf)
37+
buf = Rex::Text.to_bash(buf, default_wrap, var_name)
3638
when 'c'
37-
buf = Rex::Text.to_c(buf)
39+
buf = Rex::Text.to_c(buf, default_wrap, var_name)
3840
when 'csharp'
39-
buf = Rex::Text.to_csharp(buf)
41+
buf = Rex::Text.to_csharp(buf, default_wrap, var_name)
4042
when 'js_be'
4143
buf = Rex::Text.to_unescape(buf, ENDIAN_BIG)
4244
when 'js_le'
4345
buf = Rex::Text.to_unescape(buf, ENDIAN_LITTLE)
4446
when 'java'
45-
buf = Rex::Text.to_java(buf)
47+
buf = Rex::Text.to_java(buf, var_name)
4648
when 'powershell', 'ps1'
47-
buf = Rex::Text.to_powershell(buf)
49+
buf = Rex::Text.to_powershell(buf, var_name)
4850
when 'vbscript'
49-
buf = Rex::Text.to_vbscript(buf)
51+
buf = Rex::Text.to_vbscript(buf, var_name)
5052
when 'vbapplication'
51-
buf = Rex::Text.to_vbapplication(buf)
53+
buf = Rex::Text.to_vbapplication(buf, var_name)
5254
else
5355
raise ArgumentError, "Unsupported buffer format: #{fmt}", caller
5456
end

lib/msf/core/payload_generator.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class PayloadGenerator
7070
# @!attribute template
7171
# @return [String] The path to an executable template to use
7272
attr_accessor :template
73+
# @!attribute var_name
74+
# @return [String] The custom variable string for certain output formats
75+
attr_accessor :var_name
7376

7477

7578
# @param opts [Hash] The options hash
@@ -105,6 +108,7 @@ def initialize(opts={})
105108
@space = opts.fetch(:space, 1.gigabyte)
106109
@stdin = opts.fetch(:stdin, nil)
107110
@template = opts.fetch(:template, '')
111+
@var_name = opts.fetch(:var_name, 'buf')
108112

109113
@framework = opts.fetch(:framework)
110114

@@ -213,10 +217,10 @@ def format_payload(shellcode)
213217
if Rex::Arch.endian(arch) != ENDIAN_BIG
214218
raise IncompatibleEndianess, "Big endian format selected for a non big endian payload"
215219
else
216-
::Msf::Simple::Buffer.transform(shellcode, format)
220+
::Msf::Simple::Buffer.transform(shellcode, format, @var_name)
217221
end
218222
when *::Msf::Simple::Buffer.transform_formats
219-
::Msf::Simple::Buffer.transform(shellcode, format)
223+
::Msf::Simple::Buffer.transform(shellcode, format, @var_name)
220224
when *::Msf::Util::EXE.to_executable_fmt_formats
221225
::Msf::Util::EXE.to_executable_fmt(framework, arch, platform_list, shellcode, format, exe_options)
222226
else

msfvenom

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ require 'msf/core/payload_generator'
121121
opts[:list_options] = true
122122
end
123123

124+
opt.on('-v', '--var-name <name>', String, 'Specify a custom variable name to use for certain output formats') do |x|
125+
opts[:var_name] = x
126+
end
127+
124128
opt.on_tail('-h', '--help', 'Show this message') do
125129
raise UsageError, "#{opt}"
126130
end

spec/lib/msf/core/payload_generator_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
reference_name: 'x86/shikata_ga_nai'
6363
)
6464
}
65+
let(:var_name) { 'buf' }
6566

6667
subject(:payload_generator) {
6768
described_class.new(generator_opts)
@@ -482,7 +483,7 @@
482483
let(:format) { 'c' }
483484

484485
it 'applies the appropriate transform format' do
485-
::Msf::Simple::Buffer.should_receive(:transform).with(shellcode, format)
486+
::Msf::Simple::Buffer.should_receive(:transform).with(shellcode, format, var_name)
486487
payload_generator.format_payload(shellcode)
487488
end
488489
end

0 commit comments

Comments
 (0)