#!/bin/bash
###########
# 
# check for ip in kernel routing table, firewall, cphulkd brutes, cphulkd blacklist, and ipsets.
# https://confluence.endurance.com/display/HGS/IsNulled
# https://stash.endurance.com/projects/HGADMIN/repos/isnulled
# Please submit all bug reports at bugs.hostgator.com
#
# (C) 2015 - 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.
# v0.4: 12/2015: jlong: added ipdrop_global ipset check, cphulkd login_track check.

# 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.

ip_bin='/sbin/ip'
customer_ip=${1}
hex_ip=$(printf '%02X' ${customer_ip//./ }; echo)
padded_ip="00000000000000000000FFFF${hex_ip}"

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=${customer_ip}

if [ -z $(echo ${customer_ip} | 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 "${customer_ip} 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 ${customer_ip} ] ; 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.4 By HostGator"
        if [ -z $1 ]; then
                use ;
        fi
        for ourown in $($ip_bin addr show|egrep '^ *inet'|grep brd|awk -- '{ print $2; }'|sed -e 's:/[0-9]*$::'); 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_bin 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_bin route | grep 'unreachable' | grep ${1} | awk '{print $2}') ] ; then
          $ip_bin route del unreachable ${1}
        elif [ ! -z $($ip_bin ip route | grep 'blackhole' | grep ${1} | awk '{print $2}') ] ; then
          $ip_bin 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 logged by the firewall $(grep 'kernel' /var/log/messages | grep -c ${1}) Times."
 else
     print "[+] ${1} has not been logged by the firewall on this server."
 fi
}

firewall_check() {
    if [[ -f /etc/firewall/conf/ipdrop_global ]]; then
        if grep -q ${1} /etc/firewall/conf/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/conf/ipdrop_global
                ipset del ipdrop_global ${1}
                print "[+] ${1} has been ${COLOR_GREEN}REMOVED${COLOR_DEFAULT} from: /etc/firewall/conf/ipdrop_global." 
            fi
        else
            print "[+] ${1} is NOT blocked by IPDROP_GLOBAL."
        fi
    fi
}

firewall_local_check() {
    if [[ -f /etc/firewall/conf/ipdrop_local ]]; then
        if grep -q ${1} /etc/firewall/conf/ipdrop_local; then
            print "[!]${1} ${COLOR_RED}BLOCKED${COLOR_DEFAULT} by IPDROP_LOCAL."
            if [ ${2} == "n" ]; then
                echo "" > /dev/null
            else
                sed -i "/${1}/D" /etc/firewall/conf/ipdrop_local
                ipset del ipdrop_local ${1}
                print "[+] ${1} has been ${COLOR_GREEN}REMOVED${COLOR_DEFAULT} from: /etc/firewall/conf/ipdrop_local" 
            fi
        else
            print "[+] ${1} is NOT blocked by IPDROP_LOCAL ipset."
        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 cphulkd brutes table.";
        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 brutes table."
        fi
    else
        print "[+]${1} is NOT blocked in cphulkd brutes table."
    fi
}

logintrack_check() {
	if mysql cphulkd -e "select hex(ADDRESS) from login_track where hex(ADDRESS) = '${padded_ip}';" -B|grep -q ${padded_ip};then
		print "[!] ${1} ${COLOR_RED}BLOCKED${COLOR_DEFAULT} by cphulkd login_track table.";
		if [ ${2} == "n" ]; then
			echo ""> /dev/null
		else
			mysql cphulkd -e "DELETE FROM login_track where hex(ADDRESS) = '${padded_ip}';"
			print "[+] ${1} Has been ${COLOR_GREEN}REMOVED${COLOR_DEFAULT} from the cphulkd login_track table."
		fi
	else
		print "[+]${1} is NOT blocked in cphulkd login_track table."
	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 table.";
        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 table."
        fi
    else
        print "[+] ${1} is NOT blocked in the cphulkd blacklist table."
    fi
}

ipset_check() {
	if ipset list $ipset_name|grep -q ${1}; then
	print "[!] ${1} ${COLOR_RED}BLOCKED${COLOR_DEFAULT} by ipset $ipset_name.";
	if [ ${2} == "n" ]; then
            echo ""> /dev/null
        else
			ipset del $ipset_name ${1}
			print "[+] ${1} Has been ${COLOR_GREEN}REMOVED${COLOR_DEFAULT} from ipset ${ipset_name}."
			
		fi
	else
		print "[+] ${1} is NOT blocked in ipset ${ipset_name}."
	fi
}
		
isnulled_preflight "${customer_ip}"
print "##################################"
isnulled_check "${customer_ip}" "${remove}"
isnulled_check "${classc}" "${remove}"
isnulled_check "${classb}" "${remove}"
isnulled_check "${classa}" "${remove}"
print "##################################"
cphulkd_check "${customer_ip}" "${remove}"
cphulkd_check "${classc}" "${remove}"
cphulkd_check "${classb}" "${remove}"
cphulkd_check "${classa}" "${remove}"
print "##################################"
blacklist_check "${customer_ip}" "${remove}"
blacklist_check "${classc}" "${remove}"
blacklist_check "${classb}" "${remove}"
blacklist_check "${classa}" "${remove}"
print "##################################"
logintrack_check "${customer_ip}" "${remove}"
logintrack_check "${classc}" "${remove}"
logintrack_check "${classb}" "${remove}"
logintrack_check "${classa}" "${remove}"
print "##################################"
ipset_check "${customer_ip}" "${remove}"
ipset_check "${classc}" "${remove}"
ipset_check "${classb}" "${remove}"
ipset_check "${classa}" "${remove}"
print "##################################"
firewall_check "${customer_ip}" "${remove}"
firewall_check "${classc}" "${remove}"
firewall_check "${classb}" "${remove}"
firewall_check "${classa}" "${remove}"
print "##################################"
firewall_local_check "${customer_ip}" "${remove}"
firewall_local_check "${classc}" "${remove}"
firewall_local_check "${classb}" "${remove}"
firewall_local_check "${classa}" "${remove}"
