debian-cis/bin/hardening/99.5.4_ssh_keys_from.sh
Charles Herlin d2bbf754ac Migrate generic checks from secaudit to cis-hardening
new file:   99.3.1_acc_shadow_sha512.sh
new file:   99.3.2_acc_sudoers_no_all.sh
new file:   99.4_net_fw_default_policy_drop.sh
new file:   99.5.1_ssh_auth_pubk_only.sh
new file:   99.5.2.1_ssh_cry_kex.sh
new file:   99.5.2.2_ssh_cry_mac.sh
new file:   99.5.2.3_ssh_cry_rekey.sh
new file:   99.5.3_ssh_disable_features.sh
new file:   99.5.4_ssh_keys_from.sh
new file:   99.5.5_ssh_strict_modes.sh
new file:   99.5.6_ssh_sys_accept_env.sh
new file:   99.5.7_ssh_sys_no_legacy.sh
new file:   99.5.8_ssh_sys_sandbox.sh
new file:   99.5.9_ssh_log_level.sh

Fix descriptions in comment section for 99.* secaudit checks

Remove duplicated legacy services that are already taken care of by vanilla cis

Enable custom configuration of checks in config-file, no more hard coded conf
Add test to disable check if debian version is too old
Add excused IPs while checking "from" field of authorized_keys
Escaping dots in IPs
Manage Kex for different debian versions
Add tests for generic checks and add apply for ssh config
Apply shellcheck recommendations on audit/hardening scripts
Update script to check for allowed IPs only, remove bastion related
Fill `apply` func for ssh config related scripts
Add and update tests scenarii

Disable shellcheck test for external source 1091

As of today, the entire project is not shellcheck compliant, I prefer
disabling the test that warns about not finding external source (that
arent compliant). I will enable it again when the project library will
be shellchecked
https://github.com/koalaman/shellcheck/wiki/SC1091

Refactor password policy check with one check by feature

Previous file will now only look for bad passwords in /etc/shadow
I added two checks that look for the compliant configuration lines in
conf files /etc/logins.defs and /etc/pam.d/common-passwords

FIX: merge chained sed and fix regex

FIX: update regex to capture more output
FIX: fix pattern to ignore commented lines, add apply

Also add tests to ensure that commented lines are not detected as valid
configuration

CHORE: cleanup test situation with file and users removal
IMP: add case insensitive option when looking for patterns in files
CHORE: removed duplicated line in test file
2019-02-11 18:05:03 +01:00

175 lines
5.6 KiB
Bash
Executable File

#!/bin/bash
# run-shellcheck
#
# OVH Security audit
#
#
# Check <from> field in ssh authorized keys files for users with login shell, and bastions IP if available.
#
set -e # One error, it is over
set -u # One variable unset, it is over
# shellcheck disable=2034
DESCRIPTION="Check <from> field in ssh authorized keys files for users with login shell, and allowed IP if available."
# Regex looking for empty, hash starting lines, or 'from="127.127.127,127.127.127" ssh'
# shellcheck disable=2089
REGEX="^(from=(?:'|\")(,?(\d{1,3}(\.\d{1,3}){3}))+(?:'|\")\s+ssh|#|$)"
AUTHKEYFILE_PATTERN=""
AUTHKEYFILE_PATTERN_DEFAULT=".ssh/authorized_keys .ssh/authorized_keys2"
ALLOWED_IPS=""
ALLOWED_NOLOGIN_SHELLS="/bin/false /usr/sbin/nologin"
# Check functions
check_ip() {
file=$1
if [ -z "$ALLOWED_IPS" ]; then
warn "No allowed IPs to treat";
return ;
fi
for line in $($SUDO_CMD grep -ne "from" "$file" | tr -s " " | sed 's/ /_/g' ); do
linum=$(echo "$line" | cut -d ':' -f 1)
ips=$(echo "$line" | cut -d '"' -f 2 | tr ',' ' ')
ok_ips_allowed=""
bad_ips=""
for ip in $ips; do
ip_escaped=$(sed 's/\./\\./g' <<< "$ip")
if grep -qw "$ip_escaped" <<< $ALLOWED_IPS ; then
debug "Line $linum of $file allows access from exused IP (${ip})."
ok_ips_allowed+="$ip "
else
debug "Line $linum of $file allows access from ip ($ip) that is not allowed."
bad_ips+="$ip "
fi
done
ok_ips=$( sed 's/ $//' <<< "${ok_ips_allowed}")
bad_ips=$( sed 's/ $//' <<< "${bad_ips}")
if [[ -z $bad_ips ]]; then
if [[ ! -z $ok_ips ]]; then
ok "Line $linum of $file allows ssh access only from allowed IPs ($ok_ips)."
fi
else
crit "Line $linum of $file allows ssh access from (${bad_ips}) that are not allowed."
if [[ ! -z $ok_ips ]]; then
ok "Line $linum of $file allows ssh access from at least allowed IPs ($ok_ips)."
fi
fi
done
}
check_file() {
file=$1
if $SUDO_CMD [ ! -e "$file" ]; then debug "$file does not exist"; return; fi
if $SUDO_CMD [ -r "$file" ]; then
debug "Treating $file"
FOUND_AUTHKF=1
if $SUDO_CMD grep -vqP "$REGEX" "${file}" ; then
bad_lines="$(grep -vnP "$REGEX" "${file}" | cut -d ':' -f 1 | tr '\n' ' ' | sed 's/ $//' )"
crit "There are anywhere access keys in ${file} at lines (${bad_lines})."
else
ok "File ${file} is cleared from anywhere access keys."
check_ip "$file"
fi
else
crit "Cannot read ${file} for ${user}."
fi
}
check_dir() {
directory=$1
if $SUDO_CMD [ ! -x "$directory" ]; then
crit "Cannot read ${directory}."
return
fi
for file in $AUTHKEYFILE_PATTERN; do
check_file "${directory}"/${file}
done
}
# This function will be called if the script status is on enabled / audit mode
audit () {
# Retrieve authorized_key file pattern from sshd_config
if $SUDO_CMD [ ! -r /etc/ssh/sshd_config ]; then
crit "/etc/ssh/sshd_config is not readable."
else
ret=$($SUDO_CMD grep -iP "^AuthorizedKeysFile" /etc/ssh/sshd_config || echo '#KO' )
if [ "x$ret" = "x#KO" ]; then
debug "No AuthorizedKeysFile defined in sshd_config."
else
AUTHKEYFILE_PATTERN=$(echo "$ret" | sed 's/AuthorizedKeysFile//i' | sed 's#%h/##' | tr -s "[:space:]")
debug "Found pattern in sshdconfig : ${AUTHKEYFILE_PATTERN}."
fi
fi
if [ -z "$AUTHKEYFILE_PATTERN" ] ; then
AUTHKEYFILE_PATTERN=$AUTHKEYFILE_PATTERN_DEFAULT
debug "Set default pattern for authorized_keys file."
fi
for line in $($SUDO_CMD cat /etc/passwd | cut -d ":" -f 1,7); do
# Checking if at least one AuthKeyFile has been found for this user
FOUND_AUTHKF=0
user=$(echo "$line" | cut -d ":" -f 1);
shell=$(echo "$line" | cut -d ':' -f 2);
if grep -q "$shell" <<< "$ALLOWED_NOLOGIN_SHELLS" ; then
continue
else
info "User $user has a valid shell.";
if [ "x$user" = "xroot" ]; then
check_dir /root
continue
elif $SUDO_CMD [ ! -d /home/"$user" ]; then
info "User $user has no home directory."
continue
fi
check_dir /home/"${user}"
if [ $FOUND_AUTHKF = 0 ]; then
warn "$user has a valid shell but no authorized_keys file"
fi
fi
done
}
# This function will be called if the script status is on enabled mode
apply () {
:
}
create_config() {
cat <<EOF
status=disabled
# Put authorized IPs you want to allow in "from" field of authorized_keys
ALLOWED_IPS=""
EOF
}
# This function will check config parameters required
check_config() {
:
}
# Source Root Dir Parameter
if [ -r /etc/default/cis-hardening ]; then
. /etc/default/cis-hardening
fi
if [ -z "$CIS_ROOT_DIR" ]; then
echo "There is no /etc/default/cis-hardening file nor cis-hardening directory in current environment."
echo "Cannot source CIS_ROOT_DIR variable, aborting."
exit 128
fi
# Main function, will call the proper functions given the configuration (audit, enabled, disabled)
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
fi