Module: Metasploit::Framework::Compiler::Mingw

Included in:
X64, X86
Defined in:
lib/metasploit/framework/compiler/mingw.rb

Defined Under Namespace

Classes: CompiledPayloadNotFoundError, UncompilablePayloadError, X64, X86

Constant Summary collapse

MINGW_X86 =
'i686-w64-mingw32-gcc'
MINGW_X64 =
'x86_64-w64-mingw32-gcc'
INCLUDE_DIR =
File.join(Msf::Config.data_directory, 'headers', 'windows', 'c_payload_util')
UTILITY_DIR =
File.join(Msf::Config.data_directory, 'utilities', 'encrypted_payload')

Instance Method Summary collapse

Instance Method Details

#build_cmd(src) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/metasploit/framework/compiler/mingw.rb', line 21

def build_cmd(src)
  src_file = "#{self.file_name}.c"
  exe_file = "#{self.file_name}.exe"

  cmd = ''
  link_options = '-Wl,'

  File.write(src_file, src)

  opt_level = [ 'Os', 'O0', 'O1', 'O2', 'O3', 'Og' ].include?(self.opt_lvl) ? "-#{self.opt_lvl} " : "-O2 "

  cmd << "#{self.mingw_bin} "
  cmd << "#{src_file} -I #{INCLUDE_DIR} "
  cmd << "-o #{exe_file} "

  # gives each function its own section
  # allowing them to be reordered
  cmd << '-ffunction-sections '
  cmd << '-fno-asynchronous-unwind-tables '
  cmd << '-nostdlib '
  cmd << '-fno-ident '
  cmd << opt_level

  link_options << '--no-seh,'
  link_options << '-s,' if self.strip_syms
  link_options << "-T#{self.link_script}" if self.link_script

  cmd << link_options

  cmd
end

#cleanup_filesObject


53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/metasploit/framework/compiler/mingw.rb', line 53

def cleanup_files
  src_file = "#{self.file_name}.c"
  exe_file = "#{self.file_name}.exe"

  unless self.keep_src
    File.delete(src_file) if File.exist?(src_file)
  end

  unless self.keep_exe
    File.delete(exe_file) if File.exist?(exe_file)
  end
rescue Errno::ENOENT
  print_error("Failed to delete file")
end

#compile_c(src) ⇒ Object


14
15
16
17
18
19
# File 'lib/metasploit/framework/compiler/mingw.rb', line 14

def compile_c(src)
  cmd = build_cmd(src)

  stdin_err, status = Open3.capture2e(cmd)
  stdin_err
end