Ensure sudo Does Not Accept Passwords on a Visible Terminal - sudo visiblepw
Description
The sudo visiblepw option lets sudo read a password from a terminal
that cannot turn echo off, so the password shows on the screen as the user
types it. Make sure that no Defaults entry in /etc/sudoers
or in the sudo configuration snippets in /etc/sudoers.d/ enables
visiblepw. The option is off by default, and !visiblepw
turns it off explicitly.
Rationale
A password typed on a terminal that echoes it can be read from the screen, from
a terminal log or from a session recording.
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 visiblepw with !visiblepw 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:],])visiblepw\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:],])visiblepw\b/\1!visiblepw/' "$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-86527-9
- low_complexity
- low_disruption
- medium_severity
- no_reboot_needed
- restrict_strategy
- sudo_remove_visiblepw
- 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-86527-9
- low_complexity
- low_disruption
- medium_severity
- no_reboot_needed
- restrict_strategy
- sudo_remove_visiblepw
- name: Replace visiblepw with !visiblepw in the Defaults entries of the sudoers files
ansible.builtin.replace:
regexp: ^([ \t]*Defaults\b[^#\n]*[\s,])visiblepw\b
replace: \g<1>!visiblepw
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-86527-9
- low_complexity
- low_disruption
- medium_severity
- no_reboot_needed
- restrict_strategy
- sudo_remove_visiblepw