Guest User

Script to Block China IP's (IPTables/BASH)

a guest
Aug 25th, 2016
1,450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ### Block all traffic from -----> Use ISO code ###
  4. ISO="cn"
  5. ### Set PATH ###
  6. IPT=/sbin/iptables
  7.  
  8. wget -nv http://www.ipdeny.com/ipblocks/data/countries/all-zones.tar.gz
  9. tar -zxvf all-zones.tar.gz
  10.  
  11. echo 1 > /proc/sys/net/ipv4/ip_forward
  12. iptables -t nat -I PREROUTING 1 -j LOG
  13. iptables -t nat -I POSTROUTING 1 -j LOG
  14. iptables -t nat -I OUTPUT 1 -j LOG
  15.  
  16. cleanOldRules(){
  17. $IPT -F
  18. $IPT -X
  19. $IPT -t nat -F
  20. $IPT -t nat -X
  21. $IPT -t mangle -F
  22. $IPT -t mangle -X
  23. $IPT -P INPUT ACCEPT
  24. $IPT -P OUTPUT ACCEPT
  25. $IPT -P FORWARD ACCEPT
  26. }
  27.  
  28. # clean old rules
  29. cleanOldRules
  30.  
  31. for c in $ISO
  32. do
  33. for x in $(cat $c.zone)
  34. do
  35. prips $x >> tmp.txt 2>&1
  36. echo "DEBUG:" $x "extracting ... then onto the next"
  37. #iptables -A INPUT -s $x -j DROP
  38. done
  39. done
  40.  
  41. echo "Fin"
  42. echo "Onto the next bit, Setting up NAT in IPTables"
  43.  
  44. cat tmp.txt | while read LINE
  45. do
  46.  
  47. #echo "DEBUG:Creating IP Table Rule :- iptables -A INPUT -s" $LINE "-j DROP"
  48. #iptables -A INPUT -s $LINE -j DROP LOG --log-prefix "iptables Denied by JAMIE_SCRIPT: " --log-level 7
  49.  
  50. echo "DEBUG: Dropping and Logging" $LINE
  51.  
  52. iptables -A INPUT -s $LINE -j LOG --log-prefix "STOP_d: " --log-level 7
  53. iptables -A INPUT -s $LINE -j DROP
  54.  
  55. #echo "DEBUG:iptables -t nat -A OUTPUT -d" $LINE "-j DNAT --to-destination" $LINE
  56. #iptables -t nat -A OUTPUT -d $LINE -j DNAT --to-destination $LINE
  57. done
  58.  
  59. echo "Done, Housekeeping..."
  60.  
  61. rm *.zone
  62.  
  63. http://tgtechnotes.blogspot.co.uk/
Advertisement
Add Comment
Please, Sign In to add comment