2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2010-05-21 06:20:10 +00:00
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This module exposes a simple method to create an payload in an executable.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
|
|
|
|
module Msf
|
|
|
|
module Exploit::EXE
|
|
|
|
|
2013-09-05 21:16:05 +00:00
|
|
|
def initialize(info = {})
|
|
|
|
super
|
|
|
|
|
|
|
|
# NOTE: Any new options here should also be dealt with in
|
|
|
|
# EncodedPayload#encoded_exe in lib/msf/core/encoded_payload.rb
|
|
|
|
register_advanced_options(
|
|
|
|
[
|
2014-03-28 16:45:28 +00:00
|
|
|
OptBool.new( 'EXE::EICAR', [ false, 'Generate an EICAR file instead of regular payload exe']),
|
2013-09-05 21:16:05 +00:00
|
|
|
OptPath.new( 'EXE::Custom', [ false, 'Use custom exe instead of automatically generating a payload exe']),
|
|
|
|
OptPath.new( 'EXE::Path', [ false, 'The directory in which to look for the executable template' ]),
|
|
|
|
OptPath.new( 'EXE::Template', [ false, 'The executable template file name.' ]),
|
2013-09-21 12:13:18 +00:00
|
|
|
OptBool.new( 'EXE::Inject', [ false, 'Set to preserve the original EXE function' ]),
|
|
|
|
OptBool.new( 'EXE::OldMethod',[ false, 'Set to use the substitution EXE generation method.' ]),
|
|
|
|
OptBool.new( 'EXE::FallBack', [ false, 'Use the default template in case the specified one is missing' ]),
|
2014-03-28 16:45:28 +00:00
|
|
|
OptBool.new( 'MSI::EICAR', [ false, 'Generate an EICAR file instead of regular payload msi']),
|
2013-09-05 21:16:05 +00:00
|
|
|
OptPath.new( 'MSI::Custom', [ false, 'Use custom msi instead of automatically generating a payload msi']),
|
|
|
|
OptPath.new( 'MSI::Path', [ false, 'The directory in which to look for the msi template' ]),
|
|
|
|
OptPath.new( 'MSI::Template', [ false, 'The msi template file name' ]),
|
2013-09-21 12:13:18 +00:00
|
|
|
OptBool.new( 'MSI::UAC', [ false, 'Create an MSI with a UAC prompt (elevation to SYSTEM if accepted)' ])
|
2013-09-05 21:16:05 +00:00
|
|
|
], self.class)
|
|
|
|
end
|
|
|
|
|
2014-03-28 16:45:28 +00:00
|
|
|
# Avoid stating the string directly, don't want to get caught by local
|
|
|
|
# antivirus!
|
|
|
|
def get_eicar_exe
|
|
|
|
obfus_eicar = ["x5o!p%@ap[4\\pzx54(p^)7cc)7}$eicar", "standard", "antivirus", "test", "file!$h+h*"]
|
|
|
|
obfus_eicar.join("-").upcase
|
|
|
|
end
|
|
|
|
|
2014-11-06 04:10:54 +00:00
|
|
|
def get_custom_exe(path = nil)
|
2013-09-05 21:16:05 +00:00
|
|
|
path ||= datastore['EXE::Custom']
|
|
|
|
print_status("Using custom payload #{path}, RHOST and RPORT settings will be ignored!")
|
|
|
|
datastore['DisablePayloadHandler'] = true
|
2014-11-06 19:31:37 +00:00
|
|
|
exe = nil
|
2014-10-23 06:10:38 +00:00
|
|
|
::File.open(path,'rb') {|f| exe = f.read(f.stat.size)}
|
2013-09-05 21:16:05 +00:00
|
|
|
exe
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_payload_exe(opts = {})
|
|
|
|
return get_custom_exe if datastore.include? 'EXE::Custom'
|
2014-04-01 14:05:55 +00:00
|
|
|
return get_eicar_exe if datastore['EXE::EICAR']
|
2013-09-05 21:16:05 +00:00
|
|
|
|
|
|
|
exe_init_options(opts)
|
|
|
|
|
|
|
|
pl = opts[:code]
|
|
|
|
pl ||= payload.encoded
|
|
|
|
|
|
|
|
# Fall back to x86...
|
2014-10-23 06:10:38 +00:00
|
|
|
opts[:arch] = [ARCH_X86] if !opts[:arch] || opts[:arch].length < 1
|
|
|
|
|
2013-09-05 21:16:05 +00:00
|
|
|
# Ensure we have an array
|
2014-10-23 06:10:38 +00:00
|
|
|
opts[:arch] = [opts[:arch]] unless opts[:arch].kind_of? Array
|
2013-09-05 21:16:05 +00:00
|
|
|
|
|
|
|
# Transform the PlatformList
|
2014-10-23 06:10:38 +00:00
|
|
|
if opts[:platform].kind_of? Msf::Module::PlatformList
|
2013-09-05 21:16:05 +00:00
|
|
|
opts[:platform] = opts[:platform].platforms
|
|
|
|
end
|
|
|
|
|
|
|
|
exe = Msf::Util::EXE.to_executable(framework, opts[:arch], opts[:platform], pl, opts)
|
|
|
|
exe_post_generation(opts)
|
|
|
|
exe
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_payload_exe_service(opts = {})
|
|
|
|
return get_custom_exe if datastore.include? 'EXE::Custom'
|
2014-04-01 14:05:55 +00:00
|
|
|
return get_eicar_exe if datastore['EXE::EICAR']
|
2013-09-05 21:16:05 +00:00
|
|
|
|
|
|
|
exe_init_options(opts)
|
|
|
|
|
|
|
|
# NOTE: Only Windows is supported here.
|
|
|
|
pl = opts[:code]
|
|
|
|
pl ||= payload.encoded
|
|
|
|
|
2013-11-20 20:10:47 +00:00
|
|
|
#Ensure opts[:arch] is an array
|
|
|
|
opts[:arch] = [opts[:arch]] unless opts[:arch].kind_of? Array
|
|
|
|
|
2014-10-23 06:10:38 +00:00
|
|
|
if opts[:arch] && (opts[:arch].index(ARCH_X64) or opts[:arch].index(ARCH_X86_64))
|
2013-09-05 21:16:05 +00:00
|
|
|
exe = Msf::Util::EXE.to_win64pe_service(framework, pl, opts)
|
|
|
|
else
|
|
|
|
exe = Msf::Util::EXE.to_win32pe_service(framework, pl, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
exe_post_generation(opts)
|
|
|
|
exe
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_payload_dll(opts = {})
|
|
|
|
return get_custom_exe if datastore.include? 'EXE::Custom'
|
2014-04-01 14:05:55 +00:00
|
|
|
return get_eicar_exe if datastore['EXE::EICAR']
|
2013-09-05 21:16:05 +00:00
|
|
|
|
|
|
|
exe_init_options(opts)
|
2014-06-19 16:46:05 +00:00
|
|
|
plat = opts[:platform]
|
2013-09-05 21:16:05 +00:00
|
|
|
pl = opts[:code]
|
|
|
|
pl ||= payload.encoded
|
|
|
|
|
2013-11-20 20:10:47 +00:00
|
|
|
#Ensure opts[:arch] is an array
|
|
|
|
opts[:arch] = [opts[:arch]] unless opts[:arch].kind_of? Array
|
|
|
|
|
2014-06-19 16:46:05 +00:00
|
|
|
# NOTE: Only x86_64 linux is supported here.
|
2014-10-23 06:10:38 +00:00
|
|
|
if plat.index(Msf::Module::Platform::Linux)
|
|
|
|
if opts[:arch] && (opts[:arch].index(ARCH_X64) || opts[:arch].index(ARCH_X86_64))
|
2014-06-19 16:46:05 +00:00
|
|
|
dll = Msf::Util::EXE.to_linux_x64_elf_dll(framework, pl,opts)
|
|
|
|
end
|
2014-10-23 06:10:38 +00:00
|
|
|
elsif plat.index(Msf::Module::Platform::Windows)
|
|
|
|
if opts[:arch] && (opts[:arch].index(ARCH_X64) || opts[:arch].index(ARCH_X86_64))
|
2014-06-19 16:46:05 +00:00
|
|
|
dll = Msf::Util::EXE.to_win64pe_dll(framework, pl, opts)
|
|
|
|
else
|
|
|
|
dll = Msf::Util::EXE.to_win32pe_dll(framework, pl, opts)
|
|
|
|
end
|
2013-09-05 21:16:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
exe_post_generation(opts)
|
|
|
|
dll
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_payload_msi(opts = {})
|
|
|
|
return get_custom_exe(datastore['MSI::Custom']) if datastore.include? 'MSI::Custom'
|
2014-04-01 14:05:55 +00:00
|
|
|
return get_eicar_exe if datastore['MSI::EICAR']
|
2013-09-05 21:16:05 +00:00
|
|
|
|
|
|
|
exe = generate_payload_exe(opts)
|
|
|
|
|
|
|
|
opts.merge! ({
|
|
|
|
:msi_template => datastore['MSI::Template'],
|
|
|
|
:msi_template_path => datastore['MSI::Path'],
|
2013-09-29 16:08:04 +00:00
|
|
|
:uac => datastore['MSI::UAC']
|
2013-09-05 21:16:05 +00:00
|
|
|
})
|
|
|
|
|
2014-10-23 06:10:38 +00:00
|
|
|
Msf::Util::EXE.to_exe_msi(framework, exe, opts)
|
2013-09-05 21:16:05 +00:00
|
|
|
end
|
2013-03-10 20:54:37 +00:00
|
|
|
|
2010-09-21 00:13:30 +00:00
|
|
|
protected
|
2013-08-30 21:28:33 +00:00
|
|
|
def exe_init_options(opts)
|
|
|
|
opts.merge!(
|
|
|
|
{
|
|
|
|
:template_path => datastore['EXE::Path'],
|
|
|
|
:template => datastore['EXE::Template'],
|
|
|
|
:inject => datastore['EXE::Inject'],
|
|
|
|
:fallback => datastore['EXE::FallBack'],
|
|
|
|
:sub_method => datastore['EXE::OldMethod']
|
|
|
|
})
|
|
|
|
|
|
|
|
# Prefer the target's platform/architecture information, but use
|
|
|
|
# the module's if no target specific information exists
|
2013-11-20 20:28:00 +00:00
|
|
|
opts[:platform] ||= payload_instance.platform if self.respond_to? :payload_instance
|
2013-08-30 21:28:33 +00:00
|
|
|
opts[:platform] ||= target_platform if self.respond_to? :target_platform
|
|
|
|
opts[:platform] ||= platform if self.respond_to? :platform
|
2013-11-20 20:14:59 +00:00
|
|
|
opts[:arch] ||= payload_instance.arch if self.respond_to? :payload_instance
|
2013-08-30 21:28:33 +00:00
|
|
|
opts[:arch] ||= target_arch if self.respond_to? :target_arch
|
|
|
|
opts[:arch] ||= arch if self.respond_to? :arch
|
|
|
|
end
|
|
|
|
|
|
|
|
def exe_post_generation(opts)
|
2014-11-06 04:10:54 +00:00
|
|
|
if opts[:fellback]
|
2013-08-30 21:28:33 +00:00
|
|
|
print_status("Warning: Falling back to default template: #{opts[:fellback]}")
|
|
|
|
end
|
|
|
|
end
|
2010-05-21 06:20:10 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|