# @(#)security 5.21 (Berkeley) %G%
# Script to check basic system security. Checks included are:
# Password and group file check.
# Root umask, dot in root's path.
# Root and uucp in /etc/ftpusers.
# Entry for uudecode in /etc/aliases, if uudecode is setuid.
# Special users that have .rhosts files, .rhosts file syntax.
# Home directory and dot-file ownership and permissions.
# Mailbox ownership and permissions.
# Globally exported NFS file systems.
# Changes in setuid and device files.
# Block and character disk device ownership and permissions.
# File list from /etc/mtree/special.
# System binaries as described by files in /etc/mtree.
PATH
=/sbin
:/usr
/sbin
:/bin
:/usr
/bin
trap 'rm -f $ERR $TMP1 $TMP2 $TMP3 $LIST $OUTPUT' 0
# Check the password file syntax.
printf("Line %d is a blank line.\n", NR);
printf("Line %d has the wrong number of fields.\n", NR);
if ($1 !~ /^[A-Za-z0-9]*$/)
printf("Login %s has non-numeric characters.\n", $1);
printf("Login %s has more than 8 characters.\n", $1);
printf("Login %s has no password.\n", $1);
if (length($2) != 13 && ($10 ~ /.*sh$/ || $10 == ""))
printf("Login %s is off but still has a valid shell.\n", $1);
if ($3 == 0 && $1 != "root" && $1 != "toor")
printf("Login %s has a user id of 0.\n", $1);
printf("Login %s has a negative user id.\n", $1);
printf("Login %s has a negative group id.\n", $1);
printf "\nChecking the $MP file:\n"
awk -F: '{ print $1 }' $MP |
sort |
uniq -d > $OUTPUT
printf "\n$MP has duplicate user names.\n"
awk -F: '{ print $1 " " $3 }' $MP |
sort -n +1 |
tee $TMP1 |
uniq -d -f 1 |
awk '{ print $2 }' > $TMP2
printf "\n$MP has duplicate user id's.\n"
# Check the group file syntax.
printf("Line %d is a blank line.\n", NR);
printf("Line %d has the wrong number of fields.\n", NR);
if ($1 !~ /^[A-za-z0-9]*$/)
printf("Group %s has non-numeric characters.\n", $1);
printf("Group %s has more than 8 characters.\n", $1);
printf("Login %s has a negative group id.\n", $1);
printf "\nChecking the $GRP file:\n"
awk -F: '{ print $1 }' $GRP |
sort |
uniq -d > $OUTPUT
printf "\n$GRP has duplicate group names.\n"
# Check for root paths, umask values in startup files.
list
="/etc/csh.cshrc /etc/csh.login ${rhome}/.cshrc ${rhome}/.login"
if egrep umask $i > /dev
/null
; then
{ print "Root umask is group writeable" }
{ print "Root umask is other writeable" }' >> $OUTPUT
/bin
/csh
-f -s << end-of-csh > /dev/null 2>&1
/bin/ls -ldgT \$path > $TMP1
print "The root path includes .";
{ print "Root path directory " $10 " is group writeable." } \
{ print "Root path directory " $10 " is other writeable." }' \
if [ $umaskset = "no" -o -s $OUTPUT ] ; then
printf "\nChecking root csh paths, umask values:\n$list\n"
if [ $umaskset = "no" ] ; then
printf "\nRoot csh startup files do not set the umask.\n"
if egrep umask $i > /dev
/null
; then
{ print "Root umask is group writeable" } \
{ print "Root umask is other writeable" }' >> $OUTPUT
/bin
/sh
<< end-of-sh > /dev/null 2>&1
list=\`echo \$PATH | /usr/bin/sed -e 's/:/ /g'\`
/bin/ls -ldgT \$list > $TMP1
print "The root path includes .";
{ print "Root path directory " $10 " is group writeable." } \
{ print "Root path directory " $10 " is other writeable." }' \
if [ $umaskset = "no" -o -s $OUTPUT ] ; then
printf "\nChecking root sh paths, umask values:\n$list\n"
if [ $umaskset = "no" ] ; then
printf "\nRoot sh startup files do not set the umask.\n"
# Root and uucp should both be in /etc/ftpusers.
if egrep root
/etc
/ftpusers
> /dev
/null
; then
printf "\nRoot not listed in /etc/ftpusers file.\n"
if egrep uucp /etc
/ftpusers
> /dev
/null
; then
printf "\nUucp not listed in /etc/ftpusers file.\n"
# Uudecode should not be in the aliases file.
if grep -w uudecode /etc
/aliases
; then
printf "\nThere is an entry for uudecode in the /etc/aliases file.\n"
# Check for plus signs in /etc/hosts.equiv.
if egrep '\+|^$' /etc
/hosts.equiv
> /dev
/null
; then
printf "\nEmpty line or + in /etc/hosts.equiv file.\n"
# Check for special users with .rhosts files. Only root and toor should
# have a .rhosts files. Also, .rhosts files should not have blank lines
awk -F: '$1 != "root" && $1 != "toor" && \
($3 < 100 || $1 == "ftp" || $1 == "uucp") \
{ print $1 " " $6 }' /etc
/passwd |
while read uid homedir
; do
if [ -f ${homedir}/.rhosts
] ; then
rhost
=`ls -ldgT ${homedir}/.rhosts`
printf "\nChecking for special users with .rhosts files.\n"
awk -F: '{ print $1 " " $6 }' /etc
/passwd | \
while read uid homedir
; do
if [ -f ${homedir}/.rhosts
] && \
egrep '\+|^$' ${homedir}/.rhosts
> /dev
/null
; then
printf "$uid: empty line or + in .rhosts file.\n"
printf "\nChecking .rhosts files syntax.\n"
# Check home directories, dot files.
# Directories should not be owned by someone else or writeable.
awk -F: '{ print $1 " " $6 }' /etc
/passwd | \
while read uid homedir
; do
if [ -d ${homedir}/ ] ; then
file=`ls -ldgT ${homedir}`
awk '$1 != $4 && $4 != "root" \
{ print "user " $1 " home directory is owned by " $4 }
{ print "user " $1 " home directory is group writeable" }
{ print "user " $1 " home directory is other writeable" }' > $OUTPUT
printf "\nChecking home directories.\n"
# Files that should not be owned by someone else or readable.
awk -F: '{ print $1 " " $6 }' /etc
/passwd | \
while read uid homedir
; do
printf "$uid $f `ls -ldgT $file`\n"
awk '$1 != $5 && $5 != "root" \
{ print "user " $1 " " $2 " file is owned by " $5 }
{ print "user " $1 " " $2 " file is group readable" }
{ print "user " $1 " " $2 " file is other readable" }
{ print "user " $1 " " $2 " file is group writeable" }
{ print "user " $1 " " $2 " file is other writeable" }' > $OUTPUT
# Files that should not be owned by someone else or writeable.
list
=".bashrc .cshrc .emacsrc .exrc .forward .klogin .login .logout \
awk -F: '{ print $1 " " $6 }' /etc
/passwd | \
while read uid homedir
; do
printf "$uid $f `ls -ldgT $file`\n"
awk '$1 != $5 && $5 != "root" \
{ print "user " $1 " " $2 " file is owned by " $5 }
{ print "user " $1 " " $2 " file is group writeable" }
{ print "user " $1 " " $2 " file is other writeable" }' >> $OUTPUT
printf "\nChecking dot files.\n"
# Check mailbox ownership and permissions.
ls -l /var
/mail |
sed 1d | \
{ print "user " $9 " mailbox is owned by " $3 }
{ print "user " $9 " mailbox is " $1 ", group " $4 }' > $OUTPUT
printf "\nChecking mailbox ownership.\n"
# Check for globally exported file systems.
for (i = 2; i <= NF; ++i) {
print "File system " $1 " globally exported, read-only."
print "File system " $1 " globally exported, read-write."
}' < /etc
/exports
> $OUTPUT
printf "\nChecking for globally exported file systems.\n"
# Display setuid and device changes.
printf "\nChecking setuid files and devices:\n"
(find / ! -fstype local -a -prune -o \
\
( -perm -u+s
-o -perm -g+s
-o ! -type d
-a ! -type f
-a ! -type l
-a \
sort |
sed -e 's/^/ls -ldgT /' | sh
> $LIST) 2> $OUTPUT
# Display any errors that occurred during system file walk.
printf "Setuid/device find errors:\n"
# Display any changes in the setuid file list.
egrep -v '^[bc]' $LIST > $TMP1
# Check to make sure uudecode isn't setuid.
if grep -w uudecode $TMP1 > /dev
/null
; then
printf "\nUudecode is setuid.\n"
CUR
=/var
/backups
/setuid.current
BACK
=/var
/backups
/setuid.backup
if cmp -s $CUR $TMP1 ; then
join -110 -210 -v2 $CUR $TMP1 > $OUTPUT
printf "Setuid additions:\n"
join -110 -210 -v1 $CUR $TMP1 > $OUTPUT
printf "Setuid deletions:\n"
sort +9 $TMP2 $CUR $TMP1 | \
sed -e 's/[ ][ ]*/ /g' |
uniq -u > $OUTPUT
printf "Setuid changes:\n"
printf "Setuid additions:\n"
# Check for block and character disk devices, that are readable or writeable
# or not owned by root.operator.
egrep '^c.*/rdk[0-9][0-9]*[a-h]$' $LIST >> $TMP1
egrep '^c.*/rfd[0-9][0-9]*[a-h]$' $LIST >> $TMP1
egrep '^c.*/rhd[0-9][0-9]*[a-h]$' $LIST >> $TMP1
egrep '^c.*/rhk[0-9][0-9]*[a-h]$' $LIST >> $TMP1
egrep '^c.*/rhp[0-9][0-9]*[a-h]$' $LIST >> $TMP1
egrep '^c.*/rjb[0-9][0-9]*[a-h]$' $LIST >> $TMP1
egrep '^c.*/rkra[0-9][0-9]*[a-h]$' $LIST >> $TMP1
egrep '^c.*/rra[0-9][0-9]*[a-h]$' $LIST >> $TMP1
egrep '^c.*/rrb[0-9][0-9]*[a-h]$' $LIST >> $TMP1
egrep '^c.*/rrd[0-9][0-9]*[a-h]$' $LIST >> $TMP1
egrep '^c.*/rrl[0-9][0-9]*[a-h]$' $LIST >> $TMP1
egrep '^c.*/rrx[0-9][0-9]*[a-h]$' $LIST >> $TMP1
egrep '^c.*/rrz[0-9][0-9]*[a-h]$' $LIST >> $TMP1
egrep '^c.*/rsd[0-9][0-9]*[a-h]$' $LIST >> $TMP1
egrep '^c.*/rup[0-9][0-9]*[a-h]$' $LIST >> $TMP1
egrep '^c.*/rwd[0-9][0-9]*[a-h]$' $LIST >> $TMP1
awk '$3 != "root" || $4 != "operator" || $1 !~ /.rw-r-----/ \
{ printf("Disk %s is user %s, group %s, permissions %s.\n", \
$11, $3, $4, $1); }' < $TMP1 > $OUTPUT
printf "\nChecking disk ownership and permissions.\n"
egrep '^[bc]' $LIST |
sort +10 > $TMP1
# Display any changes in the device file list.
CUR
=/var
/backups
/device.current
BACK
=/var
/backups
/device.backup
if cmp -s $CUR $TMP1 ; then
join -111 -211 -v2 $CUR $TMP1 > $OUTPUT
printf "Device additions:\n"
join -111 -211 -v1 $CUR $TMP1 > $OUTPUT
printf "Device deletions:\n"
# Report any block device change. Ignore character
# devices, only the name is significant.
sed -e 's/[ ][ ]*/ /g' | \
printf "Block device changes:\n"
printf "Device additions:\n"
# Create the mtree tree specifications using:
# mtree -cx -pDIR -kcksum,gid,mode,nlink,size,link,time,uid > DIR.secure
# chown root.wheel DIR.SECURE
# Note, this is not complete protection against Trojan horsed binaries, as
# the hacker can modify the tree specification to match the replaced binary.
# For details on really protecting yourself against modified binaries, see
# the mtree(8) manual page.
mtree
-e -p / -f /etc
/mtree
/special
> $OUTPUT
printf "\nChecking special files and directories.\n"
tree
=`sed -n -e '3s/.* //p' -e 3q $file`
mtree
-f $file -p $tree > $TMP1
printf "\nChecking $tree:\n" >> $OUTPUT
printf "\nChecking system binaries:\n"