Advertisement
rs232

p2partisan 1.05

Mar 22nd, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.62 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # p2partisan v1.5 (22/03/2014)
  4. #
  5. # <CONFIGURATION> ###########################################
  6. # Adjust location where the files are kept
  7. cd /cifs1/p2partisan
  8. #
  9. # Edit the file "blacklists" to customise if needed
  10. # Edit the "whitelist" to overwrite the blacklist if needed
  11. #
  12. #Maximum number of logs to be recorded in a given 60 sec
  13. maxloghour=120
  14. # to troubleshoot blocked connection close all the secondary
  15. # traffic e.g. p2p and try a connection to the blocked
  16. # site/port you should find a reference in the logs.
  17. #
  18. # ports to be whitelisted. Whitelisted ports will never be
  19. # blocked no matter what the source/destination IP is.
  20. # This is very important if you're running a service like
  21. # e.g. SMTP/HTTP/IMAP/else. Separate value in the list below
  22. # with commas - NOTE: keep at least 80 and 443 in this list
  23. whiteports="80,443,993,25,21"
  24. #
  25. # Fastrouting will process the IP classes very quickly but use
  26. # Lot of resources. If you disable the effect is transparent
  27. # but the full process will take minutes rather than seconds
  28. # 0=disabled 1=enabled
  29. fastroutine=1
  30. #
  31. # </CONFIGURATION> ###########################################
  32.  
  33.     [ -f iptables-add ] && rm iptables-add
  34.     [ -f iptables-del ] && rm iptables-del
  35.     [ -f ipset-del ] && rm ipset-del
  36.      
  37. echo "### PREPARATION ###"
  38. echo "loading modules"
  39. # Loading ipset modules
  40. lsmod | grep "ipt_set" > /dev/null 2>&1 || \
  41. for module in ip_set ip_set_iptreemap ipt_set
  42.         do
  43.         insmod $module
  44.         done
  45.  
  46. counter=0
  47.      
  48. echo "loading ports $whiteports exemption"
  49. iptabweb=`iptables -L FORWARD | grep "ports www,https" | wc -l`
  50. if [ $iptabweb -eq 0 ]; then
  51.     echo "iptables -A FORWARD -p tcp --match multiport --sports $whiteports -j ACCEPT
  52. iptables -A FORWARD -p tcp --match multiport --dports $whiteports -j ACCEPT" >> iptables-add
  53. elif [ $iptabweb -ne 2 ]; then
  54.     echo "iptables -D FORWARD -p tcp --match multiport --sports $whiteports -j ACCEPT
  55. iptables -D FORWARD -p tcp --match multiport --dports $whiteports -j ACCEPT" >> iptables-add
  56. fi
  57. echo "iptables -D FORWARD -p tcp --match multiport --sports $whiteports -j ACCEPT
  58. iptables -D FORWARD -p tcp --match multiport --dports $whiteports -j ACCEPT" >> iptables-del
  59.  
  60. echo "### WHITELIST ###"
  61. echo "loading the whitelist"
  62. #Load the whitelist
  63. if [ "$(ipset --swap whitelist whitelist 2>&1 | grep 'Unknown set')" != "" ]
  64.     then
  65.     ipset --create whitelist iptreemap
  66.     cat whitelist |
  67.     (
  68.     while read IP
  69.     do
  70.             echo "$IP" | grep "^#" >/dev/null 2>&1 && continue
  71.             echo "$IP" | grep "^$" >/dev/null 2>&1 && continue
  72.                     ipset -A whitelist $IP
  73.             done
  74.     )
  75. fi
  76. echo "ipset -X whitelist" >> ipset-del
  77.      
  78. iptabwhite=`iptables -L FORWARD | grep whitelist | wc -l`
  79. if [ $iptabwhite -eq 0 ]; then
  80.     echo "Preparing the whitelist for the iptables"
  81.     echo "iptables -A FORWARD -m set --set whitelist src,dst -j ACCEPT" >> iptables-add
  82. elif [ $iptabwhite -gt 1 ]; then
  83.     echo "Re-setting whitelist iptables"
  84.     echo "iptables -D FORWARD -m set --set whitelist src,dst -j ACCEPT" >> iptables-add
  85. fi
  86. echo "iptables -D FORWARD -m set --set whitelist src,dst -j ACCEPT" >> iptables-del
  87.            
  88. # set iptables to log blacklisted related drops
  89. logging=`iptables -L | grep "Chain LOGGING" | wc -l`
  90. if [ $logging = 0 ]; then
  91.    echo "iptables -N LOGGING " >> iptables-add
  92. fi
  93. echo "iptables -F LOGGING
  94. iptables -A LOGGING -m limit --limit $maxloghour/hour -j LOG --log-prefix "Blacklist-Dropped: " --log-level 1
  95. iptables -A LOGGING -j DROP" >> iptables-add
  96.  
  97. echo "### BLACKLISTs ###"
  98. cat blacklists |
  99.    (
  100.     while read line
  101.     do
  102.             echo "$line" | grep "^#" >/dev/null 2>&1 && continue
  103.             echo "$line" | grep "^$" >/dev/null 2>&1 && continue
  104.             counter=`expr $counter + 1`
  105.             name=`echo $line |cut -d ' ' -f1`
  106.             url=`echo $line |cut -d ' ' -f2`
  107.             echo "loading blacklist #$counter --> ***$name***"
  108.      
  109.     if [[ $fastroutine -eq 1 ]]; then
  110.      
  111.     if [ "$(ipset --swap $name $name 2>&1 | grep 'Unknown set')" != "" ]
  112.       then
  113.       [ -e $name.gz ] || wget -q -O $name.gz "$url"
  114.       { echo "-N $name iptreemap"
  115.         gunzip -c  $name.gz | \
  116.         sed -e "/^[\t ]*#.*\|^[\t ]*$/d;s/^.*:/-A $name /"
  117.         echo COMMIT
  118.       } | ipset -R
  119.     fi
  120.      
  121.     else
  122.      
  123.         if [ "$(ipset --swap $name $name 2>&1 | grep 'Unknown set')" != "" ]
  124.             then
  125.             ipset --create $name iptreemap
  126.             [ -e $name.lst ] || wget -q -O - "$url" | gunzip | cut -d: -f2 | grep -E "^[-0-9.]+$" > $name.lst
  127.             for IP in $(cat $name.lst)
  128.                     do
  129.                     ipset -A $name $IP
  130.                     done
  131.             fi
  132.              
  133.     fi
  134.  
  135.         echo "ipset -X $name " >> ipset-del
  136.         iptabin=`iptables -L FORWARD | grep $name | wc -l`
  137.         if [ $iptabin -eq 0 ]; then
  138.             echo "Preparing blacklist ***$name*** into the FORWARD iptables"
  139.             echo "iptables -A FORWARD -m set --set $name src,dst -j LOGGING" >> iptables-add
  140.         elif [ $iptabin -gt 1 ]; then
  141.             echo "Re-setting FORWARD iptables"
  142.             echo "iptables -D FORWARD -m set --set $name src,dst -j LOGGING" >> iptables-add
  143.         fi
  144.         echo "iptables -D FORWARD -m set --set $name src,dst -j LOGGING" >> iptables-del
  145.     done
  146.     )
  147. echo "iptables -F LOGGING " >> iptables-del
  148. echo "iptables -X LOGGING " >> iptables-del
  149. chmod 777 ./iptables-*
  150. chmod 777 ./ipset-*
  151. echo "### NOTEs ###"
  152. echo "Tomato is now running the script: iptables-add"
  153. echo "If you wish to remove p2partisan from your system"
  154. echo "run the command ./iptables-del ; ./ipset-del"
  155. ./iptables-add  #protecting the LAN
  156. echo "### DONE ###"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement