#!/usr/bin/env bash{a_output=();a_output2=()f_file_test_chk(){a_out2=()maxperm="$(printf'%o'$((0777&~$perm_mask)))"[$(($l_mode&$perm_mask))-gt0]&&\a_out2+=("o Mode: \"$l_mode\" should be \"$maxperm\" or more restrictive")[[!"$l_user"=~$l_auser]]&&\a_out2+=("o Owned by: \"$l_user\" and should be owned by \"${l_auser//|/ or }\"")[[!"$l_group"=~$l_agroup]]&&\a_out2+=("o Group owned by: \"$l_group\" and should be group owned by \"${l_agroup//|/ or }\"")["${#a_out2[@]}"-gt0]&&a_output2+=(" - File: \"$l_fname\" is:""${a_out2[@]}")}whileIFS=read-r-d$'\0'l_file;dowhileIFS=:read-rl_fnamel_model_userl_group;doifgrep-Pq--'\/(apt)\h*$'<<<"$(dirname"$l_fname")";thenperm_mask='0133'l_auser="root"l_agroup="(root|adm)";f_file_test_chk
elsecase"$(basename"$l_fname")"inlastlog|lastlog.*|wtmp|wtmp.*|wtmp-*|btmp|btmp.*|btmp-*|README)perm_mask='0113'l_auser="root"l_agroup="(root|utmp)"f_file_test_chk;;cloud-init.log*|localmessages*|waagent.log*)perm_mask='0133'l_auser="(root|syslog)"l_agroup="(root|adm)"f_file_test_chk;;secure{,*.*,.*,-*}|auth.log|syslog|messages)perm_mask='0137'l_auser="(root|syslog)"l_agroup="(root|adm)"f_file_test_chk;;SSSD|sssd)perm_mask='0117'l_auser="(root|SSSD)"l_agroup="(root|SSSD)"f_file_test_chk;;gdm|gdm3)perm_mask='0117'l_auser="root"l_agroup="(root|gdm|gdm3)"f_file_test_chk;;*.journal|*.journal~)perm_mask='0137'l_auser="root"l_agroup="(root|systemd-journal)"f_file_test_chk;;*)perm_mask='0137'l_auser="(root|syslog)"l_agroup="(root|adm)"if["$l_user"="root"]||!grep-Pq--"^\h*$(awk-F:'$1=="'"$l_user"'"{print $7}'/etc/passwd)\b"/etc/shells;then!grep-Pq--"$l_auser"<<<"$l_user"&&l_auser="(root|syslog|$l_user)"!grep-Pq--"$l_agroup"<<<"$l_group"&&l_agroup="(root|adm|$l_group)"fif_file_test_chk;;esacfidone<<(stat-Lc'%n:%#a:%U:%G'"$l_file")done<<(find-L/var/log-typef\(-perm/0137-o!-userroot-o!-grouproot\)-print0)if["${#a_output2[@]}"-le0];thena_output+=(" - All files in \"/var/log/\" have appropriate permissions and ownership")printf'\n%s'"- Audit Result:"" ** PASS **""${a_output[@]}"""elseprintf'\n%s'"- Audit Result:"" ** FAIL **"" - Reason(s) for audit failure:""${a_output2[@]}"""fi}
#!/usr/bin/env bash{a_output2=()f_file_test_fix(){a_out2=()maxperm="$(printf'%o'$((0777&~$perm_mask)))"if[$(($l_mode&$perm_mask))-gt0];thena_out2+=("o Mode: \"$l_mode\" should be \"$maxperm\" or more restrictive""x Removing excess permissions")chmod"$l_rperms""$l_fname"fiif[[!"$l_user"=~$l_auser]];thena_out2+=("o Owned by: \"$l_user\" and should be owned by \"${l_auser//|/ or }\"""x Changing ownership to: \"$l_fix_account\"")chown"$l_fix_account""$l_fname"fiif[[!"$l_group"=~$l_agroup]];thena_out2+=("o Group owned by: \"$l_group\" and should be group owned by \"${l_agroup//|/ or }\"""x Changing group ownership to: \"$l_fix_account\"")chgrp"$l_fix_account""$l_fname"fi["${#a_out2[@]}"-gt0]&&a_output2+=(" - File: \"$l_fname\" is:""${a_out2[@]}")}l_fix_account='root'whileIFS=read-r-d$'\0'l_file;dowhileIFS=:read-rl_fnamel_model_userl_group;doifgrep-Pq--'\/(apt)\h*$'<<<"$(dirname"$l_fname")";thenperm_mask='0133'l_rperms="u-x,go-wx"l_auser="root"l_agroup="(root|adm)";f_file_test_fix
elsecase"$(basename"$l_fname")"inlastlog|lastlog.*|wtmp|wtmp.*|wtmp-*|btmp|btmp.*|btmp-*|README)perm_mask='0113'l_rperms="ug-x,o-wx"l_auser="root"l_agroup="(root|utmp)"f_file_test_fix;;cloud-init.log*|localmessages*|waagent.log*)perm_mask='0133'l_rperms="u-x,go-wx"l_auser="(root|syslog)"l_agroup="(root|adm)"file_test_fix;;secure|auth.log|syslog|messages)perm_mask='0137'l_rperms="u-x,g-wx,o-rwx"l_auser="(root|syslog)"l_agroup="(root|adm)"f_file_test_fix;;SSSD|sssd)perm_mask='0117'l_rperms="ug-x,o-rwx"l_auser="(root|SSSD)"l_agroup="(root|SSSD)"f_file_test_fix;;gdm|gdm3)perm_mask='0117'l_rperms="ug-x,o-rwx"l_auser="root"l_agroup="(root|gdm|gdm3)"f_file_test_fix;;*.journal|*.journal~)perm_mask='0137'l_rperms="u-x,g-wx,o-rwx"l_auser="root"l_agroup="(root|systemd-journal)"f_file_test_fix;;*)perm_mask='0137'l_rperms="u-x,g-wx,o-rwx"l_auser="(root|syslog)"l_agroup="(root|adm)"if["$l_user"="root"]||!grep-Pq--"^\h*$(awk-F:'$1=="'"$l_user"'" {print $7}'/etc/passwd)\b"/etc/shells;then!grep-Pq--"$l_auser"<<<"$l_user"&&l_auser="(root|syslog|$l_user)"!grep-Pq--"$l_agroup"<<<"$l_group"&&l_agroup="(root|adm|$l_group)"fif_file_test_fix;;esacfidone<<(stat-Lc'%n:%#a:%U:%G'"$l_file")done<<(find-L/var/log-typef\(-perm/0137-o!-userroot-o!-grouproot\)-print0)if["${#a_output2[@]}"-le0];then# If all files passed, then we report no changesa_output+=(" - All files in \"/var/log/\" have appropriate permissions and ownership")printf'\n%s'"- All files in \"/var/log/\" have appropriate permissions and ownership"" o No changes required"""elseprintf'\n%s'"${a_output2[@]}"""fi}
Note: You may also need to change the configuration for your logging software or services for any logs that had incorrect permissions.
If there are services that log to other locations, ensure that those log files have the appropriate permissions.