Skip to content

Commit 8a1813f

Browse files
author
damien cavagnini
committed
- etc_passwd_groups_in_etc_group.sh -> 7.2.3
1 parent 6c2ecf8 commit 8a1813f

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# shellcheck shell=bash
2+
# run-shellcheck
3+
test_audit() {
4+
describe prepare failing test
5+
local test_user="wrong_group_user"
6+
useradd -M "$test_user"
7+
sed -i "/^$test_user:/d" /etc/group
8+
9+
describe Tests purposely failing
10+
register_test retvalshouldbe 1
11+
# shellcheck disable=2154
12+
run noncompliant "${CIS_CHECKS_DIR}/${script}.sh" --audit-all
13+
14+
describe fixing situation
15+
userdel -r "$test_user"
16+
17+
describe Checking resolved state
18+
register_test retvalshouldbe 0
19+
run resolved "${CIS_CHECKS_DIR}/${script}.sh" --audit-all
20+
21+
}

0 commit comments

Comments
 (0)