#!/bin/ksh

# snort_monitor by tgiles (tgiles@gsihosting.com)
# This script will monitor the /var/log/messages file for level 1 (highest) snort alert events. Once an event is received,
# the alert is dumped into /tmp and emailed off to the arms of a greatful team, eager to research and address the issue.

rm -rf /tmp/snort_alert

logger "snort alert monitor is starting up..." # This will defeat a possible race condition. leave it in.

file=/var/log/messages

tail -f -n1 $file |&
while read -p line
do

# This will look for any Priority 1 snort alerts and write them out if they happen.
# you can add any number of alert items below. as long as they show up in a grep, they can be alerted on.

echo $line | grep "\[Priority: 1\]" && echo $line > /tmp/snort_alert
#echo $line | grep "\[Priority: 2\]" && echo $line > /tmp/snort_alert
#echo $line | grep "\[Priority: 3\]" && echo $line > /tmp/snort_alert

# Here's a sample for your own alerts. anything can be grepped for.
# echo $line | grep "my special alert item here" && echo $line > /tmp/snort_alert

sleep 1 # Sleeping to beat another possible race condition.

while [ -e /tmp/snort_alert ]; do
                SUBJECT="IDS alert from $HOSTNAME@complianthost.com"
                # Email To ?
                EMAIL="tgiles@gsihosting.com" # change this to affected team
                # Email text/message
                EMAILMESSAGE="/tmp/snort_alert"
                echo "">>$EMAILMESSAGE
                echo "
				
				============
				
				Please review the above snort alert as well as the snort alert documentation available in the wiki.
				* Review firewalls to see if traffic is allowed
				* search ERP for any notes on affected IP addresses
				* Document each affected device
				* Note results of any research performed on the alert
				
				Once done, please forward your research results to compsec for review. 		
				
				https://wiki.corp.gsihosting.com/wiki/How_To_Work_A_Snort_Ticket
				
			
                                                        ">> $EMAILMESSAGE
                #echo "This is email text" >>$EMAILMESSAGE
                # send an email using /bin/mail
                /bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE

                # clean up so no dupes show up.
                rm -rf /tmp/snort_alert
done

done
