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_users, #is_root?, #whoami

Methods included from File

#_read_file_meterpreter, #_unix_max_line_length, #_write_file_unix_shell, #append_file, #attributes, #cd, #chmod, #dir, #directory?, #executable?, #exist?, #expand_path, #exploit_data, #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?, #upload_and_chmodx, #upload_file, #writable?, #write_file

Methods included from Common

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

Instance Method Details

#initialize(info = {}) ⇒ Object


14
15
16
17
18
19
# File 'lib/msf/core/post/linux/compile.rb', line 14

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

#live_compile?Boolean

Returns:

  • (Boolean)

21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/msf/core/post/linux/compile.rb', line 21

def live_compile?
  return false unless datastore['COMPILE'].eql?('Auto') || datastore['COMPILE'].eql?('True')

  if has_gcc?
    vprint_good 'gcc is installed'
    return true
  end

  unless datastore['COMPILE'].eql? 'Auto'
    fail_with Module::Failure::BadConfig, 'gcc is not installed. Set COMPILE False to upload a pre-compiled executable.'
  end
end

#strip_comments(c_code) ⇒ Object


57
58
59
# File 'lib/msf/core/post/linux/compile.rb', line 57

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

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


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/msf/core/post/linux/compile.rb', line 34

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

  gcc_cmd = "gcc -o #{path} #{path}.c"
  if session.type.eql? 'shell'
    gcc_cmd = "PATH=\"$PATH:/usr/bin/\" #{gcc_cmd}"
  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
    fail_with Module::Failure::BadConfig, "#{path}.c failed to compile. Set COMPILE False to upload a pre-compiled executable."
  end

  chmod path
end