#SETUP VARIABLES INT=<network card name> INTIP="<internal ip address range>" INTBC="<broadcast address for the internal range>" HOSTIP="<the ip address of the host this script will run on>" IPT=/sbin/iptables MPROBE=/sbin/modprobe |
#SETUP KERNEL MODULES $MPROBE -v ip_tables $MPROBE -v ip_conntrack $MPROBE -v iptable_filter $MPROBE -v iptable_mangle $MPROBE -v iptable_nat $MPROBE -v ipt_LOG $MPROBE -v ipt_REJECT $MPROBE -v ipt_limit $MPROBE -v ipt_state $MPROBE -v ip_conntrack_ftp #SETUP KERNEL OPTIONS echo 0 > /proc/sys/net/ipv4/ip_forward |
#FLUSH IPTABLES RULESET $IPT -F $IPT -t nat -F $IPT -X $IPT -t nat -X #DEFAULT POLICIES $IPT -P INPUT DROP $IPT -P FORWARD DROP $IPT -P OUTPUT DROP $IPT -t nat -P PREROUTING DROP $IPT -t nat -P POSTROUTING DROP $IPT -t nat -P OUTPUT DROP #SETUP LOCAL INTERFACE $IPT -A INPUT -i lo -j ACCEPT $IPT -A OUTPUT -o lo -j ACCEPT #SETUP FIREWALL RULES $IPT -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT $IPT -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT $IPT -t nat -A PREROUTING -m state --state ESTABLISHED,RELATED -j ACCEPT $IPT -t nat -A POSTROUTING -m state --state ESTABLISHED,RELATED -j ACCEPT $IPT -t nat -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT |
#INTERNAL INTERFACE SANITY CHECKS $IPT -A INPUT -s ! $INTIP -i $INT -j DROP #BAD PACKET FLAGS $IPT -N BAD_FLAGS #$IPT -A BAD_FLAGS -p tcp --tcp-option 64 -j DROP #$IPT -A BAD_FLAGS -p tcp --tcp-option 128 -j DROP $IPT -A BAD_FLAGS -p tcp --tcp-flags ACK,FIN FIN -j DROP $IPT -A BAD_FLAGS -p tcp --tcp-flags ACK,PSH PSH -j DROP $IPT -A BAD_FLAGS -p tcp --tcp-flags ACK,URG URG -j DROP $IPT -A BAD_FLAGS -p tcp --tcp-flags FIN,RST FIN,RST -j DROP $IPT -A BAD_FLAGS -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP $IPT -A BAD_FLAGS -p tcp --tcp-flags SYN,RST SYN,RST -j DROP $IPT -A BAD_FLAGS -p tcp --tcp-flags ALL ALL -j DROP $IPT -A BAD_FLAGS -p tcp --tcp-flags ALL NONE -j DROP $IPT -A BAD_FLAGS -p tcp --tcp-flags ALL FIN -j DROP $IPT -A BAD_FLAGS -p tcp --tcp-flags ALL FIN,PSH,URG -j DROP $IPT -A BAD_FLAGS -p tcp --tcp-flags ALL SYN,FIN,PSH,URG -j DROP $IPT -A BAD_FLAGS -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP $IPT -A BAD_FLAGS -p tcp --tcp-flags ALL URG,ACK,PSH,RST,SYN,FIN -j DROP $IPT -A BAD_FLAGS -j RETURN $IPT -A INPUT -p tcp -j BAD_FLAGS #FRAGMENT PACKET CHECK $IPT -N FRAG_CHK $IPT -A FRAG_CHK -j DROP $IPT -A INPUT -p ip -f -j FRAG_CHK |
#ALLOW INTERNAL RESPONSIBLE PINGING OF HOST FROM
INTERNAL NETWORK ICMPOPT="-m limit --limit 1/second --limit-burst 10" $IPT -t mangle -A PREROUTING -i $INT -s $INTIP -d $HOSTIP -p icmp $ICMPOPT -j ACCEPT $IPT -t nat -A PREROUTING -i $INT -s $INTIP -d $HOSTIP -p icmp $ICMPOPT -j ACCEPT $IPT -A INPUT -i $INT -s $INTIP -d $HOSTIP -p icmp $ICMPOPT -j ACCEPT #ALLOW HOST TO PING OTHER HOSTS $IPT -t mangle -A OUTPUT -o $INT -s $HOSTIP -p icmp -j ACCEPT $IPT -t nat -A OUTPUT -o $INT -s $HOSTIP -p icmp -j ACCEPT $IPT -A OUTPUT -o $INT -s $HOSTIP -p icmp -j ACCEPT $IPT -t nat -A POSTROUTING -o $INT -s $HOSTIP -p icmp -j ACCEPT |
#TCP PORTS TO HOST INOPT="-m state --state NEW" for TCPIN in 22 80 21 25 139 do $IPT -t mangle -A PREROUTING -i $INT -s $INTIP -d $HOSTIP $INOPT -p tcp --syn --dport $TCPIN -j ACCEPT $IPT -t nat -A PREROUTING -i $INT -s $INTIP -d $HOSTIP $INOPT -p tcp --syn --dport $TCPIN -j ACCEPT $IPT -A INPUT -i $INT -s $INTIP -d $HOSTIP $INOPT -p tcp --syn --dport $TCPIN -j ACCEPT done #UDP PORTS TO HOSTS UDPOPT="-m state --state NEW" for UDPIN in 67 137 138 do $IPT -t mangle -A PREROUTING -i $INT -p udp --dport $UDPIN $UDPOPT -j ACCEPT $IPT -t nat -A PREROUTING -i $INT -p udp --dport $UDPIN $UDPOPT -j ACCEPT $IPT -A INPUT -i $INT -p udp --dport $UDPIN $UDPOPT -j ACCEPT done |
#TCP PORTS FROM HOST OUTOPT="-m state --state NEW" for TCPOUT in 21 22 80 do $IPT -t mangle -A OUTPUT -o $INT -s $HOSTIP $OUTOPT -p tcp --syn --dport $TCPOUT -j ACCEPT $IPT -t nat -A OUTPUT -o $INT -s $HOSTIP $OUTOPT -p tcp --syn --dport $TCPOUT -j ACCEPT $IPT -A OUTPUT -o $INT -s $HOSTIP $OUTOPT -p tcp --syn --dport $TCPOUT -j ACCEPT $IPT -t nat -A POSTROUTING -o $INT -s $HOSTIP $OUTOPT -p tcp --syn --dport $TCPOUT -j ACCEPT done #UDP PORTS FROM HOSTS UDPIPT="-m state --state NEW" for UDPIN in 161 do $IPT -t mangle -A OUTPUT -o $INT -p udp --dport $UDPIN $UDPIPT -j ACCEPT $IPT -t nat -A OUTPUT -o $INT -p udp --dport $UDPIN $UDPIPT -j ACCEPT $IPT -OUTPUT -o $INT -p udp --dport $UDPIN $UDPIPT -j ACCEPT $IPT -t nat -A PREROUTING -o $INT -p udp --dport $UDPIN $UDPIPT -j ACCEPT done |