lfirewall 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. # Light 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/lfirewall/lfirewall.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. do_action=do_exec
  43. IPTABLES_CHECK=__iptables_check_action
  44. IPTABLES_ADD=__iptable_add_action
  45. IPTABLES_SET_POLICY=__iptable_set_policy_action
  46. do_exec () {
  47. case $1 in
  48. __iptable_add_action)
  49. shift
  50. iptables_option=-A
  51. ;;
  52. __iptable_set_policy_action)
  53. shift
  54. iptables_option=-P
  55. ;;
  56. *)
  57. echo "Nothing to be done for $1"
  58. ;;
  59. esac
  60. $IP_TABLES $iptables_option $*
  61. }
  62. do_check () {
  63. the_action=$1
  64. shift
  65. case $the_action in
  66. __iptable_add_action)
  67. iptables_option=-A
  68. ;;
  69. __iptable_set_policy_action)
  70. return 0
  71. ;;
  72. *)
  73. echo "Nothing to be done for $1"
  74. ;;
  75. esac
  76. default_option=-C
  77. $do_log "$the_action:" $IP_TABLES -C $*
  78. $IP_TABLES -C $*
  79. global_status=$((global_status+$?))
  80. }
  81. log_action () {
  82. echo $*
  83. }
  84. do_not_log_action () {
  85. return 0
  86. }
  87. do_log=do_not_log_action
  88. ##########################
  89. # Start the Firewall rules
  90. ##########################
  91. fw_start () {
  92. #**************************************************************************#
  93. # Input traffic:
  94. #**************************************************************************#
  95. ### Keep existing connections
  96. $do_action $IPTABLES_ADD INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  97. # Services
  98. if [ -n "$TCP_SERVICES" ] ; then
  99. for PORT in $TCP_SERVICES; do
  100. $do_action $IPTABLES_ADD INPUT -p tcp --dport ${PORT} -j ACCEPT
  101. done
  102. fi
  103. if [ -n "$UDP_SERVICES" ] ; then
  104. for PORT in $UDP_SERVICES; do
  105. $do_action $IPTABLES_ADD INPUT -p udp --dport ${PORT} -j ACCEPT
  106. done
  107. fi
  108. # Remote management
  109. if [ -n "$NETWORK_MGMT" ] ; then
  110. $do_action $IPTABLES_ADD INPUT -p tcp --src ${NETWORK_MGMT} --dport ${SSH_PORT} -j ACCEPT
  111. else
  112. $do_action $IPTABLES_ADD INPUT -p tcp --dport ${SSH_PORT} -j ACCEPT
  113. fi
  114. #**************************************************************************#
  115. # NGINX
  116. #**************************************************************************#
  117. $do_action $IPTABLES_ADD INPUT -i lo -s localhost -d localhost -j ACCEPT
  118. $do_action $IPTABLES_ADD OUTPUT -o lo -s localhost -d localhost -j ACCEPT
  119. $do_action $IPTABLES_ADD INPUT -p tcp --dport http -j ACCEPT
  120. $do_action $IPTABLES_ADD INPUT -p tcp --dport https -j ACCEPT
  121. # Remote testing
  122. ### Allows PING
  123. $do_action $IPTABLES_ADD INPUT -p icmp -j ACCEPT
  124. ### Allows LOOPBACK
  125. $do_action $IPTABLES_ADD INPUT -i lo -j ACCEPT
  126. $IP_TABLES -P INPUT DROP
  127. $do_action $IPTABLES_ADD INPUT -j LOG
  128. #**************************************************************************#
  129. # Output:
  130. #**************************************************************************#
  131. ### Allows LOOPBACK
  132. $do_action $IPTABLES_ADD OUTPUT -j ACCEPT -o lo
  133. ###
  134. $do_action $IPTABLES_ADD OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  135. # ICMP is permitted:
  136. ### Allows ping:
  137. $do_action $IPTABLES_ADD OUTPUT -p icmp -j ACCEPT
  138. # As well as the services we have defined:
  139. if [ -n "$REMOTE_TCP_SERVICES" ] ; then
  140. for PORT in $REMOTE_TCP_SERVICES; do
  141. $do_action $IPTABLES_ADD OUTPUT -p tcp --dport ${PORT} -j ACCEPT
  142. done
  143. fi
  144. if [ -n "$REMOTE_UDP_SERVICES" ] ; then
  145. for PORT in $REMOTE_UDP_SERVICES; do
  146. $do_action $IPTABLES_ADD OUTPUT -p udp --dport ${PORT} -j ACCEPT
  147. done
  148. fi
  149. # All other connections are registered in syslog
  150. $do_action $IPTABLES_ADD OUTPUT -j LOG
  151. $do_action $IPTABLES_ADD OUTPUT -j REJECT
  152. $do_action $IPTABLES_SET_POLICY OUTPUT DROP
  153. $do_action $IPTABLES_ADD FORWARD -j LOG
  154. #**************************************************************************#
  155. # DOS attack protection
  156. #**************************************************************************#
  157. # See http://rockdio.org/ayudatech/how-to-stop-small-ddos-attacks-some-basic-security-advice/
  158. #
  159. $IP_TABLES -I INPUT -p tcp --dport 80 -i $NETWORK_IF -m state --state NEW -m recent --set
  160. $IP_TABLES -I INPUT -p tcp --dport 80 -i $NETWORK_IF -m state --state NEW -m recent --update --seconds 30 --hitcount 20 -j DROP
  161. $IP_TABLES -I INPUT -p tcp --dport 443 -i $NETWORK_IF -m state --state NEW -m recent --set
  162. $IP_TABLES -I INPUT -p tcp --dport 443 -i $NETWORK_IF -m state --state NEW -m recent --update --seconds 30 --hitcount 20 -j DROP
  163. #**************************************************************************#
  164. # Other network protections
  165. # (some will only work with some kernel versions)
  166. #**************************************************************************#
  167. echo 1 > /proc/sys/net/ipv4/tcp_syncookies
  168. echo 0 > /proc/sys/net/ipv4/ip_forward
  169. echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
  170. echo 1 > /proc/sys/net/ipv4/conf/all/log_martians
  171. echo 1 > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
  172. echo 1 > /proc/sys/net/ipv4/conf/all/rp_filter
  173. echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects
  174. echo 0 > /proc/sys/net/ipv4/conf/all/accept_source_route
  175. iptables -t filter -A OUTPUT -p tcp --dport 22 -j ACCEPT
  176. iptables -t filter -A INPUT -p tcp --dport 22 -j ACCEPT
  177. }
  178. ##########################
  179. # Stop the Firewall rules
  180. ##########################
  181. fw_stop () {
  182. $IP_TABLES -F
  183. $IP_TABLES -t nat -F
  184. $IP_TABLES -t mangle -F
  185. $IP_TABLES -P INPUT DROP
  186. $IP_TABLES -P FORWARD DROP
  187. $IP_TABLES -P OUTPUT ACCEPT
  188. }
  189. ##########################
  190. # Clear the Firewall rules
  191. ##########################
  192. fw_clear () {
  193. $IP_TABLES -F
  194. $IP_TABLES -t nat -F
  195. $IP_TABLES -t mangle -F
  196. $IP_TABLES -P INPUT ACCEPT
  197. $IP_TABLES -P FORWARD ACCEPT
  198. $IP_TABLES -P OUTPUT ACCEPT
  199. }
  200. ##########################
  201. # Test the Firewall rules
  202. ##########################
  203. fw_save () {
  204. $IP_TABLES-save > /etc/iptables.backup
  205. }
  206. fw_restore () {
  207. if [ -e /etc/iptables.backup ]; then
  208. $IP_TABLES-restore < /etc/iptables.backup
  209. fi
  210. }
  211. fw_test () {
  212. fw_save
  213. sleep 30 && echo "Restore previous Firewall rules..." && fw_restore &
  214. fw_stop
  215. fw_start
  216. }
  217. case "$1" in
  218. start|restart)
  219. echo -n "Starting firewall.."
  220. fw_stop
  221. fw_start
  222. echo "done."
  223. ;;
  224. stop)
  225. echo "###############################################################"
  226. echo "I do not stop for now."
  227. echo "Use 'clear' to remove all firewall blocking rules."
  228. echo "Use 'dropall' to stop any traffic and block everything."
  229. echo "###############################################################"
  230. ;;
  231. clear)
  232. echo -n "Clearing firewall rules.."
  233. fw_clear
  234. echo "done."
  235. ;;
  236. dropall)
  237. echo -n "Droping all connections !!!"
  238. fw_stop
  239. echo "done."
  240. ;;
  241. test)
  242. echo -n "Test Firewall rules..."
  243. fw_test
  244. echo -n "Previous configuration will be restore in 30 seconds"
  245. ;;
  246. status)
  247. do_action=do_check
  248. global_status=0
  249. if [ "$2" = "-v" ] ; then
  250. do_log=log_action
  251. fi
  252. # Start will not really start but exec the "check" action
  253. fw_start
  254. if [ 0 -eq "$global_status" ] ; then
  255. echo "Firewall rules match configuration"
  256. exit 0
  257. else
  258. echo "Some firewall rules are not set correctly"
  259. exit $global_status
  260. fi
  261. ;;
  262. *)
  263. echo "Usage: $0 {start|dropall|stop|restart|clear|test}"
  264. echo "###############################################################"
  265. echo "# Be aware that 'stop' drop all incoming/outgoing traffic !!! #"
  266. echo "###############################################################"
  267. echo "Use clear option to allow all traffic."
  268. exit 1
  269. ;;
  270. esac
  271. exit 0