Advertisement
dev247

iptables-rules-ddos

Aug 12th, 2016
1,573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. # IPTables DDOS protection (TCP)
  2.  
  3. ### anti-ddos kernel settings (sysctl.conf)
  4. kernel.printk = 4 4 1 7
  5. kernel.panic = 10
  6. kernel.sysrq = 0
  7. kernel.shmmax = 4294967296
  8. kernel.shmall = 4194304
  9. kernel.core_uses_pid = 1
  10. kernel.msgmnb = 65536
  11. kernel.msgmax = 65536
  12. vm.swappiness = 20
  13. vm.dirty_ratio = 80
  14. vm.dirty_background_ratio = 5
  15. fs.file-max = 2097152
  16. net.core.netdev_max_backlog = 262144
  17. net.core.rmem_default = 31457280
  18. net.core.rmem_max = 67108864
  19. net.core.wmem_default = 31457280
  20. net.core.wmem_max = 67108864
  21. net.core.somaxconn = 65535
  22. net.core.optmem_max = 25165824
  23. net.ipv4.neigh.default.gc_thresh1 = 4096
  24. net.ipv4.neigh.default.gc_thresh2 = 8192
  25. net.ipv4.neigh.default.gc_thresh3 = 16384
  26. net.ipv4.neigh.default.gc_interval = 5
  27. net.ipv4.neigh.default.gc_stale_time = 120
  28. net.netfilter.nf_conntrack_max = 10000000
  29. net.netfilter.nf_conntrack_tcp_loose = 0
  30. net.netfilter.nf_conntrack_tcp_timeout_established = 1800
  31. net.netfilter.nf_conntrack_tcp_timeout_close = 10
  32. net.netfilter.nf_conntrack_tcp_timeout_close_wait = 10
  33. net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 20
  34. net.netfilter.nf_conntrack_tcp_timeout_last_ack = 20
  35. net.netfilter.nf_conntrack_tcp_timeout_syn_recv = 20
  36. net.netfilter.nf_conntrack_tcp_timeout_syn_sent = 20
  37. net.netfilter.nf_conntrack_tcp_timeout_time_wait = 10
  38. net.ipv4.tcp_slow_start_after_idle = 0
  39. net.ipv4.ip_local_port_range = 1024 65000
  40. net.ipv4.ip_no_pmtu_disc = 1
  41. net.ipv4.route.flush = 1
  42. net.ipv4.route.max_size = 8048576
  43. net.ipv4.icmp_echo_ignore_broadcasts = 1
  44. net.ipv4.icmp_ignore_bogus_error_responses = 1
  45. net.ipv4.tcp_congestion_control = htcp
  46. net.ipv4.tcp_mem = 65536 131072 262144
  47. net.ipv4.udp_mem = 65536 131072 262144
  48. net.ipv4.tcp_rmem = 4096 87380 33554432
  49. net.ipv4.udp_rmem_min = 16384
  50. net.ipv4.tcp_wmem = 4096 87380 33554432
  51. net.ipv4.udp_wmem_min = 16384
  52. net.ipv4.tcp_max_tw_buckets = 1440000
  53. net.ipv4.tcp_tw_recycle = 0
  54. net.ipv4.tcp_tw_reuse = 1
  55. net.ipv4.tcp_max_orphans = 400000
  56. net.ipv4.tcp_window_scaling = 1
  57. net.ipv4.tcp_rfc1337 = 1
  58. net.ipv4.tcp_syncookies = 1
  59. net.ipv4.tcp_synack_retries = 1
  60. net.ipv4.tcp_syn_retries = 2
  61. net.ipv4.tcp_max_syn_backlog = 16384
  62. net.ipv4.tcp_timestamps = 1
  63. net.ipv4.tcp_sack = 1
  64. net.ipv4.tcp_fack = 1
  65. net.ipv4.tcp_ecn = 2
  66. net.ipv4.tcp_fin_timeout = 10
  67. net.ipv4.tcp_keepalive_time = 600
  68. net.ipv4.tcp_keepalive_intvl = 60
  69. net.ipv4.tcp_keepalive_probes = 10
  70. net.ipv4.tcp_no_metrics_save = 1
  71. net.ipv4.ip_forward = 0
  72. net.ipv4.conf.all.accept_redirects = 0
  73. net.ipv4.conf.all.send_redirects = 0
  74. net.ipv4.conf.all.accept_source_route = 0
  75. net.ipv4.conf.all.rp_filter = 1
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. ### 1: Drop invalid packets ###
  86. /sbin/iptables -t mangle -A PREROUTING -m conntrack --ctstate INVALID -j DROP
  87.  
  88. ### 2: Drop TCP packets that are new and are not SYN ###
  89. /sbin/iptables -t mangle -A PREROUTING -p tcp ! --syn -m conntrack --ctstate NEW -j DROP
  90.  
  91. ### 3: Drop SYN packets with suspicious MSS value ###
  92. /sbin/iptables -t mangle -A PREROUTING -p tcp -m conntrack --ctstate NEW -m tcpmss ! --mss 536:65535 -j DROP
  93.  
  94. ### 4: Block packets with bogus TCP flags ###
  95. /sbin/iptables -t mangle -A PREROUTING -p tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -j DROP
  96. /sbin/iptables -t mangle -A PREROUTING -p tcp --tcp-flags FIN,SYN FIN,SYN -j DROP
  97. /sbin/iptables -t mangle -A PREROUTING -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
  98. /sbin/iptables -t mangle -A PREROUTING -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
  99. /sbin/iptables -t mangle -A PREROUTING -p tcp --tcp-flags FIN,RST FIN,RST -j DROP
  100. /sbin/iptables -t mangle -A PREROUTING -p tcp --tcp-flags FIN,ACK FIN -j DROP
  101. /sbin/iptables -t mangle -A PREROUTING -p tcp --tcp-flags ACK,URG URG -j DROP
  102. /sbin/iptables -t mangle -A PREROUTING -p tcp --tcp-flags ACK,FIN FIN -j DROP
  103. /sbin/iptables -t mangle -A PREROUTING -p tcp --tcp-flags ACK,PSH PSH -j DROP
  104. /sbin/iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL ALL -j DROP
  105. /sbin/iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL NONE -j DROP
  106. /sbin/iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL FIN,PSH,URG -j DROP
  107. /sbin/iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL SYN,FIN,PSH,URG -j DROP
  108. /sbin/iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP
  109.  
  110. ### 5: Block spoofed packets ###
  111. /sbin/iptables -t mangle -A PREROUTING -s 224.0.0.0/3 -j DROP
  112. /sbin/iptables -t mangle -A PREROUTING -s 169.254.0.0/16 -j DROP
  113. /sbin/iptables -t mangle -A PREROUTING -s 172.16.0.0/12 -j DROP
  114. /sbin/iptables -t mangle -A PREROUTING -s 192.0.2.0/24 -j DROP
  115. /sbin/iptables -t mangle -A PREROUTING -s 192.168.0.0/16 -j DROP
  116. /sbin/iptables -t mangle -A PREROUTING -s 10.0.0.0/8 -j DROP
  117. /sbin/iptables -t mangle -A PREROUTING -s 0.0.0.0/8 -j DROP
  118. /sbin/iptables -t mangle -A PREROUTING -s 240.0.0.0/5 -j DROP
  119. /sbin/iptables -t mangle -A PREROUTING -s 127.0.0.0/8 ! -i lo -j DROP
  120.  
  121. ### 6: Drop ICMP (you usually don't need this protocol) ###
  122. /sbin/iptables -t mangle -A PREROUTING -p icmp -j DROP
  123.  
  124. ### 7: Drop fragments in all chains ###
  125. /sbin/iptables -t mangle -A PREROUTING -f -j DROP
  126.  
  127. ### 8: Limit connections per source IP ###
  128. /sbin/iptables -A INPUT -p tcp -m connlimit --connlimit-above 111 -j REJECT --reject-with tcp-reset
  129.  
  130. ### 9: Limit RST packets ###
  131. /sbin/iptables -A INPUT -p tcp --tcp-flags RST RST -m limit --limit 2/s --limit-burst 2 -j ACCEPT
  132. /sbin/iptables -A INPUT -p tcp --tcp-flags RST RST -j DROP
  133.  
  134. ### 10: Limit new TCP connections per second per source IP ###
  135. /sbin/iptables -A INPUT -p tcp -m conntrack --ctstate NEW -m limit --limit 60/s --limit-burst 20 -j ACCEPT
  136. /sbin/iptables -A INPUT -p tcp -m conntrack --ctstate NEW -j DROP
  137.  
  138. ### SSH brute-force protection ###
  139. /sbin/iptables -A INPUT -p tcp --dport ssh -m conntrack --ctstate NEW -m recent --set
  140. /sbin/iptables -A INPUT -p tcp --dport ssh -m conntrack --ctstate NEW -m recent --update --seconds 60 --hitcount 10 -j DROP
  141.  
  142. ### Protection against port scanning ###
  143. /sbin/iptables -N port-scanning
  144. /sbin/iptables -A port-scanning -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s --limit-burst 2 -j RETURN
  145. /sbin/iptables -A port-scanning -j DROP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement