2016-04-14 17:55:14 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
2019-03-01 12:12:42 +01:00
|
|
|
# run-shellcheck
|
2016-04-14 17:55:14 +02:00
|
|
|
#
|
2019-02-06 15:19:14 +01:00
|
|
|
# CIS Debian Hardening
|
2016-04-14 17:55:14 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
#
|
2017-10-31 17:44:15 +01:00
|
|
|
# 8.2.4 Create and Set Permissions on syslog-ng Log Files (Scored)
|
2016-04-14 17:55:14 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
set -e # One error, it's over
|
|
|
|
set -u # One variable unset, it's over
|
|
|
|
|
2019-03-01 12:12:42 +01:00
|
|
|
# shellcheck disable=2034
|
2017-05-18 18:40:09 +02:00
|
|
|
HARDENING_LEVEL=3
|
2019-03-01 12:12:42 +01:00
|
|
|
# shellcheck disable=2034
|
2017-10-31 17:44:15 +01:00
|
|
|
DESCRIPTION="Create and set permissions on syslog-ng logfiles."
|
2017-05-18 18:40:09 +02:00
|
|
|
|
2019-03-01 12:12:42 +01:00
|
|
|
PERMISSIONS=''
|
|
|
|
USER=''
|
|
|
|
GROUP=''
|
|
|
|
EXCEPTIONS=''
|
2016-04-14 17:55:14 +02:00
|
|
|
|
|
|
|
# This function will be called if the script status is on enabled / audit mode
|
|
|
|
audit () {
|
2019-03-01 12:12:42 +01:00
|
|
|
FILES=$(grep "file(" "$SYSLOG_BASEDIR"/syslog-ng.conf | grep '"' | cut -d'"' -f 2)
|
2016-04-14 17:55:14 +02:00
|
|
|
for FILE in $FILES; do
|
2019-03-01 12:12:42 +01:00
|
|
|
does_file_exist "$FILE"
|
|
|
|
if [ "$FNRET" != 0 ]; then
|
2018-03-22 18:17:17 +01:00
|
|
|
warn "$FILE does not exist"
|
2016-04-14 17:55:14 +02:00
|
|
|
else
|
2019-03-01 12:12:42 +01:00
|
|
|
FOUND_EXC=0
|
2019-03-18 16:19:05 +01:00
|
|
|
if grep -q "$FILE" <(tr ' ' '\n' <<< "$EXCEPTIONS" | cut -d ":" -f 1); then
|
2019-03-01 12:12:42 +01:00
|
|
|
debug "$FILE is found in exceptions"
|
|
|
|
debug "Setting special user:group:perm"
|
|
|
|
FOUND_EXC=1
|
|
|
|
local user_bak="$USER"
|
|
|
|
local group_bak="$GROUP"
|
|
|
|
local perm_bak="$PERMISSIONS"
|
|
|
|
USER="$(tr ' ' '\n' <<< "$EXCEPTIONS" | grep "$FILE" | cut -d':' -f 2)"
|
|
|
|
GROUP="$(tr ' ' '\n' <<< "$EXCEPTIONS" | grep "$FILE" | cut -d':' -f 3)"
|
|
|
|
PERMISSIONS="$(tr ' ' '\n' <<< "$EXCEPTIONS" | grep "$FILE" | cut -d':' -f 4)"
|
|
|
|
fi
|
|
|
|
has_file_correct_ownership "$FILE" "$USER" "$GROUP"
|
|
|
|
if [ "$FNRET" = 0 ]; then
|
|
|
|
ok "$FILE has correct ownership ($USER:$GROUP)"
|
2016-04-20 14:36:55 +02:00
|
|
|
else
|
2016-04-25 15:15:49 +02:00
|
|
|
crit "$FILE ownership was not set to $USER:$GROUP"
|
2016-04-20 14:36:55 +02:00
|
|
|
fi
|
2019-03-01 12:12:42 +01:00
|
|
|
has_file_correct_permissions "$FILE" "$PERMISSIONS"
|
|
|
|
if [ "$FNRET" = 0 ]; then
|
|
|
|
ok "$FILE has correct permissions ($PERMISSIONS)"
|
2016-04-20 14:36:55 +02:00
|
|
|
else
|
2016-04-21 18:32:36 +02:00
|
|
|
crit "$FILE permissions were not set to $PERMISSIONS"
|
2019-03-01 12:12:42 +01:00
|
|
|
fi
|
|
|
|
if [ "$FOUND_EXC" = 1 ]; then
|
|
|
|
debug "Resetting user:group:perm"
|
|
|
|
USER="$user_bak"
|
|
|
|
GROUP="$group_bak"
|
|
|
|
PERMISSIONS="$perm_bak"
|
|
|
|
fi
|
2016-04-14 17:55:14 +02:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# This function will be called if the script status is on enabled mode
|
|
|
|
apply () {
|
|
|
|
for FILE in $FILES; do
|
2019-03-01 12:12:42 +01:00
|
|
|
does_file_exist "$FILE"
|
|
|
|
if [ "$FNRET" != 0 ]; then
|
2016-04-14 17:55:14 +02:00
|
|
|
info "$FILE does not exist"
|
2019-03-01 13:08:07 +01:00
|
|
|
filedir=$(dirname "${FILE#/var/log/}")
|
|
|
|
if [ ! "$filedir" = "." ] && [ ! -d /var/log/"$filedir" ]; then
|
|
|
|
debug "Creating /var/log/$filedir for $FILE"
|
2019-03-18 16:19:05 +01:00
|
|
|
debug "mkdir -p /var/log/$filedir"
|
2019-03-01 13:08:07 +01:00
|
|
|
mkdir -p /var/log/"$filedir"
|
|
|
|
fi
|
2019-03-01 12:12:42 +01:00
|
|
|
touch "$FILE"
|
2016-04-14 17:55:14 +02:00
|
|
|
fi
|
2019-03-01 12:12:42 +01:00
|
|
|
FOUND_EXC=0
|
|
|
|
if grep "$FILE" <(tr ' ' '\n' <<< "$EXCEPTIONS" | cut -d ":" -f 1); then
|
|
|
|
debug "$FILE is found in exceptions"
|
|
|
|
debug "Setting special user:group:perm"
|
|
|
|
FOUND_EXC=1
|
|
|
|
local user_bak="$USER"
|
|
|
|
local group_bak="$GROUP"
|
|
|
|
local perm_bak="$PERMISSIONS"
|
|
|
|
USER="$(tr ' ' '\n' <<< "$EXCEPTIONS" | grep "$FILE" | cut -d':' -f 2)"
|
|
|
|
GROUP="$(tr ' ' '\n' <<< "$EXCEPTIONS" | grep "$FILE" | cut -d':' -f 3)"
|
|
|
|
PERMISSIONS="$(tr ' ' '\n' <<< "$EXCEPTIONS" | grep "$FILE" | cut -d':' -f 4)"
|
|
|
|
fi
|
|
|
|
has_file_correct_ownership "$FILE" "$USER" "$GROUP"
|
|
|
|
if [ "$FNRET" = 0 ]; then
|
2016-04-14 17:55:14 +02:00
|
|
|
ok "$FILE has correct ownership"
|
|
|
|
else
|
2016-04-25 15:15:49 +02:00
|
|
|
warn "fixing $FILE ownership to $USER:$GROUP"
|
2019-03-01 12:12:42 +01:00
|
|
|
chown "$USER":"$GROUP" "$FILE"
|
2016-04-14 17:55:14 +02:00
|
|
|
fi
|
2019-03-01 12:12:42 +01:00
|
|
|
has_file_correct_permissions "$FILE" "$PERMISSIONS"
|
|
|
|
if [ "$FNRET" = 0 ]; then
|
2016-04-14 17:55:14 +02:00
|
|
|
ok "$FILE has correct permissions"
|
|
|
|
else
|
|
|
|
info "fixing $FILE permissions to $PERMISSIONS"
|
2019-03-01 12:12:42 +01:00
|
|
|
chmod 0"$PERMISSIONS" "$FILE"
|
|
|
|
fi
|
|
|
|
if [ "$FOUND_EXC" = 1 ]; then
|
|
|
|
debug "Resetting user:group:perm"
|
|
|
|
USER="$user_bak"
|
|
|
|
GROUP="$group_bak"
|
|
|
|
PERMISSIONS="$perm_bak"
|
2016-04-14 17:55:14 +02:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2017-05-18 18:40:09 +02:00
|
|
|
# This function will create the config file for this check with default values
|
|
|
|
create_config() {
|
|
|
|
cat <<EOF
|
2019-02-14 14:33:21 +01:00
|
|
|
status=audit
|
2017-05-18 18:40:09 +02:00
|
|
|
SYSLOG_BASEDIR='/etc/syslog-ng'
|
2019-03-01 12:12:42 +01:00
|
|
|
PERMISSIONS='640'
|
|
|
|
USER='root'
|
|
|
|
GROUP='adm'
|
|
|
|
# Put exceptions here with file:user:group:permissions
|
|
|
|
# example: /dev/null:root:root:666
|
|
|
|
EXCEPTIONS=''
|
2017-05-18 18:40:09 +02:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2016-04-14 17:55:14 +02:00
|
|
|
# This function will check config parameters required
|
|
|
|
check_config() {
|
2019-03-01 12:12:42 +01:00
|
|
|
does_user_exist "$USER"
|
|
|
|
if [ "$FNRET" != 0 ]; then
|
2016-04-14 17:55:14 +02:00
|
|
|
crit "$USER does not exist"
|
|
|
|
exit 128
|
|
|
|
fi
|
|
|
|
does_group_exist $GROUP
|
2019-03-01 12:12:42 +01:00
|
|
|
if [ "$FNRET" != 0 ]; then
|
2016-04-14 17:55:14 +02:00
|
|
|
crit "$GROUP does not exist"
|
|
|
|
exit 128
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Source Root Dir Parameter
|
2017-10-25 14:50:39 +02:00
|
|
|
if [ -r /etc/default/cis-hardening ]; then
|
2016-04-18 17:39:14 +02:00
|
|
|
. /etc/default/cis-hardening
|
2017-10-25 14:50:39 +02:00
|
|
|
fi
|
|
|
|
if [ -z "$CIS_ROOT_DIR" ]; then
|
2017-11-17 15:13:27 +01:00
|
|
|
echo "There is no /etc/default/cis-hardening file nor cis-hardening directory in current environment."
|
2017-10-25 14:50:39 +02:00
|
|
|
echo "Cannot source CIS_ROOT_DIR variable, aborting."
|
|
|
|
exit 128
|
|
|
|
fi
|
2016-04-14 17:55:14 +02:00
|
|
|
|
|
|
|
# Main function, will call the proper functions given the configuration (audit, enabled, disabled)
|
2019-03-01 12:12:42 +01:00
|
|
|
if [ -r "$CIS_ROOT_DIR"/lib/main.sh ]; then
|
|
|
|
# shellcheck source=/opt/debian-cis/lib/main.sh
|
|
|
|
. "$CIS_ROOT_DIR"/lib/main.sh
|
2016-04-21 23:19:50 +02:00
|
|
|
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
|