Module: Msf::Post::Process

Includes:
File
Defined in:
lib/msf/core/post/process.rb

Instance Method Summary collapse

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?, #stat, #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, #peer, #report_virtualization, #rhost, #rport

Instance Method Details

#get_processesObject


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/msf/core/post/process.rb', line 22

def get_processes
  if session.type == 'meterpreter'
    return session.sys.process.get_processes.map { |p| p.slice('name', 'pid') }
  end
  processes = []
  if session.platform == 'windows'
    tasklist = cmd_exec('tasklist').split("\n")
    4.times { tasklist.delete_at(0) }
    tasklist.each do |p|
      properties = p.split
      process = {}
      process['name'] = properties[0]
      process['pid'] = properties[1].to_i
      processes.push(process)
    end
    # adding manually because this is common for all windows I think and splitting for this was causing problem for other processes.
    processes.prepend({ 'name' => '[System Process]', 'pid' => 0 })
  else
    if command_exists?('ps')
      ps_aux = cmd_exec('ps aux').split("\n")
      ps_aux.delete_at(0)
      ps_aux.each do |p|
        properties = p.split
        process = {}
        process['name'] = properties[10].gsub(/\[|\]/,"")
        process['pid'] = properties[1].to_i
        processes.push(process)
      end
    elsif directory?('/proc')
      directories_proc = dir('/proc/')
      directories_proc.each do |elem|
        elem.to_s.gsub(/ *\n+/, '')
        next unless elem[-1].match? /\d/

        process = {}
        process['pid'] = elem.to_i
        status = read_file("/proc/#{elem}/status") # will return nil if the process `elem` PID got vanished
        next unless status

        process['name'] = status.split(/\n|\t/)[1]
        processes.push(process)
      end
    else
      raise "Can't enumerate processes because `ps' command and `/proc' directory doesn't exist."
    end
  end
  return processes
end

#has_pid?(pid) ⇒ Boolean

Checks if the remote system has a process with ID pid

Returns:

  • (Boolean)

17
18
19
20
# File 'lib/msf/core/post/process.rb', line 17

def has_pid?(pid)
  pid_list = get_processes.collect { |e| e['pid'] }
  pid_list.include?(pid)
end

#initialize(info = {}) ⇒ Object


5
6
7
8
9
10
11
12
# File 'lib/msf/core/post/process.rb', line 5

def initialize(info = {})
  super(update_info(
    info,
    'Compat' => { 'Meterpreter' => { 'Commands' => %w{
      stdapi_sys_process_get_processes
    } } }
  ))
end