Module: Msf::Post::Linux::Compile

Includes:
Common, File, Unix
Defined in:
lib/msf/core/post/linux/compile.rb

Instance Method Summary collapse

Methods included from Unix

#enum_user_directories, #get_groups, #get_session_pid, #get_users, #is_root?, #whoami

Methods included from File

#_append_file_powershell, #_append_file_unix_shell, #_can_echo?, #_read_file_meterpreter, #_read_file_powershell, #_read_file_powershell_fragment, #_shell_command_with_success_code, #_shell_process_with_success_code, #_unix_max_line_length, #_win_ansi_append_file, #_win_ansi_write_file, #_win_bin_append_file, #_win_bin_write_file, #_write_file_meterpreter, #_write_file_powershell, #_write_file_powershell_fragment, #_write_file_unix_shell, #append_file, #attributes, #cd, #chmod, #copy_file, #dir, #directory?, #executable?, #exist?, #expand_path, #exploit_data, #exploit_source, #file?, #file_local_write, #file_remote_digestmd5, #file_remote_digestsha1, #file_remote_digestsha2, #immutable?, #mkdir, #pwd, #read_file, #readable?, #rename_file, #rm_f, #rm_rf, #setuid?, #stat, #upload_and_chmodx, #upload_file, #writable?, #write_file

Methods included from Common

#clear_screen, #cmd_exec, #cmd_exec_get_pid, #cmd_exec_with_result, #command_exists?, #create_process, #get_env, #get_envs, #peer, #report_virtualization, #rhost, #rport

Instance Method Details

#get_compilerObject



18
19
20
21
22
23
24
25
26
# File 'lib/msf/core/post/linux/compile.rb', line 18

def get_compiler
  if has_gcc?
    return 'gcc'
  elsif has_clang?
    return 'clang'
  else
    return nil
  end
end

#initialize(info = {}) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/msf/core/post/linux/compile.rb', line 10

def initialize(info = {})
  super
  register_options( [
    OptEnum.new('COMPILE', [true, 'Compile on target', 'Auto', ['Auto', 'True', 'False']]),
    OptEnum.new('COMPILER', [true, 'Compiler to use on target', 'Auto', ['Auto', 'gcc', 'clang']]),
  ], self.class)
end

#live_compile?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/msf/core/post/linux/compile.rb', line 28

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

  if datastore['COMPILER'] == 'gcc' && has_gcc?
    vprint_good 'gcc is installed'
    return true
  elsif datastore['COMPILER'] == 'clang' && has_clang?
    vprint_good 'clang is installed'
    return true
  elsif datastore['COMPILER'] == 'Auto' && get_compiler.present?
    return true
  end

  unless datastore['COMPILE'] == 'Auto'
    fail_with Module::Failure::BadConfig, "#{datastore['COMPILER']} is not installed. Set COMPILE False to upload a pre-compiled executable."
  end

  false
end

#strip_comments(c_code) ⇒ Object



81
82
83
# File 'lib/msf/core/post/linux/compile.rb', line 81

def strip_comments(c_code)
  c_code.gsub(%r{/\*.*?\*/}m, '').gsub(%r{^\s*//.*$}, '')
end

#upload_and_compile(path, data, compiler_args = '') ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/msf/core/post/linux/compile.rb', line 48

def upload_and_compile(path, data, compiler_args='')
  write_file "#{path}.c", strip_comments(data)

  compiler = datastore['COMPILER']
  if datastore['COMPILER'] == 'Auto'
    compiler = get_compiler
    fail_with(Module::Failure::BadConfig, "Unable to find a compiler on the remote target.") unless compiler.present?
  end

  compiler_cmd = "#{compiler} -o '#{path}' '#{path}.c'"
  if session.type == 'shell'
    compiler_cmd = "PATH=\"$PATH:/usr/bin/\" #{compiler_cmd}"
  end

  unless compiler_args.to_s.blank?
    compiler_cmd << " #{compiler_args}"
  end

  verification_token = Rex::Text.rand_text_alphanumeric(8)
  success = cmd_exec("#{compiler_cmd} && echo #{verification_token}")&.include?(verification_token)

  rm_f "#{path}.c"

  unless success
    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
  end

  chmod path
end