Module: Msf::Post::Linux::System

Includes:
Common, File, Unix
Defined in:
lib/msf/core/post/linux/system.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, #_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?, #initialize, #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?, #get_env, #get_envs, #initialize, #peer, #report_virtualization, #rhost, #rport

Instance Method Details

#get_container_typeString

Determine if system is a container

Returns:

  • (String)


395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/msf/core/post/linux/system.rb', line 395

def get_container_type
  # Checking file paths for solution
  container_type =
    if file?('/.dockerenv') || file?('/.dockerinit')
      'Docker'
    elsif file?('/run/.containerenv')
      'Podman'
    elsif directory?('/dev/lxc')
      'LXC'
    elsif file?('/proc/sys/kernel/osrelease') && read_file('/proc/sys/kernel/osrelease').grep(/WSL|Microsoft/i).any?
      # Check for WSL, as suggested in https://github.com/Microsoft/WSL/issues/423#issuecomment-221627364
      'WSL'
    elsif (cgroup = read_file('/proc/1/cgroup'))
      # Check cgroup on PID 1
      case cgroup.tr("\n", ' ')
      when /docker/i
        return 'Docker'
      when /lxc/i
        return 'LXC'
      end
    else
      # Check for the "container" environment variable
      case get_env('container')
      when 'lxc'
        return 'LXC'
      when 'systemd-nspawn'
        return 'systemd nspawn'
      when 'podman'
        return 'Podman'
      else
        'Unknown'
      end
    end
  unless container_type == 'Unknown'
    report_host({
      host: rhost,
      virtual_host: container_type
    })
  end
  container_type
end

#get_cpu_infoHash

Gets basic information about the system’s CPU.

Returns:

  • (Hash)


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/msf/core/post/linux/system.rb', line 156

def get_cpu_info
  info = {}
  orig = read_file('/proc/cpuinfo').to_s
  cpuinfo = orig.split("\n\n")[0]
  # This is probably a more platform independent way to parse the results (compared to splitting and assigning preset indices to values)
  cpuinfo.split("\n").each do |l|
    info[:speed_mhz] = l.split(': ')[1].to_i if l.include? 'cpu MHz'
    info[:product] = l.split(': ')[1] if l.include? 'model name'
    info[:vendor] = l.split(': ')[1] if l.include? 'vendor_id'
  end
  info[:cores] = orig.split("\n\n").size
  info
rescue StandardError
  raise 'Could not get CPU information'
end

#get_hostnameString

Gets the hostname of the system

Returns:

  • (String)


176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/msf/core/post/linux/system.rb', line 176

def get_hostname
  hostname =
    if command_exists?('uname')
      cmd_exec('uname -n').to_s
    else
      read_file('/proc/sys/kernel/hostname').to_s.chomp
    end
  report_host({ host: rhost, name: hostname })
  hostname
rescue StandardError
  raise 'Unable to retrieve hostname'
end

#get_mount_path(filepath) ⇒ String

Gets the mount point of ‘filepath`

Parameters:

  • filepath (String)

    The filepath to get the mount point

Returns:

  • (String)


288
289
290
291
292
# File 'lib/msf/core/post/linux/system.rb', line 288

def get_mount_path(filepath)
  cmd_exec("df \"#{filepath}\" | tail -1").split(' ')[5]
rescue StandardError
  raise "Unable to get mount path of #{filepath}"
end

#get_pathString

Gets the $PATH environment variable

Returns:

  • (String)


146
147
148
149
150
# File 'lib/msf/core/post/linux/system.rb', line 146

def get_path
  cmd_exec('echo $PATH').to_s
rescue StandardError
  raise 'Unable to determine path'
end

#get_shell_nameString

Gets the name of the current shell

Returns:

  • (String)


193
194
195
196
197
198
199
200
201
# File 'lib/msf/core/post/linux/system.rb', line 193

def get_shell_name
  if command_exists?('ps')
    cmd_exec('ps -p $$').to_s.split("\n").last.split(' ')[3]
  else
    cmd_exec('echo $0').split('-')[1]
  end
rescue StandardError
  raise 'Unable to gather shell name'
end

#get_shell_pidString

Gets the pid of the current shell

Returns:

  • (String)


207
208
209
# File 'lib/msf/core/post/linux/system.rb', line 207

def get_shell_pid
  cmd_exec('echo $$').to_s
end

#get_suid_files(findpath = '/') ⇒ Array

Gathers all SUID files on the filesystem. NOTE: This uses the Linux ‘find` command. It will most likely take a while to get all files. Consider specifying a more narrow find path.

Parameters:

  • findpath (defaults to: '/')

    The path on the system to start searching

Returns:

  • (Array)


137
138
139
140
141
# File 'lib/msf/core/post/linux/system.rb', line 137

def get_suid_files(findpath = '/')
  cmd_exec("find #{findpath} -perm -4000 -print -xdev").to_s.split("\n").delete_if { |i| i.include? 'Permission denied' }
rescue StandardError
  raise 'Could not retrieve all SUID files'
end

#get_sysinfoObject

Returns a Hash containing Distribution Name, Version and Kernel Information



14
15
16
17
18
19
20
21
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/msf/core/post/linux/system.rb', line 14

def get_sysinfo
  system_data = {}
  etc_files = cmd_exec('ls /etc').split

  kernel_version = cmd_exec('uname -a')
  system_data[:kernel] = kernel_version

  # Debian
  if etc_files.include?('debian_version')
    version = read_file('/etc/issue').gsub(/\n|\\n|\\l/, '').strip
    if kernel_version =~ /Ubuntu/
      system_data[:distro] = 'ubuntu'
    else
      system_data[:distro] = 'debian'
    end
    system_data[:version] = version

  # Amazon / CentOS
  elsif etc_files.include?('system-release')
    version = read_file('/etc/system-release').gsub(/\n|\\n|\\l/, '').strip
    if version.include? 'CentOS'
      system_data[:distro] = 'centos'
    elsif version.include? 'Fedora'
      system_data[:distro] = 'fedora'
    else
      system_data[:distro] = 'amazon'
    end
    system_data[:version] = version

  # Alpine
  elsif etc_files.include?('alpine-release')
    version = read_file('/etc/alpine-release').gsub(/\n|\\n|\\l/, '').strip
    system_data[:distro] = 'alpine'
    system_data[:version] = version

  # Fedora
  elsif etc_files.include?('fedora-release')
    version = read_file('/etc/fedora-release').gsub(/\n|\\n|\\l/, '').strip
    system_data[:distro] = 'fedora'
    system_data[:version] = version

  # Oracle Linux
  elsif etc_files.include?('enterprise-release')
    version = read_file('/etc/enterprise-release').gsub(/\n|\\n|\\l/, '').strip
    system_data[:distro] = 'oracle'
    system_data[:version] = version

  # RedHat
  elsif etc_files.include?('redhat-release')
    version = read_file('/etc/redhat-release').gsub(/\n|\\n|\\l/, '').strip
    system_data[:distro] = 'redhat'
    system_data[:version] = version

  # Arch
  elsif etc_files.include?('arch-release')
    version = read_file('/etc/arch-release').gsub(/\n|\\n|\\l/, '').strip
    system_data[:distro] = 'arch'
    system_data[:version] = version

  # Slackware
  elsif etc_files.include?('slackware-version')
    version = read_file('/etc/slackware-version').gsub(/\n|\\n|\\l/, '').strip
    system_data[:distro] = 'slackware'
    system_data[:version] = version

  # Mandrake
  elsif etc_files.include?('mandrake-release')
    version = read_file('/etc/mandrake-release').gsub(/\n|\\n|\\l/, '').strip
    system_data[:distro] = 'mandrake'
    system_data[:version] = version

  # SuSE
  elsif etc_files.include?('SuSE-release')
    version = read_file('/etc/SuSE-release').gsub(/\n|\\n|\\l/, '').strip
    system_data[:distro] = 'suse'
    system_data[:version] = version

  # OpenSUSE
  elsif etc_files.include?('SUSE-brand')
    version = read_file('/etc/SUSE-brand').scan(/^VERSION\s*=\s*([\d.]+)/).flatten.first
    system_data[:distro] = 'suse'
    system_data[:version] = version

  # Gentoo
  elsif etc_files.include?('gentoo-release')
    version = read_file('/etc/gentoo-release').gsub(/\n|\\n|\\l/, '').strip
    system_data[:distro] = 'gentoo'
    system_data[:version] = version

  # Openwall
  elsif etc_files.include?('owl-release')
    version = read_file('/etc/owl-release').gsub(/\n|\\n|\\l/, '').strip
    system_data[:distro] = 'openwall'
    system_data[:version] = version

  # Generic
  elsif etc_files.include?('issue')
    version = read_file('/etc/issue').gsub(/\n|\\n|\\l/, '').strip
    system_data[:distro] = 'linux'
    system_data[:version] = version

  # Others, could be a mismatch like ssh_login to cisco device
  else
    system_data[:distro] = 'linux'
    system_data[:version] = ''

  end

  report_host({
    host: rhost,
    os_name: system_data[:distro],
    os_flavor: system_data[:version]
  })

  system_data
end

#glibc_versionString

Gets the version of glibc

Returns:

  • (String)


275
276
277
278
279
280
281
# File 'lib/msf/core/post/linux/system.rb', line 275

def glibc_version
  raise 'glibc is not installed' unless command_exists? 'ldd'

  cmd_exec('ldd --version').scan(/^ldd\s+\(.*\)\s+([\d.]+)/).flatten.first
rescue StandardError
  raise 'Could not determine glibc version'
end

#has_gcc?Boolean

Checks if the system has gcc installed

Returns:

  • (Boolean)


215
216
217
218
219
# File 'lib/msf/core/post/linux/system.rb', line 215

def has_gcc?
  command_exists? 'gcc'
rescue StandardError
  raise 'Unable to check for gcc'
end

#interfacesArray

Gets all the interfaces of the device

Returns:

  • (Array)


318
319
320
321
322
323
324
325
326
327
# File 'lib/msf/core/post/linux/system.rb', line 318

def interfaces
  result = []
  data = cmd_exec('for fn in /sys/class/net/*; do echo $fn; done')
  parts = data.split("\n")
  parts.each do |line|
    line = line.split('/')[-1]
    result.insert(-1, line)
  end
  result
end

#ipsArray

Gets all the IP directions of the device

Returns:

  • (Array)


298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/msf/core/post/linux/system.rb', line 298

def ips
  lines = read_file('/proc/net/fib_trie')
  result = []
  previous_line = ''
  lines.each_line do |line|
    if line.include?('/32 host LOCAL')
      previous_line = previous_line.split('-- ')[1].strip
      unless result.include? previous_line
        result.insert(-1, previous_line)
      end
    end
    previous_line = line
  end
  result
end

#listen_tcp_portsArray

Parsing information based on: github.com/sensu-plugins/sensu-plugins-network-checks/blob/master/bin/check-netstat-tcp.rb

Gets all the listening tcp ports in the device

Returns:

  • (Array)


352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/msf/core/post/linux/system.rb', line 352

def listen_tcp_ports
  ports = []
  content = read_file('/proc/net/tcp')
  content.each_line do |line|
    next unless (m = line.match(/^\s*\d+:\s+(.{8}|.{32}):(.{4})\s+(.{8}|.{32}):(.{4})\s+(.{2})/))

    connection_state = m[5].to_s
    next unless connection_state == '0A'

    connection_port = m[2].to_i(16)
    unless ports.include?(connection_port)
      ports.insert(-1, connection_port)
    end
  end
  ports
end

#listen_udp_portsArray

Parsing information based on: github.com/sensu-plugins/sensu-plugins-network-checks/blob/master/bin/check-netstat-tcp.rb

Gets all the listening udp ports in the device

Returns:

  • (Array)


374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/msf/core/post/linux/system.rb', line 374

def listen_udp_ports
  ports = []
  content = read_file('/proc/net/udp')
  content.each_line do |line|
    next unless (m = line.match(/^\s*\d+:\s+(.{8}|.{32}):(.{4})\s+(.{8}|.{32}):(.{4})\s+(.{2})/))

    connection_state = m[5].to_s
    next unless connection_state == '07'

    connection_port = m[2].to_i(16)
    if ports.include?(connection_port) == false
      ports.insert(-1, connection_port)
    end
  end
  return ports
end

#macsArray

Gets all the macs of the device

Returns:

  • (Array)


333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/msf/core/post/linux/system.rb', line 333

def macs
  result = []
  str_macs = cmd_exec('for fn in /sys/class/net/*; do echo $fn; done')
  parts = str_macs.split("\n")
  parts.each do |line|
    rut = line + '/address'
    mac_array = read_file(rut)
    mac_array.each_line do |mac|
      result.insert(-1, mac.strip)
    end
  end
  result
end

#noexec?(file_path) ⇒ Boolean

Checks if ‘file_path` is mounted on a noexec mount point

Returns:

  • (Boolean)


225
226
227
228
229
230
231
232
233
234
# File 'lib/msf/core/post/linux/system.rb', line 225

def noexec?(file_path)
  mount = read_file('/proc/mounts').to_s
  mount_path = get_mount_path(file_path)
  mount.lines.each do |l|
    return true if l =~ Regexp.new("#{mount_path} (.*)noexec(.*)")
  end
  false
rescue StandardError
  raise 'Unable to check for noexec volume'
end

#nosuid?(file_path) ⇒ Boolean

Checks if ‘file_path` is mounted on a nosuid mount point

Returns:

  • (Boolean)


240
241
242
243
244
245
246
247
248
249
# File 'lib/msf/core/post/linux/system.rb', line 240

def nosuid?(file_path)
  mount = read_file('/proc/mounts').to_s
  mount_path = get_mount_path(file_path)
  mount.lines.each do |l|
    return true if l =~ Regexp.new("#{mount_path} (.*)nosuid(.*)")
  end
  false
rescue StandardError
  raise 'Unable to check for nosuid volume'
end

#protected_hardlinks?Boolean

Checks for protected hardlinks on the system

Returns:

  • (Boolean)


255
256
257
258
259
# File 'lib/msf/core/post/linux/system.rb', line 255

def protected_hardlinks?
  read_file('/proc/sys/fs/protected_hardlinks').to_s.strip.eql? '1'
rescue StandardError
  raise 'Could not determine protected_hardlinks status'
end

#protected_symlinks?Boolean

Checks for protected symlinks on the system

Returns:

  • (Boolean)


265
266
267
268
269
# File 'lib/msf/core/post/linux/system.rb', line 265

def protected_symlinks?
  read_file('/proc/sys/fs/protected_symlinks').to_s.strip.eql? '1'
rescue StandardError
  raise 'Could not determine protected_symlinks status'
end