Skip to content

Commit 4b79889

Browse files
jvoisinsmcintyre-r7
andcommitted
Add clang support for live_compile?/upload_and_compile?
Co-authored-by: Spencer McIntyre <[email protected]>
1 parent 8e94a0d commit 4b79889

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

lib/msf/core/post/linux/compile.rb

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,47 @@ def initialize(info = {})
1111
super
1212
register_options( [
1313
OptEnum.new('COMPILE', [true, 'Compile on target', 'Auto', ['Auto', 'True', 'False']]),
14+
OptEnum.new('COMPILER', [true, 'Compiler to use on target', 'gcc', ['gcc', 'clang']]),
1415
], self.class)
1516
end
1617

1718
def live_compile?
1819
return false unless %w{ Auto True }.include?(datastore['COMPILE'])
1920

20-
if has_gcc?
21+
if datastore['COMPILER'] == 'gcc' && has_gcc?
2122
vprint_good 'gcc is installed'
2223
return true
24+
elsif datastore['COMPILER'] == 'clang' && has_clang?
25+
vprint_good 'clang is installed'
26+
return true
27+
end
2328
end
2429

2530
unless datastore['COMPILE'] == 'Auto'
26-
fail_with Module::Failure::BadConfig, 'gcc is not installed. Set COMPILE False to upload a pre-compiled executable.'
31+
fail_with Module::Failure::BadConfig, "#{datastore['COMPILER']} is not installed. Set COMPILE False to upload a pre-compiled executable."
2732
end
33+
34+
false
2835
end
2936

30-
def upload_and_compile(path, data, gcc_args='')
37+
def upload_and_compile(path, data, compiler_args='')
3138
write_file "#{path}.c", strip_comments(data)
3239

33-
gcc_cmd = "gcc -o '#{path}' '#{path}.c'"
40+
compiler_cmd = "#{datastore['COMPILER']} -o '#{path}' '#{path}.c'"
3441
if session.type == 'shell'
35-
gcc_cmd = "PATH=\"$PATH:/usr/bin/\" #{gcc_cmd}"
42+
compiler_cmd = "PATH=\"$PATH:/usr/bin/\" #{compiler_cmd}"
3643
end
3744

38-
unless gcc_args.to_s.blank?
39-
gcc_cmd << " #{gcc_args}"
45+
unless compiler_args.to_s.blank?
46+
compiler_cmd << " #{compiler_args}"
4047
end
4148

42-
output = cmd_exec gcc_cmd
49+
verification_token = Rex::Text.rand_text_alphanumeric(8)
50+
success = cmd_exec("#{compiler_cmd} && echo #{verification_token}")&.include?(verification_token)
51+
4352
rm_f "#{path}.c"
4453

45-
unless output.blank?
46-
print_error output
54+
unless success
4755
message = "#{path}.c failed to compile."
4856
# don't mention the COMPILE option if it was deregistered
4957
message << ' Set COMPILE to False to upload a pre-compiled executable.' if options.include?('COMPILE')

lib/msf/core/post/linux/system.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,16 @@ def has_gcc?
218218
raise 'Unable to check for gcc'
219219
end
220220

221+
#
222+
# Checks if the system has clang installed
223+
# @return [Boolean]
224+
#
225+
def has_clang?
226+
command_exists? 'clang'
227+
rescue StandardError
228+
raise 'Unable to check for clang'
229+
end
230+
221231
#
222232
# Checks if `file_path` is mounted on a noexec mount point
223233
# @return [Boolean]

0 commit comments

Comments
 (0)