diff --git a/bin/hardening/2.27_nfs_nosuid.sh b/bin/hardening/2.27_nfs_nosuid.sh new file mode 100755 index 0000000..35d5b9b --- /dev/null +++ b/bin/hardening/2.27_nfs_nosuid.sh @@ -0,0 +1,86 @@ +#!/bin/bash + +# +# harbian audit 9 Hardening +# + +# +# 2.27 Set nosuid option for nfs filesystem/Partition (Scored) +# Authors : Samson wen, Samson +# todo: Multiple cases + +set -e # One error, it's over +set -u # One variable unset, it's over + +HARDENING_LEVEL=2 + +# Quick factoring as many script use the same logic +PARTITION_TYPE="nfs" +OPTION="nosuid" + +# This function will be called if the script status is on enabled / audit mode +audit () { + info "Verifying that $PARTITION_TYPE is a filesystem/partition" + is_mounted "$PARTITION_TYPE" + if [ $FNRET -gt 0 ]; then + no_entity " There is no mount directory with file system type $PARTITION_TYPE" + FNRET=2 + else + ok "$PARTITION_TYPE is a partition" + has_mount_option $PARTITION_TYPE $OPTION + if [ $FNRET -gt 0 ]; then + crit "$PARTITION_TYPE has no option $OPTION in fstab!" + FNRET=1 + else + ok "$PARTITION_TYPE has $OPTION in fstab" + has_mounted_option $PARTITION_TYPE $OPTION + if [ $FNRET -gt 0 ]; then + warn "$PARTITION_TYPE is not mounted with $OPTION at runtime" + FNRET=3 + else + ok "$PARTITION_TYPE mounted with $OPTION" + fi + fi + fi +} + +# This function will be called if the script status is on enabled mode +apply () { + if [ $FNRET = 0 ]; then + ok "$PARTITION_TYPE is correctly set" + elif [ $FNRET = 2 ]; then + no_entity " There is no mount directory with file system type $PARTITION_TYPE" + elif [ $FNRET = 1 ]; then + info "Adding $OPTION to fstab" + add_option_to_fstab $PARTITION_TYPE $OPTION + info "Remounting $PARTITION_TYPE from fstab" + remount_partition $PARTITION_TYPE + elif [ $FNRET = 3 ]; then + info "Remounting $PARTITION_TYPE from fstab" + remount_partition $PARTITION_TYPE + fi +} + +# This function will check config parameters required +check_config() { + # No param for this script + : +} + +# Source Root Dir Parameter +if [ -r /etc/default/cis-hardening ]; then + . /etc/default/cis-hardening +fi +if [ -z "$CIS_ROOT_DIR" ]; then + echo "There is no /etc/default/cis-hardening file nor cis-hardening directory in current environment." + echo "Cannot source CIS_ROOT_DIR variable, aborting." + exit 128 +fi + +# Main function, will call the proper functions given the configuration (audit, enabled, disabled) +if [ -r $CIS_ROOT_DIR/lib/main.sh ]; then + . $CIS_ROOT_DIR/lib/main.sh +else + echo "Cannot find main.sh, have you correctly defined your root directory? Current value is $CIS_ROOT_DIR in /etc/default/cis-hardening" + exit 128 +fi diff --git a/lib/utils.sh b/lib/utils.sh index 70465b4..f50969a 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -266,7 +266,7 @@ is_kernel_option_enabled() { # Verify $1 is a partition declared in fstab is_a_partition() { - local PARTITION_NAME=$1 + local PARTITION=$1 FNRET=128 if $(grep "[[:space:]]$1[[:space:]]" /etc/fstab | grep -vqE "^#"); then debug "$PARTITION found in fstab" @@ -279,7 +279,7 @@ is_a_partition() { # Verify that $1 is mounted at runtime is_mounted() { - local PARTITION_NAME=$1 + local PARTITION=$1 if $(grep -q "[[:space:]]$1[[:space:]]" /proc/mounts); then debug "$PARTITION found in /proc/mounts, it's mounted" FNRET=0 @@ -341,6 +341,19 @@ add_option_to_fstab() { sed -ie "s;\(.*\)\(\s*\)\s\($PARTITION\)\s\(\s*\)\(\w*\)\(\s*\)\(\w*\)*;\1\2 \3 \4\5\6\7,$OPTION;" /etc/fstab } +# Setup mount option in fstab (todo) +add_option_to_fstab_by_type() { + local FSTYPE=$1 + local OPTION=$2 + debug "Setting $OPTION for $FSTYPE in fstab by filesystem type" + backup_file "/etc/fstab" + # For example : + # /dev/sda9 /storage nfs auto,acl,errors=remount-ro 0 2 + # /dev/sda9 /storage nfs auto,acl,errors=remount-ro,nosuid 0 2 + debug "Sed command : sed -ie \"s;\(.*\)\(\s*\)\s\(\s*)\s\(\s*\)\(\w*\)\(\s*\)\(\w*\)*;\1\2 \3 \4\5\6\7,$OPTION;\" /etc/fstab" + sed -ie "s;\(.*\)\(\s*\)\s\(\s*\)\s\(\s*\)\(\w*\)\(\s*\)\(\w*\)*;\1\2 \3 \4\5\6\7,$OPTION;" /etc/fstab +} + remount_partition() { local PARTITION=$1 debug "Remounting $PARTITION"