- #!/bin/bash
- VERSION="1.0.1"
- #------- CONFIGURATION -------#
- HOSTNAME="ftp.fiend.ath.cx"
- ROUTER1="tarpit"
- ROUTER2="switch"
- APACHE_LOGS="/var/log/apache2/*access_log*"
- POSTFIX_LOGS="/var/log/messages*"
- SUBNET_THRESHOLD=10
- #WWW Search strings
- www_list=( ""EZI_HTTP_NETDEV_DISCOVER"" ""/cgi/"" ""scanner"" ""admin"" ""robots.txt"" )
- #SMTP Search strings
- mail_list=( ""NOQUEUE"" ""relay access denied"" ""reject"" )
- #Additional IPs (Not found in logs)
- ban_list=( ""78.150.0.0/16"" ""194.97.153.0/24"" ""193.254.186.0/24"" ""72.148.0.0/16"" ""72.148.40.0/24"" ""220.240.0.0/16"" ""79.210.98.0/24"" )
- #------- END -------#
- sh_hp(){
- echo -e "$0 version: $VERSION \n Usage $0 [OPTION] [ARGUMENT] \n Examples: $0 -add 12.12.12.0/24 \n \n Options: \n -build -- Build the firewall \n -clear -- Clear the firewall \n -rules -- Prints the Iptable rules \n -add [IP ADDRESS] -- Adds a host or subnet to the firewall \n -del [RULE NUMBER] -- Deletes a rule from the firewall \n -reroute [HOSTNAME] -- Reroute traffic to a different router \n -help -- Prints this info \n"
- }
- sh_ip(){
- nslookup $1 | grep -a2 $1 | grep Address | cut -d: -f2 | cut -d. -f1,2,3,4
- }
- sh_fw(){
- iptables -L INPUT -v -n --line-numbers
- }
- cr_fw(){
- iptables -P INPUT ACCEPT && iptables -P OUTPUT ACCEPT && iptables -P FORWARD ACCEPT && iptables -F && iptables -X && iptables -Z && echo "Iptables cleared."
- }
- rm_rle(){
- if [ "$#" -gt 1 ] && [ "$#" - eq 2 ]; then iptables -D INPUT $1 && echo "$2"; elif [ "$#" -lt 2 ] && [ "$#" -gt 0 ]; then iptables -D INPUT $1; fi
- }
- mk_rle(){
- if [ "$#" -lt 2 ]; then iptables -I INPUT -s "$1" -j DROP; else iptables -I INPUT -s "$1" && echo "Ruled created for $2"; fi
- }
- mk_ban(){
- if [ ${#ban_list[@]} -gt 0 ]; then i=0; while [ "$i" -lt "${#ban_list[@]}" ]; do mk_rle "${ban_list[$i]}" "${ban_list[$i]}"; let "i = $i + 1"; done; fi
- }
- ch_ban()
- {
- t=0; m=0
- while [ "$t" -lt "${#ban_list[@]}" ]; do if [ "$1" == "`echo ${ban_list[$t]} | cut -d. -f1,2,3`" ]; then m=1; fi; let "t = $t + 1"; done
- echo $m
- }
- search(){
- #loop for apache
- j=0
- while [ "$j" -lt "${#www_list[@]}" ]; do zgrep --no-filename -i -u "${www_list[$j]}" $APACHE_LOGS | cut -d \ -f1 | cut -d. -f1,2,3; let "j = $j + 1"; done
- #loop for postfix
- i=0
- while [ "$i" -lt "${#mail_list[@]}" ]; do zgrep --no-filename -i -u ${mail_list[$i]} $POSTFIX_LOGS | cut -d[ -f3 | cut -d] -f1 | cut -d. -f1,2,3; let "i = $i + 1"; done
- }
- mk_fw(){
- ip_list=( `search | sort -u` ); i=0; cr_fw;
- while [ "$i" -lt "${#ip_list[@]}" ]; do if [ "${ip_list[$i]}" != `sh_ip $HOSTNAME | cut -d. -f1,2,3` ]; then if [ `ch_ban ${ip_list[$i]}` -eq 0 ]; then mk_rle "${ip_list[$i]}.0/24" "${ip_list[$i]}.0/24"; fi; fi; let "i = $i + 1"; done; mk_ban; echo "Firewall built."
- }
- case "$1" in
- "")
- sh_hp
- ;;
- --help|-h)
- sh_hp
- shift
- ;;
- --build|-b)
- mk_fw
- shift
- ;;
- --clear|-c)
- cr_fw
- shift
- ;;
- --rules|-r)
- sh_fw
- shift
- ;;
- --add|-a)
- mk_rle $2
- shift
- ;;
- --delete|-d)
- rm_rle $2
- shift
- ;;
- esac
- exit 0
Posted by logged0ut on Sun 16th Dec 11:41 (modification of post by view diff)
download | new post
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.