#!/bin/sh
#set -x
SYSCTL="/sbin/sysctl -w"
IPT="/sbin/iptables"
IPTS="/sbin/iptables-save"
IPTR="/sbin/iptables-restore"
# Local Interface Information
LOCAL_IFACE="eth#"
LOCAL_IP="#.#.#.#"
LOCAL_NET="#.#.#.#/255.255.255.0"
UNIMED_NET="#.#.#.#/255.255.255.0"
FARMACIA_NET="#.#.#.#/255.255.255.0"
LOCAL_BCAST="#.#.#.#"
# Ext Interface
EXT_IFACE="eth#"
EXT_ADDRESS="200.#.#.#"
# Localhost Interface
LO_IFACE="lo"
LO_IP="127.0.0.1"
# Save and Restore arguments handled here
if [ "$1" = "save" ]
then
echo -n "Saving firewall to /etc/sysconfig/iptables ... "
$IPTS > /etc/sysconfig/iptables
echo "done"
exit 0
elif [ "$1" = "restore" ]
then
echo -n "Restoring firewall from /etc/sysconfig/iptables ... "
$IPTR < /etc/sysconfig/iptables
echo "done"
exit 0
fi
echo "Loading kernel modules ..."
/sbin/modprobe ip_tables
/sbin/modprobe ip_conntrack
/sbin/modprobe iptable_filter
/sbin/modprobe iptable_mangle
/sbin/modprobe iptable_nat
/sbin/modprobe ipt_LOG
/sbin/modprobe ipt_limit
/sbin/modprobe ipt_MASQUERADE
/sbin/modprobe ipt_owner
/sbin/modprobe ip_nat_ftp
/sbin/modprobe ip_conntrack_ftp
/sbin/modprobe ip_conntrack_irc
if [ "$SYSCTL" = "" ]
then
echo "1" > /proc/sys/net/ipv4/ip_forward
else
$SYSCTL net.ipv4.ip_forward="1"
fi
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 Defaul
$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
if [ "$1" = "stop" ]
then
echo "Firewall completely flushed! Now running with no firewall."
exit 0
fi
$IPT -P INPUT DROP
$IPT -P OUTPUT DROP
$IPT -P FORWARD DROP
echo "Create and populate custom rule chains ..."
$IPT -N bad_packets
$IPT -N bad_tcp_packets
$IPT -N icmp_packets
$IPT -N udp_inbound
$IPT -N udp_outbound
$IPT -N tcp_inbound
$IPT -N tcp_outbound
$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
$IPT -A bad_packets -p tcp -j bad_tcp_packets
$IPT -A bad_packets -p ALL -j RETURN
$IPT -A bad_tcp_packets -p tcp -i $LOCAL_IFACE -j RETURN
$IPT -A bad_tcp_packets -p tcp -i tun+ -j RETURN
$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
$IPT -A bad_tcp_packets -p tcp -j RETURN
$IPT -A icmp_packets -p ICMP -s 0/0 --icmp-type 8 -j ACCEPT
$IPT -A icmp_packets -p ICMP -s 0/0 --icmp-type 11 -j ACCEPT
$IPT -A icmp_packets -p ICMP -j RETURN
$IPT -A udp_inbound -p UDP -s 0/0 --destination-port ## -j ACCEPT
$IPT -A udp_inbound -p UDP -s 0/0 --destination-port #### -j ACCEPT
$IPT -A udp_inbound -p UDP -s 0/0 --destination-port ### -j DROP
$IPT -A udp_inbound -p UDP -s 0/0 --destination-port ### -j DROP
$IPT -A udp_inbound -p UDP -j RETURN
$IPT -A udp_outbound -p UDP -s 0/0 -j ACCEPT
$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port ## -j ACCEPT
$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port ## -j ACCEPT
$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port ## -j ACCEPT
$IPT -A tcp_inbound -p TCP -s 200.219.214.6 --destination-port 25 -j ACCEPT
$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port ## -j ACCEPT
$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port ## -j ACCEPT
$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port ## -j ACCEPT
$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port 443 -j ACCEPT
$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port ### -j ACCEPT
$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port #### -j ACCEPT
$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port ##### -j ACCEPT
$IPT -A tcp_inbound -p TCP -s 0/0 --destination-port ##### -j ACCEPT
$IPT -A tcp_inbound -p TCP -j RETURN
$IPT -A tcp_outbound -p TCP -d $EXT_ADDRESS --dport 25 -j ACCEPT
$IPT -A tcp_outbound -p TCP -d #.#.#.# --dport ## -j ACCEPT
$IPT -A tcp_outbound -p TCP -d 0/0 --dport ## -j DROP
$IPT -A tcp_outbound -p TCP -s 0/0 -j ACCEPT
echo "Process INPUT chain ..."
$IPT -A INPUT -p ALL -i $LO_IFACE -j ACCEPT
$IPT -A INPUT -p ALL -j bad_packets
$IPT -A INPUT -p ALL -i $LOCAL_IFACE -s $LOCAL_NET -j ACCEPT
$IPT -A INPUT -p ALL -i $LOCAL_IFACE -s $UNIMED_NET -j ACCEPT
$IPT -A INPUT -p ALL -i $LOCAL_IFACE -s $FARMACIA_NET -j ACCEPT
$IPT -A INPUT -p ALL -i tun+ -s #.#.#.#/255.255.255.0 -j ACCEPT
$IPT -A INPUT -p ALL -i $LOCAL_IFACE -d $LOCAL_BCAST -j ACCEPT
$IPT -A FORWARD -d 65.49.14.0/24 -j LOG --log-prefix "=ultrasurf= "
$IPT -A INPUT -p ALL -i $EXT_IFACE -m state --state ESTABLISHED,RELATED \
-j ACCEPT
$IPT -A INPUT -p TCP -i $EXT_IFACE -j tcp_inbound
$IPT -A INPUT -p UDP -i $EXT_IFACE -j udp_inbound
$IPT -A INPUT -p ICMP -i $EXT_IFACE -j icmp_packets
$IPT -A INPUT -p ALL -d 255.255.255.255 -j DROP
$IPT -A INPUT -m limit --limit 3/minute --limit-burst 3 -j LOG \
--log-prefix "INPUT packet died: "
echo "Process FORWARD chain ..."
$IPT -A FORWARD -p tcp -i $LOCAL_IFACE -j tcp_outbound
$IPT -A FORWARD -p udp -i $LOCAL_IFACE -j udp_outbound
$IPT -A FORWARD -p ALL -i $LOCAL_IFACE -j ACCEPT
$IPT -A FORWARD -p ALL -i tun+ -j ACCEPT
$IPT -A FORWARD -i $EXT_IFACE -p tcp -d #.#.#.# --dport ## \
-j ACCEPT
$IPT -A FORWARD -i $EXT_IFACE -p tcp -d #.#.#.# --dport #### \
-j ACCEPT
$IPT -A FORWARD -i $EXT_IFACE -m state --state ESTABLISHED,RELATED \
-j ACCEPT
$IPT -A FORWARD -m limit --limit 3/minute --limit-burst 3 -j LOG \
--log-prefix "FORWARD packet died: "
echo "Process OUTPUT chain ..."
$IPT -A OUTPUT -m state -p icmp --state INVALID -j DROP
$IPT -A OUTPUT -p ALL -s $LO_IP -j ACCEPT
$IPT -A OUTPUT -p ALL -o $LO_IFACE -j ACCEPT
$IPT -A OUTPUT -p ALL -s $LOCAL_IP -j ACCEPT
$IPT -A OUTPUT -p ALL -o $LOCAL_IFACE -j ACCEPT
$IPT -A OUTPUT -p ALL -o tun+ -j ACCEPT
$IPT -A OUTPUT -p ALL -o $EXT_IFACE -j ACCEPT
$IPT -A OUTPUT -m limit --limit 3/minute --limit-burst 3 -j LOG \
--log-prefix "OUTPUT packet died: "
echo "Load rules for nat table ..."
$IPT -t nat -A PREROUTING -p tcp -i $EXT_IFACE \
-d #.#.#.# --dport #### \
-j DNAT --to #.#.#.#
$IPT -t nat -A PREROUTING -p tcp -i $LOCAL_IFACE -s $LOCAL_NET \
-d #.#.#.# --dport ## -j ACCEPT
$IPT -t nat -A PREROUTING -p tcp -i $LOCAL_IFACE -s $LOCAL_NET \
-d #.#.#.# --dport ## -j ACCEPT
$IPT -t nat -A PREROUTING -p tcp -i $LOCAL_IFACE -s $LOCAL_NET \
-d #.#.#.# --dport # -j ACCEPT
$IPT -t nat -A PREROUTING -p tcp -i $LOCAL_IFACE -s $LOCAL_NET \
--dport ## -j REDIRECT --to-ports 3128
$IPT -t nat -A POSTROUTING -o $EXT_IFACE -s $LOCAL_NET \
-j MASQUERADE
/etc/init.d/fail2ban restart