I have written a little script to scan users home directories for certain commands located inside a file. The script is setup to include a small help section and allows for passing a username argument to override scanning of all users home directories.
A lot of searching and trial and error has went into this script on my part. I would appreciate it if some of you that actually know what you're doing can take a look at it for me. If you could give me some pointers, ideas, or suggestions I would be grateful.
#!/bin/bash
# default folder we will scan
DIR=/home
# a list of commands we need to check for
# seperate commands with a pipe |
LOOKFOR='@system|@shell_exec|@include|@shell'
function help {
# setup small help list
echo -e "Pass an aurgument via command line or run without args."
echo -e " example: ./filescanner.sh -u username\n"
echo -e " -u\t pass a username to run against a single account."
echo -e " -h\t This help message.\n"
}
function scanDir {
# check if $1 else scan all user homes
if [ "$1" ]; then
# do stuff on single user
if [ -d "$DIR/$1" ]; then
list="$DIR/$1"
else
echo "User not found in the \$HOME directory"
exit
fi
else
# scan all home directories where a user account exists
list=`grep $DIR /etc/passwd | cut -d: -f6`
fi
echo "Checking files for $LOOKFOR."
for i in $list; do
path="$i/public_html"
if [ -d "$path" ]; then
# check if our path is a directory
echo "Checking $i/public_html/"
grep -H -E $LOOKFOR $i/public_html/* -R | cut -d: -f1
else
echo "No files to check; $path not a directory."
fi
done
}
while getopts "hu:" flag
do
case $flag in
u )
# echo "$flag" $OPTIND $OPTARG
# read flag and pass to scanDir function
scanDir $OPTARG
exit
;;
h )
# if h call help function
help
exit
;;
* )
echo -e "Invalid option. Please use -h for help.\n"
exit
;;
esac
done
# no options passed scan all
scanDir
Best Regards,
Brandon