
 #!/bin/sh
 #
 # rc.firewall-2.4
 FWVER=0.62
 #
 #               Initial SIMPLE IP Masquerade test for 2.4.x kernels
 #               using IPTABLES.  
 #
 #               Once IP Masquerading has been tested, with this simple 
 #               ruleset, it is highly recommended to use a stronger 
 #               IPTABLES ruleset either given later in this HOWTO or 
 #               from another reputable resource.
 #
 #
 #
 # Log:
 #       0.62 - Fixed a typo on the MASQ enable line that used eth0
 #              instead of $EXTIF
 #       0.61 - Changed the firewall to use variables for the internal
 #              and external interfaces.
 #       0.60 - 0.50 had a mistake where the ruleset had a rule to DROP
 #              all forwarded packets but it didn't have a rule to ACCEPT
 #              any packets to be forwarded either
 #            - Load the ip_nat_ftp and ip_conntrack_ftp modules by default
 #       0.50 - Initial draft
 #

 echo -e "\n\nLoading simple rc.firewall version $FWVER..\n"


 # The location of the 'iptables' program
 #
 #   If your Linux distribution came with a copy of iptables, most
 #   likely it is located in /sbin.  If you manually compiled 
 #   iptables, the default location is in /usr/local/sbin
 #
 # ** Please use the "whereis iptables" command to figure out 
 # ** where your copy is and change the path below to reflect 
 # ** your setup
 #
 IPTABLES=/sbin/iptables
 #IPTABLES=/usr/local/sbin/iptables


 #Setting the EXTERNAL and INTERNAL interfaces for the network
 #
 #  Each IP Masquerade network needs to have at least one
 #  external and one internal network.  The external network
 #  is where the natting will occur and the internal network
 #  should preferably be addressed with a RFC1918 private address
 #  scheme.
 #
 #  For this example, "eth0" is external and "eth1" is internal"
 #
 #  NOTE:  If this doesnt EXACTLY fit your configuration, you must 
 #         change the INET_IFACE or LAN_IFACE variables above. For example: 
 #
 #               INET_IFACE="ppp0" 
 #
 #            if you are a modem user.
 #
 LAN_IP="192.168.0.1/32"
 LAN_BCAST_ADRESS="192.168.0.255/32"
 LAN_IFACE="eth0"
 
 INET_IFACE="ppp0"
 
 LO_IFACE="lo"
 LO_IP="127.0.0.1/32"
 
 echo "   External Interface:  $INET_IFACE"
 echo "   Internal Interface:  $LAN_IFACE"


 #======================================================================
 #== No editing beyond this line is required for initial MASQ testing ==


 echo -en "   loading modules: "

 # Need to verify that all modules have all required dependencies
 #
 echo "  - Verifying that all kernel modules are ok"
 /sbin/depmod -a

 # With the new IPTABLES code, the core MASQ functionality is now either
 # modular or compiled into the kernel.  This HOWTO shows ALL IPTABLES
 # options as MODULES.  If your kernel is compiled correctly, there is
 # NO need to load the kernel modules manually.  
 #
 #  NOTE: The following items are listed ONLY for informational reasons.
 #        There is no reason to manual load these modules unless your
 #        kernel is either mis-configured or you intentionally disabled
 #        the kernel module autoloader.
 #

 # Upon the commands of starting up IP Masq on the server, the
 # following kernel modules will be automatically loaded:
 #
 # NOTE:  Only load the IP MASQ modules you need.  All current IP MASQ 
 #        modules are shown below but are commented out from loading.
 # ===============================================================

 #Load the main body of the IPTABLES module - "iptable"
 #  - Loaded automatically when the "iptables" command is invoked
 #
 #  - Loaded manually to clean up kernel auto-loading timing issues
 #
 echo -en "ip_tables, "
 /sbin/insmod ip_tables


 #Load the IPTABLES filtering module - "iptable_filter" 
 #  - Loaded automatically when filter policies are activated


 #Load the stateful connection tracking framework - "ip_conntrack"
 #
 # The conntrack  module in itself does nothing without other specific 
 # conntrack modules being loaded afterwards such as the "ip_conntrack_ftp"
 # module
 #
 #  - This module is loaded automatically when MASQ functionality is 
 #    enabled 
 #
 #  - Loaded manually to clean up kernel auto-loading timing issues
 #
 echo -en "ip_conntrack, "
 /sbin/insmod ip_conntrack


 #Load the FTP tracking mechanism for full FTP tracking
 #
 # Enabled by default -- insert a "#" on the next line to deactivate
 #
 echo -en "ip_conntrack_ftp, "
 /sbin/insmod ip_conntrack_ftp


 #Load the general IPTABLES NAT code - "iptable_nat"
 #  - Loaded automatically when MASQ functionality is turned on
 # 
 #  - Loaded manually to clean up kernel auto-loading timing issues
 #
 echo -en "iptable_nat, "
 /sbin/insmod iptable_nat


 #Loads the FTP NAT functionality into the core IPTABLES code
 # Required to support non-PASV FTP.
 #
 # Enabled by default -- insert a "#" on the next line to deactivate
 #
 echo -en "ip_nat_ftp, "
 /sbin/insmod ip_nat_ftp


 # Just to be complete, here is a list of the remaining kernel modules 
 # and their function.  Please note that several modules should be only
 # loaded by the correct master kernel module for proper operation.
 # --------------------------------------------------------------------
 #
 #    ipt_mark       - this target marks a given packet for future action.
 #                     This automatically loads the ipt_MARK module
 #
 #    ipt_tcpmss     - this target allows to manipulate the TCP MSS
 #                     option for braindead remote firewalls.
 #                     This automatically loads the ipt_TCPMSS module
 #
 #    ipt_limit      - this target allows for packets to be limited to
 #                     to many hits per sec/min/hr
 #
 #    ipt_multiport  - this match allows for targets within a range
 #                     of port numbers vs. listing each port individually
 echo -en "ipt_multiport, "
 /sbin/insmod ipt_multiport
 
 #
 #    ipt_state      - this match allows to catch packets with various
 #                     IP and TCP flags set/unset
 #
 #    ipt_unclean    - this match allows to catch packets that have invalid
 #                     IP/TCP flags set
 #
 #    iptable_filter - this module allows for packets to be DROPped, 
 #                     REJECTed, or LOGged.  This module automatically 
 #                     loads the following modules:
 #
 #                     ipt_LOG - this target allows for packets to be 
 #                               logged
 #
 #                     ipt_REJECT - this target DROPs the packet and returns 
 #                                  a configurable ICMP packet back to the 
 #                                  sender.
 echo -en "iptable_filter, "
 /sbin/insmod iptable_filter
 
 # 
 #    iptable_mangle - this target allows for packets to be manipulated
 #                     for things like the TCPMSS option, etc.
 echo -en "iptable_mangle, "
 /sbin/insmod iptable_mangle
 
 echo ".  Done loading modules."



 #CRITICAL:  Enable IP forwarding since it is disabled by default since
 #
 #           Redhat Users:  you may try changing the options in
 #                          /etc/sysconfig/network from:
 #
 #                       FORWARD_IPV4=false
 #                             to
 #                       FORWARD_IPV4=true
 #
 echo "   enabling forwarding.."
 echo "1" > /proc/sys/net/ipv4/ip_forward


 # Dynamic IP users:
 #
 #   If you get your IP address dynamically from SLIP, PPP, or DHCP, 
 #   enable this following option.  This enables dynamic-address hacking
 #   which makes the life with Diald and similar programs much easier.
 #
 echo "   enabling DynamicAddr.."
 echo "1" > /proc/sys/net/ipv4/ip_dynaddr


 # Enable simple IP forwarding and Masquerading
 #
 #  NOTE:  In IPTABLES speak, IP Masquerading is a form of SourceNAT or SNAT.
 #
 #  NOTE #2:  The following is an example for an internal LAN address in the
 #            192.168.0.x network with a 255.255.255.0 or a "24" bit subnet mask
 #            connecting to the Internet on external interface "eth0".  This
 #            example will MASQ internal traffic out to the Internet not not
 #            allow non-initiated traffic into your internal network.
 #
 #            
 #         ** Please change the above network numbers, subnet mask, and your 
 #         *** Internet connection interface name to match your setup
 #         
 
 #Clearing any previous configuration
 #
 #  Unless specified, the defaults for INPUT, OUTPUT and FORWARD is DROP
 #
 echo "   clearing any existing rules and setting default policy.."
 $IPTABLES -P INPUT ACCEPT
 $IPTABLES -F INPUT 
 $IPTABLES -P OUTPUT ACCEPT
 $IPTABLES -F OUTPUT 
 $IPTABLES -P FORWARD ACCEPT
 $IPTABLES -F FORWARD 
 $IPTABLES -t nat -F
 #$IPTABLES -F icmp_packets
 #$IPTABLES -F tcp_packets
 #$IPTABLES -F udpincoming_packets
 #$IPTABLES -F allowed
 #$IPTABLES -X icmp_packets
 #$IPTABLES -X tcp_packets
 #$IPTABLES -X udpincoming_packets
 #$IPTABLES -X allowed

#*******************************************************************************
 # Now we set the allowed ports and protocols....anything not included below
 # should be disallowed by default from the INPUT, FORWARD & OUTPUT chains
 
 # Programs, protocols and ports allowed to access the network
 #
 # SSH - from 192.168.0.2				
 # http - all $LAN_IFACE
 # ftp - sometime in the future
 # tcp - 
 # udp - 
 # smtp - 
 # pop3 - 
 # MSN Gaming Zone - Special ports
 # MCM2 - Special game ports
 # ICQ - Special ports
 # Time Daemon - for accessing time server
 # NFS - sometime in the future
 # Samba  - sometime in the future
 # Printer port

 #
 # POSTROUTING chain.
 #
 
 # Enabling SNAT (MASQUERADE) functionality on $INET_IFACE
 
  $IPTABLES -t nat -A POSTROUTING -o $INET_IFACE -j MASQUERADE

 #
 # Bad TCP packets we don't want
 #
 
 #$IPTABLES -A FORWARD -p tcp ! --syn -m state --state NEW -j LOG --log-prefix "New not syn: "
 #$IPTABLES -A FORWARD -p tcp ! --syn -m state --state NEW -j DROP
 
 #
 # Accept the packets we actually want to forward
 #
 
 $IPTABLES -A FORWARD -i $LAN_IFACE -j ACCEPT
 #$IPTABLES -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
 #$IPTABLES -A FORWARD -m limit --limit 3/minute --limit-burst 3 -j LOG --log-level DEBUG --log-prefix "IPT FORWARD packet died: "
 $IPTABLES -t nat -A PREROUTING -i $INET_IFACE -p TCP --dport 6667 -j DNAT --to 192.168.0.2:6667
 $IPTABLES -t nat -A PREROUTING -i $INET_IFACE -p TCP --dport 28800:29000 -j DNAT --to 192.168.0.2:28800:29000
 $IPTABLES -t nat -A PREROUTING -i $INET_IFACE -p TCP --dport 47624 -j DNAT --to 192.168.0.2:47624
 $IPTABLES -t nat -A PREROUTING -i $INET_IFACE -p TCP --dport 2300:2400 -j DNAT --to 192.168.0.2:2300:2400
 $IPTABLES -t nat -A PREROUTING -i $INET_IFACE -p TCP --dport 443 -j DNAT --to 192.168.0.2:443
 $IPTABLES -t nat -A PREROUTING -i $INET_IFACE -p UDP --sport 2300:2400 -j DNAT --to 192.168.0.2:2300:2400
 $IPTABLES -t nat -A PREROUTING -i $INET_IFACE -p UDP --sport 6073 -j DNAT --to 192.168.0.2:6073
 #$IPTABLES -A FORWARD -i $INET_IFACE -o $LAN_IFACE -p TCP -s 0/0 -d 192.168.0.2 --dport 6667 -j ACCEPT
 #$IPTABLES -A FORWARD -i $INET_IFACE -o $LAN_IFACE -p TCP -s 0/0 -d 192.168.0.2 --dport 28800:29000 -j ACCEPT
 #$IPTABLES -A FORWARD -i $INET_IFACE -o $LAN_IFACE -p TCP -s 0/0 -d 192.168.0.2 --dport 47624 -j ACCEPT
 #$IPTABLES -A FORWARD -i $INET_IFACE -o $LAN_IFACE -p TCP -s 0/0 -d 192.168.0.2 --dport 2300:2400 -j ACCEPT
 #$IPTABLES -A FORWARD -i $INET_IFACE -o $LAN_IFACE -p TCP -s 0/0 -d 192.168.0.2 --dport 443 -j ACCEPT
 #$IPTABLES -A FORWARD -i $INET_IFACE -o $LAN_IFACE -p UDP -s 0/0 -d 192.168.0.2 --sport 2300:2400 -j ACCEPT
 #$IPTABLES -A FORWARD -i $INET_IFACE -o $LAN_IFACE -p UDP -s 0/0 -d 192.168.0.2 --sport 6073 -j ACCEPT
 
 # Create separate chains for ICMP, TCP, and UDP to traverse
 #
 
 #$IPTABLES -N icmp_packets
 #$IPTABLES -N tcp_packets
 #$IPTABLES -N udpincoming_packets
 
 #
 # The allowed chain for TCP connections
 #
 
 #$IPTABLES -N allowed
 #$IPTABLES -A allowed -p TCP --syn -j ACCEPT
 #$IPTABLES -A allowed -p TCP -m state --state ESTABLISHED,RELATED -j ACCEPT
 #$IPTABLES -A allowed -p TCP -j DROP
 
 #
 # ICMP rules
 #
 
 #$IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 0 -j ACCEPT
 #$IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 3 -j ACCEPT
 #$IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 5 -j ACCEPT
 #$IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 11 -j ACCEPT
  
 #
 # TCP rules
 #
 
 #$IPTABLES -A tcp_packets -p TCP -s 0/0 --destination-port 21 -j allowed
 #$IPTABLES -A tcp_packets -p TCP -s 0/0 --destination-port 22 -j allowed
 #$IPTABLES -A tcp_packets -p TCP -s 0/0 --destination-port 80 -j allowed
 #$IPTABLES -A tcp_packets -p TCP -s 0/0 --destination-port 113 -j allowed
     
 #
 # UDP ports
 #
 
 #$IPTABLES -A udpincoming_packets -p UDP -s 0/0 --source-port 53 -j ACCEPT
 #$IPTABLES -A udpincoming_packets -p UDP -s 0/0 --source-port 123 -j ACCEPT
 #$IPTABLES -A udpincoming_packets -p UDP -s 0/0 --source-port 2074 -j ACCEPT
 #$IPTABLES -A udpincoming_packets -p UDP -s 0/0 --source-port 4000 -j ACCEPT
  
 #
 # PREROUTING chain.
 #
 # Do some checks for obviously spoofed IP's
 #
 
 $IPTABLES -t nat -A PREROUTING -i $INET_IFACE -s 192.168.0.0/16 -j DROP
 $IPTABLES -t nat -A PREROUTING -i $INET_IFACE -s 10.0.0.0/8 -j DROP
 $IPTABLES -t nat -A PREROUTING -i $INET_IFACE -s 172.16.0.0/12 -j DROP

 
 #
 # INPUT chain.
 #
 # Take care of bad TCP packets that we don't want
 #
 
 #$IPTABLES -A INPUT -p tcp ! --syn -m state --state NEW -j LOG --log-prefix "New not syn: "
 #$IPTABLES -A INPUT -p tcp ! --syn -m state --state NEW -j DROP
 
 #
 # Rules for incoming packets from the internet
 #
 
 #$IPTABLES -A INPUT -p ICMP -i $INET_IFACE -j icmp_packets
 #$IPTABLES -A INPUT -p TCP -i $INET_IFACE -j tcp_packets
 #$IPTABLES -A INPUT -p UDP -i $INET_IFACE -j udpincoming_packets
 
 #
 # Rules for special networks not part of the Internet
 #
 
 #$IPTABLES -A INPUT -p ALL -i $LAN_IFACE -d $LAN_BCAST_ADRESS -j ACCEPT
 #$IPTABLES -A INPUT -p ALL -i $LO_IFACE -d $LO_IP -j ACCEPT
 #$IPTABLES -A INPUT -p ALL -i $LAN_IFACE -d $LAN_IP -j ACCEPT
 #$IPTABLES -A INPUT -m limit --limit 3/minute --limit-burst 3 -j LOG --log-level DEBUG --log-prefix "IPT INPUT packet died: "
 
 #
 # OUTPUT chain.
 #
 
 #$IPTABLES -A OUTPUT -p tcp ! --syn -m state --state NEW -j LOG --log-prefix "NEW not syn: "
 #$IPTABLES -A OUTPUT -p tcp ! --syn -m state --state NEW -j DROP
 
 #$IPTABLES -A OUTPUT -p ALL -s $LO_IP -j ACCEPT
 #$IPTABLES -A OUTPUT -p ALL -s $LAN_IP -j ACCEPT
 #$IPTABLES -A OUTPUT -m limit --limit 3/minute --limit-burst 3 -j LOG --log-level DEBUG --log-prefix "IPT OUTPUT packet died: "
 
 #
 #
 #
 #
 # echo -e "\nrc.firewall-2.2 v$FWVER done.\n"
 