Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/metasploit/framework/compiler/mingw.rb
Views: 11784
require 'open3'1module Metasploit2module Framework3module Compiler4module Mingw5MINGW_X86 = 'i686-w64-mingw32-gcc'6MINGW_X64 = 'x86_64-w64-mingw32-gcc'78INCLUDE_DIR = File.join(Msf::Config.data_directory, 'headers', 'windows', 'c_payload_util')9UTILITY_DIR = File.join(Msf::Config.data_directory, 'utilities', 'encrypted_payload')10OPTIMIZATION_FLAGS = [ 'Os', 'O0', 'O1', 'O2', 'O3', 'Og' ]1112def compile_c(src)13cmd = build_cmd(src)1415if self.show_compile_cmd16print("#{cmd}\n")17end1819stdin_err, status = Open3.capture2e(cmd)20stdin_err21end2223def build_cmd(src)24src_file = "#{self.file_name}.c"25exe_file = "#{self.file_name}.exe"2627cmd = ''28link_options = '-Wl,'2930File.write(src_file, src)3132opt_level = OPTIMIZATION_FLAGS.include?(self.opt_lvl) ? "-#{self.opt_lvl} " : "-O2 "3334cmd << "#{self.mingw_bin} "35cmd << "#{src_file} -I #{INCLUDE_DIR} "36cmd << "#{self.include_dirs.map { |include_dir| "-iquote #{include_dir}" }.join(' ')} " if self.include_dirs.any?37cmd << "-o #{exe_file} "3839# gives each function its own section40# allowing them to be reordered41cmd << '-ffunction-sections '42cmd << '-fno-asynchronous-unwind-tables '43cmd << '-fno-ident '44cmd << opt_level4546if self.compile_options47cmd << self.compile_options48else49link_options << '--image-base=0x0,'50cmd << '-nostdlib '51end5253link_options << '--no-seh'54link_options << ',-s' if self.strip_syms55link_options << ",-T#{self.link_script}" if self.link_script5657cmd << link_options5859cmd60end6162def cleanup_files63src_file = "#{self.file_name}.c"64exe_file = "#{self.file_name}.exe"6566unless self.keep_src67File.delete(src_file) if File.exist?(src_file)68end6970unless self.keep_exe71File.delete(exe_file) if File.exist?(exe_file)72end73rescue Errno::ENOENT74print_error("Failed to delete file")75end7677class X8678include Mingw7980attr_reader :file_name, :keep_exe, :keep_src, :strip_syms, :link_script, :opt_lvl, :mingw_bin, :compile_options, :show_compile_cmd, :include_dirs8182def initialize(opts={})83@file_name = opts[:f_name]84@keep_exe = opts[:keep_exe]85@keep_src = opts[:keep_src]86@strip_syms = opts[:strip_symbols]87@show_compile_cmd = opts[:show_compile_cmd]88@link_script = opts[:linker_script]89@compile_options = opts[:compile_options]90@opt_lvl = opts[:opt_lvl]91@include_dirs = opts[:include_dirs] || []92@mingw_bin = MINGW_X8693end9495def self.available?96!!(Msf::Util::Helper.which(MINGW_X86))97end98end99100class X64101include Mingw102103attr_reader :file_name, :keep_exe, :keep_src, :strip_syms, :link_script, :opt_lvl, :mingw_bin, :compile_options, :show_compile_cmd, :include_dirs104105def initialize(opts={})106@file_name = opts[:f_name]107@keep_exe = opts[:keep_exe]108@keep_src = opts[:keep_src]109@strip_syms = opts[:strip_symbols]110@show_compile_cmd = opts[:show_compile_cmd]111@link_script = opts[:linker_script]112@compile_options = opts[:compile_options]113@opt_lvl = opts[:opt_lvl]114@include_dirs = opts[:include_dirs] || []115@mingw_bin = MINGW_X64116end117118def self.available?119!!(Msf::Util::Helper.which(MINGW_X64))120end121end122123class UncompilablePayloadError < StandardError124def initialize(msg='')125super(msg)126end127end128129class CompiledPayloadNotFoundError < StandardError130def initialize(msg='Compiled executable not found')131super(msg)132end133end134end135end136end137end138139140