Advertisement
rs232

p2partisan 1.07

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