rose firewall 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/bin/sh
  2. # rose firewall
  3. # script to make firewall so ease
  4. # and Activates/Deactivates the firewall at boot time
  5. # Copyright (c) 2012 ali abdul ghani <blade.vp2020@gmail.com>
  6. # This Program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. # This Program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14.  # You should have received a copy of the GNU General Public License
  15. # along with this library. If not, see <http://www.gnu.org/licenses/>.
  16. ### BEGIN INIT INFO
  17. # Required-Start: $local_fs
  18. # Required-Stop: $local_fs
  19. # Default-Start: S
  20. # Default-Stop: 0 6
  21. # X-Start-Before: $network
  22. # X-Stop-After: $network
  23. ### END INIT INFO
  24. #
  25. #
  26. # Caveats:
  27. # - This configuration applies to all network interfaces
  28. # if you want to restrict this to only a given interface use
  29. # '-i INTERFACE' in the iptables calls.
  30. # - Remote access for TCP/UDP services is granted to any host,
  31. # you probably will want to restrict this using '--source'.
  32. #
  33. # chkconfig: 2345 9 91
  34. #
  35. # You can test this script before applying with the following shell
  36. # snippet, if you do not type anything in 10 seconds the firewall
  37. # rules will be cleared.
  38. #---------------------------------------------------------------
  39. # while true; do test=""; read -t 20 -p "OK? " test ; \
  40. # [ -z "$test" ] && /etc/init.d/myfirewall clear ; done
  41. #---------------------------------------------------------------
  42. PATH=/bin:/sbin:/usr/bin:/usr/sbin
  43. # Services that the system will offer to the network
  44. TCP_SERVICES=""
  45. UDP_SERVICES=""
  46. # Services the system will use from the network
  47. REMOTE_TCP_SERVICES=""
  48. REMOTE_UDP_SERVICES=""
  49. if ! [ -x /sbin/iptables ]; then
  50. exit 0
  51. fi
  52. fw_start () {
  53. # Input traffic:
  54. /sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  55. # Services
  56. if [ -n "$TCP_SERVICES" ] ; then
  57. for PORT in $TCP_SERVICES; do
  58. /sbin/iptables -A INPUT -p tcp --dport ${PORT} -j ACCEPT
  59. done
  60. fi
  61. if [ -n "$UDP_SERVICES" ] ; then
  62. for PORT in $UDP_SERVICES; do
  63. /sbin/iptables -A INPUT -p udp --dport ${PORT} -j ACCEPT
  64. done
  65. fi
  66. /sbin/iptables -A INPUT -p icmp -j ACCEPT
  67. /sbin/iptables -A INPUT -i lo -j ACCEPT
  68. /sbin/iptables -P INPUT DROP
  69. /sbin/iptables -A INPUT -j LOG
  70. # Output:
  71. /sbin/iptables -A OUTPUT -j ACCEPT -o lo
  72. /sbin/iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  73. # ICMP is permitted:
  74. /sbin/iptables -A OUTPUT -p icmp -j ACCEPT
  75. if [ -n "$REMOTE_TCP_SERVICES" ] ; then
  76. for PORT in $REMOTE_TCP_SERVICES; do
  77. /sbin/iptables -A OUTPUT -p tcp --dport ${PORT} -j ACCEPT
  78. done
  79. fi
  80. if [ -n "$REMOTE_UDP_SERVICES" ] ; then
  81. for PORT in $REMOTE_UDP_SERVICES; do
  82. /sbin/iptables -A OUTPUT -p udp --dport ${PORT} -j ACCEPT
  83. done
  84. fi
  85. # All other connections are registered in syslog
  86. /sbin/iptables -A OUTPUT -j LOG
  87. /sbin/iptables -A OUTPUT -j REJECT
  88. /sbin/iptables -P OUTPUT DROP
  89. # Other network protections
  90. # (some will only work with some kernel versions)
  91. echo 1 > /proc/sys/net/ipv4/tcp_syncookies
  92. echo 0 > /proc/sys/net/ipv4/ip_forward
  93. echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
  94. echo 1 > /proc/sys/net/ipv4/conf/all/log_martians
  95. #echo 1 > /proc/sys/net/ipv4/ip_always_defrag
  96. echo 1 > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
  97. echo 1 > /proc/sys/net/ipv4/conf/all/rp_filter
  98. echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects
  99. echo 0 > /proc/sys/net/ipv4/conf/all/accept_source_route
  100. }
  101. fw_stop () {
  102. /sbin/iptables -F
  103. /sbin/iptables -t nat -F
  104. /sbin/iptables -t mangle -F
  105. /sbin/iptables -P INPUT DROP
  106. /sbin/iptables -P FORWARD DROP
  107. /sbin/iptables -P OUTPUT ACCEPT
  108. }
  109. fw_clear () {
  110. /sbin/iptables -F
  111. /sbin/iptables -t nat -F
  112. /sbin/iptables -t mangle -F
  113. /sbin/iptables -P INPUT ACCEPT
  114. /sbin/iptables -P FORWARD ACCEPT
  115. /sbin/iptables -P OUTPUT ACCEPT
  116. }
  117. case "$1" in
  118. start|restart)
  119. echo -n "Starting firewall.."
  120. fw_stop
  121. fw_start
  122. echo "done."
  123. ;;
  124. stop)
  125. echo -n "Stopping firewall.."
  126. fw_stop
  127. echo "done."
  128. ;;
  129. clear)
  130. echo -n "Clearing firewall rules.."
  131. fw_clear
  132. echo "done."
  133. ;;
  134. *)
  135. echo "Usage: $0 {start|stop|restart|clear}"
  136. exit 1
  137. ;;
  138. esac
  139. exit 0