Ensure sudo Does Not Show Password Feedback - sudo pwfeedback
Description
The sudo pwfeedback option makes sudo print an asterisk for every
character of the password the user types. Make sure that no Defaults
entry in /etc/sudoers or in the sudo configuration snippets in
/etc/sudoers.d/ enables pwfeedback. The option is off by
default, and !pwfeedback turns it off explicitly.
Rationale
Password feedback shows the length of the password to anyone who can see the
terminal, which narrows a guessing attack.
Shell script
The following script can be run on the host to remediate the issue.
#!/bin/bash
# Remediation is applicable only in certain platforms
if rpm --quiet -q kernel-core && { rpm --quiet -q sudo; }; then
# Replace pwfeedback with !pwfeedback in the Defaults entries
for f in /etc/sudoers /etc/sudoers.d/*; do
# sudo skips the files of /etc/sudoers.d whose names contain a "." or end in "~"
case "$f" in
/etc/sudoers.d/*.* | *~) continue ;;
esac
if [ -f "$f" ] && grep -qE '^([[:blank:]]*Defaults\b[^#]*[[:space:],])pwfeedback\b' "$f"; then
# The copy sits beside the file, so visudo resolves relative includes
# as it does for the file, and sudo skips it for the "." in its name
sudoers_new=$(mktemp "$f.XXXXXX")
sed -E 's/^([[:blank:]]*Defaults\b[^#]*[[:space:],])pwfeedback\b/\1!pwfeedback/' "$f" > "$sudoers_new"
if /usr/sbin/visudo -qcf "$sudoers_new"; then
cat "$sudoers_new" > "$f"
else
echo "Fail to validate remediated $f, keeping the original file" >&2
fi
rm -f "$sudoers_new"
fi
done
else
>&2 echo 'Remediation is not applicable, nothing was done'
fi
Ansible playbook
The following playbook can be run with Ansible to remediate the issue.
- name: Gather the package facts
package_facts:
manager: auto
tags:
- CCE-86499-1
- low_complexity
- low_disruption
- low_severity
- no_reboot_needed
- restrict_strategy
- sudo_remove_pwfeedback
- name: Find the /etc/sudoers.d/ files that sudo reads
ansible.builtin.find:
paths:
- /etc/sudoers.d/
excludes:
- '*.*'
- '*~'
register: sudoers
when:
- '"kernel-core" in ansible_facts.packages'
- '"sudo" in ansible_facts.packages'
tags:
- CCE-86499-1
- low_complexity
- low_disruption
- low_severity
- no_reboot_needed
- restrict_strategy
- sudo_remove_pwfeedback
- name: Replace pwfeedback with !pwfeedback in the Defaults entries of the sudoers
files
ansible.builtin.replace:
regexp: ^([ \t]*Defaults\b[^#\n]*[\s,])pwfeedback\b
replace: \g<1>!pwfeedback
path: '{{ item.path }}'
validate: /usr/sbin/visudo -cf %s
with_items:
- path: /etc/sudoers
- '{{ sudoers.files }}'
when:
- '"kernel-core" in ansible_facts.packages'
- '"sudo" in ansible_facts.packages'
tags:
- CCE-86499-1
- low_complexity
- low_disruption
- low_severity
- no_reboot_needed
- restrict_strategy
- sudo_remove_pwfeedback