Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Compiling Encrypted Payloads on MacOS #15703

Merged
merged 1 commit into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/metasploit/framework/compiler/mingw.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ module Mingw

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
Expand All @@ -37,11 +42,14 @@ def build_cmd(src)
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
Expand Down Expand Up @@ -69,13 +77,14 @@ def cleanup_files
class X86
include Mingw

attr_reader :file_name, :keep_exe, :keep_src, :strip_syms, :link_script, :opt_lvl, :mingw_bin, :compile_options
attr_reader :file_name, :keep_exe, :keep_src, :strip_syms, :link_script, :opt_lvl, :mingw_bin, :compile_options, :show_compile_cmd

def initialize(opts={})
@file_name = opts[:f_name]
@keep_exe = opts[:keep_exe]
@keep_src = opts[:keep_src]
@strip_syms = opts[:strip_symbols]
@show_compile_cmd = opts[:show_compile_cmd]
@link_script = opts[:linker_script]
@compile_options = opts[:compile_options]
@opt_lvl = opts[:opt_lvl]
Expand All @@ -90,13 +99,14 @@ def self.available?
class X64
include Mingw

attr_reader :file_name, :keep_exe, :keep_src, :strip_syms, :link_script, :opt_lvl, :mingw_bin, :compile_options
attr_reader :file_name, :keep_exe, :keep_src, :strip_syms, :link_script, :opt_lvl, :mingw_bin, :compile_options, :show_compile_cmd

def initialize(opts={})
@file_name = opts[:f_name]
@keep_exe = opts[:keep_exe]
@keep_src = opts[:keep_src]
@strip_syms = opts[:strip_symbols]
@show_compile_cmd = opts[:show_compile_cmd]
@link_script = opts[:linker_script]
@compile_options = opts[:compile_options]
@opt_lvl = opts[:opt_lvl]
Expand Down
1 change: 1 addition & 0 deletions lib/msf/core/payload/windows/encrypted_payload_opts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def initialize(info={})
register_advanced_options(
[
OptBool.new('StripSymbols', [ false, 'Payload will be compiled without symbols', true ]),
OptBool.new('ShowCompileCMD', [ false, 'Display the command used to compile payload', false ]),
OptEnum.new('OptLevel', [ false, 'The optimization level to compile with', 'O2', [ 'Og', 'Os', 'O0', 'O1', 'O2', 'O3' ] ]),
OptBool.new('KeepSrc', [ false, 'Keep source code after compiling it', false ]),
OptBool.new('KeepExe', [ false, 'Keep executable after compiling the payload', false ]),
Expand Down
2 changes: 2 additions & 0 deletions lib/msf/core/payload/windows/encrypted_reverse_tcp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def generate(opts={})
opt_lvl: datastore['OptLevel'],
keep_src: datastore['KeepSrc'],
keep_exe: datastore['KeepExe'],
show_compile_cmd: datastore['ShowCompileCMD'],
f_name: Tempfile.new(staged? ? 'reverse_pic_stager' : 'reverse_pic_stageless').path,
arch: self.arch_to_s
}
Expand Down Expand Up @@ -145,6 +146,7 @@ def generate_stage(opts={})
linker_script: link_script,
keep_src: datastore['KeepSrc'],
keep_exe: datastore['KeepExe'],
show_compile_cmd: datastore['ShowCompileCMD'],
f_name: Tempfile.new('reverse_pic_stage').path,
arch: self.arch_to_s
}
Expand Down