home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / c / cops_104.zip / cops_104 / suid.chk < prev    next >
Text File  |  1992-03-10  |  7KB  |  250 lines

  1. :
  2. #
  3. #  Usage: suid.chk [-s secure_dir] [-S start_dir] [-m user] [-o file] [-n] [-x]
  4. #
  5. #
  6. #   Shell script intended to be run periodically by cron in order
  7. #   to spot changes in files with the suid or sgid bits set.
  8. #
  9. #    suid.chk    840919        Prentiss Riddle
  10. #
  11. #     This changes into the $SECURE directory first, then 
  12. #   uses find(1) to search the directories in $SEARCH for all
  13. #   files with the 4000 or 2000 permission bits set.  $STOP is a file
  14. #   containing "ls -lga" output for known setuid or setgid programs.
  15. #   Any additions or changes to this list represent potential security
  16. #   problems, so they are reported to the users named in $INFORM.
  17. #
  18. # [-m user]  mail to user specified here.
  19. #
  20. # [-n]  Do *not* follow NFS mounted partitions.  This probably won't
  21. #    work on most machines -- check the string; this works on a
  22. #    sun: "-type d \( -fstype nfs -prune \)"; you can send me
  23. #    what works on your machine.
  24. #
  25. # [-o file]  Write results to this file instead of mailing.
  26. #
  27. # [-s secure_dir]  Set the secure dir, good for running this in cron -- 
  28. #    else it'll think it's "/", and you'll chmod that to 700 :-)
  29. #
  30. # [-S search_dir]  Start the "find" here, instead of the default "/"
  31. #
  32. # [-x]  Look for strangely named files, too.  Really primative right
  33. #    now; I'm pondering what to do with this.  Ideally, you'd pass
  34. #    all the files through a filter (preferably perl), but then I'd
  35. #    have to rethink the general suid finding stuff, or do a separate
  36. #    find.  Yuck, both ways.  I have a perl filter that does the
  37. #    right thing, or close to it; it's in "extra_src/bad_dir.pl".
  38. #    You say something like: "find $1 -exec perl ./bad_dir.pl {} \;"
  39. #
  40. #  Lots of changes by dan:
  41. #    Changed the program/doc names and some of the temp files to
  42. #    make it fit in with the rest of the programs, flags SUID shell
  43. #    scripts and world writeable SUID files, too, added command line
  44. #    flags, look for strange files, etc.
  45.  
  46. #  CHANGE THIS LINE!
  47. INFORM="foo@bar.edu"
  48. #
  49.  
  50. TEST=/bin/test
  51. ECHO=/bin/echo
  52. LS=/bin/ls
  53. CAT=/bin/cat
  54. CP=/bin/cp
  55. MAIL=/bin/mail
  56. CHMOD=/bin/chmod
  57. SORT=/usr/bin/sort
  58. COMM=/usr/bin/comm
  59. FIND=/usr/bin/find
  60. RM=/bin/rm
  61. MV=/bin/mv
  62. AWK=/bin/awk
  63. SED=/bin/sed
  64. GREP=/bin/grep
  65. EGREP=/usr/bin/egrep
  66. YPCAT=/usr/bin/ypcat
  67. DATE=/bin/date
  68. NONFS=false
  69.  
  70. #   Arg stuff:
  71. while $TEST $# != 0 ; do
  72.         case "$1" in
  73.         -m)     INFORM=$2 ; shift ;;
  74.         -n)     NONFS=true ;;
  75.         -o)     OUTFILE=$2 ; shift ;;
  76.         -s)     SECURE=$2 ; shift ;;
  77.         -S)     SEARCH=$2 ; shift ;;
  78.         -x)     STRANGE=true ;;
  79.         *)      $ECHO "Usage $0 [-s secure_dir] [-m user] [-o outfile] [-n] [-x]" ; exit 2 ; ;;
  80.         esac
  81.         shift
  82.         done
  83.  
  84. #   Checking for non-executable SUID files;
  85. #
  86. #   simple way; just see if file says it's a script -- this is a *definite*
  87. # no-no, and the default:
  88. #    type_filter="$GREP script"
  89. #
  90. #   Safer/paranoid way; anything but an executable is flagged (may not be
  91. # good over NFS mounts with different binaries...
  92. #    type_filter="$GREP -v xecut"
  93. #
  94. #   You may want to grep out "ermission" string, too, in case NFS mount
  95. # stuff that you can't read gives you "permission denied", even as root:
  96. #    type_filter="$EGREP"' -v '"xecut|ermiss"
  97. #
  98. type_filter="$GREP script"
  99.  
  100. # did you use the -s option?
  101. if $TEST -z "$SECURE" ; then
  102.     SECURE=.
  103.     fi
  104.  
  105. if $TEST -z "$SEARCH" ; then
  106.     SEARCH=/
  107.     fi
  108.  
  109. #
  110. # Warning messages used below:
  111. WARN_NEW="These files are newly setuid/setgid"
  112. WARN_NO="These files are no longer setuid/setgid"
  113.  
  114. # Strange stuff; "..." directories, etc.
  115. if $TEST "$STRANGE" = "true" ; then
  116.     STRANGE_DIRS="-o -type d ( ! -name '.' -a ! -name '..' \
  117.               -a ! -name '[A-z0-9]*' -a ! -name '.[A-z0-9]*' )"
  118.     WARN_NO=$WARN_NO"/strange"
  119.     WARN_NEW=$WARN_NEW"/strange"
  120.     fi
  121.  
  122. # Yellow Pages check further down...
  123. etc_passwd=/etc/passwd
  124. STOP=./suid.stop
  125. TEMPOLD=./fsold$$
  126. TEMPCUR=./fscur$$
  127. TEMPNEW=./fsnew$$
  128. TEMPGON=./fsgon$$
  129. TEMPM=./fsm$$
  130.  
  131. umask 077
  132. OLDCWD=`pwd`
  133.  
  134. if $TEST ! -d "$SECURE" ; then
  135.     $ECHO "Error -- Security directory $SECURE doesn't exist"
  136.     exit 1
  137.     fi
  138.  
  139. $CHMOD 700 $SECURE
  140. cd $SECURE
  141.  
  142. #
  143. # The actual Find!  Never thought you'd make it, eh?
  144. #
  145. if $TEST "$NONFS" = "false" ; then
  146.     $FIND $SEARCH -type f \( -perm -4000 -o -perm -2000 \) $STRANGE_DIRS \
  147.         -exec $LS -ldga {} \; | $SORT > $TEMPCUR
  148. else
  149.     # this is the trouble spot:
  150.     $FIND $SEARCH -type d \( -fstype nfs -prune \) -o \
  151.         -type f \( -perm -4000 -o -perm -2000 \) $STRANGE_DIRS \
  152.         -exec $LS -ldga {} \; | $SORT > $TEMPCUR
  153.     fi
  154.  
  155. # find the setuid programs and sort
  156.  
  157. # compare with the sorted stop list
  158. if $TEST ! -f "$STOP" ; then
  159.     $CP /dev/null $TEMPOLD
  160. else
  161.     $SORT <$STOP >$TEMPOLD
  162.     fi
  163.  
  164. $COMM -13 $TEMPOLD $TEMPCUR | $SORT +8 >$TEMPNEW
  165. $COMM -23 $TEMPOLD $TEMPCUR | $SORT +8 >$TEMPGON
  166.  
  167. # report changes
  168. if $TEST -s $TEMPNEW -o -f $TEMPGON; then
  169.  
  170.     # YP?  Thanks again, to Rob Kolstad...
  171.     # Scratch files for testing:
  172.     yp_passwd=./ypsuid.$$
  173.  
  174.     # generic test to check for yp use?
  175.     if $TEST -f $YPCAT -a -s $YPCAT ; then
  176.         $YPCAT passwd > $yp_passwd
  177.         if $TEST $? -eq 0 ; then
  178.             etc_passwd=$yp_passwd
  179.             fi
  180.         fi
  181.  
  182.     # get the hostname:
  183.     if $TEST -s /bin/hostname ; then
  184.         HOSTNAME=`/bin/hostname`
  185.     elif $TEST -s /bin/uname ; then
  186.         HOSTNAME=`/bin/uname -n`
  187.     elif $TEST -s /usr/bin/uuname ; then
  188.         HOSTNAME=`/usr/bin/uuname -l`
  189.         fi
  190.     if $TEST -z "$HOSTNAME" ; then
  191.         HOSTNAME="foobar"
  192.         fi
  193.  
  194.     $ECHO >>$TEMPM
  195.         $ECHO ATTENTION:                        >> $TEMPM
  196.         $ECHO "SUID Security Report for "`$DATE`>> $TEMPM
  197.  
  198.         $ECHO "from host $HOSTNAME"             >> $TEMPM
  199.     $ECHO >>$TEMPM
  200.  
  201. # NEW STUFF... $TEMPNEW holds the new SUID files; stuff the results in $TEMPM:
  202.     for i in `$AWK '{print $NF}' $TEMPNEW`
  203.         do
  204.         # don't want SUID files to be world writable!
  205.         ./is_able $i w w >> $TEMPM
  206.  
  207.         type=`file "$i" | $SED 's/.*://' | $type_filter`
  208.  
  209.         if $TEST -n "$type" ; then
  210.             owner=`$LS -ldga $i | $AWK '{print $3}'`
  211.             uid=`$AWK -F: '/^'"$owner"'/{print $3}' $etc_passwd`
  212.  
  213.             # set to nobody, if can't find 'em in the password file
  214.             if $TEST -z "$uid" ; then
  215.                 uid="-2"
  216.                 fi
  217.  
  218.             if $TEST "$uid" -eq "0" ; then
  219.                 $ECHO Warning!  ROOT owned SUID file $i is type: $type! >> $TEMPM
  220.             else
  221.                 $ECHO Warning!  User: $owner SUID file $i is type: $type! >> $TEMPM
  222.                 fi
  223.             fi
  224.         done
  225.  
  226.     if $TEST -s $TEMPNEW; then
  227.         $ECHO $WARN_NEW":" >>$TEMPM
  228.         $ECHO '' >>$TEMPM
  229.         $CAT $TEMPNEW >>$TEMPM
  230.         $ECHO '' >>$TEMPM
  231.     fi
  232.     if $TEST -s $TEMPGON; then
  233.         $ECHO $WARN_NO":" >>$TEMPM
  234.         $ECHO '' >>$TEMPM
  235.         $CAT $TEMPGON >>$TEMPM
  236.     fi
  237.  
  238.     # mail or save to a file?
  239.     if $TEST -z "$OUTFILE" ; then
  240.         $MAIL $INFORM <$TEMPM
  241.     else
  242.         $MV $TEMPM $OUTFILE
  243.         fi
  244.     $RM -f $TEMPM
  245. fi
  246. $RM -f $TEMPOLD $TEMPCUR $TEMPNEW $TEMPGON $yp_passwd
  247.  
  248. #  end it all....
  249. exit 0
  250.