|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# run-shellcheck |
| 4 | +# |
| 5 | +# CIS Debian Hardening |
| 6 | +# |
| 7 | + |
| 8 | +# |
| 9 | +# Ensure all groups in /etc/passwd exist in /etc/group (Manual) |
| 10 | +# |
| 11 | + |
| 12 | +set -e # One error, it's over |
| 13 | +set -u # One variable unset, it's over |
| 14 | + |
| 15 | +# shellcheck disable=2034 |
| 16 | +HARDENING_LEVEL=1 |
| 17 | +# shellcheck disable=2034 |
| 18 | +DESCRIPTION="Ensure all groups in /etc/passwd exist in /etc/group" |
| 19 | + |
| 20 | +# This function will be called if the script status is on enabled / audit mode |
| 21 | +audit() { |
| 22 | + local invalid_group_gid="" |
| 23 | + local passwd_group_gid="" |
| 24 | + local group_gid="" |
| 25 | + |
| 26 | + # straight from the debian CIS pdf, works fine as is |
| 27 | + passwd_group_gid=("$(awk -F: '{print $4}' /etc/passwd | sort -u)") |
| 28 | + group_gid=("$(awk -F: '{print $3}' /etc/group | sort -u)") |
| 29 | + passwd_group_diff=("$(printf '%s\n' "${group_gid[@]}" "${passwd_group_gid[@]}" | sort | uniq -u)") |
| 30 | + |
| 31 | + while IFS= read -r l_gid; do |
| 32 | + invalid_group_gid=$(awk -F: '($4 == '"$l_gid"')' /etc/passwd) |
| 33 | + if [ -n "$invalid_group_gid" ]; then |
| 34 | + crit "group with gid $invalid_group_gid is present in /etc/passwd but absent from /etc/group" |
| 35 | + fi |
| 36 | + done < <(printf '%s\n' "${passwd_group_gid[@]}" "${passwd_group_diff[@]}" | sort | uniq -D | uniq) |
| 37 | + |
| 38 | +} |
| 39 | + |
| 40 | +# This function will be called if the script status is on enabled mode |
| 41 | +apply() { |
| 42 | + # the CIS recommendation is to do it in an automated way, while also "Investigate to determine if the account is logged in and what it is being used for, to |
| 43 | + # determine if it needs to be forced off" |
| 44 | + # so we do this manually |
| 45 | + info "Please review the faulty accounts and update their password configuration, or set them as exceptions in the configuration" |
| 46 | +} |
| 47 | + |
| 48 | +# This function will check config parameters required |
| 49 | +check_config() { |
| 50 | + : |
| 51 | +} |
| 52 | + |
| 53 | +# Source Root Dir Parameter |
| 54 | +if [ -r /etc/default/cis-hardening ]; then |
| 55 | + # shellcheck source=../../debian/default |
| 56 | + . /etc/default/cis-hardening |
| 57 | +fi |
| 58 | +if [ -z "$CIS_LIB_DIR" ]; then |
| 59 | + echo "There is no /etc/default/cis-hardening file nor cis-hardening directory in current environment." |
| 60 | + echo "Cannot source CIS_LIB_DIR variable, aborting." |
| 61 | + exit 128 |
| 62 | +fi |
| 63 | + |
| 64 | +# Main function, will call the proper functions given the configuration (audit, enabled, disabled) |
| 65 | +if [ -r "${CIS_LIB_DIR}"/main.sh ]; then |
| 66 | + # shellcheck source=../../lib/main.sh |
| 67 | + . "${CIS_LIB_DIR}"/main.sh |
| 68 | +else |
| 69 | + echo "Cannot find main.sh, have you correctly defined your root directory? Current value is $CIS_LIB_DIR in /etc/default/cis-hardening" |
| 70 | + exit 128 |
| 71 | +fi |
0 commit comments