#!/bin/bash
###########
# 
# check for ip in kernel routing table, firewall, cphulkd brutes, cphulkd blacklist, and iptables.
# gatorwiki.hostgator.com
# git.toolbox.hostgator.com/isnulled
# Please submit all bug reports at bugs.hostgator.com
#
# (C) 2011 - HostGator.com, LLC
###########
# v0.1: 11/2008: jmay: Initial Release for IP, /24 and /16
# v0.2: 10/2010: ncuster: Fixed greps to only drop IPs that are blocked
# v0.3: 11/2012: rheckel: Added firewall, cphulkd brutes, IP verification, and cphulkd blacklist checks.

# 1) check for ip in kernel routing table and iptables.
# 2) check for /24 in kernel routing table and iptables.
# 3) check for /16 in kernel routing table and iptables.
print() { printf "$*\n" ; }
quit()  { if [ ! -z "$1" ]; then echo "[!] $*" ; exit 2 ; fi }
use()   { echo "Usage: $0 <IP Address> <y|n>*** " ; echo "   *** y only if you want it to auto remove!" ; exit 2 ; }
ip=${1}

if [ -z $(echo ${1} | egrep '\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b') ]; then
    echo "${1} is not a valid IP address";
    use;
fi

classa=$(echo ${1} | awk -F . '{print $1".0.0.0/8"}')
classb=$(echo ${1} | awk -F . '{print $1"."$2".0.0/16"}')
classc=$(echo ${1} | awk -F . '{print $1"."$2"."$3".0/24"}')
COLOR_RED='\033[0;31m'
COLOR_GREEN='\033[0;32m'
COLOR_DEFAULT='\033[0m'

if [ -z ${1} ] ; then
 use ;
elif [ -z ${2} ] ; then
 remove="n"
elif [ ${2} == "n" ] ; then
 remove="n"
elif [ ${2} == '--clean' ] ; then
        remove="y"
        print "*** Enabling Removal! ***"
elif [ ${2} == 'y' ]; then
 remove="y"
 print "*** Enabling Removal! ***"
fi

isnulled_preflight() {
        print "[+] Is Nulled v0.3 By HostGator"
        if [ -z $1 ]; then
                use ;
        fi
        for ourown in $(/sbin/ifconfig -a |grep -Po '(?<=addr:)(?:(?:\d{1,3}[.]){3}\d{1,3})(?=\s)' | grep -v 127.0.0.1); do
            if [[ $ip == $ourown ]]; then
                print "[-] ${COLOR_RED} Will not continue ${COLOR_DEFAULT}"
                print "[--] $ip is one of this machine's service IPs"
                exit 1
            fi
        done 
}

isnulled_check() {
 if [ ! -z $(ip route | grep 'unreachable' | grep ${1} | awk '{print $2}') ] || [ ! -z $(ip route | grep 'blackhole' | grep ${1} | awk '{print $2}') ]; then
     print "[!] ${1} ${COLOR_RED}NULLED${COLOR_DEFAULT} by: /sbin/route or /sbin/ip route"
     if [ ${2} == "n" ]; then
        echo "" > /dev/null
     else
        print "[+] ${1} has been ${COLOR_GREEN}REMOVED${COLOR_DEFAULT} from: /sbin/route or /sbin/ip route"
        if [ ! -z $(ip route | grep 'unreachable' | grep ${1} | awk '{print $2}') ] ; then
          ip route del unreachable ${1}
        elif [ ! -z $(ip route | grep 'blackhole' | grep ${1} | awk '{print $2}') ] ; then
          ip route del blackhole ${1}
        fi
     fi
 elif [  $(iptables -L INPUT -n | egrep '(DROP|RESET|REJECT)' |grep -c ${1}) -ne '0' ]; then
     print "[!] ${1} ${COLOR_RED}NULLED${COLOR_DEFAULT} by: /sbin/iptables Input Line: $(iptables -L INPUT -n --line | grep ${1} | awk -F'/[*]|[*]/' '{ if ($2 ~ /[a-zA-Z0-9]/) {split($1,linenum," "); print linenum[1]" -"$2} else {split($1,linenum," "); print linenum[1]}}' | head -n 1)"
     if [ ${2} == "n" ]; then
        echo "" > /dev/null
     else
        print "[+] ${1} has been ${COLOR_GREEN}REMOVED${COLOR_DEFAULT} from: /sbin/iptables"
        iptables -D INPUT $(iptables -L INPUT -n --line | egrep '(DROP|RESET|REJECT)' | grep ${1} | awk '{print $1}' | head -n 1)
     fi
 elif [ $(grep 'kernel' /var/log/messages | grep -c "SRC=${1}") != '0' ]; then
     print "[!] ${1} was found to be dropped by the firewall $(grep 'kernel' /var/log/messages | grep -c ${1}) Times."
 else
     print "[+] ${1} Is NOT Nulled on this server."
 fi
}

firewall_check() {
    if [[ -f /etc/firewall/IPDROP_GLOBAL ]]; then
        if grep -q ${1} /etc/firewall/IPDROP_GLOBAL; then
            print "[!]${1} ${COLOR_RED}BLOCKED${COLOR_DEFAULT} by IPDROP_GLOBAL"
            if [ ${2} == "n" ]; then
                echo "" > /dev/null
            else
                sed -i "/${1}/D" /etc/firewall/IPDROP_GLOBAL
                print "[+] ${1} has been ${COLOR_GREEN}REMOVED${COLOR_DEFAULT} from: /etc/firewall/IPDROP_GLOBAL" 
            fi
        else
            print "[+] ${1} is NOT blocked by IPDROP_GLOBAL"
        fi
    fi
}

cphulkd_check() {
    if mysql cphulkd -e 'SELECT IP FROM brutes' -B | grep -q ${1}; then
        print "[!] ${1} ${COLOR_RED}BLOCKED${COLOR_DEFAULT} by bruteforce";
        if [ ${2} == "n" ]; then
            echo ""> /dev/null
        else
            mysql cphulkd -e "DELETE FROM brutes WHERE IP = '${1}'"
            print "[+] ${1} Has been ${COLOR_GREEN}REMOVED${COLOR_DEFAULT} from the bruteforce table"
        fi
    else
        print "[+]${1} is NOT blocked in bruteforce tables"
    fi
}

blacklist_check() {
    if mysql cphulkd -e 'SELECT IP FROM blacklist' -B | grep -q ${1}; then
        print "[!] ${1} ${COLOR_RED}BLOCKED${COLOR_DEFAULT} by cpHulkd blacklist";
        if [ ${2} == "n" ]; then
            echo ""> /dev/null
        else
            mysql cphulkd -e "DELETE FROM blacklist WHERE IP = '${1}'"
            print "[+] ${1} Has been ${COLOR_GREEN}REMOVED${COLOR_DEFAULT} from the cpHulkd blacklist"
        fi
    else
        print "[+] ${1} is NOT blocked in the cpHulkd blacklist"
    fi
}


isnulled_preflight "${1}"
print "##################################"
isnulled_check "${1}" "${remove}"
isnulled_check "${classc}" "${remove}"
isnulled_check "${classb}" "${remove}"
isnulled_check "${classa}" "${remove}"
print "##################################"
cphulkd_check "${1}" "${remove}"
cphulkd_check "${classc}" "${remove}"
cphulkd_check "${classb}" "${remove}"
cphulkd_check "${classa}" "${remove}"
print "##################################"
blacklist_check "${1}" "${remove}"
blacklist_check "${classc}" "${remove}"
blacklist_check "${classb}" "${remove}"
blacklist_check "${classa}" "${remove}"
print "##################################"
firewall_check "${1}" "${remove}"
firewall_check "${classc}" "${remove}"
firewall_check "${classb}" "${remove}"
firewall_check "${classa}" "${remove}"
