Run the following script to verify SSH private host key files are mode 0600 or more restrictive, owned be the root user, and owned be the group root or group designated to own openSSH private keys:
#!/usr/bin/env bash{l_output=""l_skgn="ssh_keys"# Group designated to own openSSH keysl_skgid="$(awk-F:'($1 == "'"$l_skgn"'"){print $3}'/etc/group)"awk'{print}'<<<"$(find/etc/ssh-xdev-typef-name'ssh_host_*_key'-execstat-L-c"%n %#a %U %G %g"{}+)"|(whileread-rl_filel_model_ownerl_groupl_gid;do[-n"$l_skgid"]&&l_cga="$l_skgn"||l_cga="root"["$l_gid"="$l_skgid"]&&l_pmask="0137"||l_pmask="0177"l_maxperm="$(printf'%o'$((0777&~$l_pmask)))"[$(($l_mode&$l_pmask))-gt0]&&l_output="$l_output\n - File: \"$l_file\" is mode \"$l_mode\" should be mode: \"$l_maxperm\" or more restrictive"["$l_owner"!="root"]&&l_output="$l_output\n - File: \"$l_file\" is owned by: \"$l_owner\" should be owned by \"root\""if["$l_group"!="root"]&&["$l_gid"!="$l_skgid"];thenl_output="$l_output\n - File: \"$l_file\" is owned by group \"$l_group\" should belong to group \"$l_cga\""fidoneif[-z"$l_output"];thenecho-e"\n- Audit Result:\n *** PASS ***\n"elseecho-e"\n- Audit Result:\n *** FAIL ***$l_output\n"fi)}
#!/usr/bin/env bash{l_skgn="ssh_keys"# Group designated to own openSSH keysl_skgid="$(awk-F:'($1 == "'"$l_skgn"'"){print $3}'/etc/group)"awk'{print}'<<<"$(find/etc/ssh-xdev-typef-name'ssh_host_*_key'-execstat-L-c"%n %#a %U %G %g"{}+)"|(whileread-rl_filel_model_ownerl_groupl_gid;do[-n"$l_skgid"]&&l_cga="$l_skgn"||l_cga="root"["$l_gid"="$l_skgid"]&&l_pmask="0137"||l_pmask="0177"l_maxperm="$(printf'%o'$((0777&~$l_pmask)))"if[$(($l_mode&$l_pmask))-gt0];thenecho-e" - File: \"$l_file\" is mode \"$l_mode\" changing to mode: \"$l_maxperm\""if[-n"$l_skgid"];thenchmodu-x,g-wx,o-rwx"$l_file"elsechmodu-x,go-rwx"$l_file"fifiif["$l_owner"!="root"];thenecho-e" - File: \"$l_file\" is owned by: \"$l_owner\" changing owner to \"root\""chownroot"$l_file"fiif["$l_group"!="root"]&&["$l_gid"!="$l_skgid"];thenecho-e" - File: \"$l_file\" is owned by group \"$l_group\" should belong to group \"$l_cga\""chgrp"$l_cga""$l_file"fidone)}