finish wiring up the final generation

formating and main generate methods wired up
still need to add some final tests
bug/bundler_fix
David Maloney 2014-02-04 15:52:18 -06:00
parent c8b7dc30b4
commit 70d8246791
2 changed files with 90 additions and 7 deletions

View File

@ -6,9 +6,15 @@ module Msf
class IncompatibleArch < StandardError
end
class IncompatibleEndianess < StandardError
end
class EncoderSpaceViolation < StandardError
end
class InvalidFormat < StandardError
end
class PayloadGenerator
@ -66,6 +72,32 @@ module Msf
raise ArgumentError, "Invalid Format Selected" unless format_is_valid?
end
# @return [String] A string containing the bytes of the payload in the format selected
def generate_payload
if platform == "java" or arch == "java"
generate_java_payload
else
raw_payload = generate_raw_payload
raw_payload = add_shellcode(raw_payload)
encoded_payload = encode_payload(raw_payload)
encoded_payload = prepend_nops(encoded_payload)
format_payload(encoded_payload)
end
end
# @return [String] Java payload as a JAR or WAR file
def generate_java_payload
payload_module = framework.payloads.create(payload)
case format
when "war"
payload_module.generate_war.pack
when "raw"
payload_module.generate_jar.pack
else
raise InvalidFormat, "#{format} is not a valid format for Java payloads"
end
end
# @raise [Msf::IncompatiblePlatform] if no platform was selected for a stdin payload
# @raise [Msf::IncompatibleArch] if no arch was selected for a stdin payload
# @raise [Msf::IncompatiblePlatform] if the platform is incompatible with the payload
@ -115,6 +147,34 @@ module Msf
end
end
# @param shellcode [String] the processed shellcode to be formatted
# @return [String] The final formatted form of the payload
def format_payload(shellcode)
case format.downcase
when "js_be"
if Rex::Arch.endian(arch) != ENDIAN_BIG
raise IncompatibleEndianess, "Big endian format selected for a non big endian payload"
else
::Msf::Simple::Buffer.transform(shellcode, format)
end
when *::Msf::Simple::Buffer.transform_formats
::Msf::Simple::Buffer.transform(shellcode, format)
when *::Msf::Util::EXE.to_executable_fmt_formats
::Msf::Util::EXE.to_executable_fmt(framework, arch, platform, shellcode, format, exe_options)
else
raise InvalidFormat, "you have selected an invalid payload format"
end
end
# @return [Hash] The hash needed for generating an executable format
def exe_options
{
inject: keep,
template_path: File.dirname(template),
template: File.basename(template)
}
end
# @param shellcode [String] The shellcode to encode
# @return [String] The encoded shellcode
def encode_payload(shellcode)
@ -239,7 +299,7 @@ module Msf
# @return [False] if the format is not valid
def format_is_valid?
formats = (::Msf::Util::EXE.to_executable_fmt_formats + ::Msf::Simple::Buffer.transform_formats).uniq
formats.include? format
formats.include? format.downcase
end

View File

@ -111,12 +111,6 @@ describe Msf::PayloadGenerator do
it { should_not raise_error }
end
context 'when not given a format' do
let(:format) { nil }
it { should raise_error(ArgumentError, "Invalid Format Selected") }
end
context 'when given an invalid format' do
let(:format) { "foobar" }
@ -423,6 +417,35 @@ describe Msf::PayloadGenerator do
end
end
context '#format_payload' do
context 'when format is js_be' do
let(:format) { "js_be"}
context 'and arch is x86' do
it 'should raise an IncompatibleEndianess error' do
expect{payload_generator.format_payload(shellcode)}.to raise_error(Msf::IncompatibleEndianess, "Big endian format selected for a non big endian payload")
end
end
end
context 'when format is a transform format' do
let(:format) { 'c' }
it 'applies the appropriate transform format' do
::Msf::Simple::Buffer.should_receive(:transform).with(shellcode, format)
payload_generator.format_payload(shellcode)
end
end
context 'when format is an executable format' do
let(:format) { 'exe' }
it 'applies the appropriate executable format' do
::Msf::Util::EXE.should_receive(:to_executable_fmt).with(framework, arch, platform, shellcode, format, payload_generator.exe_options)
payload_generator.format_payload(shellcode)
end
end
end
end