Skip to content

Commit

Permalink
Improve regex in Bash and Ansible remediation
Browse files Browse the repository at this point in the history
The former regex were not matching all possible cases.

Signed-off-by: Marcus Burghardt <[email protected]>
  • Loading branch information
marcusburghardt committed Jul 17, 2024
1 parent 4c304c0 commit c9225b5
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
- name: '{{{ rule_title }}} - Ensure That Only the Correct Hashing Algorithm Option For pam_unix.so Is Used in {{{ pam_file }}}'
ansible.builtin.replace:
dest: "{{ pam_file_path }}"
regexp: (.*password.*pam_unix\.so.*)\b{{ item }}\b=?[0-9a-zA-Z]*(.*)
regexp: (^\s*password.*pam_unix\.so.*)\b{{ item }}\b\s*(.*)
replace: '\1\2'
when:
item != var_password_hashing_algorithm_pam
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ declare -a HASHING_ALGORITHMS_OPTIONS=("sha512" "yescrypt" "gost_yescrypt" "blow

for hash_option in "${HASHING_ALGORITHMS_OPTIONS[@]}"; do
if [ "$hash_option" != "$var_password_hashing_algorithm_pam" ]; then
if grep -qP "^\s*password\s+.*\s+pam_unix.so\s+.*\s+$hash_option\b" "$PAM_FILE_PATH"; then
if grep -qP "^\s*password\s+.*\s+pam_unix.so\s+.*\b$hash_option\b" "$PAM_FILE_PATH"; then
{{{ bash_remove_pam_module_option_configuration("$PAM_FILE_PATH", 'password', ".*", 'pam_unix.so', "$hash_option") }}}
fi
fi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
- name: '{{{ rule_title }}} - Ensure That Only the Correct Hashing Algorithm Option For pam_unix.so Is Used in {{{ pam_file }}}'
ansible.builtin.replace:
dest: "{{ pam_file_path }}"
regexp: (.*password.*pam_unix\.so.*)\b{{ item }}\b=?[0-9a-zA-Z]*(.*)
regexp: (^\s*password.*pam_unix\.so.*)\b{{ item }}\b\s*(.*)
replace: '\1\2'
when:
item != var_password_hashing_algorithm_pam
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ CONTROL="sufficient"
{{% if 'ubuntu' in product -%}}
# Can't use macro bash_ensure_pam_module_configuration because the control
# contains special characters and is not static ([success=N default=ignore)
if ! grep -qP "^\s*password\s+.*\s+pam_unix.so\s+.*\s+$var_password_hashing_algorithm_pam\b" "$PAM_FILE_PATH"; then
if ! grep -qP "^\s*password\s+.*\s+pam_unix.so\s+.*\b$var_password_hashing_algorithm_pam\b" "$PAM_FILE_PATH"; then
sed -i -E --follow-symlinks "/\s*password\s+.*\s+pam_unix.so.*/ s/$/ $var_password_hashing_algorithm_pam/" "$PAM_FILE_PATH"
fi
{{%- else -%}}
Expand All @@ -27,7 +27,7 @@ declare -a HASHING_ALGORITHMS_OPTIONS=("sha512" "yescrypt" "gost_yescrypt" "blow

for hash_option in "${HASHING_ALGORITHMS_OPTIONS[@]}"; do
if [ "$hash_option" != "$var_password_hashing_algorithm_pam" ]; then
if grep -qP "^\s*password\s+.*\s+pam_unix.so\s+.*\s+$hash_option\b" "$PAM_FILE_PATH"; then
if grep -qP "^\s*password\s+.*\s+pam_unix.so\s+.*\b$hash_option\b" "$PAM_FILE_PATH"; then
{{{ bash_remove_pam_module_option_configuration("$PAM_FILE_PATH", 'password', ".*", 'pam_unix.so', "$hash_option") }}}
fi
fi
Expand Down

0 comments on commit c9225b5

Please sign in to comment.