Instructions to set up a basic LAMP+SSH server environment
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
5.0 KiB

  1. ###############################
  2. # SIMPLE FIREWALL RULES FOR IPTABLES
  3. #
  4. #
  5. # These rules are intended to be used
  6. # without other firewalls such as UFW.
  7. # If you have additional firewall settings
  8. # in your system/iptables, take care adapting
  9. # these rules in to your current firewall ruleset.
  10. #
  11. # It is highly recommended to remove all conflicting
  12. # firewall configuration
  13. #
  14. # I do not take responsibility of breaking
  15. # your working firewall configuration!
  16. #
  17. ############
  18. #
  19. # The rules in this file do the following:
  20. #
  21. # A) do not respond to incoming ping requests
  22. # Can be used as a replacement for sysctl 'net.ipv4.icmp_echo_ignore_all=1' setting
  23. #
  24. # B) Reject connection if connection cycle is too intense
  25. # from one client. This setting may be useful against all kind of intense brute force
  26. # attacks.
  27. #
  28. # C) drop all incoming traffic by default, except for
  29. # SSH, HTTP and HTTPS protocols
  30. #
  31. #
  32. ############
  33. #
  34. # INSTALLATION
  35. #
  36. # NOTE: Intended to be used without UFW or any other
  37. # firewall settings!!
  38. #
  39. # 1) Recommended: Remove existing firewall front-ends such as UFW from your system
  40. #
  41. # 2) Delete all previous firewall rules by issuing
  42. # sudo iptables --flush && sudo iptables --delete-chain
  43. #
  44. # 3) Check output of 'iptables -S'. It should be
  45. # -P INPUT ACCEPT
  46. # -P FORWARD ACCEPT
  47. # -P OUTPUT ACCEPT
  48. #
  49. # 4) In this file, change SSH, HTTP and HTTPS port numbers to fit your server environment
  50. #
  51. # Default values are:
  52. #
  53. # SSH: 22
  54. # HTTP: 80
  55. # HTTPS: 443
  56. #
  57. # Default setting for bruteforce prevention is 10 maximum connection attempts in 30 seconds
  58. # Adapt the values to your server environment.
  59. #
  60. # 5) Save this file to /etc/iptables/iptables.rules
  61. #
  62. # 6) Check that it is used by 'iptables-restore' command
  63. #
  64. # In systemd environments, check the value of 'ExecStart' and 'ExecReload'
  65. # in file /lib/systemd/system/iptables.service. The entries should be as follows:
  66. #
  67. # ExecStart=/usr/bin/iptables-restore /etc/iptables/iptables.rules
  68. # ExecReload=/usr/bin/iptables-restore /etc/iptables/iptables.rules
  69. #
  70. # 7) Once you have double-checked that the parameters in this file are correct (step 4), run
  71. # sudo iptables-restore /etc/iptables/iptables.rules
  72. # sudo systemctl enable iptables && sudo systemctl start iptables
  73. #
  74. # 8) Check that the rules have been applied:
  75. # sudo iptables -S
  76. #
  77. #
  78. ###############################
  79. # USEFUL LINKS
  80. #
  81. # https://www.thegeekstuff.com/scripts/iptables-rules
  82. # https://gist.github.com/thomasfr/9712418
  83. # http://blog.sevagas.com/?Iptables-firewall-versus-nmap-and,31
  84. #
  85. ###############################
  86. #
  87. # BEGINNING OF FIREWALL RULES
  88. #
  89. *filter
  90. ###############################
  91. # Default policy for this chain - drop all input traffic
  92. # This is a dangerous setting. If you drop all incoming connections,
  93. # make sure you have accepted at least incoming SSH connection below.
  94. # Otherwise you will be locked out from the server!
  95. #
  96. # Do not use 'REJECT' because it gives a response to hostile clients such
  97. # as bruteforcers and port scanners. Instead, drop incoming packets
  98. # and do not give reponse at all.
  99. #
  100. -P INPUT DROP
  101. ###############################
  102. # We are not a router, we drop all (non-existent) forward connections
  103. #
  104. -P FORWARD DROP
  105. ###############################
  106. # By default, all outgoing traffic from the server is accepted
  107. #
  108. -P OUTPUT ACCEPT
  109. ###############################
  110. # Drop all incoming ping requests
  111. #
  112. -A INPUT -p icmp --icmp-type echo-request -j DROP
  113. ###############################
  114. # Allow loopback connections
  115. #
  116. -A INPUT -i lo -j ACCEPT
  117. #-A OUTPUT -o lo -j ACCEPT
  118. ###############################
  119. # Block bruteforce attacks
  120. # Works against dirbuster, nmap and similar tools.
  121. #
  122. #
  123. # Default values are allowing max 10 connections from a client within 30 seconds
  124. # Please adjust these values for your server environment
  125. #
  126. # Based on: https://rudd-o.com/linux-and-free-software/a-better-way-to-block-brute-force-attacks-on-your-ssh-server
  127. # If you need to enable this for specific TCP ports, add the following parameter:
  128. # -m multiport --dports 80
  129. -A INPUT -p tcp -m tcp -m state --state NEW -m recent --set --name BRUTEFORCE --rsource
  130. #-A INPUT -p tcp -m tcp -m multiport --dports 80 -m recent --rcheck --seconds 30 --hitcount 10 --rttl --name BRUTEFORCE --rsource -j LOG --log-prefix "Brute force attack detected "
  131. -A INPUT -p tcp -m tcp -m recent --rcheck --seconds 30 --hitcount 10 --rttl --name BRUTEFORCE --rsource -j REJECT --reject-with tcp-reset
  132. ###############################
  133. # Allow incoming SSH connections
  134. #
  135. -A INPUT -p tcp --dport 765 -m state --state NEW -j ACCEPT
  136. #-A OUTPUT -p tcp --sport 765 -m state --state NEW -j ACCEPT
  137. ###############################
  138. # Allow incoming HTTP/HTTPS connections
  139. #
  140. -A INPUT -p tcp -m multiport --dports 80,443 -m state --state NEW -j ACCEPT
  141. #-A OUTPUT -p tcp -m multiport --sports 80,443 -m state --state NEW -j ACCEPT
  142. ###############################
  143. # Allow established and related connections
  144. #
  145. -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
  146. #-A OUTPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
  147. ###############################
  148. COMMIT
  149. # END OF FIREWALL RULES