Advertisement
Guest User

Untitled

a guest
Dec 24th, 2013
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.59 KB | None | 0 0
  1. #!/bin/sh
  2. #################################################################################################
  3. ## 25/12/2013 --- RT-AC56U / RT-ACRT66U / RT-AC68U Firewall Addition v1.0 Beta                  #
  4. ######################################################################################################
  5. ###          ----- Make Sure To Edit The Following Files -----                                       #
  6. ### /jffs/scripts/firewall-start                    <-- Blacklists IP's From /jffs/scripts/ipset.txt #
  7. ### /jffs/scripts/ipset.txt                   <-- Banned IP List/IPSet Rules                         #
  8. ######################################################################################################
  9.  
  10. ##############################
  11. #####Commands / Variables#####
  12. ##############################
  13. UNBANSINGLE="unban"          # <-- Remove Single IP From Blacklist
  14. UNBANALL="unbanall"          # <-- Unbans All IPs In Blacklist
  15. REMOVEBANS="removeall"       # <-- Remove All Entries From Blacklist
  16. SAVEIPSET="save"             # <-- Save Blacklists to /jffs/scripts/ipset.txt
  17. BANSINGLE="ban"              # <-- Adds Entry To Blacklist
  18. BANCOUNTRYSINGLE="country"   # <-- Adds entire country to blacklist
  19. BANCOUNTRYLIST="bancountry"  # <-- Bans specified countries in this file
  20. HIDEMYASS="hideme"           # <-- Switch to unrestricted DNS (proxydns.co)
  21. BACKUPRULES="backup"         # <-- Backup IPSet Rules to /jffs/scripts/ipset2.txt
  22. ##############################
  23.  
  24.  
  25. started=`date`
  26. bannedips=/jffs/scripts/ipamount
  27.  
  28. ###############################################################################################
  29. # Unban / Unbanall / Removeall / Scan / Ban / Country / Bancountry / Hideme / Findme / Backup #
  30. ###############################################################################################
  31.  
  32. if [ X"$@" = X"$UNBANSINGLE" ]
  33. then
  34.     echo "Input IP Address To Unban"
  35.     read unbannedip
  36.     logger -t Firewall "[Unbanning And Removing $unbannedip From Blacklist] ... ... ..."
  37.     ipset -q -D Blacklist $unbannedip
  38.     echo "`sed /$unbannedip/d /jffs/scripts/ipset.txt`" > /jffs/scripts/ipset.txt
  39.     echo "$unbannedip Is Now Unbanned"
  40.    
  41. elif [ X"$@" = X"$UNBANALL" ]
  42. then
  43.     echo "[Unbanning All IP's] ... ... ..."
  44.     logger -t Firewall "[Unbanning All IP's] ... ... ..."  
  45.     ipset flush
  46.    
  47. elif [ X"$@" = X"$REMOVEBANS" ]
  48. then
  49.     expr `ipset list | wc -l` - 15 > /jffs/scripts/ipamount
  50.     echo "[Deleting All `cat $bannedips` Entries From Blacklist] ... ... ..."
  51.     logger -t Firewall "[Deleting `cat $bannedips` Entries From Blacklist] ... ... ..."
  52.     ipset flush
  53.     ipset save > /tmp/home/root/ipset.txt
  54.    
  55. elif [ X"$@" = X"$SAVEIPSET" ]
  56. then
  57.     echo "[Saving Blacklists] ... ... ..."
  58.     ipset save > /jffs/scripts/ipset.txt
  59.     echo "`sed '/crond: USER admin/d' /tmp/syslog.log`" > /tmp/syslog.log
  60.    
  61. elif [ X"$@" = X"$BANSINGLE" ]
  62. then
  63.     echo "Input IP Address"
  64.     read bannedip
  65.     logger -t Firewall "[Adding $bannedip To Blacklist] ... ... ..."
  66.     ipset -q -A Blacklist $bannedip
  67.     echo "$bannedip Is Now Banned"
  68.    
  69. elif [ X"$@" = X"$BANCOUNTRYSINGLE" ]
  70. then
  71.     echo "Input Country Abreviation"
  72.     read country
  73.     for ip in $(wget -q -O - http://www.ipdeny.com/ipblocks/data/countries/$country.zone)
  74.     do
  75.     ipset -q -A BlockedCountries $ip
  76.     done
  77.    
  78. elif [ X"$@" = X"$BANCOUNTRYLIST" ]
  79. then
  80.     echo "[Banning Spam Countries] ... ... ..."
  81.     for country in cn pk ur af
  82.     do
  83.     for IP in $(wget -q -O - http://www.ipdeny.com/ipblocks/data/countries/$country.zone)
  84.     do
  85.     ipset -q -A BlockedCountries $IP
  86.     done
  87.     done
  88.  
  89. elif [ X"$@" = X"$HIDEMYASS" ]
  90. then
  91.     echo "Switching To Unrestricted Proxy DNS"
  92.     logger -t Firewall "[Switching To Unrestricted Proxy DNS] ... ... ..."
  93.     echo "nameserver 74.207.242.213" > /etc/resolv.conf
  94.     echo "nameserver 50.116.28.138" >> /etc/resolv.conf
  95.     killall dnsmasq
  96.     dnsmasq
  97.    
  98.    
  99. elif [ X"$@" = X"$BACKUPRULES" ]
  100. then
  101.     echo "Backing Up Current IPSet Rules"
  102.     cp -f /jffs/scripts/ipset.txt /jffs/scripts/ipset2.txt
  103.  
  104. else
  105.     echo "[IP Banning Started] ... ... ..."
  106.     logger -t Firewall "[IP Banning Started] ... ... ..."
  107.     # load ipset modules
  108.     IPSET_PATH=/lib/modules/2.6.22.19/kernel/net/ipv4/netfilter
  109.     insmod $IPSET_PATH/ip_set.ko
  110.     insmod $IPSET_PATH/ip_set_nethash.ko
  111.     insmod $IPSET_PATH/ip_set_iphash.ko
  112.     insmod $IPSET_PATH/ipt_set.ko
  113.     sleep 2
  114.     echo "0 * * * * /jffs/scripts/firewall-start save" > /var/spool/cron/crontabs/admin
  115.     echo "0 5 * * * /jffs/scripts/firewall-start backup" >> /var/spool/cron/crontabs/admin
  116.     [ -n "`pidof crond`" ] && killall -q crond
  117.     sleep 1
  118.     crond
  119.    
  120.     ipset -! restore -f /jffs/scripts/ipset.txt
  121.     ipset -N -q Blacklist iphash --hashsize 1024 --maxelem 200000
  122.     ipset -N -q BlockedCountries nethash --hashsize 4096 --maxelem 200000
  123.     iptables -D logdrop -m state --state NEW -j LOG --log-prefix "DROP " --log-tcp-sequence --log-tcp-options --log-ip-options
  124.     iptables -D INPUT -m set --set Blacklist src -j DROP
  125.     iptables -D INPUT -m set --set BlockedCountries src -j DROP
  126.     iptables -D logdrop -m state --state NEW -j SET --add-set Blacklist src
  127.     iptables -I INPUT -m set --set Blacklist src -j DROP
  128.     iptables -I INPUT -m set --set BlockedCountries src -j DROP
  129.     iptables -I logdrop -m state --state NEW -j SET --add-set Blacklist src
  130.     echo "`sed '/DROP IN=/d' /tmp/syslog.log`" > /tmp/syslog.log
  131. fi
  132.  
  133. #########
  134. #Logging#
  135. #########
  136. OLDAMOUNT=`cat /jffs/scripts/ipamount`
  137. echo "Started:  $started"
  138. echo "Finished: `date`"
  139. expr `ipset list | wc -l` - 15 > /jffs/scripts/ipamount
  140. NEWAMOUNT=`cat /jffs/scripts/ipamount`
  141. echo "`cat $bannedips` IP's currently banned."
  142. logger -t Firewall "[Complete] `cat $bannedips` IPs currently banned. `expr $NEWAMOUNT - $OLDAMOUNT` New IP's Banned. "
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement