2020-06-04 15:00:35 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
#
|
2023-06-12 19:23:56 +02:00
|
|
|
# harbian-audit for Debian GNU/Linux 9/10/11/12 or CentOS 8 Hardening
|
2020-06-04 15:00:35 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
#
|
|
|
|
# 4.7 Enable SELinux targeted policy (Scored)
|
|
|
|
# Add by Author : Samson-W (samson@hardenedlinux.org)
|
|
|
|
#
|
|
|
|
|
|
|
|
set -e # One error, it's over
|
|
|
|
set -u # One variable unset, it's over
|
|
|
|
|
|
|
|
HARDENING_LEVEL=3
|
|
|
|
|
|
|
|
SELINUXCONF_FILE='/etc/selinux/config'
|
|
|
|
SELINUXTYPE_VALUE='SELINUXTYPE=default'
|
|
|
|
|
|
|
|
audit_debian () {
|
2020-06-29 12:27:51 +02:00
|
|
|
set +e
|
2020-06-29 11:51:19 +02:00
|
|
|
check_aa_status
|
2020-06-29 12:27:51 +02:00
|
|
|
set -e
|
2020-06-29 11:51:19 +02:00
|
|
|
if [ $FNRET = 0 ]; then
|
|
|
|
ok "AppArmor was actived. So pass."
|
|
|
|
return 0
|
2020-06-04 15:00:35 +02:00
|
|
|
fi
|
|
|
|
does_valid_pattern_exist_in_file $SELINUXCONF_FILE $SELINUXTYPE_VALUE
|
|
|
|
if [ ${FNRET} -eq 0 ]; then
|
|
|
|
ok "SELinux targeted policy was enabled."
|
|
|
|
FNRET=0
|
|
|
|
else
|
|
|
|
crit "SELinux targeted policy is not enable."
|
|
|
|
FNRET=1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
audit_centos () {
|
|
|
|
does_valid_pattern_exist_in_file $SELINUXCONF_FILE $SELINUXTYPE_VALUE
|
|
|
|
if [ ${FNRET} -eq 0 ]; then
|
|
|
|
ok "SELinux targeted policy was enabled."
|
|
|
|
FNRET=0
|
|
|
|
else
|
|
|
|
crit "SELinux targeted policy is not enable."
|
|
|
|
FNRET=1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# This function will be called if the script status is on enabled / audit mode
|
|
|
|
audit () {
|
2023-06-12 19:23:56 +02:00
|
|
|
if [ $OS_RELEASE -eq 2 ]; then
|
2020-06-04 15:00:35 +02:00
|
|
|
audit_centos
|
|
|
|
else
|
2023-06-12 19:23:56 +02:00
|
|
|
audit_debian
|
2020-06-04 15:00:35 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
apply_debian () {
|
2020-07-02 18:47:28 +02:00
|
|
|
set +e
|
2020-06-29 11:51:19 +02:00
|
|
|
check_aa_status
|
2020-07-02 18:47:28 +02:00
|
|
|
set -e
|
2020-06-29 11:51:19 +02:00
|
|
|
if [ $FNRET = 0 ]; then
|
|
|
|
ok "AppArmor was actived. So pass."
|
|
|
|
return 0
|
2020-06-04 15:00:35 +02:00
|
|
|
fi
|
|
|
|
if [ $FNRET = 0 ]; then
|
|
|
|
ok "SELinux targeted policy was enabled."
|
|
|
|
elif [ $FNRET = 1 ]; then
|
2020-06-05 10:34:54 +02:00
|
|
|
warn "Set SELinux targeted policy to enable, and need reboot"
|
2020-06-04 15:00:35 +02:00
|
|
|
replace_in_file $SELINUXCONF_FILE 'SELINUXTYPE=.*' $SELINUXTYPE_VALUE
|
|
|
|
else
|
|
|
|
:
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
apply_centos () {
|
|
|
|
if [ $FNRET = 0 ]; then
|
|
|
|
ok "SELinux targeted policy was enabled."
|
|
|
|
elif [ $FNRET = 1 ]; then
|
2020-06-05 10:34:54 +02:00
|
|
|
warn "Set SELinux targeted policy to enable, and need reboot"
|
2020-06-04 15:00:35 +02:00
|
|
|
replace_in_file $SELINUXCONF_FILE 'SELINUXTYPE=.*' $SELINUXTYPE_VALUE
|
|
|
|
else
|
|
|
|
:
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# This function will be called if the script status is on enabled mode
|
|
|
|
apply () {
|
2023-06-12 19:23:56 +02:00
|
|
|
if [ $OS_RELEASE -eq 2 ]; then
|
2020-06-04 15:00:35 +02:00
|
|
|
apply_centos
|
|
|
|
else
|
2023-06-12 19:23:56 +02:00
|
|
|
apply_debian
|
2020-06-04 15:00:35 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# This function will check config parameters required
|
|
|
|
check_config() {
|
|
|
|
if [ $OS_RELEASE -eq 2 ]; then
|
|
|
|
SELINUXTYPE_VALUE='SELINUXTYPE=targeted'
|
|
|
|
else
|
|
|
|
:
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# 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
|