4.2.4_logs_permissions

This commit is contained in:
Thibault Ayanides 2020-10-05 13:17:44 +02:00
parent 922f28c200
commit d6e5803252
3 changed files with 87 additions and 1 deletions

View File

@ -0,0 +1,62 @@
#!/bin/bash
#
# CIS Debian Hardening
#
#
# 4.2.4 Ensure permissions on all logfiles are configured (Scored)
#
set -e # One error, it's over
set -u # One variable unset, it's over
HARDENING_LEVEL=2
DESCRIPTION="Check permissions on logs (other has no permissions on any files andgroup does not have write or execute permissions on any file)"
DIR='/var/log'
PERMISSIONS='640'
# This function will be called if the script status is on enabled / audit mode
audit () {
have_files_in_dir_correct_permissions $DIR $PERMISSIONS
if [ $FNRET = 0 ]; then
ok "Logs in $DIR have correct permissions"
else
crit "Some logs in $DIR permissions were not set to $PERMISSIONS"
fi
}
# This function will be called if the script status is on enabled mode
apply () {
have_files_in_dir_correct_permissions $DIR $PERMISSIONS
if [ $FNRET = 0 ]; then
ok "$FILE has correct permissions"
else
info "fixing $DIR logs permissions to $PERMISSIONS"
find $DIR -type f -exec chmod 0$PERMISSIONS {} \;
fi
}
# This function will check config parameters required
check_config() {
:
}
# 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

View File

@ -88,13 +88,27 @@ has_file_correct_permissions() {
local FILE=$1 local FILE=$1
local PERMISSIONS=$2 local PERMISSIONS=$2
if [ $($SUDO_CMD stat -L -c "%a" $1) = "$PERMISSIONS" ]; then if [ $($SUDO_CMD stat -L -c "%a" $FILE) = "$PERMISSIONS" ]; then
FNRET=0 FNRET=0
else else
FNRET=1 FNRET=1
fi fi
} }
have_files_in_dir_correct_permissions(){
local DIR=$1
local PERMISSIONS=$2
FNRET=0
for perm in $($SUDO_CMD find "$DIR" -type f -exec stat -L -c "%a" {} \;);
do
if [ "$perm" != "$PERMISSIONS" ]; then
FNRET=1
break
fi
done
}
does_pattern_exist_in_file_nocase() { does_pattern_exist_in_file_nocase() {
_does_pattern_exist_in_file "-Ei" $* _does_pattern_exist_in_file "-Ei" $*
} }

View File

@ -0,0 +1,10 @@
# run-shellcheck
test_audit() {
describe Running on blank host
register_test retvalshouldbe 0
dismiss_count_for_test
# shellcheck disable=2154
run blank /opt/debian-cis/bin/hardening/"${script}".sh --audit-all
# TODO fill comprehensive tests
}