#!/bin/bash
#Written by mshooltz, any questions contact me at mshoolt@liquidweb.com


function modsec2
{
echo "MODSEC2 Whitelisting for dummies"
echo "What would you like to do?"
echo "1.)Whitelist a uri?"
echo "2.)Whitelist a domain?"
echo "3.)Whitelist a rule id?"

echo "[ENTER]"
read m2option
echo "You have selected $m2option"

if [ -z $m2option ]; then
        exit
fi

        if [ $m2option = "1" ]; then
                option1m2
        fi

                if [ $m2option = "2" ]; then
                        option2m2
                fi

}
function modsec1
{
        echo "modsec1 found"
}

function uri
{
        echo "SecRule REQUEST_URI \"$ruri\" phase:1,nolog,allow,ctl:ruleEngine=Off" >> /usr/local/apache/conf/modsec2/whitelist.conf
        echo "SecRule REQUEST_URI \"$ruri\" phase:2,nolog,allow,ctl:ruleEngine=Off" >> /usr/local/apache/conf/modsec2/whitelist.conf
        echo "Restarting Apache please wait............."
                /etc/init.d/httpd stop > /dev/null 2>&1
                /etc/init.d/httpd startssl > /dev/null 2>&1
        echo "Complete, the REQUEST_URI $ruri is no longer protected"
 }

function server
{

        echo "SecRule SERVER_NAME \"$domain\" phase:1,nolog,allow,ctl:ruleEngine=Off" >> /usr/local/apache/conf/modsec2/whitelist.conf
        echo "SecRule SERVER_NAME \"$domain\" phase:2,nolog,allow,ctl:ruleEngine=Off" >> /usr/local/apache/conf/modsec2/whitelist.conf
        echo "Restarting Apache please wait............."
                /etc/init.d/httpd stop > /dev/null 2>&1
                /etc/init.d/httpd startssl > /dev/null 2>&1
        echo "Complete, The domain $domain is no longer protected"

 }

#function rule
#{


#}

function option1m2
{

        echo "Please enter the Request_URI to whitelist[ENTER]"
                read ruri

                if [ -z $ruri ]; then
                       option1m2
                fi

        echo "Is this correct (Y/n) - $ruri[ENTER]"
        read answer0

                if [ -z "$answer0" ]; then
                       uri
                fi
                        if [ "$answer0" = "n" ]; then
                                option1m2
                        fi
                                if [ "$answer0" = "y" ]; then
                                        uri
                                fi
}

function option2m2
{

        echo "Please enter the Domain to whitelist[ENTER]"
                read domain

                if [ -z $domain ]; then
                       option2m2
                fi

        echo "Is this correct (Y/n) - $domain[ENTER]"
        read answer

                if [ -z "$answer" ]; then
                       server
                fi

                        if [ "$answer" = "n" ]; then
                               option2m2
                        fi

                                if [ "$answer" = "y" ]; then
                                        server
                                fi

}


##################################################################


if ! id | grep -q "uid=0(root)" ; then
  echo "ERROR:  You must be root to run this."
  exit 1
fi

cpanelVerify=$(chkconfig --list | grep cpanel)
if [[ $cpanelVerify == *cpanel* ]]; then
        echo "cPanel Found!"
elif [[ $cpanelVerify != *cpanel* ]]; then
        echo "I cannot find cPanel. Exiting..."
        exit
fi

apacheversion=$(/usr/sbin/httpd -v |grep "Server version:"|awk '{print $3}')
if [[ $apacheversion == *2.* ]]; then
        echo "Mod_sec 2 Found!"
        modsec2
elif [[ $apacheversion != *1.* ]]; then
        echo "Mod_sec 1 Found"
        modsec1
fi

