iptables-vpn.sh 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/bin/sh
  2. DEF_IF=$(route | grep '^default' | grep -o '[^ ]*$')
  3. iptables -P INPUT ACCEPT
  4. iptables -P OUTPUT ACCEPT
  5. iptables -P FORWARD ACCEPT
  6. iptables -X
  7. iptables -X -t nat
  8. iptables -F
  9. iptables -F -t nat
  10. ##############################
  11. ### ATTACKS
  12. ##############################
  13. # All TCP sessions should begin with SYN
  14. iptables -A INPUT -p tcp ! --syn -m state --state NEW -s 0.0.0.0/0 -j DROP
  15. # Limit the number of incoming tcp connections
  16. # incoming syn-flood protection
  17. iptables -N syn_flood
  18. iptables -A INPUT -p tcp --syn -j syn_flood
  19. iptables -A syn_flood -m limit --limit 1/s --limit-burst 3 -j RETURN
  20. iptables -A syn_flood -j DROP
  21. # fragmented ICMP - sign of DoS attack
  22. iptables -A INPUT --fragment -p ICMP -j DROP
  23. #Limiting the incoming icmp ping request:
  24. iptables -A INPUT -p icmp -m limit --limit 1/s --limit-burst 1 -j ACCEPT
  25. iptables -A INPUT -p icmp -j DROP
  26. iptables -A OUTPUT -p icmp -j ACCEPT
  27. #Force Fragments packets check
  28. iptables -A INPUT -f -j DROP
  29. #Incoming malformed XMAS packets
  30. iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
  31. # Drop all NULL packets
  32. iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
  33. # invalid and suspicious packets
  34. iptables -A INPUT -m state --state INVALID -j DROP
  35. # Stealth scan 1
  36. iptables -A INPUT -p tcp --tcp-flags ALL NONE -j LOG --log-prefix "FWLOG: Stealth scan (1): "
  37. iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
  38. # Stealth scan 2
  39. iptables -A INPUT -p tcp --tcp-flags ALL ALL -j LOG --log-prefix "FWLOG: Stealth scan (2): "
  40. iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
  41. # Stealth scan 3
  42. iptables -A INPUT -p tcp --tcp-flags ALL FIN,URG,PSH -j LOG --log-prefix "FWLOG: Stealth scan (3): "
  43. iptables -A INPUT -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP
  44. # Stealth scan 4
  45. iptables -A INPUT -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j LOG --log-prefix "FWLOG: Stealth scan (4): "
  46. iptables -A INPUT -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP
  47. # Stealth scan 5
  48. iptables -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j LOG --log-prefix "FWLOG: Stealth scan (5): "
  49. iptables -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
  50. # Stealth scan 6
  51. iptables -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -j LOG --log-prefix "FWLOG: Stealth scan (6): "
  52. iptables -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
  53. # Port scan
  54. iptables -N port-scan
  55. iptables -A port-scan -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j RETURN
  56. iptables -A port-scan -j DROP
  57. iptables -A INPUT -i $DEF_IF -m state --state NEW,ESTABLISHED,RELATED -p udp -m multiport --dports 22,80,443,995,5555,400,500,1194:1196 -j ACCEPT
  58. iptables -A INPUT -i $DEF_IF -m state --state NEW,ESTABLISHED,RELATED -p tcp -m multiport --dports 22,80,443,995,5555,400,500,1194:1196 -j ACCEPT
  59. iptables -A OUTPUT -o $DEF_IF -p tcp -m multiport --sports 22,80,443,995,5555,400,500,1194:1196 -m state --state RELATED,ESTABLISHED -j ACCEPT
  60. iptables -A OUTPUT -o $DEF_IF -p udp -m multiport --sports 22,80,443,995,5555,400,500,1194:1196 -m state --state RELATED,ESTABLISHED -j ACCEPT
  61. iptables -A INPUT -i $DEF_IF -m state --state NEW,ESTABLISHED,RELATED -p udp -m multiport --dports 993,8080,3128 -j ACCEPT
  62. iptables -A INPUT -i $DEF_IF -m state --state NEW,ESTABLISHED,RELATED -p tcp -m multiport --dports 993,8080,3128 -j ACCEPT
  63. iptables -A OUTPUT -o $DEF_IF -p tcp -m multiport --sports 993,8080,3128 -m state --state RELATED,ESTABLISHED -j ACCEPT
  64. iptables -A OUTPUT -o $DEF_IF -p udp -m multiport --sports 993,8080,3128 -m state --state RELATED,ESTABLISHED -j ACCEPT
  65. iptables -A OUTPUT -p udp -o $DEF_IF -j ACCEPT
  66. iptables -A INPUT -p udp -i $DEF_IF -j ACCEPT
  67. #minecraft
  68. iptables -A INPUT -i $DEF_IF -m state --state NEW,ESTABLISHED,RELATED -p tcp -m multiport --dports 25655:25680 -j ACCEPT
  69. iptables -A OUTPUT -o $DEF_IF -p tcp -m multiport --sports 25655:25680 -m state --state RELATED,ESTABLISHED -j ACCEPT
  70. #allow tun+
  71. iptables -A INPUT -i tun+ -j ACCEPT
  72. iptables -A OUTPUT -o tun+ -j ACCEPT
  73. iptables -A FORWARD -i tun+ -j ACCEPT
  74. iptables -A FORWARD -i tun+ -o $DEF_IF -m state --state RELATED,ESTABLISHED -j ACCEPT
  75. iptables -A FORWARD -i $DEF_IF -o tun+ -m state --state RELATED,ESTABLISHED -j ACCEPT
  76. iptables -A FORWARD -i tun+ -o ens0 -m state --state RELATED,ESTABLISHED -j ACCEPT
  77. iptables -A FORWARD -i ens0 -o tun+ -m state --state RELATED,ESTABLISHED -j ACCEPT
  78. #iptables -t nat -A PREROUTING -i tap_soft -p udp --dport 53 -j DNAT --to-destination 192.168.199.1:53
  79. #iptables -t nat -A PREROUTING -i tap_soft -p udp --dport 5353 -j DNAT --to-destination 192.168.199.1:53
  80. #iptables -t nat -A PREROUTING -i tap_soft -p tcp --dport 5353 -j DNAT --to-destination 192.168.199.1:53
  81. #iptables -t nat -A PREROUTING -i tap_soft -p tcp --dport 53 -j DNAT --to-destination 192.168.199.1:53
  82. iptables -A INPUT -i tap_soft -p udp --dport 53 -d 208.67.222.123 -j ACCEPT
  83. iptables -A INPUT -i tap_soft -p udp --dport 53 -d 208.67.220.123 -j ACCEPT
  84. iptables -A INPUT -i tap_soft -p udp --dport 53 -d 81.218.119.11 -j ACCEPT
  85. iptables -A INPUT -i tap_soft -p udp --dport 53 -d 209.88.198.133 -j ACCEPT
  86. iptables -A INPUT -i tap_soft -p udp --dport 53 -d 199.85.126.20 -j ACCEPT
  87. iptables -A INPUT -i tap_soft -p udp --dport 53 -d 199.85.127.20 -j ACCEPT
  88. iptables -A INPUT -i tap_soft -p udp --dport 53 -d 5.5.0.1 -j ACCEPT
  89. iptables -A INPUT -i tap_soft -p udp --dport 53 -d 8.8.8.8 -j ACCEPT
  90. iptables -A INPUT -i tap_soft -p udp --dport 53 -d 8.8.4.4 -j ACCEPT
  91. iptables -A INPUT -i tap_soft -p udp --dport 53 -d 208.67.220.220 -j ACCEPT
  92. iptables -A INPUT -i tap_soft -p udp --dport 53 -d 208.67.222.222 -j ACCEPT
  93. iptables -A INPUT -i tap_soft -p udp --dport 53 -j ACCEPT
  94. iptables -A INPUT -i tap_soft -p udp --dport 5353 -j ACCEPT
  95. #allow ssh,www,https, letsencrypt
  96. iptables -A OUTPUT -p tcp -m multiport --dports 22,80,443,54321 -m state --state NEW,ESTABLISHED -j ACCEPT
  97. iptables -A INPUT -p tcp -m multiport --sports 22,80,443,54321 -m state --state ESTABLISHED -j ACCEPT
  98. iptables -A INPUT -p tcp -m multiport --dports 22,80,443,54321 -m state --state NEW,ESTABLISHED -j ACCEPT
  99. iptables -A OUTPUT -p tcp -m multiport --sports 22,80,443,54321 -m state --state ESTABLISHED -j ACCEPT
  100. iptables -A OUTPUT -p tcp -m multiport --dports 995,3128,992,5555,8080 -m state --state NEW,ESTABLISHED -j ACCEPT
  101. iptables -A INPUT -p tcp -m multiport --sports 995,3128,992,5555,8080 -m state --state ESTABLISHED -j ACCEPT
  102. iptables -A INPUT -p tcp -m multiport --dports 995,3128,992,5555,8080 -m state --state NEW,ESTABLISHED -j ACCEPT
  103. iptables -A OUTPUT -p tcp -m multiport --sport 995,3128,992,5555,8080 -m state --state ESTABLISHED -j ACCEPT
  104. iptables -A OUTPUT -p tcp -m state --state NEW,ESTABLISHED -j ACCEPT
  105. iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
  106. #rsync
  107. iptables -A INPUT -p tcp --dport 873 -m state --state NEW,ESTABLISHED -j ACCEPT
  108. iptables -A OUTPUT -p tcp --sport 873 -m state --state ESTABLISHED -j ACCEPT
  109. iptables -A INPUT -p tcp --dport 51413 -m state --state NEW,ESTABLISHED -j ACCEPT
  110. iptables -A OUTPUT -p tcp --sport 51413 -m state --state ESTABLISHED -j ACCEPT
  111. #mysql
  112. iptables -A INPUT -p tcp --dport 3306 -m state --state NEW,ESTABLISHED -j ACCEPT
  113. iptables -A OUTPUT -p tcp --sport 3306 -m state --state ESTABLISHED -j ACCEPT
  114. iptables -A INPUT -i tap_soft -j ACCEPT
  115. iptables -A OUTPUT -o tap_soft -j ACCEPT
  116. iptables -A FORWARD -i tap_soft -j ACCEPT
  117. iptables -A FORWARD -i tap_soft -o $DEF_IF -m state --state RELATED,ESTABLISHED -j ACCEPT
  118. iptables -A FORWARD -i $DEF_IF -o tap_soft -m state --state RELATED,ESTABLISHED -j ACCEPT
  119. iptables -A INPUT -i lo -j ACCEPT
  120. iptables -A OUTPUT -o lo -j ACCEPT
  121. iptables -A OUTPUT -p udp -m multiport --dports 53,67,68 -j ACCEPT
  122. iptables -A INPUT -p udp -m multiport --sports 53,67,68 -j ACCEPT
  123. iptables -A OUTPUT -p tcp -m multiport --dports 53,67,68 -j ACCEPT
  124. iptables -A INPUT -p tcp -m multiport --sports 53,67,68 -j ACCEPT
  125. iptables -A OUTPUT -p udp -m multiport --dports 60000:61000 -j ACCEPT
  126. iptables -A INPUT -p udp -m multiport --sports 60000:61000 -j ACCEPT
  127. iptables -A OUTPUT -p tcp -m multiport --dports 60000:61000 -j ACCEPT
  128. iptables -A INPUT -p tcp -m multiport --sports 60000:61000 -j ACCEPT
  129. #nat
  130. iptables -t nat -A POSTROUTING -s 10.0.0.0/8 -j MASQUERADE
  131. iptables -t nat -A POSTROUTING -s 172.16.0.0/12 -j MASQUERADE
  132. iptables -t nat -A POSTROUTING -s 192.168.0.0/16 -j MASQUERADE
  133. #ping
  134. iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
  135. iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT
  136. iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT
  137. iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT
  138. iptables -P INPUT DROP
  139. iptables -P FORWARD DROP
  140. iptables -P OUTPUT DROP
  141. iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
  142. #save rules
  143. iptables-save > /etc/iptables/rules.v4
  144. echo 1 > /proc/sys/net/ipv4/ip_forward
  145. sudo sed -i 's/#net.ipv4.ip_forward/net.ipv4.ip_forward/g' /etc/sysctl.conf
  146. sudo sed -i 's/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/g' /etc/sysctl.conf
  147. sudo sysctl -p /etc/sysctl.conf