#! /usr/bin/env bash{a_output=();a_output2=();a_modprope_config=();a_excluded=();a_available_modules=()a_ignore=("xfs""vfat""ext2""ext3""ext4")a_cve_exists=("afs""ceph""cifs""exfat""fat""fscache""fuse""gfs2")f_module_chk(){l_out2="";grep-Pq--"\b$l_mod_name\b"<<<"${a_cve_exists[*]}"&&l_out2=" <- CVE exists!"if!grep-Pq--'\bblacklist\h+'"$l_mod_name"'\b'<<<"${a_modprope_config[*]}";thena_output2+=(" - Kernel module: \"$l_mod_name\" is not fully disabled $l_out2")elif!grep-Pq--'\binstall\h+'"$l_mod_name"'\h+(\/usr)?\/bin\/(false|true)\b'<<<"${a_modprope_config[*]}";thena_output2+=(" - Kernel module: \"$l_mod_name\" is not fully disabled $l_out2")fiiflsmod|grep"$l_mod_name"&>/dev/null;then# Check if the module is currently loadedl_output2+=(" - Kernel module: \"$l_mod_name\" is loaded""")fi}whileIFS=read-r-d$'\0'l_module_dir;doa_available_modules+=("$(basename"$l_module_dir")")done<<(find"$(readlink-f/lib/modules/"$(uname-r)"/kernel/fs)"-mindepth1-maxdepth1-typed!-empty-print0)whileIFS=read-rl_exclude;doifgrep-Pq--"\b$l_exclude\b"<<<"${a_cve_exists[*]}";thena_output2+=(" - ** WARNING: kernel module: \"$l_exclude\" has a CVE and is currently mounted! **")elifgrep-Pq--"\b$l_exclude\b"<<<"${a_available_modules[*]}";thena_output+=(" - Kernel module: \"$l_exclude\" is currently mounted - do NOT unload or disable")fi!grep-Pq--"\b$l_exclude\b"<<<"${a_ignore[*]}"&&a_ignore+=("$l_exclude")done<<(findmnt-knD|awk'{print $2}'|sort-u)whileIFS=read-rl_config;doa_modprope_config+=("$l_config")done<<(modprobe--showconfig|grep-P'^\h*(blacklist|install)')forl_mod_namein"${a_available_modules[@]}";do# Iterate over all filesystem modules[["$l_mod_name"=~overlay]]&&l_mod_name="${l_mod_name::-2}"ifgrep-Pq--"\b$l_mod_name\b"<<<"${a_ignore[*]}";thena_excluded+=(" - Kernel module: \"$l_mod_name\"")elsef_module_chk
fidone["${#a_excluded[@]}"-gt0]&&printf'%s\n'""" -- INFO --"\"Thefollowingintentionallyskipped" \"${a_excluded[@]}"if["${#a_output2[@]}"-le0];thenprintf'%s\n'""" - No unused filesystem kernel modules are enabled""${a_output[@]}"""elseprintf'%s\n'"""-- Audit Result: --"" ** REVIEW the following **""${a_output2[@]}"["${#a_output[@]}"-gt0]&&printf'%s\n'"""-- Correctly set: --""${a_output[@]}"""fi}
WARNING: disabling or denylisting filesystem modules that are in use on the system may be FATAL. It is extremely important to thoroughly review this list.
- IF - the module is available in the running kernel:
Unload the filesystem kernel module from the kernel
Create a file ending in .conf with install filesystem kernel modules /bin/false in the /etc/modprobe.d/ directory
Create a file ending in .conf with deny list filesystem kernel modules in the /etc/modprobe.d/ directory
WARNING: unloading, disabling or denylisting filesystem modules that are in use on the system maybe FATAL. It is extremely important to thoroughly review the filesystems returned by the audit before following the remediation procedure.
#!/usr/bin/env bash{a_output2=();a_output3=();l_dl=""# Initialize arrays and clear variablesl_mod_name="gfs2"# set module namel_mod_type="fs"# set module typel_mod_path="$(readlink-f/lib/modules/**/kernel/$l_mod_type|sort-u)"f_module_fix(){l_dl="y"# Set to ignore duplicate checksa_showconfig=()# Create array with modprobe outputwhileIFS=read-rl_showconfig;doa_showconfig+=("$l_showconfig")done<<(modprobe--showconfig|grep-P--'\b(install|blacklist)\h+'"${l_mod_name//-/_}"'\b')iflsmod|grep"$l_mod_name"&>/dev/null;then# Check if the module is currently loadeda_output2+=(" - unloading kernel module: \"$l_mod_name\"")modprobe-r"$l_mod_name"2>/dev/null;rmmod"$l_mod_name"2>/dev/null
fiif!grep-Pq--'\binstall\h+'"${l_mod_name//-/_}"'\h+(\/usr)?\/bin\/(true|false)\b'<<<"${a_showconfig[*]}";thena_output2+=(" - setting kernel module: \"$l_mod_name\" to \"$(readlink-f/bin/false)\"")printf'%s\n'"install $l_mod_name$(readlink-f/bin/false)">>/etc/modprobe.d/"$l_mod_name".conf
fiif!grep-Pq--'\bblacklist\h+'"${l_mod_name//-/_}"'\b'<<<"${a_showconfig[*]}";thena_output2+=(" - denylisting kernel module: \"$l_mod_name\"")printf'%s\n'"blacklist $l_mod_name">>/etc/modprobe.d/"$l_mod_name".conf
fi}forl_mod_base_directoryin$l_mod_path;do# Check if the module exists on the systemif[-d"$l_mod_base_directory/${l_mod_name/-/\/}"]&&[-n"$(ls-A"$l_mod_base_directory/${l_mod_name/-/\/}")"];thena_output3+=(" - \"$l_mod_base_directory\"")[["$l_mod_name"=~overlay]]&&l_mod_name="${l_mod_name::-2}"["$l_dl"!="y"]&&f_module_fix
elseecho-e" - kernel module: \"$l_mod_name\" doesn't exist in \"$l_mod_base_directory\""fidone["${#a_output3[@]}"-gt0]&&printf'%s\n'""" -- INFO --"" - module: \"$l_mod_name\" exists in:""${a_output3[@]}"["${#a_output2[@]}"-gt0]&&printf'%s\n'"""${a_output2[@]}"||printf'%s\n'""" - No changes needed"printf'%s\n'""" - remediation of kernel module: \"$l_mod_name\" complete"""}