Skip to content

Commit 1aa4198

Browse files
author
damien cavagnini
committed
- etc_shadow_fields_not_empty.sh -> 7.2.2
1 parent ddd069a commit 1aa4198

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/bin/bash
2+
3+
# run-shellcheck
4+
#
5+
# CIS Debian Hardening
6+
#
7+
8+
#
9+
# Ensure /etc/shadow password fields are not empty (Automated)
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 /etc/shadow password fields are not empty"
19+
EXCEPTIONS=""
20+
21+
# This function will be called if the script status is on enabled / audit mode
22+
audit() {
23+
INVALID_USERS=""
24+
25+
local tmp_invalid_users=""
26+
tmp_invalid_users=$($SUDO_CMD cat /etc/shadow | awk -F: '($2 == "" ) { print $1 }')
27+
28+
if [ -n "$tmp_invalid_users" ]; then
29+
for user in $tmp_invalid_users; do
30+
if [ -n "$EXCEPTIONS" ]; then
31+
if ! grep -w "$user" <<<"$EXCEPTIONS" >/dev/null; then
32+
crit "$user does not have a password"
33+
INVALID_USERS="$INVALID_USERS $user"
34+
fi
35+
else
36+
crit "$user does not have a password"
37+
INVALID_USERS="$INVALID_USERS $user"
38+
fi
39+
done
40+
fi
41+
42+
}
43+
44+
# This function will be called if the script status is on enabled mode
45+
apply() {
46+
47+
if [ -n "$INVALID_USERS" ]; then
48+
for user in $INVALID_USERS; do
49+
info "locking $user"
50+
passwd -l "$user"
51+
done
52+
fi
53+
}
54+
55+
# This function will check config parameters required
56+
check_config() {
57+
:
58+
}
59+
60+
# maybe someone is gonna have a legit use case....
61+
create_config() {
62+
cat <<EOF
63+
# shellcheck disable=2034
64+
status=audit
65+
# Put here the accounts that should keep their non shadowed password
66+
EXCEPTIONS=''
67+
EOF
68+
}
69+
70+
# Source Root Dir Parameter
71+
if [ -r /etc/default/cis-hardening ]; then
72+
# shellcheck source=../../debian/default
73+
. /etc/default/cis-hardening
74+
fi
75+
if [ -z "$CIS_LIB_DIR" ]; then
76+
echo "There is no /etc/default/cis-hardening file nor cis-hardening directory in current environment."
77+
echo "Cannot source CIS_LIB_DIR variable, aborting."
78+
exit 128
79+
fi
80+
81+
# Main function, will call the proper functions given the configuration (audit, enabled, disabled)
82+
if [ -r "${CIS_LIB_DIR}"/main.sh ]; then
83+
# shellcheck source=../../lib/main.sh
84+
. "${CIS_LIB_DIR}"/main.sh
85+
else
86+
echo "Cannot find main.sh, have you correctly defined your root directory? Current value is $CIS_LIB_DIR in /etc/default/cis-hardening"
87+
exit 128
88+
fi
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# shellcheck shell=bash
2+
# run-shellcheck
3+
test_audit() {
4+
describe prepare test
5+
useradd -M user_no_password
6+
sed -i 's/user_no_password:!:/user_no_password::/' /etc/shadow
7+
8+
describe On purpose failing test
9+
register_test retvalshouldbe 1
10+
# shellcheck disable=2154
11+
run failed "${CIS_CHECKS_DIR}/${script}.sh" --audit-all
12+
13+
describe correcting situation using exceptions
14+
sed -i 's/EXCEPTIONS=.*$/EXCEPTIONS=user_no_password/' "${CIS_CONF_DIR}/conf.d/${script}.cfg"
15+
16+
describe resolved test
17+
register_test retvalshouldbe 0
18+
run resolved "${CIS_CHECKS_DIR}/${script}.sh" --audit-all
19+
20+
describe fix by locking user
21+
sed -i 's/EXCEPTIONS=.*$/EXCEPTIONS=""/' "${CIS_CONF_DIR}/conf.d/${script}.cfg"
22+
sed -i 's/audit/enabled/' "${CIS_CONF_DIR}/conf.d/${script}.cfg"
23+
"${CIS_CHECKS_DIR}/${script}.sh" --apply || true
24+
25+
describe resolved test
26+
register_test retvalshouldbe 0
27+
run resolved "${CIS_CHECKS_DIR}/${script}.sh" --audit-all
28+
29+
describe clean test
30+
userdel user_no_password
31+
32+
}

0 commit comments

Comments
 (0)