Advertisement
rs232

p2partisan 2.10

May 6th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 14.81 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # p2partisan v2.1 (06/05/2014)
  4. #
  5. # <CONFIGURATION> ###########################################
  6. # Adjust location where the files are kept
  7. P2Partisandir=/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 min
  16. # Consider set this very low (like 3 or 6) once your are
  17. # happy with the installation. To troubleshoot blocked
  18. # connection close all the secondary traffic e.g. p2p
  19. # and try a connection to the blocked site/port you should
  20. # find a reference in the logs.
  21. maxloghour=6
  22. #
  23. # What do you want to block?
  24. # 1) Input (Router only, does your generate P2P traffic?)
  25. # 2) LAN (LAN clients only)
  26. # 3) Both *default
  27. protection=3
  28. #
  29. # ports to be whitelisted. Whitelisted ports will never be
  30. # blocked no matter what the source/destination IP is.
  31. # This is very important if you're running a service like
  32. # e.g. SMTP/HTTP/IMAP/else. Separate value in the list below
  33. # with commas - NOTE: Leave 80 and 443 untouched, add custom ports only
  34. # you might want to add remote admin and VPN ports here if any.
  35. # Standard iptables syntax, number divided by "," or ":" for a range
  36. # e.g. 80,443,2100:21300
  37. whiteports="21,25,80,123,443,993,1194:1197"
  38. #
  39. # Fastrouting will process the IP classes very quickly but use
  40. # Lot of resources. If you disable the effect is transparent
  41. # but the full process will take minutes rather than seconds
  42. # 0=disabled 1=enabled
  43. fastroutine=1
  44. #
  45. # Schedule updates? (once a week is plenty)
  46. schedule="30 4 * * 1"
  47. #
  48. # </CONFIGURATION> ###########################################
  49.  
  50. pidfile=/var/run/p2partisan.pid
  51. cd $P2Partisandir
  52.  
  53. # Wait until Internet is available
  54. while :
  55. do
  56.     ping -c 3 8.8.8.8 >/dev/null 2>&1
  57.     if [ $? = 0 ]; then
  58.         break
  59.     fi
  60.     sleep 2
  61. done
  62.  
  63.  
  64.  
  65. alias ipset='/usr/sbin/ipset'
  66. alias iptables='/usr/sbin/iptables'
  67. alias service='/sbin/service'
  68. alias plog='logger -t P2PARTISAN -s'
  69. now=`date`
  70. wanif=`nvram get wan_ifname`
  71.  
  72.  
  73. psoftstop() {
  74.     ./iptables-del 2> /dev/null
  75.     plog "Stopping P2Partisan"
  76.     [ -f $pidfile ] && rm -f "$pidfile" 2> /dev/null
  77. }
  78.  
  79. pblock() {
  80. iptables -N PARANOIA-DROP 2> /dev/null
  81. iptables -A PARANOIA-DROP -j DROP 2> /dev/null
  82. iptables -I wanin 1 -i $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  83. iptables -I wanout 1 -o $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  84. iptables -I INPUT 1 -i $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  85. iptables -I OUTPUT 1 -o $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  86. }
  87.  
  88. punblock() {
  89.     while iptables -L wanin | grep "PARANOIA-DROP"
  90.     do
  91.         iptables -D wanin -i $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  92.     done
  93.     while iptables -L wanout | grep "PARANOIA-DROP"
  94.     do
  95.         iptables -D wanout -o $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  96.     done
  97.     while iptables -L INPUT | grep "PARANOIA-DROP"
  98.     do
  99.         iptables -D INPUT -i $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  100.     done
  101.     while iptables -L OUTPUT | grep "PARANOIA-DROP"
  102.     do
  103.         iptables -D OUTPUT -o $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  104.     done
  105. iptables -F PARANOIA-DROP 2> /dev/null
  106. iptables -X PARANOIA-DROP 2> /dev/null
  107. }
  108.  
  109. pforcestop() {
  110.     while iptables -L wanin | grep P2PARTISAN-IN
  111.     do
  112.         iptables -D wanin -i $wanif -m state --state NEW -j P2PARTISAN-IN 2> /dev/null
  113.     done
  114.     while iptables -L wanout | grep P2PARTISAN-OUT
  115.     do
  116.         iptables -D wanout -o $wanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null
  117.     done
  118.     while iptables -L INPUT | grep P2PARTISAN-IN
  119.     do
  120.         iptables -D INPUT -i $wanif -m state --state NEW -j P2PARTISAN-IN 2> /dev/null
  121.     done
  122.     while iptables -L OUTPUT | grep P2PARTISAN-OUT
  123.     do
  124.         iptables -D OUTPUT -o $wanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null
  125.     done
  126.     iptables -F P2PARTISAN-DROP 2> /dev/null
  127.     iptables -F P2PARTISAN-IN 2> /dev/null
  128.     iptables -F P2PARTISAN-OUT 2> /dev/null
  129.     iptables -X P2PARTISAN-DROP 2> /dev/null   
  130.     iptables -X P2PARTISAN-IN 2> /dev/null
  131.     iptables -X P2PARTISAN-OUT 2> /dev/null
  132.     ipset -F
  133.     for i in `ipset --list | grep Name | cut -f2 -d ":" `; do
  134.         ipset -X $i
  135.     done
  136.     [ -f iptables-add ] && rm iptables-add
  137.     [ -f iptables-del ] && rm iptables-del
  138.     [ -f ipset-del ] && rm ipset-del
  139.     [ -f $pidfile ] && rm -f "$pidfile" 2> /dev/null
  140. plog "Stopping P2Partisan"
  141. }
  142.  
  143. pstatus() {
  144.     running0=`iptables -L P2PARTISAN-IN  2> /dev/null | grep -v target | grep -v Chain 2> /dev/null | wc -l`
  145.     running1=`iptables -L P2PARTISAN-OUT  2> /dev/null | grep -v target | grep -v Chain 2> /dev/null | wc -l`
  146.     running2=`iptables -L P2PARTISAN-DROP  2> /dev/null | grep -v target | grep -v Chain 2> /dev/null | wc -l`
  147.     running3=`iptables -L | grep P2PARTISAN-IN  2> /dev/null | wc -l`
  148.     running4=`[ -f $pidfile ] && echo 1 || echo 0`
  149.     running5=`nvram get script_fire | grep p2partisan >/dev/null && echo Yes || echo No`
  150.     running6=`cru l | grep P2Partisan-update >/dev/null && echo Yes || echo No`
  151.     running7=`tail -200 /var/log/messages | grep Dropped | tail -1`
  152.    
  153.     if [[ $running0 -eq "0" ]] || [[ $running1 -eq "0" ]] || [[ $running2 -eq "0" ]] || [[ $running3 -eq "0" ]] || [[ $running3 -eq "0" ]]; then
  154.         echo "### P2Partisan status ####################################
  155.     P2Partisan running: No
  156.     P2Partisan autorun: $running5
  157.     P2Partisan scheduled: $running6
  158. ### Last log recorded ####################################"
  159.     else
  160.         echo "### P2Partisan status ####################################
  161.     P2Partisan running: Yes
  162.     P2Partisan autorun: $running5
  163.     P2Partisan scheduled: $running6
  164. ### Last log recorded ####################################"
  165.     fi
  166.     echo "Remember your max logs per hour is set to: $maxloghour
  167. $running7
  168. ##########################################################"
  169. }
  170.  
  171. pautorunset() {
  172.     p=`nvram get script_fire | grep p2partisan | wc -l`
  173.     if [ $p -eq "0" ] ; then
  174.     t=`nvram get script_fire`; t=`printf "$t\n$P2Partisandir/p2partisan.sh\n"` ; nvram set "script_fire=$t"
  175.     fi
  176.     plog "P2Partisan AUTORUN is ON"
  177. }
  178.  
  179. pautorununset() {
  180.     p=`nvram get script_fire | grep p2partisan | wc -l`
  181.     if [ $p -eq "1" ]; then
  182.     t=`nvram get script_fire`; t=`printf "$t\n$P2Partisandir/p2partisan.sh\n" | grep -v p2partisan` ; nvram set "script_fire=$t"
  183.     fi
  184.     plog "P2Partisan AUTORUN is OFF"
  185. }
  186.  
  187. pscheduleset() {
  188.     cru d P2Partisan-update
  189.     cru a P2Partisan-update "$schedule $P2Partisandir/p2partisan.sh paranoia-update"
  190.     plog "P2Partisan AUTO UPDATE is ON"
  191. }
  192.  
  193. pscheduleunset() {
  194.     cru d P2Partisan-update
  195.     plog "P2Partisan AUTO UPDATE is OFF"
  196. }
  197.  
  198. pstart() {
  199.     running4=`[ -f $pidfile ] && echo 1 || echo 0`
  200.     if [ $running4 -eq "0" ]; then
  201.  
  202.     echo $$ > $pidfile
  203.  
  204.     sleep 2
  205.    
  206.     [ -f iptables-add ] && rm iptables-add
  207.     [ -f iptables-del ] && rm iptables-del
  208.     [ -f ipset-del ] && rm ipset-del
  209.      
  210.         echo "### PREPARATION ###"
  211.         echo "loading modules"
  212.         # Loading ipset modules
  213.         ipset_test=`lsmod | grep "ipt_set" | wc -l`
  214.         if [ $ipset_test -gt "0" ]; then
  215.             echo "Loading the ipset module"
  216.         else
  217.             echo "###########################################
  218. ATTENTION: ipset not found! Please check if
  219. your tomato release has support for ipset
  220. ###########################################"
  221.         fi
  222.     lsmod | grep "ipt_set" > /dev/null 2>&1 || \
  223.         for module in ip_set ip_set_iptreemap ipt_set
  224.             do
  225.                 insmod $module
  226.             done
  227.  
  228. counter=0
  229. pos=1
  230.         echo "loading ports $whiteports exemption"
  231.  
  232.    
  233.         echo "# $now
  234. iptables -N P2PARTISAN-IN 2> /dev/null
  235. iptables -N P2PARTISAN-OUT 2> /dev/null
  236. iptables -N P2PARTISAN-DROP 2> /dev/null
  237. iptables -F P2PARTISAN-IN 2> /dev/null
  238. iptables -F P2PARTISAN-OUT 2> /dev/null
  239. iptables -F P2PARTISAN-DROP 2> /dev/null
  240. iptables -A P2PARTISAN-IN -p tcp --match multiport --sports $whiteports -j ACCEPT 2> /dev/null
  241. iptables -A P2PARTISAN-IN -p udp --match multiport --sports $whiteports -j ACCEPT 2> /dev/null
  242. iptables -A P2PARTISAN-IN -p tcp --match multiport --dports $whiteports -j ACCEPT 2> /dev/null
  243. iptables -A P2PARTISAN-IN -p udp --match multiport --dports $whiteports -j ACCEPT 2> /dev/null
  244. iptables -A P2PARTISAN-OUT -p tcp --match multiport --sports $whiteports -j ACCEPT 2> /dev/null
  245. iptables -A P2PARTISAN-OUT -p udp --match multiport --sports $whiteports -j ACCEPT 2> /dev/null
  246. iptables -A P2PARTISAN-OUT -p tcp --match multiport --dports $whiteports -j ACCEPT 2> /dev/null
  247. iptables -A P2PARTISAN-OUT -p udp --match multiport --dports $whiteports -j ACCEPT 2> /dev/null" >> iptables-add
  248.  
  249.  
  250.         echo "# $now
  251. iptables -D wanin -i $wanif -m state --state NEW -j P2PARTISAN-IN 2> /dev/null
  252. iptables -D wanout -o $wanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null
  253. iptables -D INPUT -i $wanif -m state --state NEW -j P2PARTISAN-IN 2> /dev/null
  254. iptables -D OUTPUT -o $wanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null
  255. iptables -F P2PARTISAN-DROP 2> /dev/null
  256. iptables -F P2PARTISAN-IN 2> /dev/null
  257. iptables -F P2PARTISAN-OUT 2> /dev/null
  258. iptables -X P2PARTISAN-IN 2> /dev/null
  259. iptables -X P2PARTISAN-OUT 2> /dev/null
  260. iptables -X P2PARTISAN-DROP 2> /dev/null" >> iptables-del
  261.  
  262.  
  263. echo "### WHITELIST ###"
  264. echo "loading the whitelist"
  265. #Load the whitelist
  266. if [ "$(ipset --swap whitelist whitelist 2>&1 | grep 'Unknown set')" != "" ]
  267.     then
  268.     ipset --create whitelist iptreemap
  269.     cat whitelist | grep -v "^10." | grep -v "^172.16." | grep -v "^192.168." |
  270.     (
  271.     while read IP
  272.     do
  273.             echo "$IP" | grep "^#" >/dev/null 2>&1 && continue
  274.             echo "$IP" | grep "^$" >/dev/null 2>&1 && continue
  275.                     ipset -A whitelist $IP
  276.             done
  277.     )
  278. fi
  279.         echo "# $now
  280. ipset -F
  281. ipset -X whitelist" > ipset-del
  282.  
  283.             echo "Preparing the whitelist for the iptables"
  284.             echo "iptables -A P2PARTISAN-IN -m set --set whitelist src -j ACCEPT 2> /dev/null
  285. iptables -A P2PARTISAN-OUT -m set --set whitelist dst -j ACCEPT 2> /dev/null" >> iptables-add
  286.  
  287.         if [ $syslogs -eq "1" ]; then        
  288.             echo "iptables -A P2PARTISAN-DROP -m limit --limit $maxloghour/hour --limit-burst 1 -j LOG --log-prefix \"P2Partisan Dropped: \" --log-level 1 2> /dev/null" >> iptables-add
  289.         fi
  290.         echo "iptables -A P2PARTISAN-DROP -j DROP 2> /dev/null"  >> iptables-add
  291.  
  292.  
  293. echo "### BLACKLISTs ###"
  294. cat blacklists |
  295.    (
  296.     while read line
  297.     do
  298.             echo "$line" | grep "^#" >/dev/null 2>&1 && continue
  299.             echo "$line" | grep "^$" >/dev/null 2>&1 && continue
  300.             counter=`expr $counter + 1`
  301.             name=`echo $line |cut -d ' ' -f1`
  302.             url=`echo $line |cut -d ' ' -f2`
  303.             echo "loading blacklist #$counter --> ***$name***"
  304.      
  305.     if [ $fastroutine -eq "1" ]; then
  306.      
  307.     if [ "$(ipset --swap $name $name 2>&1 | grep 'Unknown set')" != "" ]
  308.       then
  309.       [ -e $name.gz ] || wget -q -O $name.gz "$url"
  310.       { echo "-N $name iptreemap"
  311.         gunzip -c  $name.gz | \
  312.         sed -e "/^[\t ]*#.*\|^[\t ]*$/d;s/^.*:/-A $name /"
  313.         echo COMMIT
  314.       } | ipset -R
  315.     fi
  316.      
  317.     else
  318.      
  319.         if [ "$(ipset --swap $name $name 2>&1 | grep 'Unknown set')" != "" ]
  320.             then
  321.             ipset --create $name iptreemap
  322.             [ -e $name.lst ] || wget -q -O - "$url" | gunzip | cut -d: -f2 | grep -E "^[-0-9.]+$" > $name.lst
  323.             for IP in $(cat $name.lst)
  324.                     do
  325.                     ipset -A $name $IP
  326.                     done
  327.             fi
  328.              
  329.     fi
  330.  
  331.                 echo "ipset -X $name " >> ipset-del
  332.                 echo "iptables -A P2PARTISAN-IN -m set --set $name src -j P2PARTISAN-DROP 2> /dev/null
  333. iptables -A P2PARTISAN-OUT -m set --set $name dst -j P2PARTISAN-DROP 2> /dev/null" >> iptables-add 
  334.             done
  335.     )
  336.  
  337.  
  338.         if [ $protection -eq "1" ]; then
  339.             echo "iptables -I INPUT $pos -i $wanif -m state --state NEW -j P2PARTISAN-IN 2> /dev/null
  340. iptables -I OUTPUT $pos -o $wanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null" >> iptables-add
  341.         elif [ $protection -eq "2" ]; then
  342.             echo "iptables -I wanin $pos -i $wanif -m state --state NEW -j P2PARTISAN-IN 2> /dev/null
  343. iptables -I wanout $pos -o $wanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null" >> iptables-add
  344.         elif [ $protection -eq "3" ]; then
  345.             echo "iptables -I INPUT $pos -i $wanif -m state --state NEW -j P2PARTISAN-IN 2> /dev/null
  346. iptables -I OUTPUT $pos -o $wanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null
  347. iptables -I wanin $pos -i $wanif -m state --state NEW -j P2PARTISAN-IN 2> /dev/null
  348. iptables -I wanout $pos -o $wanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null" >> iptables-add
  349.         fi
  350.  
  351. chmod 777 ./iptables-*
  352. chmod 777 ./ipset-*
  353. ./iptables-add  #protecting
  354.  
  355. plog "... P2Partisan started."
  356.  
  357. p=`nvram get dnsmasq_custom | grep log-async | wc -l`
  358. if [ $p -eq "1" ]; then
  359.     plog "log-async found under dnsmasq -> OK"
  360. else
  361.     plog "
  362. It appears like you don't have a log-async parameter
  363. in your dnsmasq config. This is strongly suggested
  364. due to the amount of logs involved. please consider
  365. adding the following command under Advanced/DHCP/DNS
  366. /Dnsmasq Custom configuration
  367.  
  368. log-async=10
  369. "
  370. fi
  371.  
  372. punblock  #remove new connection DROPs if any
  373.  
  374. else
  375.         echo "
  376.     It appears like P2Partisan is already running. Skipping...
  377.            
  378.     If this is not what you expected? Try:
  379.     p2partisan.sh update
  380.         "
  381.     fi
  382. }
  383.  
  384.  
  385. for p in $1
  386. do
  387. case "$p" in
  388.         "start")
  389.                 pstart
  390.                 exit
  391.                 ;;     
  392.         "stop")
  393.                 pforcestop
  394.                 exit
  395.                 ;;
  396.         "restart")
  397.                 pscheduleunset
  398.                 psoftstop
  399.                 pscheduleset
  400.                 ;;
  401.         "status")
  402.                 pstatus
  403.                 exit               
  404.                 ;;     
  405.         "update")
  406.                 pscheduleunset
  407.                 pforcestop
  408.                 pscheduleset
  409.                 ;;
  410.         "paranoia-update")
  411.                 pscheduleunset
  412.                 pblock
  413.                 pforcestop
  414.                 pscheduleset
  415.                 ;;
  416.         "autorun-on")
  417.                 pautorunset
  418.                 exit
  419.                 ;;
  420.         "autorun-off")
  421.                 pautorununset
  422.                 exit
  423.                 ;;
  424.         "autoupdate-on")
  425.                 pscheduleset
  426.                 exit
  427.                 ;;
  428.         "autoupdate-off")
  429.                 pscheduleunset
  430.                 exit
  431.                 ;;
  432.         "help")
  433.                 echo
  434. P2Partisan parameters:
  435.                
  436.     help        Display this text      
  437.     start       Starts the process (this runs also if no option
  438.             is provided)
  439.     stop        Stops P2Partisan
  440.     restart     Soft restart, quick, updates iptables only
  441.     update      Hard restart, slow removes p2partisan, updates
  442.             the lists and does a fresh start
  443.     paranoia-update Like update but block new connections until
  444.             P2Partisan is running again
  445.     status      Display P2Partisan running status + extra info
  446.     autorun-on  Sets P2Partisan to boot with the router
  447.     autorun-off Sets P2Partisan not to boot with the router
  448.     autoupdate-on   Sets automatic updates on
  449.     autoupdate-off  Sets automatic updates off
  450.                 "
  451.                 exit
  452.                 ;;
  453.         *)
  454.                 echo "parameter not valid. please run:
  455.                
  456.     p2partisan.sh help
  457.     "
  458.                 exit
  459.             ;;
  460.  
  461. esac
  462. done
  463.  
  464. pstart
  465.  
  466.  
  467. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement