找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3887|回复: 7

[ 推荐 ] 一款处于开发初期的防火墙脚本。

[复制链接]
发表于 2007-6-8 18:23:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

×
klondike-firewall  :Stateful netfilter/iptables packet-filtering firewall bash scripts

yukondude 从 2001 年开始在自己的 Linux 路由器和独立服务器上的使用的脚本。应该是从 google code 开始之后才有的这个项目计划。作者自称自己的路由器和独立服务器没有受到过攻击,有点自大了 (运气好了点而已)
在我发布这个主题的时候,作者刚刚发布0.8 版本 才几个小时,也是第一个作者认为真正可用的版本。这个版本文件名似乎有问题,可能需要将 8 改成 tar。

有兴趣的可以直接把这个脚本用在Linux系统上。

我只是大致看了下脚本内容,没有检查过(历来如此:L )。看起来结构比较清晰。
我的意图是希望有兴趣做自己路由器的朋友能够关注某个正在开发的项目,尤其是初期。能看到很多的变化,其实了解了别人的想法、思路,与自己的比较之后,再做什么也便容易的多了。

下面是用在路由器上的脚本:

  1. #!/bin/bash
  2. ################################################################################
  3. # klondike-router
  4. # A shell script to configure stateful packet-filter rules for a netfilter/
  5. # iptables routing firewall.
  6. #-------------------------------------------------------------------------------
  7. # chkconfig: 35 11 89
  8. # description: Configuration rules for stateful packet-filter iptables firewall.
  9. #-------------------------------------------------------------------------------
  10. # This script must run after the main iptables rc script (usually,
  11. # /etc/rc.d/init.d/iptables) and should also run after the main networking rc
  12. # script (usually /etc/rc.d/init.d/network). For safety's sake, the main
  13. # iptables script should run before networking starts, and should set the
  14. # default policy for all built-in chains to DROP.
  15. #
  16. # Replace all items marked "TODO" with your own network values. The existing
  17. # rules assume a private class-C LAN, so you'll have to adjust as necessary.
  18. # Of course, you'll also have to set up your own rules.
  19. #
  20. # LAN: Private network behind router.
  21. # NET: Public network.
  22. #   
  23. # The default setup (once proper IP addresses are in place) configures a routing
  24. # firewall that forwards incoming HTTP, HTTPS, SMTP, and SSH traffic from NET to
  25. # a server on the private LAN. The routing server also acts as a DNS proxy and
  26. # NTP time server to clients on the LAN. The routing server may also initiate
  27. # HTTP, RSYNC, and Passive FTP requests to the NET.
  28. #-------------------------------------------------------------------------------
  29. # By Dave Rogers [thedude strudel yukondude full-stop com]
  30. # yukon dude software [www.yukondude.com]
  31. # Whitehorse, Yukon, Canada
  32. #-------------------------------------------------------------------------------
  33. # Copyright ?2002-2007 Dave Rogers
  34. #
  35. # This program is free software; you can redistribute it and/or modify
  36. # it under the terms of the GNU General Public License as published by
  37. # the Free Software Foundation; either version 2 of the License, or
  38. # (at your option) any later version.
  39. #
  40. # This program is distributed in the hope that it will be useful,
  41. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  42. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  43. # GNU General Public License for more details.
  44. #
  45. # You should have received a copy of the GNU General Public License
  46. # along with this program; if not, write to the Free Software
  47. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  48. #-------------------------------------------------------------------------------
  49. # Inspired by:
  50. # Netfilter by Paul "Rusty" Russell
  51. #   [www.netfilter.org]
  52. # Iptables Tutorial by Oskar Andreasson
  53. #   [iptables-tutorial.frozentux.net/iptables-tutorial.html]
  54. # Connection Tracking by James C. Stephens
  55. #   [www.sns.ias.edu/~jns/wp/2006/01/24/iptables-how-does-it-work]
  56. # Linux IP Masquerade HOWTO by David A. Ranch
  57. #   [www.tldp.org/HOWTO/IP-Masquerade-HOWTO]
  58. # Firewall Ruleset by vogt@hansenet.com
  59. #   [link no longer valid]
  60. # Redundant Internet Connections Using Linux by Seann Herdejurgen
  61. #   [www.samag.com/documents/s=1824/sam0201h/0201h.htm]
  62. # rc.firewall.iptables.dual version 1.2b3 by obsid@sentry.net
  63. #   [www.sentry.net/~obsid/IPTables/rc.scripts.dir/current/ \
  64. #   rc.firewall.iptables.dual]
  65. #-------------------------------------------------------------------------------
  66. # $Id: klondike-router.sh 2 2007-05-25 22:32:22Z yukondude $
  67. ################################################################################
  68. #-------------------------------------------------------------------------------
  69. # Dependency Checks
  70. # Bail if iptables isn't in the path.
  71. if [ -z $(which iptables 2>/dev/null) ]; then
  72.   echo -n "$0 is unable to load the firewall rules: "
  73.   echo "the iptables utility is not in the path."
  74.   exit 1
  75. fi
  76. #-------------------------------------------------------------------------------
  77. # Network Interfaces
  78. # TODO: Change interfaces.
  79. IFACE_LAN="eth1" # LAN-facing interface
  80. IFACE_NET="eth0" # NET-facing interface
  81. IFACE_LOC="lo" # Loopback
  82. #-------------------------------------------------------------------------------
  83. # Host and Network IP Addresses
  84. # TODO: Change LAN-facing addresses.
  85. IPADDR_LAN="192.168.1.1/32" # LAN-facing NIC IP
  86. IPNETW_LAN="192.168.1.0/24" # LAN-facing network
  87. IPBCAST_LAN="192.168.1.255" # LAN-facing broadcast IP
  88. # TODO: Change NET-facing addresses.
  89. IPADDR_NET="199.247.1.1/32" # NET-facing NIC IP
  90. IPNETW_NET="! ${IPNETW_LAN}" # NET-facing network (essentially, everything else)
  91. IPBCAST_NET="199.247.1.255" # NET-facing broadcast IP
  92. IPNETW_LOC="127.0.0.0/8"
  93. #-------------------------------------------------------------------------------
  94. # Private and Reserved Network IP Addresses
  95. # TODO: Change if you don't use a private class-C LAN.
  96. PRIVATE="10.0.0.0/8 172.16.0.0/12 224.0.0.0/4" # Impossible IPs.
  97. PRIVATE_LAN="192.168.0.0/16" # Possible LAN-facing IPs.
  98. # Addresses reserved by IANA (subject to change, so I just picked the outliers).
  99. #   [http://www.iana.org/assignments/ipv4-address-space]
  100. RESERVED="0.0.0.0/8 1.0.0.0/8 2.0.0.0/8 240.0.0.0/4"
  101. #-------------------------------------------------------------------------------
  102. # Known Remote Host IP Addresses
  103. # TODO: Change DNS server addresses.
  104. DNS_NET_1="10.11.12.13"
  105. DNS_NET_2="10.11.12.13"
  106. # TODO: Add any other fixed IPs of interest.
  107. #-------------------------------------------------------------------------------
  108. # Known IP Protocols (other than TCP, UDP, ICMP)
  109. IPPROT_IGMP="2"
  110. #-------------------------------------------------------------------------------
  111. # ICMP Message Types
  112. ICMP_PONG="0"
  113. ICMP_UNREACHABLE="3"
  114. ICMP_PING="8"
  115. ICMP_TIME_EXCEEDED="11"
  116. ICMP_TRACEROUTE="30"
  117. #-------------------------------------------------------------------------------
  118. # Known TCP and UDP Ports and Port Ranges
  119. # TODO: Add/remove ports as necessary.
  120. PORT_FTP="21"
  121. PORT_FTP_CMD="21"
  122. PORT_FTP_DATA="20"
  123. PORT_SSH="22"
  124. PORT_TELNET="23"
  125. PORT_SMTP="25"
  126. PORT_DNS="53"
  127. PORT_HTTP="80"
  128. PORT_POP="110"
  129. PORT_AUTH="113"
  130. PORT_NTP="123"
  131. PORT_MSRPC="135"
  132. PORT_MSNBNS="137"
  133. PORT_MSNBDG="138"
  134. PORT_MSNBSSN="139"
  135. PORT_HTTPS="443"
  136. PORT_MSDS="445"
  137. PORT_RSYNC="873"
  138. PORTS_TRACEROUTE="32769:65535"
  139. #-------------------------------------------------------------------------------
  140. # Internal Port-Forwarded Service Addresses
  141. # TODO: Add/remove port-forwarded IPs as necessary.
  142. HTTP_LAN="192.168.1.2"
  143. HTTPS_LAN="192.168.1.2"
  144. SMTP_LAN="192.168.1.2"
  145. SSH_LAN="192.168.1.2"
  146. #-------------------------------------------------------------------------------
  147. # Privileged and Unprivileged Port Ranges
  148. PORTS_PRIV="0:1023"
  149. PORTS_UNPRIV="1024:65535"
  150. #-------------------------------------------------------------------------------
  151. # Log Levels for Target Chains
  152. LOG_LEVEL_ATTACK="3"  # err
  153. LOG_LEVEL_ILLEGAL="4" # warning
  154. LOG_LEVEL_UNKNOWN="4" # warning
  155. LOG_LEVEL_FLOOD="5"   # notice
  156. LOG_LEVEL_SCAN="5"    # notice
  157. LOG_LEVEL_WATCH="6"   # info
  158. #-------------------------------------------------------------------------------
  159. # Configure Kernel Networking Parameters
  160. # See www.tldp.org/HOWTO/Adv-Routing-HOWTO-13.html for further explanation.
  161. config_kernel() {
  162.   # Load necessary kernel modules. Most are loaded automatically, but
  163.   # ip_conntrack_ftp and ip_nat_ftp must be loaded explicitly to enable FTP
  164.   # connection tracking, and FTP SNAT, respectively.
  165.   modprobe ip_conntrack_ftp
  166.   modprobe ip_nat_ftp
  167.   # Set the maximum number of connections to track if not already > 4096.
  168.   if [ -e /proc/sys/net/ipv4/ip_conntrack_max ]; then
  169.     if [ $(cat /proc/sys/net/ipv4/ip_conntrack_max) -le 4096 ]; then
  170.       echo "4096" > /proc/sys/net/ipv4/ip_conntrack_max
  171.     fi
  172.   fi
  173.   # Set local port range for TCP/UDP connections.
  174.   if [ -e /proc/sys/net/ipv4/ip_local_port_range ]; then
  175.     echo -e "32768\t61000" > /proc/sys/net/ipv4/ip_local_port_range
  176.   fi
  177.   # Disable source-routed packets.
  178.   if [ -e /proc/sys/net/ipv4/conf/all/accept_source_route ]; then
  179.     for i in /proc/sys/net/ipv4/conf/*/accept_source_route; do
  180.       echo "0" > $i;
  181.     done
  182.   fi
  183.   # Enable reverse path filter to combat spoofing.
  184.   if [ -e /proc/sys/net/ipv4/conf/all/rp_filter ]; then
  185.     for i in /proc/sys/net/ipv4/conf/*/rp_filter; do
  186.       echo "1" > $i;
  187.     done
  188.   fi
  189.   # Don't reply to smurf ping broadcasts.
  190.   if [ -e /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts ]; then
  191.     echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
  192.   fi
  193.   # Log spoofed, source-routed, or redirect packets.
  194.   if [ -e /proc/sys/net/ipv4/conf/all/log_martians ]; then
  195.     echo "1" > /proc/sys/net/ipv4/conf/all/log_martians
  196.   fi
  197.   # Disable external ICMP redirects.
  198.   if [ -e /proc/sys/net/ipv4/conf/$IFACE_NET/accept_redirects ]; then
  199.     echo "0" > /proc/sys/net/ipv4/conf/$IFACE_NET/accept_redirects
  200.   fi
  201.   # Ignore ICMP responses to hosts misinterpreting broadcast traffic.
  202.   if [ -e /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses ]; then
  203.     echo "1" > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
  204.   fi
  205.   # Enable IP forwarding.
  206.   if [ -e /proc/sys/net/ipv4/ip_forward ]; then
  207.     echo "1" > /proc/sys/net/ipv4/ip_forward
  208.   fi
  209. }
  210. #-------------------------------------------------------------------------------
  211. # Remove All Chains and Rules
  212. flush() {
  213.   # Flush the rules from the built-in chains.
  214.   iptables -F
  215.   iptables -F -t nat
  216.   iptables -F -t mangle
  217.   # Drop any custom chains.
  218.   iptables -X
  219.   iptables -X -t nat
  220.   iptables -X -t mangle
  221.   
  222.   # Zero the counters.
  223.   iptables -Z
  224.   iptables -Z -t nat
  225.   iptables -Z -t mangle
  226. }
  227. #-------------------------------------------------------------------------------
  228. # Set Policy for Built-in Chains
  229. # Use DROP as the default policy unless specified otherwise.
  230. set_policy() {
  231.   iptables -P INPUT ${1:-DROP}
  232.   iptables -P FORWARD ${1:-DROP}
  233.   iptables -P OUTPUT ${1:-DROP}
  234. }
  235. #-------------------------------------------------------------------------------
  236. # Install Target Chains
  237. # All rules must jump to one of these target chains.
  238. install_targets() {
  239.   # Accept incoming packets. This chain exists primarily for accounting purposes.
  240.   iptables -N TGT_ACCEPT_IN
  241.   iptables -A TGT_ACCEPT_IN -j ACCEPT
  242.   # Accept outgoing packets. This chain exists primarily for accounting purposes.
  243.   iptables -N TGT_ACCEPT_OUT
  244.   iptables -A TGT_ACCEPT_OUT -j ACCEPT
  245.   # Drop incoming packets. This chain exists primarily for accounting purposes.
  246.   iptables -N TGT_DROP_IN
  247.   iptables -A TGT_DROP_IN -j DROP
  248.   # Drop outgoing packets. This chain exists primarily for accounting purposes.
  249.   iptables -N TGT_DROP_OUT
  250.   iptables -A TGT_DROP_OUT -j DROP
  251.   # Reject and reset incoming TCP packets. This is a "friendly" way to respond
  252.   # to certain unwanted protocol probes such as AUTH (IDENTD).
  253.   iptables -N TGT_TCP_REJECT_IN
  254.   iptables -A TGT_TCP_REJECT_IN -p tcp -j REJECT --reject-with tcp-reset
  255.   # Reject and reset outgoing TCP packets. This is a "friendly" way to respond
  256.   # to certain unwanted protocol probes such as AUTH (IDENTD).
  257.   iptables -N TGT_TCP_REJECT_OUT
  258.   iptables -A TGT_TCP_REJECT_OUT -p tcp -j REJECT --reject-with tcp-reset
  259.   # Log and drop malicious incoming packets that are known security exploits.
  260.   iptables -N TGT_ATTACK_IN
  261.   iptables -A TGT_ATTACK_IN -j LOG --log-level $LOG_LEVEL_ATTACK \
  262.     --log-prefix "FW_ATTACK_IN: "
  263.   iptables -A TGT_ATTACK_IN -j TGT_DROP_IN
  264.   # Log and drop malicious outgoing packets that are known security exploits.
  265.   iptables -N TGT_ATTACK_OUT
  266.   iptables -A TGT_ATTACK_OUT -j LOG --log-level $LOG_LEVEL_ATTACK \
  267.     --log-prefix "FW_ATTACK_OUT: "
  268.   iptables -A TGT_ATTACK_OUT -j TGT_DROP_OUT
  269.   # Discard annoying but otherwise innocuous incoming packets.
  270.   iptables -N TGT_DISCARD_IN
  271.   iptables -A TGT_DISCARD_IN -j TGT_DROP_IN
  272.   # Discard annoying but otherwise innocuous incoming packets.
  273.   iptables -N TGT_DISCARD_OUT
  274.   iptables -A TGT_DISCARD_OUT -j TGT_DROP_OUT
  275.   # Log and drop any suspected incoming flood attacks (more than 15 packets per
  276.   # second with a maximum burst of 30 per second). Don't log more than 5
  277.   # packets per minute so that the log doesn't fill up.
  278.   iptables -N TGT_FLOOD_IN
  279.   iptables -A TGT_FLOOD_IN -m limit --limit 15/s --limit-burst 30 -j RETURN
  280.   iptables -A TGT_FLOOD_IN -m limit --limit 5/m -j LOG \
  281.     --log-level $LOG_LEVEL_FLOOD --log-prefix "FW_FLOOD_IN: "
  282.   iptables -A TGT_FLOOD_IN -j TGT_DROP_IN
  283.   # Log and drop malformed or impossible incoming packets.
  284.   iptables -N TGT_ILLEGAL_IN
  285.   iptables -A TGT_ILLEGAL_IN -j LOG --log-level $LOG_LEVEL_ILLEGAL \
  286.     --log-prefix "FW_ILLEGAL_IN: "
  287.   iptables -A TGT_ILLEGAL_IN -j TGT_DROP_IN
  288.   # Log and drop malformed or impossible outgoing packets.
  289.   iptables -N TGT_ILLEGAL_OUT
  290.   iptables -A TGT_ILLEGAL_OUT -j LOG --log-level $LOG_LEVEL_ILLEGAL \
  291.     --log-prefix "FW_ILLEGAL_OUT: "
  292.   iptables -A TGT_ILLEGAL_OUT -j TGT_DROP_OUT
  293.   # Log and drop packets that look like incoming scans. Don't log more than 5
  294.   # packets per minute so that the log doesn't fill up.
  295.   iptables -N TGT_SCAN_IN
  296.   iptables -A TGT_SCAN_IN -m limit --limit 5/m -j LOG \
  297.     --log-level $LOG_LEVEL_SCAN --log-prefix "FW_SCAN_IN: "
  298.   iptables -A TGT_SCAN_IN -j TGT_DROP_IN
  299.   
  300.   # Log and drop unknown incoming packets. Don't log more than 5 packets per
  301.   # minute so that the log doesn't fill up.
  302.   iptables -N TGT_UNKNOWN_IN
  303.   iptables -A TGT_UNKNOWN_IN -m limit --limit 5/m -j LOG \
  304.   --log-level $LOG_LEVEL_UNKNOWN --log-prefix "FW_UNKNOWN_IN: "
  305.   iptables -A TGT_UNKNOWN_IN -j TGT_DROP_IN
  306.   # Log and drop unknown outgoing packets. Don't log more than 5 packets per
  307.   # minute so that the log doesn't fill up.
  308.   iptables -N TGT_UNKNOWN_OUT
  309.   iptables -A TGT_UNKNOWN_OUT -m limit --limit 5/m -j LOG \
  310.   --log-level $LOG_LEVEL_UNKNOWN --log-prefix "FW_UNKNOWN_OUT: "
  311.   iptables -A TGT_UNKNOWN_OUT -j TGT_DROP_OUT
  312.   # Log and accept incoming packets that we want to keep an eye on.
  313.   iptables -N TGT_WATCH_IN
  314.   iptables -A TGT_WATCH_IN -j LOG --log-level $LOG_LEVEL_WATCH \
  315.     --log-prefix "FW_WATCH_IN: " --log-ip-options
  316.   iptables -A TGT_WATCH_IN -j TGT_ACCEPT_IN
  317.   # Log and accept outgoing packets that we want to keep an eye on.
  318.   iptables -N TGT_WATCH_OUT
  319.   iptables -A TGT_WATCH_OUT -j LOG --log-level $LOG_LEVEL_WATCH \
  320.     --log-prefix "FW_WATCH_OUT: " --log-ip-options
  321.   iptables -A TGT_WATCH_OUT -j TGT_ACCEPT_OUT
  322. }
  323. #-------------------------------------------------------------------------------
  324. # Install Local Loopback Interface Rules
  325. # No restrictions on local loopback interface.
  326. install_lo_in_out() {
  327.   iptables -A INPUT -i $IFACE_LOC -j TGT_ACCEPT_IN
  328.   iptables -A OUTPUT -o $IFACE_LOC -j TGT_ACCEPT_OUT
  329. }
  330. #-------------------------------------------------------------------------------
  331. # Install BAD_TCP_ANY_IN Chain
  332. # Filters out bad TCP segments originating from any connected network.
  333. install_bad_tcp_any_in() {
  334.   iptables -N BAD_TCP_ANY_IN
  335.   iptables -A INPUT -p tcp -j BAD_TCP_ANY_IN
  336.   
  337.   # Be polite and deny inbound AUTH connections (usually from broken email
  338.   # servers).
  339.   iptables -A BAD_TCP_ANY_IN -p tcp --dport $PORT_AUTH -j TGT_TCP_REJECT_IN
  340.   # Segments with illegal TCP flags.
  341.   iptables -A BAD_TCP_ANY_IN -p tcp --tcp-option 64 -j TGT_ILLEGAL_IN
  342.   iptables -A BAD_TCP_ANY_IN -p tcp --tcp-option 128 -j TGT_ILLEGAL_IN
  343.   # Segments pretending to be part of an established connection.
  344.   iptables -A BAD_TCP_ANY_IN -p tcp ! --syn -m state --state NEW \
  345.     -j TGT_ILLEGAL_IN
  346.   
  347.   # Segments with every flag bit set (XMAS tree packets).
  348.   iptables -A BAD_TCP_ANY_IN -p tcp --tcp-flags ALL ALL -j TGT_ILLEGAL_IN
  349.   
  350.   # Segments with no flag bits set (NULL packets).
  351.   iptables -A BAD_TCP_ANY_IN -p tcp --tcp-flags ALL NONE -j TGT_ILLEGAL_IN
  352.   # SYN flood DoS attacks.
  353.   iptables -A BAD_TCP_ANY_IN -p tcp --syn -j TGT_FLOOD_IN
  354.   
  355.   # Stealth scan. Treat like flood because a few at a time are valid.
  356.   iptables -A BAD_TCP_ANY_IN -p tcp --tcp-flags SYN,ACK,FIN,RST RST \
  357.     -j TGT_FLOOD_IN
  358.   
  359.   # XMAS scan, used by NMAP.
  360.   iptables -A BAD_TCP_ANY_IN -p tcp --tcp-flags ALL FIN,URG,PSH -j TGT_SCAN_IN
  361.   
  362.   # SYN/RST scan.
  363.   iptables -A BAD_TCP_ANY_IN -p tcp --tcp-flags SYN,RST SYN,RST -j TGT_SCAN_IN
  364.   
  365.   # SYN/FIN scan.
  366.   iptables -A BAD_TCP_ANY_IN -p tcp --tcp-flags SYN,FIN SYN,FIN -j TGT_SCAN_IN
  367.   # MyDoom backdoor scan.
  368.   iptables -A BAD_TCP_ANY_IN -p tcp --dport 3127 -j TGT_SCAN_IN
  369. }
  370. #-------------------------------------------------------------------------------
  371. # Install BAD_ANY_IN Chain
  372. # Filters out bad traffic originating from any connected network.
  373. install_bad_any_in() {
  374.   iptables -N BAD_ANY_IN
  375.   iptables -A INPUT -j BAD_ANY_IN
  376.   
  377.   # Ignore any Internet Group Management Protocol (IGMP) messages.
  378.   iptables -A BAD_ANY_IN -p $IPPROT_IGMP -j TGT_DISCARD_IN
  379.   
  380.   # Invalid packet.
  381.   iptables -A BAD_ANY_IN -m state --state INVALID -j TGT_ILLEGAL_IN
  382.   
  383.   # Packet fragments.
  384.   iptables -A BAD_ANY_IN -f -j TGT_ILLEGAL_IN
  385.   
  386.   # Ping scan. Treat like flood because a few at a time are valid.
  387.   iptables -A BAD_ANY_IN -p icmp --icmp-type $ICMP_PING -j TGT_FLOOD_IN
  388.   # Packets pretending to be headed for the loopback interface.
  389.   iptables -A BAD_ANY_IN -d $IPNETW_LOC -j TGT_ILLEGAL_IN
  390.   
  391.   # Packets pretending to be from a private network address.
  392.   for NETW in $PRIVATE; do
  393.     iptables -A BAD_ANY_IN -s $NETW -j TGT_ILLEGAL_IN
  394.   done
  395.   
  396.   # Packets pretending to be from a reserved address.
  397.   for NETW in $RESERVED; do
  398.     iptables -A BAD_ANY_IN -s $NETW -j TGT_ILLEGAL_IN
  399.   done
  400.   
  401.   # Packets with a source port of zero. Started showing up in May, 2003.
  402.   iptables -A BAD_ANY_IN -p tcp --sport 0 -j TGT_ILLEGAL_IN
  403.   iptables -A BAD_ANY_IN -p udp --sport 0 -j TGT_ILLEGAL_IN
  404.   
  405.   # Throw away MS SQL Server Slammer worm crap.
  406.   iptables -A BAD_ANY_IN -p tcp --dport 1433 -j TGT_DISCARD_IN
  407.   iptables -A BAD_ANY_IN -p udp --dport 1434 -j TGT_DISCARD_IN
  408. }
  409. #-------------------------------------------------------------------------------
  410. # Install BAD_ANY_OUT Chain
  411. # Filters out bad traffic destined for any connected network.
  412. install_bad_any_out() {
  413.   iptables -N BAD_ANY_OUT
  414.   iptables -A OUTPUT -j BAD_ANY_OUT
  415.   # Packets pretending to be part of an established connection.
  416.   iptables -A BAD_ANY_OUT -p tcp ! --syn -m state --state NEW -j TGT_ILLEGAL_OUT
  417.   # Prevent information leak described by Red Hat Advisory RHSA-2002:086-05.
  418.   iptables -A BAD_ANY_OUT -p icmp -m state --state INVALID -j TGT_ILLEGAL_OUT
  419.   # Packets heading for a private network address.
  420.   for NETW in $PRIVATE; do
  421.     iptables -A BAD_ANY_OUT -d $NETW -j TGT_ILLEGAL_OUT
  422.   done
  423.   # Packets heading for a reserved address.
  424.   for NETW in $RESERVED; do
  425.     iptables -A BAD_ANY_OUT -d $NETW -j TGT_ILLEGAL_OUT
  426.   done
  427.   
  428.   # Packets with a source port of zero.
  429.   iptables -A BAD_ANY_OUT -p tcp --sport 0 -j TGT_ILLEGAL_OUT
  430.   iptables -A BAD_ANY_OUT -p udp --sport 0 -j TGT_ILLEGAL_OUT
  431. }
  432. #-------------------------------------------------------------------------------
  433. # Install BAD_NET_IN Chain
  434. # Filters out bad traffic originating from the public internet.
  435. install_bad_net_in() {
  436.   iptables -N BAD_NET_IN
  437.   iptables -A INPUT -i $IFACE_NET -j BAD_NET_IN
  438.   # Drop annoying MS worm traffic.
  439.   iptables -A BAD_NET_IN -p udp --dport $PORT_MSNBNS -j TGT_DISCARD_IN
  440.   iptables -A BAD_NET_IN -p tcp --dport $PORT_MSRPC -j TGT_DISCARD_IN
  441.   iptables -A BAD_NET_IN -p tcp --dport $PORT_MSNBSSN -j TGT_DISCARD_IN
  442.   # Broadcast packets.
  443.   iptables -A BAD_NET_IN -d $IPBCAST_NET -j TGT_ATTACK_IN
  444.   # Packets pretending to be from this address.
  445.   iptables -A BAD_NET_IN -s $IPADDR_NET -j TGT_ILLEGAL_IN
  446.   # Packets pretending to be from the LAN private network.
  447.   iptables -A BAD_NET_IN -s $PRIVATE_LAN -j TGT_ILLEGAL_IN
  448. }
  449. #-------------------------------------------------------------------------------
  450. # Install BAD_NET_OUT Chain
  451. # Filters out bad traffic destined for the public internet.
  452. install_bad_net_out() {
  453.   iptables -N BAD_NET_OUT
  454.   iptables -A OUTPUT -o $IFACE_NET -j BAD_NET_OUT
  455.   # Broadcast packets.
  456.   iptables -A BAD_NET_OUT -d $IPBCAST_NET -j TGT_ATTACK_OUT
  457.   # Packets with spoofed source address.
  458.   iptables -A BAD_NET_OUT -s ! $IPADDR_NET -j TGT_ILLEGAL_OUT
  459.   # Packets heading for the LAN private network.
  460.   iptables -A BAD_NET_OUT -d $PRIVATE_LAN -j TGT_ILLEGAL_OUT
  461. }
  462. #-------------------------------------------------------------------------------
  463. # Install BAD_LAN_IN Chain
  464. # Filters out bad traffic originating from the local private network.
  465. install_bad_lan_in() {
  466.   iptables -N BAD_LAN_IN
  467.   iptables -A INPUT -i $IFACE_LAN -j BAD_LAN_IN
  468.   # Ping broadcasts.
  469.   iptables -A BAD_LAN_IN -d $IPBCAST_LAN -p icmp -j TGT_ATTACK_IN
  470.   # Packets pretending to be from this address (that weren't actually broadcast
  471.   # by this host).
  472.   iptables -A BAD_LAN_IN -s $IPADDR_LAN -d ! $IPBCAST_LAN -j TGT_ILLEGAL_IN
  473.   # Packets pretending to be from a network other than the LAN network.
  474.   iptables -A BAD_LAN_IN -s ! $IPNETW_LAN -j TGT_ILLEGAL_IN
  475. }
  476. #-------------------------------------------------------------------------------
  477. # Install BAD_LAN_OUT Chain
  478. # Filters out bad traffic destined for the local private network.
  479. install_bad_lan_out() {
  480.   iptables -N BAD_LAN_OUT
  481.   iptables -A OUTPUT -o $IFACE_LAN -j BAD_LAN_OUT
  482.   # Ping broadcasts.
  483.   iptables -A BAD_LAN_OUT -d $IPBCAST_LAN -p icmp -j TGT_ATTACK_OUT
  484.   # Packets heading for a network other than the LAN network.
  485.   iptables -A BAD_LAN_OUT -d ! $IPNETW_LAN -j TGT_ILLEGAL_OUT
  486. }
  487. #-------------------------------------------------------------------------------
  488. # Install ICMP_ANY_IN Chain
  489. # Selectively accepts ICMP messages originating from any connected network.
  490. install_icmp_any_in() {
  491.   iptables -N ICMP_ANY_IN
  492.   iptables -A INPUT -p icmp -j ICMP_ANY_IN
  493.   # Inbound ping (echo request) messages.
  494.   iptables -A ICMP_ANY_IN -p icmp --icmp-type $ICMP_PING -m state --state NEW \
  495.     -j TGT_ACCEPT_IN
  496.   # Inbound pong (echo reply) messages from previous outbound ping queries.
  497.   iptables -A ICMP_ANY_IN -p icmp --icmp-type $ICMP_PONG -m state \
  498.     --state ESTABLISHED -j TGT_ACCEPT_IN
  499.   # Inbound time exceeded messages from previous outbound queries
  500.   # (e.g. traceroute).
  501.   iptables -A ICMP_ANY_IN -p icmp --icmp-type $ICMP_TIME_EXCEEDED -m state \
  502.     --state RELATED -j TGT_ACCEPT_IN
  503.   # Inbound unreachable messages from previous outbound queries.
  504.   iptables -A ICMP_ANY_IN -p icmp --icmp-type $ICMP_UNREACHABLE -m state \
  505.     --state RELATED -j TGT_ACCEPT_IN
  506. }
  507. #-------------------------------------------------------------------------------
  508. # Install ICMP_ANY_OUT Chain
  509. # Selectively accepts ICMP messages destined for any connected network.
  510. install_icmp_any_out() {
  511.   iptables -N ICMP_ANY_OUT
  512.   iptables -A OUTPUT -p icmp -j ICMP_ANY_OUT
  513.   # Outbound ping (echo request) messages.
  514.   iptables -A ICMP_ANY_OUT -p icmp --icmp-type $ICMP_PING -m state --state NEW \
  515.     -j TGT_ACCEPT_OUT
  516.   # Outbound pong (echo reply) messages from previous inbound ping queries.
  517.   iptables -A ICMP_ANY_OUT -p icmp --icmp-type $ICMP_PONG -m state \
  518.     --state ESTABLISHED -j TGT_ACCEPT_OUT
  519.   # Outbound time exceeded messages from previous inbound queries
  520.   # (e.g. traceroute).
  521.   iptables -A ICMP_ANY_OUT -p icmp --icmp-type $ICMP_TIME_EXCEEDED -m state \
  522.     --state RELATED -j TGT_ACCEPT_OUT
  523.   # Outbound unreachable messages from previous inbound queries.
  524.   iptables -A ICMP_ANY_OUT -p icmp --icmp-type $ICMP_UNREACHABLE -m state \
  525.     --state RELATED -j TGT_ACCEPT_OUT
  526. }
  527. #-------------------------------------------------------------------------------
  528. # Install TCP_LAN_IN Chain
  529. # Selectively accepts TCP segments originating from the local private network.
  530. install_tcp_lan_in() {
  531.   iptables -N TCP_LAN_IN
  532.   iptables -A INPUT -i $IFACE_LAN -p tcp -j TCP_LAN_IN
  533.   # Inbound Secure SHell connections.
  534.   iptables -A TCP_LAN_IN -p tcp --dport $PORT_SSH -m state \
  535.     --state NEW,ESTABLISHED -j TGT_ACCEPT_IN
  536. }
  537. #-------------------------------------------------------------------------------
  538. # Install TCP_LAN_OUT Chain
  539. # Selectively accepts TCP segments destined for the local private network.
  540. install_tcp_lan_out() {
  541.   iptables -N TCP_LAN_OUT
  542.   iptables -A OUTPUT -o $IFACE_LAN -p tcp -j TCP_LAN_OUT
  543.   # Inbound Secure SHell connections.
  544.   iptables -A TCP_LAN_OUT -p tcp --sport $PORT_SSH -m state \
  545.     --state ESTABLISHED -j TGT_ACCEPT_OUT
  546. }
  547. #-------------------------------------------------------------------------------
  548. # Install TCP_NET_IN Chain
  549. # Selectively accepts TCP segments originating from the public internet.
  550. install_tcp_net_in() {
  551.   iptables -N TCP_NET_IN
  552.   iptables -A INPUT -i $IFACE_NET -p tcp -j TCP_NET_IN
  553.   # Outbound HTTP connections.
  554.   iptables -A TCP_NET_IN -p tcp --sport $PORT_HTTP -m state \
  555.     --state ESTABLISHED -j TGT_ACCEPT_IN
  556.   # Outbound RSYNC connections.
  557.   iptables -A TCP_NET_IN -p tcp --sport $PORT_RSYNC -m state \
  558.     --state ESTABLISHED -j TGT_ACCEPT_IN
  559.   # Outbound passive FTP connections.
  560.   iptables -A TCP_NET_IN -p tcp --sport $PORT_FTP_CMD -m state \
  561.     --state ESTABLISHED -j TGT_ACCEPT_IN
  562.   iptables -A TCP_NET_IN -p tcp --sport $PORTS_UNPRIV --dport $PORTS_UNPRIV \
  563.     -m state --state ESTABLISHED -j TGT_ACCEPT_IN
  564. }
  565. #-------------------------------------------------------------------------------
  566. # Install TCP_NET_OUT Chain
  567. # Selectively accepts TCP segments destined for the public internet.
  568. install_tcp_net_out() {
  569.   iptables -N TCP_NET_OUT
  570.   iptables -A OUTPUT -o $IFACE_NET -p tcp -j TCP_NET_OUT
  571.   # Outbound HTTP connections.
  572.   iptables -A TCP_NET_OUT -p tcp --dport $PORT_HTTP -m state \
  573.     --state NEW,ESTABLISHED -j TGT_ACCEPT_OUT
  574.   # Outbound RSYNC connections.
  575.   iptables -A TCP_NET_OUT -p tcp --dport $PORT_RSYNC -m state \
  576.     --state NEW,ESTABLISHED -j TGT_ACCEPT_OUT
  577.   # Outbound passive FTP connections.
  578.   iptables -A TCP_NET_OUT -p tcp --dport $PORT_FTP_CMD -m state \
  579.     --state NEW,ESTABLISHED -j TGT_ACCEPT_OUT
  580.   iptables -A TCP_NET_OUT -p tcp --dport $PORTS_UNPRIV --sport $PORTS_UNPRIV \
  581.     -m state --state ESTABLISHED,RELATED -j TGT_ACCEPT_OUT
  582. }
  583. #-------------------------------------------------------------------------------
  584. # Install TCP_ANY_IN Chain
  585. # Selectively accepts TCP segments originating from the any connected network.
  586. install_tcp_any_in() {
  587.   iptables -N TCP_ANY_IN
  588.   iptables -A INPUT -p tcp -j TCP_ANY_IN
  589.   # Ignore annoying MS domain service gunk.
  590.   iptables -A TCP_ANY_IN -p tcp --dport $PORT_MSDS -j TGT_DISCARD_IN
  591. }
  592. #-------------------------------------------------------------------------------
  593. # Install TCP_ANY_OUT Chain
  594. # Selectively accepts TCP segments destined for the public internet.
  595. install_tcp_any_out() {
  596.   iptables -N TCP_ANY_OUT
  597.   iptables -A OUTPUT -p tcp -j TCP_ANY_OUT
  598.   # Ignore any attempts to contact DNS servers through TCP.
  599.   iptables -A TCP_ANY_OUT -p tcp --dport $PORT_DNS -j TGT_DISCARD_OUT
  600. }
  601. #-------------------------------------------------------------------------------
  602. # Install UDP_LAN_IN Chain
  603. # Selectively accepts UDP segments originating from the local private network.
  604. install_udp_lan_in() {
  605.   iptables -N UDP_LAN_IN
  606.   iptables -A INPUT -i $IFACE_LAN -p udp -j UDP_LAN_IN
  607.   # Inbound DNS queries.
  608.   iptables -A UDP_LAN_IN -p udp --dport $PORT_DNS -m state \
  609.     --state NEW,ESTABLISHED -j TGT_ACCEPT_IN
  610.   # Inbound NTP queries.
  611.   iptables -A UDP_LAN_IN -p udp --dport $PORT_NTP -m state \
  612.     --state NEW,ESTABLISHED -j TGT_ACCEPT_IN
  613.   # Ignore broadcast traffic for Samba.
  614.   iptables -A UDP_LAN_IN -p udp -m multiport --ports $PORT_MSNBNS,$PORT_MSNBDG \
  615.     -j TGT_DISCARD_IN
  616. }
  617. #-------------------------------------------------------------------------------
  618. # Install UDP_LAN_OUT Chain
  619. # Selectively accepts UDP datagrams destined for the local private network.
  620. install_udp_lan_out() {
  621.   iptables -N UDP_LAN_OUT
  622.   iptables -A OUTPUT -o $IFACE_LAN -p udp -j UDP_LAN_OUT
  623.   # Inbound DNS queries.
  624.   iptables -A UDP_LAN_OUT -p udp --sport $PORT_DNS -m state \
  625.     --state ESTABLISHED -j TGT_ACCEPT_OUT
  626.   # Inbound NTP queries.
  627.   iptables -A UDP_LAN_OUT -p udp --sport $PORT_NTP -m state \
  628.     --state ESTABLISHED -j TGT_ACCEPT_OUT
  629. }
  630. #-------------------------------------------------------------------------------
  631. # Install UDP_NET_IN Chain
  632. # Selectively accepts UDP segments originating from the public internet.
  633. install_udp_net_in() {
  634.   iptables -N UDP_NET_IN
  635.   iptables -A INPUT -i $IFACE_NET -p udp -j UDP_NET_IN
  636.   # Outbound DNS queries to primary server.
  637.   iptables -A UDP_NET_IN -s $DNS_NET_1 -p udp --sport $PORT_DNS -m state \
  638.     --state ESTABLISHED -j TGT_ACCEPT_IN
  639.   # Outbound DNS queries to secondary server.
  640.   iptables -A UDP_NET_IN -s $DNS_NET_2 -p udp --sport $PORT_DNS -m state \
  641.     --state ESTABLISHED -j TGT_ACCEPT_IN
  642.   # Outbound NTP queries.
  643.   iptables -A UDP_NET_IN -p udp --sport $PORT_NTP -m state \
  644.     --state ESTABLISHED -j TGT_ACCEPT_IN
  645. }
  646. #-------------------------------------------------------------------------------
  647. # Install UDP_NET_OUT Chain
  648. # Selectively accepts UDP datagrams destined for the public internet.
  649. install_udp_net_out() {
  650.   iptables -N UDP_NET_OUT
  651.   iptables -A OUTPUT -o $IFACE_NET -p udp -j UDP_NET_OUT
  652.   
  653.   # Outbound DNS queries to primary server.
  654.   iptables -A UDP_NET_OUT -d $DNS_NET_1 -p udp --dport $PORT_DNS -m state \
  655.     --state NEW,ESTABLISHED -j TGT_ACCEPT_OUT
  656.   # Outbound DNS queries to secondary server.
  657.   iptables -A UDP_NET_OUT -d $DNS_NET_2 -p udp --dport $PORT_DNS -m state \
  658.     --state NEW,ESTABLISHED -j TGT_ACCEPT_OUT
  659.   # Outbound NTP queries.
  660.   iptables -A UDP_NET_OUT -p udp --dport $PORT_NTP -m state \
  661.     --state NEW,ESTABLISHED -j TGT_ACCEPT_OUT
  662.   # Outbound traceroute queries.
  663.   iptables -A UDP_NET_OUT -p udp --dport $PORTS_TRACEROUTE -m state \
  664.     --state NEW -j TGT_ACCEPT_OUT
  665. }
  666. #-------------------------------------------------------------------------------
  667. # Install FORWARD_IN Chain
  668. # Filters out bad traffic, and selectively accepts forwarded connections,
  669. # destined for the local private network.
  670. install_forward_in() {
  671.   iptables -N FORWARD_IN
  672.   iptables -A FORWARD -i $IFACE_NET -o $IFACE_LAN -j FORWARD_IN
  673.   # Inbound filter rules.
  674.   iptables -A FORWARD_IN -p tcp -j BAD_TCP_ANY_IN
  675.   iptables -A FORWARD_IN -j BAD_NET_IN
  676.   iptables -A FORWARD_IN -j BAD_ANY_IN
  677.   # Inbound established connections.
  678.   iptables -A FORWARD_IN -m state --state ESTABLISHED,RELATED -j TGT_ACCEPT_IN
  679.   # Inbound DNAT new connections.
  680.   iptables -A FORWARD_IN -p tcp -d $HTTP_LAN --dport $PORT_HTTP -m state \
  681.     --state NEW -j TGT_ACCEPT_IN
  682.   iptables -A FORWARD_IN -p tcp -d $HTTPS_LAN --dport $PORT_HTTPS -m state \
  683.     --state NEW -j TGT_ACCEPT_IN
  684.   iptables -A FORWARD_IN -p tcp -d $SMTP_LAN --dport $PORT_SMTP -m state \
  685.     --state NEW -j TGT_ACCEPT_IN
  686.   iptables -A FORWARD_IN -p tcp -d $SSH_LAN --dport $PORT_SSH -m state \
  687.     --state NEW -j TGT_ACCEPT_IN
  688.   iptables -A FORWARD_IN -p tcp -d $PFTP_LAN --dport $PORT_FTP_CMD -m state \
  689.     --state NEW -j TGT_ACCEPT_IN
  690.   # TODO: Delete once forwarding rules are figured out.
  691.   iptables -A FORWARD_IN -j TGT_WATCH_IN
  692. }
  693. #-------------------------------------------------------------------------------
  694. # Install FORWARD_OUT Chain
  695. # Filters out bad traffic, and selectively accepts forwarded connections,
  696. # destined for the public internet.
  697. install_forward_out() {
  698.   iptables -N FORWARD_OUT
  699.   iptables -A FORWARD -i $IFACE_LAN -o $IFACE_NET -j FORWARD_OUT
  700.   # Outbound filter rules.
  701.   iptables -A FORWARD_OUT -p tcp -j BAD_TCP_ANY_IN
  702.   iptables -A FORWARD_OUT -j BAD_LAN_IN
  703.   iptables -A FORWARD_OUT -j BAD_ANY_IN
  704.   # Outbound new or established connections.
  705.   iptables -A FORWARD_OUT -m state --state NEW,ESTABLISHED -j TGT_ACCEPT_OUT
  706.   
  707.   # Catch the occasional TCP request that slips by the previous rule.
  708.   iptables -A FORWARD_OUT -p tcp --syn -j TGT_ACCEPT_OUT
  709.   # TODO: Delete once forwarding rules are figured out.
  710.   iptables -A FORWARD_OUT -j TGT_WATCH_OUT
  711. }
  712. #-------------------------------------------------------------------------------
  713. # Install SNAT Rules
  714. # Handle SNAT (static IP masquerade) for the local private network hosts.
  715. install_snat() {
  716.   iptables -t nat -A POSTROUTING -o $IFACE_NET -s $IPNETW_LAN -j SNAT \
  717.     --to-source ${IPADDR_NET%/*}
  718. }
  719. #-------------------------------------------------------------------------------
  720. # Install DNAT Rules
  721. # Handle DNAT (inbound port forwarding) to local private network hosts.
  722. install_dnat() {
  723.   # Inbound SMTP connections.
  724.   iptables -t nat -A PREROUTING -i $IFACE_NET -p tcp -d $IPADDR_NET \
  725.     --dport $PORT_SMTP -j DNAT --to-destination $SMTP_LAN:$PORT_SMTP
  726.   # Inbound HTTP connections.
  727.   iptables -t nat -A PREROUTING -i $IFACE_NET -p tcp -d $IPADDR_NET \
  728.     --dport $PORT_HTTP -j DNAT --to-destination $HTTP_LAN:$PORT_HTTP
  729.   # Inbound HTTPS connections.
  730.   iptables -t nat -A PREROUTING -i $IFACE_NET -p tcp -d $IPADDR_NET \
  731.     --dport $PORT_HTTPS -j DNAT --to-destination $HTTPS_LAN:$PORT_HTTPS
  732.   # Inbound SSH connections.
  733.   iptables -t nat -A PREROUTING -i $IFACE_NET -p tcp -d $IPADDR_NET \
  734.     --dport $PORT_SSH -j DNAT --to-destination $SSH_LAN:$PORT_SSH
  735. }
  736. #-------------------------------------------------------------------------------
  737. # Install TOS Mangle Rules
  738. # Configure Type of Service rules for mangle table:
  739. #   Minimize-Delay       16 (0x10)
  740. #   Maximize-Throughput   8 (0x08)
  741. #   Maximize-Reliability  4 (0x04)
  742. #   Minimize-Cost         2 (0x02)
  743. #   Normal-Service        0 (0x00)
  744. # Use with caution: Type of Service isn't widely implemented in routers and may
  745. # actually cause problems.
  746. install_mangle() {
  747.   iptables -t mangle -N MANGLE_TOS
  748.   # Maximize throughput.
  749.   iptables -t mangle -A MANGLE_TOS -p tcp --dport $PORT_FTP_DATA -j TOS --set-tos 8
  750.   iptables -t mangle -A MANGLE_TOS -p tcp --dport $PORT_HTTP -j TOS --set-tos 8
  751.   iptables -t mangle -A MANGLE_TOS -p tcp --dport $PORT_HTTPS -j TOS --set-tos 8
  752.   # Minimize delay.
  753.   iptables -t mangle -A MANGLE_TOS -p tcp --dport $PORT_FTP_CMD -j TOS --set-tos 16
  754.   iptables -t mangle -A MANGLE_TOS -p tcp --dport $PORT_SSH -j TOS --set-tos 16
  755.   iptables -t mangle -A MANGLE_TOS -p tcp --dport $PORT_SMTP -j TOS --set-tos 16
  756.   iptables -t mangle -A MANGLE_TOS -p tcp --dport $PORT_DNS -j TOS --set-tos 16
  757.   # Install MANGLE_TOS on OUTPUT chain.
  758.   iptables -t mangle -A OUTPUT -o $IFACE_NET -j MANGLE_TOS
  759.   
  760.   # Log and drop segments with no flag bits set (NULL packets).
  761.   iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL NONE -m limit \
  762.     --limit 5/minute -j LOG --log-level $LOG_LEVEL_SCAN \
  763.     --log-prefix "FW_SCAN_MANGLE: " --log-tcp-options --log-ip-options
  764.   iptables -t mangle -A PREROUTING -p tcp --tcp-flags ALL NONE -j DROP

  765.   # Install MANGLE_TOS on PREROUTING chain.
  766.   iptables -t mangle -A PREROUTING -i $IFACE_LAN -j MANGLE_TOS
  767. }
  768. #-------------------------------------------------------------------------------
  769. # Initialize Firewall
  770. init_fw() {
  771.   config_kernel
  772.   flush
  773.   set_policy ${1:-DROP}
  774. }
  775. #-------------------------------------------------------------------------------
  776. # Install Firewall Rules
  777. install_fw() {
  778.   # Install the target chains.
  779.   install_targets
  780.   # Install local loopback interface rules.
  781.   install_lo_in_out
  782.   # Special case: filter TCP packets seperately.
  783.   install_bad_tcp_any_in
  784.   
  785.   # Filter out bad packets: least to most specific.
  786.   install_bad_any_in
  787.   install_bad_any_out
  788.   install_bad_net_in
  789.   install_bad_net_out
  790.   install_bad_lan_in
  791.   install_bad_lan_out
  792.   
  793.   # Apply ICMP protocol rules: most to least specific.
  794.   install_icmp_any_in
  795.   install_icmp_any_out
  796.   
  797.   # Apply TCP protocol rules: most to least specific.
  798.   install_tcp_lan_in
  799.   install_tcp_lan_out
  800.   install_tcp_net_in
  801.   install_tcp_net_out
  802.   install_tcp_any_in
  803.   install_tcp_any_out
  804.   
  805.   # Apply UDP protocol rules: most to least specific.
  806.   install_udp_lan_in
  807.   install_udp_lan_out
  808.   install_udp_net_in
  809.   install_udp_net_out
  810.   # Log anything that falls off the end.
  811.   iptables -A INPUT -j TGT_UNKNOWN_IN
  812.   iptables -A OUTPUT -j TGT_UNKNOWN_OUT
  813.   # Apply forwarding rules.
  814.   install_forward_in
  815.   install_forward_out
  816.   # Apply NAT rules.
  817.   install_snat
  818.   install_dnat
  819.   
  820.   # Apply mangle TOS rules. Enable only if you understand TOS better than I do.
  821.   #install_mangle
  822. }
  823. #-------------------------------------------------------------------------------
  824. # Interpret Command-line Parameters
  825. case "$1" in
  826. opensesame)
  827.   # DANGER! DANGER! DANGER!
  828.   # Initialize the firewall and set the default policy to ACCEPT, allowing all
  829.   # traffic through unimpeded.
  830.   init_fw ACCEPT
  831.   ;;
  832. start | restart)
  833.   # start==restart since there's no daemon process to stop or signal.
  834.   init_fw
  835.   install_fw
  836.   ;;
  837. stop)
  838.   # Initialize the firewall and set the default policy to DROP, effectively
  839.   # stopping all traffic.
  840.   init_fw
  841.   ;;
  842. *)
  843.   echo $"Usage: $0 {start|stop|restart}"
  844.   exit 1
  845. esac
  846. #-------------------------------------------------------------------------------
  847. # All Done
  848. exit 0
  849. ################################################################################
  850. # EOF
  851. ################################################################################

复制代码
routeros
发表于 2007-6-8 21:11:57 | 显示全部楼层
你自己搞的哪个怎么样了?
routeros
回复

使用道具 举报

 楼主| 发表于 2007-6-9 03:13:10 | 显示全部楼层
自己那个暂时停下了~~~~
你没看我差不多 24 小时都在线么?
routeros
回复

使用道具 举报

发表于 2007-6-10 07:54:20 | 显示全部楼层

挺不错的

为了效率,自定义了链?还对某些应用作了加速。
routeros
回复

使用道具 举报

 楼主| 发表于 2007-6-10 08:57:39 | 显示全部楼层
自定义链几乎是必须的,主要还是为了便于管理,我最初也是不使用自定义链的,前年才改过来。
routeros
回复

使用道具 举报

发表于 2007-6-11 17:15:09 | 显示全部楼层
那么长的脚本,包传递的效率岂不是很低?
routeros
回复

使用道具 举报

 楼主| 发表于 2007-6-12 08:34:58 | 显示全部楼层
routeros
回复

使用道具 举报

发表于 2007-6-12 14:38:30 | 显示全部楼层
顶哦!!!!
routeros
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|手机版|小黑屋|软路由 ( 渝ICP备15001194号-1|渝公网安备 50011602500124号 )

GMT+8, 2024-12-24 03:15 , Processed in 0.093274 second(s), 4 queries , Gzip On, Redis On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表