From 1ec77dbb56b136f4da1e8f2baf6f1323679e61e2 Mon Sep 17 00:00:00 2001 From: Charles Herlin Date: Thu, 28 Mar 2019 17:51:02 +0100 Subject: [PATCH] FIX(13.15): fix code that did not show duplicated group Add tests Apply shellcheck recommendations --- bin/hardening/13.15_check_duplicate_gid.sh | 27 +++++++++++--------- tests/hardening/13.15_check_duplicate_gid.sh | 17 +++++++++++- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/bin/hardening/13.15_check_duplicate_gid.sh b/bin/hardening/13.15_check_duplicate_gid.sh index b17b8c2..f4ca580 100755 --- a/bin/hardening/13.15_check_duplicate_gid.sh +++ b/bin/hardening/13.15_check_duplicate_gid.sh @@ -1,5 +1,5 @@ #!/bin/bash - +# run-shellcheck # # CIS Debian Hardening # @@ -11,28 +11,30 @@ set -e # One error, it's over set -u # One variable unset, it's over +# shellcheck disable=2034 HARDENING_LEVEL=2 +# shellcheck disable=2034 DESCRIPTION="There is no duplicate GIDs." ERRORS=0 # This function will be called if the script status is on enabled / audit mode audit () { - RESULT=$(cat /etc/group | cut -f3 -d":" | sort -n | uniq -c | awk {'print $1":"$2'} ) - for LINE in $RESULT; do + RESULT=$(cut -f3 -d":" /etc/group | sort -n | uniq -c | awk '{print $1":"$2}' ) + for LINE in $RESULT; do debug "Working on line $LINE" - OCC_NUMBER=$(awk -F: {'print $1'} <<< $LINE) - GROUPID=$(awk -F: {'print $2'} <<< $LINE) - if [ $OCC_NUMBER -gt 1 ]; then - USERS=$(awk -F: '($3 == n) { print $1 }' n=$GROUPID /etc/passwd | xargs) + OCC_NUMBER=$(awk -F: '{print $1}' <<< "$LINE") + GROUPID=$(awk -F: '{print $2}' <<< "$LINE") + if [ "$OCC_NUMBER" -gt 1 ]; then + GROUP=$(awk -F: '($3 == n) { print $1 }' n="$GROUPID" /etc/group | xargs) ERRORS=$((ERRORS+1)) - crit "Duplicate GID ($GROUPID): ${USERS}" + crit "Duplicate GID ($GROUPID): ${GROUP}" fi - done + done if [ $ERRORS = 0 ]; then ok "No duplicate GIDs" - fi + fi } # This function will be called if the script status is on enabled mode @@ -56,8 +58,9 @@ if [ -z "$CIS_ROOT_DIR" ]; then 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 +if [ -r "$CIS_ROOT_DIR"/lib/main.sh ]; then + # shellcheck source=/opt/debian-cis/lib/main.sh + . "$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 diff --git a/tests/hardening/13.15_check_duplicate_gid.sh b/tests/hardening/13.15_check_duplicate_gid.sh index b333419..ce9313d 100644 --- a/tests/hardening/13.15_check_duplicate_gid.sh +++ b/tests/hardening/13.15_check_duplicate_gid.sh @@ -6,5 +6,20 @@ test_audit() { # shellcheck disable=2154 run blank /opt/debian-cis/bin/hardening/"${script}".sh --audit-all - # TODO fill comprehensive tests + + groupadd -f -g 120 grouptest + groupadd -fo -g 120 grouptest2 + + describe Duplicated groups + register_test retvalshouldbe 1 + run duplicated /opt/debian-cis/bin/hardening/"${script}".sh --audit-all + + # Cleanup + groupdel grouptest + groupdel grouptest2 + + describe Compliant state + register_test retvalshouldbe 0 + run resolved /opt/debian-cis/bin/hardening/"${script}".sh --audit-all + }