Advertisement
Guest User

Untitled

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