firewall 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: firewall.sh
  4. # Required-Start: $syslog $network
  5. # Required-Stop: $syslog $network
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: Start firewall daemon at boot time
  9. # Description: Custom Firewall scrip.
  10. ### END INIT INFO
  11. #
  12. # Simple Firewall configuration.
  13. #
  14. # Original author : Nicolargo
  15. #
  16. # chkconfig: 2345 9 91
  17. # description: Activates/Deactivates the firewall at boot time
  18. #
  19. PATH=/bin:/sbin:/usr/bin:/usr/sbin
  20. # Services that the system will offer to the network
  21. TCP_SERVICES="22" # SSH only
  22. UDP_SERVICES=""
  23. # Services the system will use from the network
  24. REMOTE_TCP_SERVICES="80 443" # web browsing
  25. REMOTE_UDP_SERVICES="53" # DNS
  26. # Network that will be used for remote mgmt
  27. # (if undefined, no rules will be setup)
  28. # NETWORK_MGMT=192.168.0.0/24
  29. # Port used for the SSH service, define this is you have setup a
  30. # management network but remove it from TCP_SERVICES
  31. SSH_PORT="22"
  32. CONFIGURATION_FILE=/etc/firewall/firewall.conf
  33. if [ -f $CONFIGURATION_FILE ] ; then
  34. . $CONFIGURATION_FILE
  35. fi
  36. IP_TABLES="/sbin/iptables"
  37. if ! [ -x $IP_TABLES ]; then
  38. exit 0
  39. fi
  40. ##########################
  41. # Start the Firewall rules
  42. ##########################
  43. fw_start () {
  44. #**************************************************************************#
  45. # Input traffic:
  46. #**************************************************************************#
  47. ### Keep existing connections
  48. $IP_TABLES -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  49. # Services
  50. if [ -n "$TCP_SERVICES" ] ; then
  51. for PORT in $TCP_SERVICES; do
  52. $IP_TABLES -A INPUT -p tcp --dport ${PORT} -j ACCEPT
  53. done
  54. fi
  55. if [ -n "$UDP_SERVICES" ] ; then
  56. for PORT in $UDP_SERVICES; do
  57. $IP_TABLES -A INPUT -p udp --dport ${PORT} -j ACCEPT
  58. done
  59. fi
  60. # Remote management
  61. if [ -n "$NETWORK_MGMT" ] ; then
  62. $IP_TABLES -A INPUT -p tcp --src ${NETWORK_MGMT} --dport ${SSH_PORT} -j ACCEPT
  63. else
  64. $IP_TABLES -A INPUT -p tcp --dport ${SSH_PORT} -j ACCEPT
  65. fi
  66. #**************************************************************************#
  67. # NGINX
  68. #**************************************************************************#
  69. $IP_TABLES -A INPUT -i lo -s localhost -d localhost -j ACCEPT
  70. $IP_TABLES -A OUTPUT -o lo -s localhost -d localhost -j ACCEPT
  71. $IP_TABLES -A INPUT -p tcp --dport http -j ACCEPT
  72. $IP_TABLES -A INPUT -p tcp --dport https -j ACCEPT
  73. # Remote testing
  74. ### Allows PING
  75. $IP_TABLES -A INPUT -p icmp -j ACCEPT
  76. ### Allows LOOPBACK
  77. $IP_TABLES -A INPUT -i lo -j ACCEPT
  78. $IP_TABLES -P INPUT DROP
  79. $IP_TABLES -A INPUT -j LOG
  80. #**************************************************************************#
  81. # Output:
  82. #**************************************************************************#
  83. ### Allows LOOPBACK
  84. $IP_TABLES -A OUTPUT -j ACCEPT -o lo
  85. ###
  86. $IP_TABLES -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  87. # ICMP is permitted:
  88. ### Allows ping:
  89. $IP_TABLES -A OUTPUT -p icmp -j ACCEPT
  90. # So are security package updates:
  91. # Note: You can hardcode the IP address here to prevent DNS spoofing
  92. # and to setup the rules even if DNS does not work but then you
  93. # will not "see" IP changes for this service:
  94. $IP_TABLES -A OUTPUT -p tcp -d security.debian.org --dport 80 -j ACCEPT
  95. $IP_TABLES -A OUTPUT -p tcp -d www.dokuwiki.org --dport 80 -j ACCEPT
  96. # As well as the services we have defined:
  97. if [ -n "$REMOTE_TCP_SERVICES" ] ; then
  98. for PORT in $REMOTE_TCP_SERVICES; do
  99. $IP_TABLES -A OUTPUT -p tcp --dport ${PORT} -j ACCEPT
  100. done
  101. fi
  102. if [ -n "$REMOTE_UDP_SERVICES" ] ; then
  103. for PORT in $REMOTE_UDP_SERVICES; do
  104. $IP_TABLES -A OUTPUT -p udp --dport ${PORT} -j ACCEPT
  105. done
  106. fi
  107. # All other connections are registered in syslog
  108. $IP_TABLES -A OUTPUT -j LOG
  109. $IP_TABLES -A OUTPUT -j REJECT
  110. $IP_TABLES -P OUTPUT DROP
  111. $IP_TABLES -A FORWARD -j LOG
  112. #**************************************************************************#
  113. # DOS attack protection
  114. #**************************************************************************#
  115. # Voir http://rockdio.org/ayudatech/how-to-stop-small-ddos-attacks-some-basic-security-advice/
  116. #
  117. $IP_TABLES -I INPUT -p tcp --dport 80 -i venet0 -m state --state NEW -m recent --set
  118. $IP_TABLES -I INPUT -p tcp --dport 80 -i venet0 -m state --state NEW -m recent --update --seconds 30 --hitcount 20 -j DROP
  119. $IP_TABLES -I INPUT -p tcp --dport 443 -i venet0 -m state --state NEW -m recent --set
  120. $IP_TABLES -I INPUT -p tcp --dport 443 -i venet0 -m state --state NEW -m recent --update --seconds 30 --hitcount 20 -j DROP
  121. #**************************************************************************#
  122. # Other network protections
  123. # (some will only work with some kernel versions)
  124. #**************************************************************************#
  125. echo 1 > /proc/sys/net/ipv4/tcp_syncookies
  126. echo 0 > /proc/sys/net/ipv4/ip_forward
  127. echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
  128. echo 1 > /proc/sys/net/ipv4/conf/all/log_martians
  129. echo 1 > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
  130. echo 1 > /proc/sys/net/ipv4/conf/all/rp_filter
  131. echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects
  132. echo 0 > /proc/sys/net/ipv4/conf/all/accept_source_route
  133. iptables -t filter -A OUTPUT -p tcp --dport 22 -j ACCEPT
  134. iptables -t filter -A INPUT -p tcp --dport 22 -j ACCEPT
  135. }
  136. ##########################
  137. # Stop the Firewall rules
  138. ##########################
  139. fw_stop () {
  140. $IP_TABLES -F
  141. $IP_TABLES -t nat -F
  142. $IP_TABLES -t mangle -F
  143. $IP_TABLES -P INPUT DROP
  144. $IP_TABLES -P FORWARD DROP
  145. $IP_TABLES -P OUTPUT ACCEPT
  146. }
  147. ##########################
  148. # Clear the Firewall rules
  149. ##########################
  150. fw_clear () {
  151. $IP_TABLES -F
  152. $IP_TABLES -t nat -F
  153. $IP_TABLES -t mangle -F
  154. $IP_TABLES -P INPUT ACCEPT
  155. $IP_TABLES -P FORWARD ACCEPT
  156. $IP_TABLES -P OUTPUT ACCEPT
  157. }
  158. ##########################
  159. # Test the Firewall rules
  160. ##########################
  161. fw_save () {
  162. $IP_TABLES-save > /etc/iptables.backup
  163. }
  164. fw_restore () {
  165. if [ -e /etc/iptables.backup ]; then
  166. $IP_TABLES-restore < /etc/iptables.backup
  167. fi
  168. }
  169. fw_test () {
  170. fw_save
  171. sleep 30 && echo "Restore previous Firewall rules..." && fw_restore &
  172. fw_stop
  173. fw_start
  174. }
  175. case "$1" in
  176. start|restart)
  177. echo -n "Starting firewall.."
  178. fw_stop
  179. fw_start
  180. echo "done."
  181. ;;
  182. stop)
  183. echo "###############################################################"
  184. echo "I do not stop for now."
  185. echo "Use 'clear' to remove all firewall blocking rules."
  186. echo "Use 'dropall' to remove all firewall blocking rules."
  187. echo "###############################################################"
  188. ;;
  189. clear)
  190. echo -n "Clearing firewall rules.."
  191. fw_clear
  192. echo "done."
  193. ;;
  194. dropall)
  195. echo -n "Droping all connections !!!"
  196. fw_stop
  197. echo "done."
  198. ;;
  199. test)
  200. echo -n "Test Firewall rules..."
  201. fw_test
  202. echo -n "Previous configuration will be restore in 30 seconds"
  203. ;;
  204. *)
  205. echo "Usage: $0 {start|dropall|stop|restart|clear|test}"
  206. echo "###############################################################"
  207. echo "# Be aware that 'stop' drop all incoming/outgoing traffic !!! #"
  208. echo "###############################################################"
  209. echo "Use clear option to allow all traffic."
  210. exit 1
  211. ;;
  212. esac
  213. exit 0