firewall 6.8 KB

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