#!/bin/bash
#
#  CGIdecoy (c) Mixter, Jan 2000
#
#  This is a simple script that examines your CGI folder,
#  and can check for vulnerable scripts (-check),
#  generate decoy scripts, which will log any access
#  over the web as a possible exploit attempt (-create),
#  or remove vulnerable scripts and previously installed
#  decoy files (-clean).
#
#  Note: for the decoy scripts to work, you must allow
#  option 'FollowSymLinks' in your cgi-bin folder!
#

rm=/bin/rm
vulndb=vuln-cgis.txt
decoy=abuselog.cgi
debug=nah

function Usage () {
 printf "Usage:\t $0 <command> <cgidir>\n"
 printf "\t<command> can be -check, -create or -clean\n"
 printf "\t<cgidir> is your CGI folder, e.g. /home/httpd/cgi-bin\n"
 exit 0
}

function Create () {

 cp -f ${decoy} $dir
 chmod 755 $dir/${decoy}

 for i in `cat ${vulndb}` ; do

   if test $debug = yup ; then printf "[ $dir/${i} ]\n" ; fi

   if ! test -e $dir/${i} ; then
     ln -s $dir/${decoy} $dir/${i}
     printf "Creating '$dir/${i}' ...\n"
   fi

 done

 exit 0

}

function Check () {

 for i in `cat ${vulndb}` ; do

   if test $debug = yup ; then printf "[ $dir/${i} ]\n" ; fi

   if test -e $dir/${i} ; then
     printf "CGI script $dir/${i} exists..."
   fi

   if test -L $dir/${i} ; then
     printf " and is a decoy.\n"
   fi

   if test -f $dir/${i} ; then
     printf " and could be vulnerable.\n"
   fi

 done

 exit 0

}

function Clean () {

 $rm -f $dir/${decoy}

 for i in `cat ${vulndb}` ; do

   if test $debug = yup ; then printf "[ $dir/${i} ]\n" ; fi

   if test -L $dir/${i} ; then
     $rm -f $dir/${i}
   fi

   if test -f $dir/${i} ; then
     $rm -i $dir/${i}
   fi

 done

 exit 0

}

if [ $# != 2 ] ; then Usage ; fi

if ! test -d $2 ; then 
  printf "Sorry, directory $2 doesn't exist.\n"
  Usage
fi

export dir=$2
export rm vulndb decoy debug

#if test `whoami` = root ; then
#  printf "Do not run this script as root, please su to the account\n"
#  printf "under which the httpd childs are running first.\n\n"
#  exit
#fi

if test $1 = -create ; then Create ; fi

if test $1 = -check ; then Check ; fi

if test $1 = -clean ; then Clean ; fi

Usage

exit 0
