#!/bin/bash
# Graudit version 1.1 (2009 June 19)
# Written by Wireghoul - http://www.justanotherhacker.com
# Released under the GPL licence

VERSION='1.1'
grdir=`dirname $0`

# Default values
context=1
color='always'
database="$grdir/signatures/default.db"
separator='##############################################'

version () {
    echo "Graudit version: $VERSION"
}

usage () {
    cat <<EOU
Usage: graudit [opts] /path/to/scan

OPTIONS
  -h show this message
  -v show version number
  -d database to use
  -c number of lines of context to display
  -z supress colors

Database is one of php, perl, rough or default, not providing a database will use default
/path is the path to the file or directory to audit
EOU
}

while getopts "hvzd:c:" opt; do
    case $opt in
        h)
            usage
            exit 1
        ;;
        v)
            version
            exit 0
        ;;
        z)
            color='none'
        ;;
        c)
            context="$OPTARG"
        ;;
        d)
            sigdb="$OPTARG"
            if [ "$sigdb" == '-' ]; then
                database='-'
            elif [ -f "$sigdb" ]; then
                database="$sigdb"
            else
                database="$grdir/signatures/$sigdb.db"
            fi
        ;;
        \?)
            echo "Invalid option: -$OPTARG" >&2
            usage
            exit 1
        ;;
    esac
done
#Remove arguments from $@
shift $((OPTIND-1))

if [ -z $1 ]; then
        usage
        exit 1
fi

# -R is recursive
# -H prints the name of the file
# -C prints # lines of context before and after the match
# -E uses extended regexp
# -f specifies the rule file (signature database)
# -n prints the line number

grep    --color=$color \
        --exclude-dir=.svn \
        --exclude-dir=.cvs \
        --exclude-dir=.git \
        -n -R -H -C $context -E \
        -f $database "$@" \
        | sed -e"s/--/$separator/"

