- #!/bin/sh
- #
- # Generated iptables firewall script for the Linux 2.4 kernel
- # Script generated by Easy Firewall Generator for IPTables
- # copyright 2002 Timothy Scott Morizot
- #
- # Redhat chkconfig comments - firewall applied early,
- # removed late
- # chkconfig: 2345 08 92
- # description: This script applies or removes iptables firewall rules
- #
- # This generator is primarily designed for RedHat installations,
- # although it should be adaptable for others.
- #
- # It can be executed with the typical start and stop arguments.
- # If used with stop, it will stop after flushing the firewall.
- # The save and restore arguments will save or restore the rules
- # from the /etc/sysconfig/iptables file.
- # Redhat installation instructions
- #
- # 1. Ensure that ipchains will not automatically start.
- # chkconfig --level 0123456 ipchains off
- # This will make sure that the ipchains init.d script
- # is not linked to an S file in any of the rc directories.
- #
- # 2. Stop ipchains if it's running.
- # service ipchains stop
- #
- # 3. Execute lsmod to see if the ipchains kernel module is still loaded.
- # If it is, use rmmod to unload it. -- rmmod ipchains
- #
- # 4. Have the system link the iptables init.d startup script into run states
- # 2, 3, and 5.
- # chkconfig --level 235 iptables on
- #
- # 5. Save this script and execute it to load the ruleset from this file.
- # You may need to run the dos2unix command on it to remove carraige returns.
- #
- # 6. Save the ruleset to /etc/sysconfig/iptables. This can be done two ways.
- # service iptables save
- # iptables-save > /etc/sysconfig/iptables
- #
- # 7. The ruleset will be restored by the /etc/init.d/iptables script on boot.
- #
- # 8. Alternatively, save the /etc/init.d/iptables script and copy this script
- # to /etc/init.d/iptables. It accepts stop, start, save, and restore
- # arguments.
- #
- # NOTE: The /etc/init.d/iptables script can be modified to run this script
- # instead. If you do so, save a copy so you can reapply your modifications
- # after upgrading the iptables package. The advantage of using this script for
- # the ongoing operation of the firewall is it gives you greater control over
- # the modules and rulesets used. The above is simpler, however.
- ###############################################################################
- #
- # Local Settings
- #
- # sysctl location. If set, it will use sysctl to adjust the kernel parameters.
- # If this is set to the empty string (or is unset), the use of sysctl
- # is disabled.
- SYSCTL="/sbin/sysctl -w"
- # To echo the value directly to the /proc file instead
- # SYSCTL=""
- # IPTables Location - adjust if needed
- IPT="/sbin/iptables"
- IPTS="/sbin/iptables-save"
- IPTR="/sbin/iptables-restore"
- # Internet Interface
- INET_IFACE="eth0"
- #INET_ADDRESS="192.168.2.150"
- # Localhost Interface
- LO_IFACE="lo"
- LO_IP="127.0.0.1"
- ###############################################################################
- #
- # Load Modules
- #
- echo "Loading kernel modules ..."
- # You should uncomment the line below and run it the first time just to
- # ensure all kernel module dependencies are OK. There is no need to run
- # every time, however.
- # /sbin/depmod -a
- # Unless you have kernel module auto-loading disabled, you should not
- # need to manually load each of these modules. Other than ip_tables,
- # ip_conntrack, and some of the optional modules, I've left these
- # commented by default. Uncomment if you have any problems or if
- # you have disabled module autoload. Note that some modules must
- # be loaded by another kernel module.
- # core netfilter module
- /sbin/modprobe ip_tables
- # the stateful connection tracking module
- /sbin/modprobe ip_conntrack
- # filter table module
- /sbin/modprobe iptable_filter
- # mangle table module
- # /sbin/modprobe iptable_mangle
- # nat table module
- # /sbin/modprobe iptable_nat
- # LOG target module
- /sbin/modprobe ipt_LOG
- # This is used to limit the number of packets per sec/min/hr
- /sbin/modprobe ipt_limit
- # masquerade target module
- # /sbin/modprobe ipt_MASQUERADE
- # filter using owner as part of the match
- # /sbin/modprobe ipt_owner
- # REJECT target drops the packet and returns an ICMP response.
- # The response is configurable. By default, connection refused.
- # /sbin/modprobe ipt_REJECT
- # This target allows packets to be marked in the mangle table
- # /sbin/modprobe ipt_mark
- # This target affects the TCP MSS
- # /sbin/modprobe ipt_tcpmss
- # This match allows multiple ports instead of a single port or range
- # /sbin/modprobe multiport
- # This match checks against the TCP flags
- /sbin/modprobe ipt_state
- # This match catches packets with invalid flags
- /sbin/modprobe ipt_unclean
- # The ftp nat module is required for non-PASV ftp support
- #/sbin/modprobe ip_nat_ftp
- # the module for full ftp connection tracking
- #/sbin/modprobe ip_conntrack_ftp
- # the module for full irc connection tracking
- #/sbin/modprobe ip_conntrack_irc
- ###############################################################################
- #
- # Kernel Parameter Configuration
- #
- # Required to enable IPv4 forwarding.
- # Redhat users can try setting FORWARD_IPV4 in /etc/sysconfig/network to true
- #if [ "$SYSCTL" = "" ]
- #then
- # echo "1" > /proc/sys/net/ipv4/ip_forward
- #else
- # $SYSCTL net.ipv4.ip_forward="1"
- #fi
- # This enables dynamic address hacking.
- # This may help if you have a dynamic IP address \(e.g. slip, ppp, dhcp\).
- if [ "$SYSCTL" = "" ]
- then
- echo "1" > /proc/sys/net/ipv4/ip_dynaddr
- else
- $SYSCTL net.ipv4.ip_dynaddr="1"
- fi
- # This enables source validation by reversed path according to RFC1812.
- # In other words, did the response packet originate from the same interface
- # through which the source packet was sent? It's recommended for single-homed
- # systems and routers on stub networks. Since those are the configurations
- # this firewall is designed to support, I turn it on by default.
- # Turn it off if you use multiple NICs connected to the same network.
- if [ "$SYSCTL" = "" ]
- then
- echo "1" > /proc/sys/net/ipv4/conf/all/rp_filter
- else
- $SYSCTL net.ipv4.conf.all.rp_filter="1"
- fi
- # This option allows a subnet to be firewalled with a single IP address.
- # It's used to build a DMZ. Since that's not a focus of this firewall
- # script, it's not enabled by default, but is included for reference.
- # See: [url]http://www.sjdjweis.com/linux/proxyarp/[/url]
- #if [ "$SYSCTL" = "" ]
- #then
- # echo "1" > /proc/sys/net/ipv4/conf/all/proxy_arp
- #else
- # $SYSCTL net.ipv4.conf.all.proxy_arp="1"
- #fi
- ###############################################################################
- #
- # Flush Any Existing Rules or Chains
- #
- echo "Flushing Tables ..."
- # Reset Default Policies
- $IPT -P INPUT ACCEPT
- $IPT -P FORWARD ACCEPT
- $IPT -P OUTPUT ACCEPT
- $IPT -t nat -P PREROUTING ACCEPT
- $IPT -t nat -P POSTROUTING ACCEPT
- $IPT -t nat -P OUTPUT ACCEPT
- $IPT -t mangle -P PREROUTING ACCEPT
- $IPT -t mangle -P OUTPUT ACCEPT
- # Flush all rules
- $IPT -F
- $IPT -t nat -F
- $IPT -t mangle -F
- # Erase all non-default chains
- $IPT -X
- $IPT -t nat -X
- $IPT -t mangle -X
- ###############################################################################
- #
- # Rules Configuration
- #
- ###############################################################################
- #
- # Filter Table
- #
- ###############################################################################
- # Set Policies
- $IPT -P INPUT DROP
- $IPT -P OUTPUT DROP
- $IPT -P FORWARD DROP
- ###############################################################################
- #
- # User-Specified Chains
- #
- # Create user chains to reduce the number of rules each packet
- # must traverse.
- echo "Create and populate custom rule chains ..."
- # Create a chain to filter INVALID packets
- $IPT -N bad_packets
- # Create another chain to filter bad tcp packets
- $IPT -N bad_tcp_packets
- # Create separate chains for icmp, tcp (incoming and outgoing),
- # and incoming udp packets.
- $IPT -N icmp_packets
- # Used for UDP packets inbound from the Internet
- $IPT -N udp_inbound
- # Used to block outbound UDP services from internal network
- # Default to allow all
- $IPT -N udp_outbound
- # Used to allow inbound services if desired
- # Default fail except for established sessions
- $IPT -N tcp_inbound
- # Used to block outbound services from internal network
- # Default to allow all
- $IPT -N tcp_outbound
- ###############################################################################
- #
- # Populate User Chains
- #
- # bad_packets chain
- #
- # Drop INVALID packets immediately
- $IPT -A bad_packets -p ALL -m state --state INVALID -j LOG \
- --log-prefix "Invalid packet:"
- $IPT -A bad_packets -p ALL -m state --state INVALID -j DROP
- # Then check the tcp packets for additional problems
- $IPT -A bad_packets -p tcp -j bad_tcp_packets
- # All good, so return
- $IPT -A bad_packets -p ALL -j RETURN
- # bad_tcp_packets chain
- #
- # All tcp packets will traverse this chain.
- # Every new connection attempt should begin with
- # a syn packet. If it doesn't, it is likely a
- # port scan. This drops packets in state
- # NEW that are not flagged as syn packets.
- $IPT -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j LOG \
- --log-prefix "New not syn:"
- $IPT -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j DROP
- # All good, so return
- $IPT -A bad_tcp_packets -p tcp -j RETURN
- # icmp_packets chain
- #
- # This chain is for inbound (from the Internet) icmp packets only.
- # Type 8 (Echo Request) is not accepted by default
- # Enable it if you want remote hosts to be able to reach you.
- # 11 (Time Exceeded) is the only one accepted
- # that would not already be covered by the established
- # connection rule. Applied to INPUT on the external interface.
- #
- # See: [url]http://www.ee.siue.edu/~rwalden/networking/icmp.html[/url]
- # for more info on ICMP types.
- #
- # Note that the stateful settings allow replies to ICMP packets.
- # These rules allow new packets of the specified types.
- # Echo - uncomment to allow your system to be pinged.
- # $IPT -A icmp_packets -p ICMP -s 0/0 --icmp-type 8 -j ACCEPT
- # Time Exceeded
- #$IPT -A icmp_packets -p ICMP -s 0/0 --icmp-type 11 -j ACCEPT
- # Not matched, so return so it will be logged
- #$IPT -A icmp_packets -p ICMP -j RETURN
- # TCP & UDP
- # Identify ports at:
- # [url]http://www.chebucto.ns.ca/~rakerman/port-table.html[/url]
- # [url]http://www.iana.org/assignments/port-numbers[/url]
- # udp_inbound chain
- #
- # This chain describes the inbound UDP packets it will accept.
- # It's applied to INPUT on the external or Internet interface.
- # Note that the stateful settings allow replies.
- # These rules are for new requests.
- # It drops netbios packets (windows) immediately without logging.
- # Drop netbios calls
- # Please note that these rules do not really change the way the firewall
- # treats netbios connections. Connections from the localhost and
- # internal interface (if one exists) are accepted by default.
- # Responses from the Internet to requests initiated by or through
- # the firewall are also accepted by default. To get here, the
- # packets would have to be part of a new request received by the
- # Internet interface. You would have to manually add rules to
- # accept these. I added these rules because some network connections,
- # such as those via cable modems, tend to be filled with noise from
- # unprotected Windows machines. These rules drop those packets
- # quickly and without logging them. This prevents them from traversing
- # the whole chain and keeps the log from getting cluttered with
- # chatter from Windows systems.
- #$IPT -A udp_inbound -p UDP -s 192.168.2.0/24 --destination-port 137 -j ACCEPT
- #$IPT -A udp_inbound -p UDP -s 192.168.2.0/24 --destination-port 138 -j ACCEPT
- # Not matched, so return for logging
- #$IPT -A udp_inbound -p UDP -j RETURN
- # udp_outbound chain
- #
- # This chain is used with a private network to prevent forwarding for
- # UDP requests on specific protocols. Applied to the FORWARD rule from
- # the internal network. Ends with an ACCEPT
- # No match, so ACCEPT
- #$IPT -A udp_outbound -p UDP -d 0/0 -j ACCEPT
- # tcp_inbound chain
- #
- # This chain is used to allow inbound connections to the
- # system/gateway. Use with care. It defaults to none.
- # It's applied on INPUT from the external or Internet interface.
- # Web Server
- # HTTP
- $IPT -A tcp_inbound -p TCP -s 0/0 --destination-port 80 -j ACCEPT
- # HTTPS (Secure Web Server)
- #$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port 443 -j ACCEPT
- # FTP Server (Control)
- $IPT -A tcp_inbound -p TCP -s 0/0 --destination-port 21 -j ACCEPT
- # FTP Client (Data Port for non-PASV transfers)
- $IPT -A tcp_inbound -p TCP -s 0/0 --source-port 20 -j ACCEPT
- # Passive FTP
- #
- # With passive FTP, the server provides a port to the client
- # and allows the client to initiate the connection rather
- # than initiating the connection with the client from the data port.
- # Web browsers and clients operating behind a firewall generally
- # use passive ftp transfers. A general purpose FTP server
- # will need to support them.
- #
- # However, by default an FTP server will select a port from the entire
- # range of high ports. It is not particularly safe to open all
- # high ports. Fortunately, that range can be restricted. This
- # firewall presumes that the range has been restricted to a specific
- # selected range. That range must also be configured in the ftp server.
- #
- # Instructions for specifying the port range for the wu-ftpd server
- # can be found here:
- # [url]http://www.wu-ftpd.org/man/ftpaccess.html[/url]
- # (See the passive ports option.)
- #
- # Instructions for the ProFTPD server can be found here:
- # [url]http://proftpd.linux.co.uk/localsite/Userguide/linked/x861.html[/url]
- $IPT -A tcp_inbound -p TCP -s 0/0 --destination-port 58000:64000 -j ACCEPT
- # sshd
- $IPT -A tcp_inbound -p TCP -s 0/0 --destination-port 22 -j ACCEPT
- # mysql
- #$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port 3306 -j ACCEPT
- #netbios
- #$IPT -A tcp_inbound -p TCP -s 192.168.2.0/24 --destination-port 139 -j ACCEPT
- # Not matched, so return so it will be logged
- $IPT -A tcp_inbound -p TCP -j RETURN
- # tcp_outbound chain
- #
- # This chain is used with a private network to prevent forwarding for
- # requests on specific protocols. Applied to the FORWARD rule from
- # the internal network. Ends with an ACCEPT
- # No match, so ACCEPT
- #$IPT -A tcp_outbound -p TCP -s 0/0 -j ACCEPT
- ###############################################################################
- #
- # INPUT Chain
- #
- echo "Process INPUT chain ..."
- # Allow all on localhost interface
- $IPT -A INPUT -p ALL -i lo -s 127.0.0.1 -j ACCEPT
- $IPT -A INPUT -p ALL -s 192.168.0.0/16 -j DROP
- $IPT -A INPUT -p ALL -s 10.0.0.0/8 -j DROP
- $IPT -A INPUT -p ALL -s 172.16.0.0/12 -j DROP
- $IPT -A INPUT -p ALL -s 127.0.0.0/8 -j DROP
- # Drop bad packets
- $IPT -A INPUT -p ALL -j bad_packets
- # DOCSIS compliant cable modems
- # Some DOCSIS compliant cable modems send IGMP multicasts to find
- # connected PCs. The multicast packets have the destination address
- # 224.0.0.1. You can accept them. If you choose to do so,
- # Uncomment the rule to ACCEPT them and comment the rule to DROP
- # them The firewall will drop them here by default to avoid
- # cluttering the log. The firewall will drop all multicasts
- # to the entire subnet (224.0.0.1) by default. To only affect
- # IGMP multicasts, change '-p ALL' to '-p 2'. Of course,
- # if they aren't accepted elsewhere, it will only ensure that
- # multicasts on other protocols are logged.
- # Drop them without logging.
- # The rule to accept the packets.
- # $IPT -A INPUT -p ALL -d 224.0.0.1 -j ACCEPT
- # Inbound Internet Packet Rules
- # Accept Established Connections
- $IPT -A INPUT -p ALL -i $INET_IFACE -m state --state ESTABLISHED,RELATED \
- -j ACCEPT
- # Route the rest to the appropriate user chain
- $IPT -A INPUT -p TCP -i $INET_IFACE -j tcp_inbound
- $IPT -A INPUT -p UDP -i $INET_IFACE -j udp_inbound
- #$IPT -A INPUT -p ICMP -i $INET_IFACE -j icmp_packets
- # Drop without logging broadcasts that get this far.
- # Cuts down on log clutter.
- # Comment this line if testing new rules that impact
- # broadcast protocols.
- #$IPT -A INPUT -p ALL -d 255.255.255.255 -j DROP
- # Log packets that still don't match
- $IPT -A INPUT -m limit --limit 1/second --limit-burst 1 -j LOG \
- --log-prefix "INPUT packet died: "
- $IPT -A INPUT -m limit --limit 1/second --limit-burst 1 -j DROP
- ###############################################################################
- #
- # FORWARD Chain
- #
- echo "Process FORWARD chain ..."
- # Used if forwarding for a private network
- ###############################################################################
- #
- # OUTPUT Chain
- #
- echo "Process OUTPUT chain ..."
- # Generally trust the firewall on output
- # However, invalid icmp packets need to be dropped
- # to prevent a possible exploit.
- $IPT -A OUTPUT -m state -p icmp --state INVALID -j DROP
- # Localhost
- #$IPT -A OUTPUT -p ALL -o $LO_IFACE -s 127.0.0.1 -j ACCEPT
- # To internet
- #$IPT -A OUTPUT -p ALL -o $INET_IFACE -j ACCEPT
- $IPT -A OUTPUT -p ALL -j ACCEPT
- # Log packets that still don't match
- #$IPT -A OUTPUT -m limit --limit 3/minute --limit-burst 3 -j LOG \
- # --log-prefix "OUTPUT packet died: "
复制代码
更多资讯查看bbs.be10.net与www.google.com
[ Last edited by iamok on 2003-5-7 at 12:04 AM ] |