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.

165 lines
4.6 KiB

  1. ###############################
  2. # SIMPLE FIREWALL RULES FOR IPTABLES
  3. #
  4. # By Pekka Helenius (~Fincer), 2018
  5. #
  6. # These rules are intended to be used
  7. # without UFW. If you have additional
  8. # firewall settings in your system/iptables,
  9. # take care adapting these rules in to your
  10. # current firewall ruleset.
  11. #
  12. # I do not take responsibility of breaking
  13. # your working firewall configuration!
  14. #
  15. ############
  16. #
  17. # The rules in this file do the following:
  18. #
  19. # A) do not respond to incoming ping requests
  20. # Can be used as a replacement for sysctl 'net.ipv4.icmp_echo_ignore_all=1' setting
  21. #
  22. # B) drop all incoming traffic by default, except for
  23. # SSH, HTTP and HTTPS protocols
  24. #
  25. # C) start dropping packets if connection cycle is too intense
  26. # from one client. This setting may be useful against port scanners.
  27. #
  28. # Ruleset C) by Anthony Maro:
  29. # https://www.ossramblings.com/using_iptables_rate_limiting_to_prevent_portscans
  30. #
  31. ############
  32. #
  33. # INSTALLATION
  34. #
  35. # NOTE: Intended to be used without UFW or any other
  36. # firewall settings!!
  37. #
  38. # 1) Recommended: Remove existing firewall front-ends such as UFW from your system
  39. #
  40. # 2) Delete all previous firewall rules by issuing
  41. # sudo iptables --flush && sudo iptables --delete-chain
  42. #
  43. # 3) Check output of 'iptables -S'. It should be
  44. # -P INPUT ACCEPT
  45. # -P FORWARD ACCEPT
  46. # -P OUTPUT ACCEPT
  47. #
  48. # 4) In this file, change SSH, HTTP and HTTPS port numbers to fit your server environment
  49. #
  50. # Default values are:
  51. #
  52. # SSH: 22
  53. # HTTP: 80
  54. # HTTPS: 443
  55. #
  56. # Default setting for C) is 10 maximum connection attempts in 30 seconds
  57. # Adapt the values to your server environment.
  58. #
  59. # 5) Save this file to /etc/iptables/iptables.rules
  60. #
  61. # 6) Check that it is used by 'iptables-restore' command
  62. #
  63. # In systemd environments, check the value of 'ExecStart' and 'ExecReload'
  64. # in file /lib/systemd/system/iptables.service. The entries should be as follows:
  65. #
  66. # ExecStart=/usr/bin/iptables-restore /etc/iptables/iptables.rules
  67. # ExecReload=/usr/bin/iptables-restore /etc/iptables/iptables.rules
  68. #
  69. # 7) Once you have double-checked that the parameters in this file are correct (step 4), run
  70. # sudo iptables-restore /etc/iptables/iptables.rules
  71. # sudo systemctl enable iptables && sudo systemctl start iptables
  72. #
  73. # 8) Check that the rules have been applied:
  74. # sudo iptables -S
  75. #
  76. #
  77. ###############################
  78. # USEFUL LINKS
  79. #
  80. # https://www.thegeekstuff.com/scripts/iptables-rules
  81. # https://gist.github.com/thomasfr/9712418
  82. #
  83. ###############################
  84. #
  85. # BEGINNING OF FIREWALL RULES
  86. #
  87. *filter
  88. ###############################
  89. # DEFAULT POLICY FOR THIS CHAIN - DROP ALL INPUT TRAFFIC
  90. #
  91. # THIS IS A DANGEROUS SETTING. IF YOU DROP ALL INCOMING
  92. # CONNECTIONS, MAKE SURE YOU HAVE ACCEPTED AT LEAST SSH CONNECTION INPUT BELOW
  93. # OTHERWISE YOU WILL BE LOCKED OUT FROM THE SERVER!
  94. #
  95. # DO NOT USE 'REJECT' BECAUSE IT GIVES A RESPONSE TO A HOSTILE CLIENTS (PORT SCANNERS)
  96. # INSTEAD, DROP INCOMING PACKETS AND DO NOT GIVE RESPONSE AT ALL
  97. #
  98. -P INPUT DROP
  99. ###############################
  100. # WE ARE NOT A ROUTER, WE DROP ALL (NON-EXISTENT) FORWARD CONNECTIONS
  101. #
  102. -P FORWARD DROP
  103. ###############################
  104. # BY DEFAULT, ALL OUTGOING TRAFFIC FROM THE SERVER IS ACCEPTED
  105. #
  106. -P OUTPUT ACCEPT
  107. ###############################
  108. # DROP ALL INCOMING PING REQUESTS
  109. #
  110. -A INPUT -p icmp --icmp-type echo-request -j DROP
  111. ###############################
  112. # ALLOW LOOPBACK CONNECTIONS
  113. #
  114. -A INPUT -i lo -j ACCEPT
  115. #-A OUTPUT -o lo -j ACCEPT
  116. ###############################
  117. # ALLOW INCOMING SSH CONNECTIONS
  118. #
  119. -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT
  120. #-A OUTPUT -p tcp --sport 22 -m state --state NEW -j ACCEPT
  121. ###############################
  122. # ALLOW INCOMING HTTP/HTTPS CONNECTIONS
  123. #
  124. -A INPUT -p tcp -m multiport --dports 80,443 -m state --state NEW -j ACCEPT
  125. #-A OUTPUT -p tcp -m multiport --sports 80,443 -m state --state NEW -j ACCEPT
  126. ###############################
  127. # ALLOW ESTABLISHED AND RELATED CONNECTIONS
  128. #
  129. -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
  130. #-A OUTPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
  131. ###############################
  132. # RULESET C)
  133. #
  134. # IF 10 NEW CONNECTIONS TO ANY PORT WITHIN 30 SECONDS IS REQUESTED BY A CLIENT, START DROPPING PACKETS FOR THE CLIENT
  135. #
  136. # SHOULD FREEZE NMAP AND OTHER PORT SCANNERS
  137. #
  138. # Source: https://www.ossramblings.com/using_iptables_rate_limiting_to_prevent_portscans
  139. -A INPUT -i $IFACE -p tcp -m state --state NEW -m recent --set --name DEFAULT --mask 255.255.255.255 --rsource
  140. -A INPUT -i $IFACE -p tcp -m state --state NEW -m recent --update --seconds 30 --hitcount 10 --name DEFAULT --mask 255.255.255.255 --rsource -j DROP
  141. ###############################
  142. COMMIT
  143. # END OF FIREWALL RULES