Module: Msf::Post::Linux::Kernel

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

Instance Method Summary collapse

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?, #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?, #create_process, #get_env, #get_envs, #initialize, #peer, #report_virtualization, #rhost, #rport

Instance Method Details

#aslr_enabled?Boolean

Returns true if Address Space Layout Randomization (ASLR) is enabled

Returns:

  • (Boolean)


183
184
185
186
187
188
# File 'lib/msf/core/post/linux/kernel.rb', line 183

def aslr_enabled?
  aslr = read_file('/proc/sys/kernel/randomize_va_space').to_s.strip
  (aslr.eql?('1') || aslr.eql?('2'))
rescue
  raise 'Could not determine ASLR status'
end

#cpu_flagsArray

Returns a list of CPU flags

Returns:

  • (Array)


111
112
113
114
115
116
117
118
119
# File 'lib/msf/core/post/linux/kernel.rb', line 111

def cpu_flags
  cpuinfo = read_file('/proc/cpuinfo').to_s

  return unless cpuinfo.include? 'flags'

  cpuinfo.scan(/^flags\s*:(.*)$/).flatten.join(' ').split(/\s/).map(&:strip).reject(&:empty?).uniq
rescue
  raise'Could not retrieve CPU flags'
end

#dmesg_restrict?Boolean

Returns true if dmesg restriction is enabled

Returns:

  • (Boolean)


230
231
232
233
234
# File 'lib/msf/core/post/linux/kernel.rb', line 230

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

#exec_shield_enabled?Boolean

Returns true if Exec-Shield is enabled

Returns:

  • (Boolean)


195
196
197
198
199
200
# File 'lib/msf/core/post/linux/kernel.rb', line 195

def exec_shield_enabled?
  exec_shield = read_file('/proc/sys/kernel/exec-shield').to_s.strip
  (exec_shield.eql?('1') || exec_shield.eql?('2'))
rescue
  raise 'Could not determine exec-shield status'
end

#grsec_installed?Boolean

Returns true if grsecurity is installed

Returns:

  • (Boolean)


261
262
263
264
265
# File 'lib/msf/core/post/linux/kernel.rb', line 261

def grsec_installed?
  File.exists?('/dev/grsec') && File.chardev?('/dev/grsec')
rescue
  raise 'Could not determine grsecurity status'
end

#kaiser_enabled?Boolean

Returns true if Kernel Address Isolation (KAISER) is enabled

Returns:

  • (Boolean)


148
149
150
151
152
# File 'lib/msf/core/post/linux/kernel.rb', line 148

def kaiser_enabled?
  cpu_flags.include? 'kaiser'
rescue
  raise 'Could not determine KAISER status'
end

#kernel_archString

Returns the kernel hardware architecture Based on values from en.wikipedia.org/wiki/Uname

Returns:

  • (String)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/msf/core/post/linux/kernel.rb', line 62

def kernel_arch
  arch = kernel_hardware
  return ARCH_X64 if arch == 'x86_64' || arch == 'amd64'
  return ARCH_AARCH64 if arch == 'aarch64' || arch == 'arm64'
  return ARCH_ARMLE if arch.start_with?'arm'
  return ARCH_X86 if arch.end_with?'86'
  return ARCH_PPC if arch == 'ppc'
  return ARCH_PPC64 if arch == 'ppc64'
  return ARCH_PPC64LE if arch == 'ppc64le'
  return ARCH_MIPS if arch == 'mips'
  return ARCH_MIPS64 if arch == 'mips64'
  return ARCH_SPARC if arch == 'sparc'
  return ARCH_RISCV32LE if arch == 'riscv32'
  return ARCH_RISCV64LE if arch == 'riscv64'
  return ARCH_LOONGARCH64 if arch == 'loongarch64'
  arch
end

#kernel_configArray

Returns the kernel boot config

Returns:

  • (Array)


85
86
87
88
89
90
91
92
93
# File 'lib/msf/core/post/linux/kernel.rb', line 85

def kernel_config
  release = kernel_release
  output = read_file("/boot/config-#{release}").to_s.strip
  return if output.empty?
  config = output.split("\n").map(&:strip).reject(&:empty?).reject {|i| i.start_with? '#'}
  config
rescue
  raise 'Could not retrieve kernel config'
end

#kernel_hardwareString

Returns the kernel hardware

Returns:

  • (String)


52
53
54
# File 'lib/msf/core/post/linux/kernel.rb', line 52

def kernel_hardware
  uname('-m')
end

#kernel_modulesArray

Returns the kernel modules

Returns:

  • (Array)


100
101
102
103
104
# File 'lib/msf/core/post/linux/kernel.rb', line 100

def kernel_modules
  read_file('/proc/modules').to_s.scan(/^[^ ]+/)
rescue
  raise 'Could not determine kernel modules'
end

#kernel_nameString

Returns the kernel name

Returns:

  • (String)


43
44
45
# File 'lib/msf/core/post/linux/kernel.rb', line 43

def kernel_name
  uname('-s')
end

#kernel_releaseString

Returns the kernel release

Returns:

  • (String)


25
26
27
# File 'lib/msf/core/post/linux/kernel.rb', line 25

def kernel_release
  uname('-r')
end

#kernel_versionString

Returns the kernel version

Returns:

  • (String)


34
35
36
# File 'lib/msf/core/post/linux/kernel.rb', line 34

def kernel_version
  uname('-v')
end

#kpti_enabled?Boolean

Returns true if Kernel Page-Table Isolation (KPTI) is enabled, false if not.

Returns:

  • (Boolean)


159
160
161
162
163
# File 'lib/msf/core/post/linux/kernel.rb', line 159

def kpti_enabled?
  cpu_flags.include? 'pti'
rescue
  raise 'Could not determine KPTI status'
end

#kptr_restrict?Boolean

Returns true if kernel pointer restriction is enabled

Returns:

  • (Boolean)


219
220
221
222
223
# File 'lib/msf/core/post/linux/kernel.rb', line 219

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

#lkrg_installed?Boolean

Returns true if Linux Kernel Runtime Guard (LKRG) kernel module is installed

Returns:

  • (Boolean)


252
253
254
255
256
# File 'lib/msf/core/post/linux/kernel.rb', line 252

def lkrg_installed?
  directory?('/proc/sys/lkrg')
rescue
  raise 'Could not determine LKRG status'
end

#mmap_min_addrInteger

Returns mmap minimum address

Returns:

  • (Integer)


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

def mmap_min_addr
  mmap_min_addr = read_file('/proc/sys/vm/mmap_min_addr').to_s.strip
  return 0 unless mmap_min_addr =~ /\A\d+\z/
  mmap_min_addr
rescue
  raise 'Could not determine system mmap_min_addr'
end

#pax_installed?Boolean

Returns true if PaX is installed

Returns:

  • (Boolean)


270
271
272
273
274
# File 'lib/msf/core/post/linux/kernel.rb', line 270

def pax_installed?
  read_file('/proc/self/status').to_s.include? 'PaX:'
rescue
  raise 'Could not determine PaX status'
end

#selinux_enforcing?Boolean

Returns true if SELinux is in enforcing mode

Returns:

  • (Boolean)


292
293
294
295
296
297
298
299
300
301
302
# File 'lib/msf/core/post/linux/kernel.rb', line 292

def selinux_enforcing?
  return false unless selinux_installed?

  sestatus = cmd_exec('/usr/sbin/sestatus').to_s.strip
  raise unless sestatus.include?('SELinux')

  return true if sestatus =~ /Current mode:\s*enforcing/
  false
rescue
  raise 'Could not determine SELinux status'
end

#selinux_installed?Boolean

Returns true if SELinux is installed

Returns:

  • (Boolean)


281
282
283
284
285
# File 'lib/msf/core/post/linux/kernel.rb', line 281

def selinux_installed?
  cmd_exec('id').to_s.include? 'context='
rescue
  raise 'Could not determine SELinux status'
end

#smap_enabled?Boolean

Returns true if kernel and hardware supports Supervisor Mode Access Prevention (SMAP), false if not.

Returns:

  • (Boolean)


126
127
128
129
130
# File 'lib/msf/core/post/linux/kernel.rb', line 126

def smap_enabled?
  cpu_flags.include? 'smap'
rescue
  raise 'Could not determine SMAP status'
end

#smep_enabled?Boolean

Returns true if kernel and hardware supports Supervisor Mode Execution Protection (SMEP), false if not.

Returns:

  • (Boolean)


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

def smep_enabled?
  cpu_flags.include? 'smep'
rescue
  raise 'Could not determine SMEP status'
end

#uname(opts = '-a') ⇒ String

Returns uname output

Returns:

  • (String)


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

def uname(opts='-a')
  cmd_exec("uname #{opts}").to_s.strip
rescue
  raise "Failed to run uname #{opts}"
end

#unprivileged_bpf_disabled?Boolean

Returns true if unprivileged bpf is disabled

Returns:

  • (Boolean)


207
208
209
210
211
212
# File 'lib/msf/core/post/linux/kernel.rb', line 207

def unprivileged_bpf_disabled?
  unprivileged_bpf_disabled = read_file('/proc/sys/kernel/unprivileged_bpf_disabled').to_s.strip
  return (unprivileged_bpf_disabled == '1' || unprivileged_bpf_disabled == '2')
rescue
  raise 'Could not determine kernel.unprivileged_bpf_disabled status'
end

#userns_enabled?Boolean

Returns true if user namespaces are enabled, false if not.

Returns:

  • (Boolean)


170
171
172
173
174
175
176
# File 'lib/msf/core/post/linux/kernel.rb', line 170

def userns_enabled?
  return false if read_file('/proc/sys/user/max_user_namespaces').to_s.strip.eql? '0'
  return false if read_file('/proc/sys/kernel/unprivileged_userns_clone').to_s.strip.eql? '0'
  true
rescue
  raise 'Could not determine userns status'
end

#yama_enabled?Boolean

Returns true if Yama is enabled

Returns:

  • (Boolean)


322
323
324
325
326
327
# File 'lib/msf/core/post/linux/kernel.rb', line 322

def yama_enabled?
  return false unless yama_installed?
  !read_file('/proc/sys/kernel/yama/ptrace_scope').to_s.strip.eql? '0'
rescue
  raise 'Could not determine Yama status'
end

#yama_installed?Boolean

Returns true if Yama is installed

Returns:

  • (Boolean)


309
310
311
312
313
314
315
# File 'lib/msf/core/post/linux/kernel.rb', line 309

def yama_installed?
  ptrace_scope = read_file('/proc/sys/kernel/yama/ptrace_scope').to_s.strip
  return true if ptrace_scope =~ /\A\d\z/
  false
rescue
  raise 'Could not determine Yama status'
end