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')
OPTIMIZATION_FLAGS =
[ 'Os', 'O0', 'O1', 'O2', 'O3', 'Og' ]

Instance Method Summary collapse

Instance Method Details

#build_cmd(src) ⇒ Object



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
52
53
54
55
56
57
58
59
60
61
# File 'lib/metasploit/framework/compiler/mingw.rb', line 24

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 = OPTIMIZATION_FLAGS.include?(self.opt_lvl) ? "-#{self.opt_lvl} " : "-O2 "

  cmd << "#{self.mingw_bin} "
  cmd << "#{src_file} -I #{INCLUDE_DIR} "
  cmd << "#{self.include_dirs.map { |include_dir| "-iquote #{include_dir}" }.join(' ')} " if self.include_dirs.any?
  cmd << "-o #{exe_file} "

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

  if self.compile_options
    cmd << self.compile_options
  else
    link_options << '--image-base=0x0,'
    cmd << '-nostdlib '
  end

  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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/metasploit/framework/compiler/mingw.rb', line 63

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



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

def compile_c(src)
  cmd = build_cmd(src)

  if self.show_compile_cmd
    print("#{cmd}\n")
  end

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