Files
metasploit-gs/lib/msf/core/post/linux/compile.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

64 lines
1.5 KiB
Ruby
Raw Normal View History

2019-09-03 18:46:13 +08:00
# -*- coding: binary -*-
module Msf
class Post
module Linux
module Compile
include ::Msf::Post::Common
include ::Msf::Post::File
include ::Msf::Post::Unix
def initialize(info = {})
super
register_options( [
OptEnum.new('COMPILE', [true, 'Compile on target', 'Auto', ['Auto', 'True', 'False']]),
], self.class)
end
def live_compile?
2021-02-04 09:25:40 -05:00
return false unless %w{ Auto True }.include?(datastore['COMPILE'])
2019-09-03 18:46:13 +08:00
if has_gcc?
vprint_good 'gcc is installed'
return true
end
2021-02-04 09:25:40 -05:00
unless datastore['COMPILE'] == 'Auto'
fail_with Module::Failure::BadConfig, 'gcc is not installed. Set COMPILE False to upload a pre-compiled executable.'
2019-09-03 18:46:13 +08:00
end
end
def upload_and_compile(path, data, gcc_args='')
2019-10-23 20:54:38 +08:00
write_file "#{path}.c", strip_comments(data)
2019-09-03 18:46:13 +08:00
gcc_cmd = "gcc -o '#{path}' '#{path}.c'"
if session.type == 'shell'
2019-10-10 13:13:40 +08:00
gcc_cmd = "PATH=\"$PATH:/usr/bin/\" #{gcc_cmd}"
2019-09-03 18:46:13 +08:00
end
unless gcc_args.to_s.blank?
gcc_cmd << " #{gcc_args}"
end
output = cmd_exec gcc_cmd
rm_f "#{path}.c"
unless output.blank?
print_error output
2021-02-04 09:25:40 -05:00
message = "#{path}.c failed to compile."
# don't mention the COMPILE option if it was deregistered
message << ' Set COMPILE to False to upload a pre-compiled executable.' if options.include?('COMPILE')
fail_with Module::Failure::BadConfig, message
2019-09-03 18:46:13 +08:00
end
chmod path
end
def strip_comments(c_code)
c_code.gsub(%r{/\*.*?\*/}m, '').gsub(%r{^\s*//.*$}, '')
end
end # Compile
end # Linux
end # Post
end # Msf