#!/bin/bash

# lt_snort_manage.sh by Tommie Giles (tommie.giles@layeredtech.com)
# this script (and the applicable library) will perform a number of standard tasks for IDS server deployments

# get your variables here!
HOME_DIR="/opt/snort"
GSI_IP="206.55.126.215"
MY_VERSION="1"

# source management library
source $HOME_DIR/scripts/manage_library.sh

# check for root's UID before starting...
ROOT_UID=0   # Root has $UID 0.
if [ "$UID" -ne "0" ]
then
  echo "Please run this as root."
  exit
fi

# logging and notification mechanism
log_something(){
    echo "`date` - $1"
    echo "`date` - $1" >> $HOME_DIR/snort_manage.log
}


# check for arguments

if [ -z "$1" ]
  then
    echo "
    No argument supplied. please select from one of the following arguments:

    -ping - script will ping IP $GSI_IP once (and trip snort canary)
    -init - sets up an init script in /etc/init.d
    -update - updates the latest snort rules on phonehome. verbose.
    -flood - sets snort to alert on *all* TCP, UDP, and ICMP traffic. For troubleshooting.
    -noflood - disables the above flooding
    -cc - enables unencrypted credit card monitoring for snort. see wiki for details.
    -nocc - disabling unencrypted credit card monitoring for snort."
    exit
fi
############################################################################################################



############################################################################################################
if [ $1 == "-ping" ]
then
    log_something "INFO pinging IP $GSI_IP..."
    ping $GSI_IP -c1 -w1
    log_something "INFO pinging IP $GSI_IP completed"
exit
fi

if [ $1 = "-init" ]
then
    log_something "INFO you selected init. installing script in /etc/init.d/snort"
    rm -rf /etc/init.d/snort
    echo "#!/bin/bash

# install this to /etc/init.d/snort

ROOT_UID=0   # Root has \$UID 0.

if [ \"\$UID\" -eq \"0\" ]  # Will the real \"root\" please stand up?
then
  echo \"You are root. Running.\"
else
  echo \"Please run this as root.\"
  exit
fi

# restart capability, added 2/4/11 by tgiles

if [ \"\$1\" = \"r\" ]
then
  echo \"restarting snort. hang on\"

  # stopping part

  SNORT_PIDS=\`cat /var/run/*snort*pid\`
  echo \"killing snort PID's:\" \$SNORT_PIDS
  kill -9 \$SNORT_PIDS
  rm -rf /var/run/*snort*pid
  echo \"snort is stopped\"
  sleep 4
rm -rf /var/log/snort/*

  # starting part

  for i in \`/sbin/ifconfig | grep eth | /usr/bin/awk ' { print \$1 } '\`
  do
  /opt/snort/bin/snort -i \$i -c /opt/snort/etc/snort.conf -D -F /opt/snort/excludes.conf --perfmon-file /opt/snort/snort.perfmon.\$i.csv &
  echo \"starting snort for \$i with PID \$!\"
  done

else

# end of snort restart capability

        if [ -a /var/run/snort_eth0.pid ]; then

echo \"snort running. Killing it off now\"

SNORT_PIDS=\`cat /var/run/*snort*pid\`
echo \"killing snort PID's:\" \$SNORT_PIDS
kill -9 \$SNORT_PIDS
rm -rf /var/run/*snort*pid

else

echo \"snort is not running. starting snort\"

for i in \`/sbin/ifconfig | grep eth | /usr/bin/awk ' { print \$1 } '\`
do
/opt/snort/bin/snort -i \$i -c /opt/snort/etc/snort.conf -D -F /opt/snort/excludes.conf --perfmon-file /opt/snort/snort.perfmon.\$i.csv &
echo \"starting snort for \$i with PID \$!\"
done

fi

fi" >> /etc/init.d/snort
chmod +x /etc/init.d/snort
log_something "INFO finished installing snort init. You can run '/etc/init.d/snort' to stop and start snort now."
exit
fi

if [ $1 == "-update" ]
then
    log_something "INFO you selected update..."
    download_rules
    extract_rules
    backup_rules
    install_rules
    disabled_rules
    rule_directory_cleanup
    rebuild_rules
    insert_default_rules
    link_library
    restart_snort
    exit
fi

if [ $1 == "-flood" ]
then
    log_something "INFO you selected flood. check /var/log/messages or splunk after enabling flood. You should see a lot."
    insert_flood_rules

exit
fi

if [ $1 == "-noflood" ]
then
    log_something "INFO you selected noflood. removing flood entries and restarting snort."
    remove_flood_rules
exit
fi

if [ $1 == "-cc" ]
then
    log_something "INFO enabling cc detection capability and restarting snort"
    enable_cc_monitor
    restart_snort
exit
fi

if [ $1 == "-nocc" ]
then
    log_something "INFO disabling cc detection capability and restarting snort"
    disable_cc_monitoring
    restart_snort

exit
fi

if [ $1 == "-v" ]
then
    echo $MY_VERSION > $HOME_DIR/scripts/current_version
exit
fi

echo "
    incorrect argument supplied. please select from one of the following arguments:

    -ping - script will ping IP $GSI_IP once (and trip snort canary)
    -init - sets up an init script in /etc/init.d
    -update - updates the latest snort rules on phonehome. verbose.
    -flood - sets snort to alert on *all* TCP, UDP, and ICMP traffic. For troubleshooting.
    -noflood - disables the above flooding
    -cc - enables unencrypted credit card monitoring for snort. see wiki for details.
    -nocc - disabling unencrypted credit card monitoring for snort."