2018-04-04 17:13:49 +00:00
|
|
|
# -*- coding: binary -*-
|
|
|
|
|
|
|
|
|
|
module Msf
|
|
|
|
|
class Post
|
|
|
|
|
module Linux
|
|
|
|
|
module Kernel
|
|
|
|
|
include ::Msf::Post::Common
|
2021-08-31 19:25:35 +05:30
|
|
|
include Msf::Post::File
|
2018-04-06 08:28:53 -07:00
|
|
|
#
|
|
|
|
|
# Returns uname output
|
|
|
|
|
#
|
|
|
|
|
# @return [String]
|
|
|
|
|
#
|
|
|
|
|
def uname(opts='-a')
|
2018-04-16 10:22:41 +00:00
|
|
|
cmd_exec("uname #{opts}").to_s.strip
|
2018-04-06 08:28:53 -07:00
|
|
|
rescue
|
|
|
|
|
raise "Failed to run uname #{opts}"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Returns the kernel release
|
|
|
|
|
#
|
|
|
|
|
# @return [String]
|
|
|
|
|
#
|
|
|
|
|
def kernel_release
|
|
|
|
|
uname('-r')
|
|
|
|
|
end
|
|
|
|
|
|
2018-04-04 17:13:49 +00:00
|
|
|
#
|
|
|
|
|
# Returns the kernel version
|
|
|
|
|
#
|
|
|
|
|
# @return [String]
|
|
|
|
|
#
|
|
|
|
|
def kernel_version
|
2018-04-06 08:28:53 -07:00
|
|
|
uname('-v')
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Returns the kernel name
|
|
|
|
|
#
|
|
|
|
|
# @return [String]
|
|
|
|
|
#
|
|
|
|
|
def kernel_name
|
|
|
|
|
uname('-s')
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Returns the kernel hardware
|
|
|
|
|
#
|
|
|
|
|
# @return [String]
|
|
|
|
|
#
|
|
|
|
|
def kernel_hardware
|
|
|
|
|
uname('-m')
|
2018-04-04 17:13:49 +00:00
|
|
|
end
|
|
|
|
|
|
2022-03-09 06:52:00 +00:00
|
|
|
#
|
|
|
|
|
# Returns the kernel hardware architecture
|
|
|
|
|
# Based on values from https://en.wikipedia.org/wiki/Uname
|
|
|
|
|
#
|
|
|
|
|
# @return [String]
|
|
|
|
|
#
|
|
|
|
|
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'
|
|
|
|
|
arch
|
|
|
|
|
end
|
|
|
|
|
|
2018-12-05 11:19:36 +00:00
|
|
|
#
|
|
|
|
|
# Returns the kernel boot config
|
|
|
|
|
#
|
|
|
|
|
# @return [Array]
|
|
|
|
|
#
|
|
|
|
|
def kernel_config
|
2021-08-31 19:46:38 +05:30
|
|
|
return unless cmd_exec('test -r /boot/config-`uname -r` && echo true').include? 'true'
|
|
|
|
|
output = cmd_exec("cat /boot/config-`uname -r`").to_s.strip
|
|
|
|
|
return if output.empty?
|
2018-12-05 23:44:13 +11:00
|
|
|
config = output.split("\n").map(&:strip).reject(&:empty?).reject {|i| i.start_with? '#'}
|
|
|
|
|
config
|
2018-12-05 11:19:36 +00:00
|
|
|
rescue
|
|
|
|
|
raise 'Could not retrieve kernel config'
|
|
|
|
|
end
|
|
|
|
|
|
2018-04-05 05:25:36 +00:00
|
|
|
#
|
|
|
|
|
# Returns the kernel modules
|
|
|
|
|
#
|
2018-04-06 15:02:43 +00:00
|
|
|
# @return [Array]
|
2018-04-05 05:25:36 +00:00
|
|
|
#
|
|
|
|
|
def kernel_modules
|
2021-08-31 19:25:35 +05:30
|
|
|
read_file('/proc/modules').to_s.scan(/^[^ ]+/)
|
2018-04-05 05:25:36 +00:00
|
|
|
rescue
|
|
|
|
|
raise 'Could not determine kernel modules'
|
|
|
|
|
end
|
|
|
|
|
|
2019-01-23 11:35:38 +00:00
|
|
|
#
|
|
|
|
|
# Returns a list of CPU flags
|
|
|
|
|
#
|
|
|
|
|
# @return [Array]
|
|
|
|
|
#
|
|
|
|
|
def cpu_flags
|
2021-08-31 19:25:35 +05:30
|
|
|
cpuinfo = read_file('/proc/cpuinfo').to_s
|
2019-01-23 11:35:38 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2018-04-04 17:13:49 +00:00
|
|
|
#
|
|
|
|
|
# Returns true if kernel and hardware supports Supervisor Mode Access Prevention (SMAP), false if not.
|
|
|
|
|
#
|
|
|
|
|
# @return [Boolean]
|
|
|
|
|
#
|
|
|
|
|
def smap_enabled?
|
2019-01-23 11:35:38 +00:00
|
|
|
cpu_flags.include? 'smap'
|
2018-04-04 17:13:49 +00:00
|
|
|
rescue
|
|
|
|
|
raise 'Could not determine SMAP status'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Returns true if kernel and hardware supports Supervisor Mode Execution Protection (SMEP), false if not.
|
|
|
|
|
#
|
|
|
|
|
# @return [Boolean]
|
|
|
|
|
#
|
|
|
|
|
def smep_enabled?
|
2019-01-23 11:35:38 +00:00
|
|
|
cpu_flags.include? 'smep'
|
2018-04-04 17:13:49 +00:00
|
|
|
rescue
|
|
|
|
|
raise 'Could not determine SMEP status'
|
|
|
|
|
end
|
|
|
|
|
|
2018-04-15 06:53:00 +00:00
|
|
|
#
|
|
|
|
|
# Returns true if Kernel Address Isolation (KAISER) is enabled
|
|
|
|
|
#
|
|
|
|
|
# @return [Boolean]
|
|
|
|
|
#
|
|
|
|
|
def kaiser_enabled?
|
2019-01-23 11:35:38 +00:00
|
|
|
cpu_flags.include? 'kaiser'
|
2018-04-15 06:53:00 +00:00
|
|
|
rescue
|
|
|
|
|
raise 'Could not determine KAISER status'
|
|
|
|
|
end
|
|
|
|
|
|
2019-01-23 11:35:38 +00:00
|
|
|
#
|
|
|
|
|
# Returns true if Kernel Page-Table Isolation (KPTI) is enabled, false if not.
|
|
|
|
|
#
|
|
|
|
|
# @return [Boolean]
|
|
|
|
|
#
|
|
|
|
|
def kpti_enabled?
|
|
|
|
|
cpu_flags.include? 'pti'
|
|
|
|
|
rescue
|
|
|
|
|
raise 'Could not determine KPTI status'
|
|
|
|
|
end
|
|
|
|
|
|
2018-04-04 17:13:49 +00:00
|
|
|
#
|
|
|
|
|
# Returns true if user namespaces are enabled, false if not.
|
|
|
|
|
#
|
|
|
|
|
# @return [Boolean]
|
|
|
|
|
#
|
|
|
|
|
def userns_enabled?
|
2021-08-31 19:25:35 +05:30
|
|
|
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'
|
2018-11-25 01:26:49 +00:00
|
|
|
true
|
2018-04-04 17:13:49 +00:00
|
|
|
rescue
|
|
|
|
|
raise 'Could not determine userns status'
|
|
|
|
|
end
|
|
|
|
|
|
2018-04-15 06:53:00 +00:00
|
|
|
#
|
|
|
|
|
# Returns true if Address Space Layout Randomization (ASLR) is enabled
|
2018-04-14 07:33:29 +00:00
|
|
|
#
|
|
|
|
|
# @return [Boolean]
|
|
|
|
|
#
|
|
|
|
|
def aslr_enabled?
|
2021-08-31 19:25:35 +05:30
|
|
|
aslr = read_file('/proc/sys/kernel/randomize_va_space').to_s.strip
|
2018-04-14 07:33:29 +00:00
|
|
|
(aslr.eql?('1') || aslr.eql?('2'))
|
|
|
|
|
rescue
|
|
|
|
|
raise 'Could not determine ASLR status'
|
|
|
|
|
end
|
|
|
|
|
|
2018-12-03 03:51:14 +00:00
|
|
|
#
|
|
|
|
|
# Returns true if Exec-Shield is enabled
|
|
|
|
|
#
|
|
|
|
|
# @return [Boolean]
|
|
|
|
|
#
|
|
|
|
|
def exec_shield_enabled?
|
2021-08-31 19:25:35 +05:30
|
|
|
exec_shield = read_file('/proc/sys/kernel/exec-shield').to_s.strip
|
2018-12-03 03:51:14 +00:00
|
|
|
(exec_shield.eql?('1') || exec_shield.eql?('2'))
|
|
|
|
|
rescue
|
|
|
|
|
raise 'Could not determine exec-shield status'
|
|
|
|
|
end
|
|
|
|
|
|
2018-04-14 07:33:29 +00:00
|
|
|
#
|
|
|
|
|
# Returns true if unprivileged bpf is disabled
|
|
|
|
|
#
|
|
|
|
|
# @return [Boolean]
|
|
|
|
|
#
|
|
|
|
|
def unprivileged_bpf_disabled?
|
2021-08-26 14:24:04 -05:00
|
|
|
unprivileged_bpf_disabled = read_file('/proc/sys/kernel/unprivileged_bpf_disabled').to_s.strip
|
2021-08-21 09:29:48 +00:00
|
|
|
return (unprivileged_bpf_disabled == '1' || unprivileged_bpf_disabled == '2')
|
2018-04-14 07:33:29 +00:00
|
|
|
rescue
|
|
|
|
|
raise 'Could not determine kernel.unprivileged_bpf_disabled status'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Returns true if kernel pointer restriction is enabled
|
|
|
|
|
#
|
|
|
|
|
# @return [Boolean]
|
|
|
|
|
#
|
|
|
|
|
def kptr_restrict?
|
2021-08-31 19:25:35 +05:30
|
|
|
read_file('/proc/sys/kernel/kptr_restrict').to_s.strip.eql? '1'
|
2018-04-14 07:33:29 +00:00
|
|
|
rescue
|
|
|
|
|
raise 'Could not determine kernel.kptr_restrict status'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Returns true if dmesg restriction is enabled
|
|
|
|
|
#
|
|
|
|
|
# @return [Boolean]
|
|
|
|
|
#
|
|
|
|
|
def dmesg_restrict?
|
2021-08-31 19:25:35 +05:30
|
|
|
read_file('/proc/sys/kernel/dmesg_restrict').to_s.strip.eql? '1'
|
2018-04-14 07:33:29 +00:00
|
|
|
rescue
|
|
|
|
|
raise 'Could not determine kernel.dmesg_restrict status'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Returns mmap minimum address
|
|
|
|
|
#
|
|
|
|
|
# @return [Integer]
|
|
|
|
|
#
|
|
|
|
|
def mmap_min_addr
|
2021-08-31 19:25:35 +05:30
|
|
|
mmap_min_addr = read_file('/proc/sys/vm/mmap_min_addr').to_s.strip
|
2018-04-14 07:33:29 +00:00
|
|
|
return 0 unless mmap_min_addr =~ /\A\d+\z/
|
|
|
|
|
mmap_min_addr
|
|
|
|
|
rescue
|
|
|
|
|
raise 'Could not determine system mmap_min_addr'
|
|
|
|
|
end
|
|
|
|
|
|
2018-12-07 14:42:16 +00:00
|
|
|
#
|
|
|
|
|
# Returns true if Linux Kernel Runtime Guard (LKRG) kernel module is installed
|
|
|
|
|
#
|
|
|
|
|
def lkrg_installed?
|
2021-08-31 19:25:35 +05:30
|
|
|
directory?('/proc/sys/lkrg')
|
2018-12-07 14:42:16 +00:00
|
|
|
rescue
|
|
|
|
|
raise 'Could not determine LKRG status'
|
|
|
|
|
end
|
|
|
|
|
|
2018-12-02 08:11:17 +00:00
|
|
|
#
|
|
|
|
|
# Returns true if grsecurity is installed
|
|
|
|
|
#
|
|
|
|
|
def grsec_installed?
|
|
|
|
|
cmd_exec('test -c /dev/grsec && echo true').to_s.strip.include? 'true'
|
|
|
|
|
rescue
|
|
|
|
|
raise 'Could not determine grsecurity status'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Returns true if PaX is installed
|
|
|
|
|
#
|
|
|
|
|
def pax_installed?
|
2021-08-31 19:25:35 +05:30
|
|
|
read_file('/proc/self/status').to_s.include? 'PaX:'
|
2018-12-02 08:11:17 +00:00
|
|
|
rescue
|
|
|
|
|
raise 'Could not determine PaX status'
|
|
|
|
|
end
|
|
|
|
|
|
2018-04-14 07:33:29 +00:00
|
|
|
#
|
|
|
|
|
# Returns true if SELinux is installed
|
|
|
|
|
#
|
|
|
|
|
# @return [Boolean]
|
|
|
|
|
#
|
|
|
|
|
def selinux_installed?
|
|
|
|
|
cmd_exec('id').to_s.include? 'context='
|
|
|
|
|
rescue
|
|
|
|
|
raise 'Could not determine SELinux status'
|
|
|
|
|
end
|
2018-04-18 07:22:20 +00:00
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Returns true if SELinux is in enforcing mode
|
|
|
|
|
#
|
|
|
|
|
# @return [Boolean]
|
|
|
|
|
#
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Returns true if Yama is installed
|
|
|
|
|
#
|
|
|
|
|
# @return [Boolean]
|
|
|
|
|
#
|
|
|
|
|
def yama_installed?
|
2021-08-31 19:46:38 +05:30
|
|
|
ptrace_scope = read_file('/proc/sys/kernel/yama/ptrace_scope').to_s.strip
|
2018-04-18 07:22:20 +00:00
|
|
|
return true if ptrace_scope =~ /\A\d\z/
|
|
|
|
|
false
|
|
|
|
|
rescue
|
|
|
|
|
raise 'Could not determine Yama status'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# Returns true if Yama is enabled
|
|
|
|
|
#
|
|
|
|
|
# @return [Boolean]
|
|
|
|
|
#
|
|
|
|
|
def yama_enabled?
|
|
|
|
|
return false unless yama_installed?
|
2021-08-31 19:25:35 +05:30
|
|
|
!read_file('/proc/sys/kernel/yama/ptrace_scope').to_s.strip.eql? '0'
|
2018-04-18 07:22:20 +00:00
|
|
|
rescue
|
|
|
|
|
raise 'Could not determine Yama status'
|
|
|
|
|
end
|
2018-04-04 17:13:49 +00:00
|
|
|
end # Kernel
|
|
|
|
|
end # Linux
|
|
|
|
|
end # Post
|
|
|
|
|
end # Msf
|