Path: blob/master/lib/msf/util/exe/windows/x64.rb
57477 views
# -*- coding: binary -*-1module Msf::Util::EXE::Windows::X642include Msf::Util::EXE::Common3include Msf::Util::EXE::Windows::Common45def self.included(base)6base.extend(ClassMethods)7end89module ClassMethods10# Construct a Windows x64 PE executable with the given shellcode.11# to_win64pe12#13# @param framework [Msf::Framework] The Metasploit framework instance.14# @param code [String] The shellcode to embed in the executable.15# @param opts [Hash] Additional options.16# @return [String] The constructed PE executable as a binary string.1718def to_win64pe(framework, code, opts = {})19# Use the standard template if not specified by the user.20# This helper finds the full path and stores it in opts[:template].21set_template_default(opts, 'template_x64_windows.exe')2223# Try to inject code into executable by adding a section without affecting executable behavior24if opts[:inject]25injector = Msf::Exe::SegmentInjector.new({26:payload => code,27:template => opts[:template],28:arch => :x64,29:secname => opts[:secname]30})31return injector.generate_pe32end3334# Append a new section instead35appender = Msf::Exe::SegmentAppender.new({36:payload => code,37:template => opts[:template],38:arch => :x64,39:secname => opts[:secname]40})41return appender.generate_pe42end4344# to_win64pe45#46# @param framework [Msf::Framework] The framework of you want to use47# @param code [String]48# @param opts [Hash]49# @return [String]50def to_win64pe(framework, code, opts = {})51# Allow the user to specify their own EXE template52set_template_default(opts, "template_x64_windows.exe")5354# Try to inject code into executable by adding a section without affecting executable behavior55if opts[:inject]56injector = Msf::Exe::SegmentInjector.new({57:payload => code,58:template => opts[:template],59:arch => :x64,60:secname => opts[:secname]61})62return injector.generate_pe63end6465# Append a new section instead66appender = Msf::Exe::SegmentAppender.new({67:payload => code,68:template => opts[:template],69:arch => :x64,70:secname => opts[:secname]71})72return appender.generate_pe73end7475# to_win64pe_service76#77# @param framework [Msf::Framework] The framework of you want to use78# @param code [String]79# @param opts [Hash]80# @option [String] :exe_type81# @option [String] :service_exe82# @option [String] :dll83# @option [String] :inject84# @return [String]85def to_win64pe_service(framework, code, opts = {})86# Allow the user to specify their own service EXE template87set_template_default(opts, "template_x64_windows_svc.exe")88opts[:exe_type] = :service_exe89exe_sub_method(code,opts)90end9192# to_win64pe_dll93#94# @param framework [Msf::Framework] The framework of you want to use95# @param code [String]96# @param opts [Hash]97# @option [String] :exe_type98# @option [String] :dll99# @option [String] :inject100# @return [String]101def to_win64pe_dll(framework, code, opts = {})102flavor = opts.fetch(:mixed_mode, false) ? 'mixed_mode' : nil103set_template_default_winpe_dll(opts, ARCH_X64, code.size, flavor: flavor)104105opts[:exe_type] = :dll106107if opts[:inject]108raise RuntimeError, 'Template injection unsupported for x64 DLLs'109else110exe_sub_method(code,opts)111end112end113114# to_win64pe_dccw_gdiplus_dll115#116# @param framework [Msf::Framework] The framework of you want to use117# @param code [String]118# @param opts [Hash]119# @option [String] :exe_type120# @option [String] :dll121# @option [String] :inject122# @return [String]123def to_win64pe_dccw_gdiplus_dll(framework, code, opts = {})124set_template_default_winpe_dll(opts, ARCH_X64, code.size, flavor: 'dccw_gdiplus')125to_win64pe_dll(framework, code, opts)126end127end128class << self129include ClassMethods130end131end132133134