#!/bin/sh

#################
# Network Zones #
#################

# Development Zone
DEV_IP="192.168.10.1"
DEV_IFACE="eth3"

# Internal Zone 
LAN_IP="192.168.1.2"
LAN_BCAST_ADRESS="192.168.1.255"
LAN_IFACE="eth1"

# Internet Zone 
INET_IP="212.19.84.47"
INET_IFACE="eth2"

# DMZ Zone
DMZ_HTTP_IP="10.10.10.10"
DMZ_IP="10.10.10.1"
DMZ_IFACE="eth0"

# Loopback Zone
LO_IP="127.0.0.1"
LO_IFACE="lo"

#############
# Variables #
#############

SERVER1="192.168.1.10"
HTTP_IP="212.19.84.47"
IPTABLES="/sbin/iptables"



$IPTABLES -F
$IPTABLES -X
$IPTABLES -Z
echo Flushing Tables
###########################################
#
# Load all required IPTables modules
#

#
# Needed to initially load modules
#
echo Loading Dependencies
/sbin/depmod -a

#
# Adds some iptables targets like LOG, REJECT and MASQUARADE.
#

/sbin/modprobe ipt_LOG
/sbin/modprobe ipt_MASQUERADE

#
# Support for connection tracking of FTP and IRC.
#
/sbin/modprobe ip_conntrack_ftp
/sbin/modprobe ip_conntrack_irc


#CRITICAL:  Enable IP forwarding since it is disabled by default.
#

echo "1" > /proc/sys/net/ipv4/ip_forward

#
# Dynamic IP users:
#
#echo "1" > /proc/sys/net/ipv4/ip_dynaddr

###########################################
#
# Chain Policies gets set up before any bad packets gets through
#
echo Adding Default Drop
$IPTABLES -P INPUT DROP
$IPTABLES -P OUTPUT DROP
$IPTABLES -P FORWARD DROP

#
# the allowed chain for TCP connections, utilized in the FORWARD chain
#

$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, utilized in the FORWARD chain
#


$IPTABLES -N icmp_packets

# Changed rules totally
$IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 8 -j ACCEPT
$IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 11 -j ACCEPT

#
# bad_tcp_packets chain
#
# Take care of bad TCP packets that we don't want.
#

$IPTABLES -N bad_tcp_packets
#$IPTABLES -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j LOG --log-prefix "New not syn:"
$IPTABLES -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j DROP

#
# Do some checks for obviously spoofed IP's
#

$IPTABLES -A bad_tcp_packets -i $INET_IFACE -s 192.168.0.0/16 -j DROP
$IPTABLES -A bad_tcp_packets -i $INET_IFACE -s 10.0.0.0/8 -j DROP
$IPTABLES -A bad_tcp_packets -i $INET_IFACE -s 172.16.0.0/12 -j DROP

###########################################
# POSTROUTING chain in the nat table
#
# Enable IP SNAT for all internal networks trying to get out on the Internet
#

$IPTABLES -t nat -A POSTROUTING -o $INET_IFACE -j SNAT --to-source $INET_IP
#$IPTABLES -t nat -A POSTROUTING -o $INET_IFACE -j MASQUERADE



###########################################
# PREROUTING chain in the nat table
#
# Enable IP Destination NAT for DMZ zone
#

$IPTABLES -t nat -A PREROUTING -p TCP -i $INET_IFACE -d $HTTP_IP --dport 80 -j DNAT --to-destination $DMZ_HTTP_IP
$IPTABLES -t nat -A PREROUTING -p TCP -i $INET_IFACE -d $HTTP_IP --dport 443 -j DNAT --to-destination $DMZ_HTTP_IP
$IPTABLES -t nat -A PREROUTING -p TCP -i $INET_IFACE -d $HTTP_IP --dport 22 -j DNAT --to-destination $DMZ_HTTP_IP
$IPTABLES -t nat -A PREROUTING -p TCP -i $INET_IFACE -d $HTTP_IP --dport 30001 -j DNAT --to-destination $DMZ_HTTP_IP
$IPTABLES -t nat -A PREROUTING -p TCP -i $INET_IFACE -d $HTTP_IP --dport 50001 -j DNAT --to-destination $DMZ_HTTP_IP
$IPTABLES -t nat -A PREROUTING -p icmp -i $INET_IFACE -d $HTTP_IP -j DNAT --to-destination $DMZ_HTTP_IP

###########################################
#
# FORWARD chain
#
#
# Bad TCP packets we don't want
#

$IPTABLES -A FORWARD -p tcp -j bad_tcp_packets

# General rules
#
# dmz to wan accept http, https, ftp
#$IPTABLES -A FORWARD -i $DMZ_IFACE -o $INET_IFACE -j ACCEPT
$IPTABLES -A FORWARD -i $DMZ_IFACE -p tcp --dport 443 -o $INET_IFACE -j ACCEPT
$IPTABLES -A FORWARD -i $DMZ_IFACE -p tcp --dport 80 -o $INET_IFACE -j ACCEPT
$IPTABLES -A FORWARD -i $DMZ_IFACE -p tcp --dport 20 -o $INET_IFACE -j ACCEPT
$IPTABLES -A FORWARD -i $DMZ_IFACE -p tcp --dport 21 -o $INET_IFACE -j ACCEPT

# wan to dmz
$IPTABLES -A FORWARD -i $INET_IFACE -o $DMZ_IFACE -m state --state ESTABLISHED,RELATED -j ACCEPT

# lan to dmz accept all
$IPTABLES -A FORWARD -i $LAN_IFACE -o $DMZ_IFACE -j ACCEPT

# dmz to lan accept smtp, dns
#$IPTABLES -A FORWARD -i $DMZ_IFACE -o $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -i $DMZ_IFACE -p tcp -d $SERVER1 --dport 25 -o $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -i $DMZ_IFACE -p tcp -d 192.168.1.9 --dport 161 -o $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -i $DMZ_IFACE -p tcp -d $SERVER1 --dport 139 -o $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -i $DMZ_IFACE -p tcp -d $SERVER1 --dport 80 -o $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -i $DMZ_IFACE -p tcp -d $SERVER1 --dport 53 -o $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -i $DMZ_IFACE -p udp -d $SERVER1 --dport 53 -o $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -i $DMZ_IFACE -p icmp -d 192.168.1.9 -o $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -i $DMZ_IFACE -p icmp -d $SERVER1 -o $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -i $DMZ_IFACE -o $LAN_IFACE -m state --state ESTABLISHED,RELATED -j ACCEPT

# Lan to Dev
$IPTABLES -A FORWARD -i $LAN_IFACE -o $DEV_IFACE -j ACCEPT

# Dev to Lan
$IPTABLES -A FORWARD -i $DEV_IFACE -p tcp -d $SERVER1 --dport 53 -o $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -i $DEV_IFACE -p udp -d $SERVER1 --dport 53 -o $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -i $DEV_IFACE -o $LAN_IFACE -m state --state ESTABLISHED,RELATED -j ACCEPT

#
# BMC server incoming
#

$IPTABLES -A FORWARD -p TCP -i $INET_IFACE -o $DMZ_IFACE -d $DMZ_HTTP_IP --dport 80 -j allowed
$IPTABLES -A FORWARD -p TCP -i $INET_IFACE -o $DMZ_IFACE -d $DMZ_HTTP_IP --dport 443 -j allowed
$IPTABLES -A FORWARD -p TCP -i $INET_IFACE -o $DMZ_IFACE -d $DMZ_HTTP_IP --dport 22 -j allowed
$IPTABLES -A FORWARD -p TCP -i $INET_IFACE -o $DMZ_IFACE -d $DMZ_HTTP_IP --dport 30001 -j allowed
$IPTABLES -A FORWARD -p TCP -i $INET_IFACE -o $DMZ_IFACE -d $DMZ_HTTP_IP --dport 50001 -j allowed
$IPTABLES -A FORWARD -p ICMP -i $INET_IFACE -o $DMZ_IFACE -d $DMZ_HTTP_IP -j icmp_packets

#
# LAN section
#

$IPTABLES -A FORWARD -i $LAN_IFACE -s 192.168.1.0/24 -j ACCEPT
$IPTABLES -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT 

#
# LOG all packets reaching here
#

$IPTABLES -A FORWARD -m limit --limit 3/minute --limit-burst 3 -j LOG --log-level DEBUG --log-prefix "FORWARD packet died: "


###########################################################
#
# Firewall rules
# Rules applying to the firewall box
#
#
# INPUT chain
#
# Bad TCP packets we don't want
#

$IPTABLES -A INPUT -p tcp -j bad_tcp_packets

#
# Packets from the Internet to this box
#

$IPTABLES -A INPUT -p ICMP -i $INET_IFACE -j icmp_packets

#
# Packets from LAN, DMZ or LOCALHOST
#

# From DMZ Interface to DMZ firewall IP
#$IPTABLES -A INPUT -p ALL -i $DMZ_IFACE -d $DMZ_IP -j ACCEPT

# From LAN Interface to LAN firewall IP
$IPTABLES -A INPUT -p ALL -i $LAN_IFACE -d $LAN_IP -j ACCEPT
#$IPTABLES -A INPUT -p ALL -i $LAN_IFACE -d $LAN_BCAST_ADRESS -j ACCEPT

# From DEV Interface to DEV firewall IP
$IPTABLES -A INPUT -p ALL -i $DEV_IFACE -d $DEV_IP -j ACCEPT

# From Localhost interface to Localhost IP
$IPTABLES -A INPUT -p ALL -i $LO_IFACE -s $LO_IP -j ACCEPT
$IPTABLES -A INPUT -p ALL -i $LO_IFACE -s $LAN_IP -j ACCEPT
$IPTABLES -A INPUT -p ALL -i $LO_IFACE -s $INET_IP -j ACCEPT
$IPTABLES -A INPUT -p ALL -i $LO_IFACE -s $DEV_IP -j ACCEPT

# All established and related packets incoming from the internet to the 
# firewall
$IPTABLES -A INPUT -p ALL -d $INET_IP -m state --state ESTABLISHED,RELATED -j ACCEPT

# Logging rule
$IPTABLES -A INPUT -m limit --limit 3/minute --limit-burst 3 -j LOG --log-level DEBUG --log-prefix "IPT INPUT packet died: "

###########################################################
#
# OUTPUT chain
#
#
# Bad TCP packets we don't want
#

$IPTABLES -A OUTPUT -p tcp -j bad_tcp_packets

#
# Allow ourself to send packets not spoofed everywhere
#

$IPTABLES -A OUTPUT -p ALL -o $LO_IFACE -s $LO_IP -j ACCEPT
$IPTABLES -A OUTPUT -p ALL -o $LAN_IFACE -s $LAN_IP -j ACCEPT
$IPTABLES -A OUTPUT -p ALL -o $INET_IFACE -s $INET_IP -j ACCEPT
$IPTABLES -A OUTPUT -p ALL -o $DMZ_IFACE -s $DMZ_IP -j ACCEPT
$IPTABLES -A OUTPUT -p ALL -o $DEV_IFACE -s $DEV_IP -j ACCEPT

#
# Logging rule 
#

$IPTABLES -A OUTPUT -m limit --limit 3/minute --limit-burst 3 -j LOG --log-level DEBUG --log-prefix "IPT OUTPUT packet died: "
