Skip to content

Commit 85f344a

Browse files
committed
Land rapid7#19410, Add clang support
Add clang support for live_compile?/upload_and_compile?
2 parents 8e94a0d + 87ccb97 commit 85f344a

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,46 @@ 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
2327
end
2428

2529
unless datastore['COMPILE'] == 'Auto'
26-
fail_with Module::Failure::BadConfig, 'gcc is not installed. Set COMPILE False to upload a pre-compiled executable.'
30+
fail_with Module::Failure::BadConfig, "#{datastore['COMPILER']} is not installed. Set COMPILE False to upload a pre-compiled executable."
2731
end
32+
33+
false
2834
end
2935

30-
def upload_and_compile(path, data, gcc_args='')
36+
def upload_and_compile(path, data, compiler_args='')
3137
write_file "#{path}.c", strip_comments(data)
3238

33-
gcc_cmd = "gcc -o '#{path}' '#{path}.c'"
39+
compiler_cmd = "#{datastore['COMPILER']} -o '#{path}' '#{path}.c'"
3440
if session.type == 'shell'
35-
gcc_cmd = "PATH=\"$PATH:/usr/bin/\" #{gcc_cmd}"
41+
compiler_cmd = "PATH=\"$PATH:/usr/bin/\" #{compiler_cmd}"
3642
end
3743

38-
unless gcc_args.to_s.blank?
39-
gcc_cmd << " #{gcc_args}"
44+
unless compiler_args.to_s.blank?
45+
compiler_cmd << " #{compiler_args}"
4046
end
4147

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

45-
unless output.blank?
46-
print_error output
53+
unless success
4754
message = "#{path}.c failed to compile."
4855
# don't mention the COMPILE option if it was deregistered
4956
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)