# Firewall setup.
#
INSIDE_DEVICE=eth0
OUTSIDE_DEVICE=eth1
INSIDE_NETWORK=192.168.1.0
INSIDE_NETMASK=24
OUTSIDE_IP=`ifconfig eth1|grep inet|cut -f2 -d:|cut -f1 -d " "`
# 设置要对外服务的内部服务器IP
#
MAILSERVER_IP=192.168.1.10
WEBSERVER_IP=192.168.1.11
# Flushing the chains.
#
# Policy for chains DROP everything
#
iptables -P OUTPUT DROP
iptables -P FORWARD DROP
# SYN-Flooding protection
# Looks good and nicked from a firewall script mentioned on floppyfw.something.
# Didn't work that well..
#
iptables -A syn-flood -j DROP
# Make sure NEW tcp connections are SYN packets
iptables -A INPUT -i ${OUTSIDE_DEVICE} -p tcp ! --syn -m state --state NEW -j DROP
#iptables -t nat -N inside-pre
#iptables -t nat -A inside-pre -p tcp -s ${INSIDE_NETWORK}/${INSIDE_NETMASK} --dport 80 -j REDIRECT
iptables -t nat -N outside-pre
iptables -t nat -A outside-pre -p tcp -d ${OUTSIDE_IP} --dport 80 -j DNAT --to ${MAILSERVER_IP}:80
iptables -t nat -A outside-pre -p tcp -d ${OUTSIDE_IP} --dport 81 -j DNAT --to ${WEBSERVER_IP}:80
iptables -t nat -A outside-pre -p tcp -d ${OUTSIDE_IP} --dport 25 -j DNAT --to ${MAILSERVER_IP}:25
iptables -t nat -A outside-pre -p tcp -d ${OUTSIDE_IP} --dport 110 -j DNAT --to${MAILSERVER_IP}:110
#---------------内部外出的源NAT设置-------------
iptables -t nat -N inside-post
iptables -t nat -A inside-post -s ${INSIDE_NETWORK}/${INSIDE_NETMASK} -j SNAT --to ${OUTSIDE_IP}
iptables -N inside-in
iptables -A inside-in -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A inside-in -s ${INSIDE_NETWORK}/${INSIDE_NETMASK} -j ACCEPT
#--用代理时请打开
#iptables -A inside-in -p tcp -s ${INSIDE_NETWORK}/${INSIDE_NETMASK} --dport 3128 -j ACCEPT
#--用代理时请屏蔽
iptables -A inside-in -p tcp -s ${INSIDE_NETWORK}/${INSIDE_NETMASK} --dport 80 -j ACCEPT
iptables -A inside-in -p icmp -m limit --limit 1/s --limit-burst 5 -j ACCEPT
iptables -N outside-in
iptables -A outside-in -s ${INSIDE_NETWORK}/${INSIDE_NETMASK} -j DROP
iptables -A outside-in -p icmp -m limit --limit 1/s --limit-burst 5 -j ACCEPT
iptables -A outside-in -d ${OUTSIDE_IP} -j ACCEPT
iptables -N inside-fw
#iptables -A inside-fw -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
#--打开全部端口,不用这条请打开下面需出去的条目
iptables -A inside-fw -p tcp -s ${INSIDE_NETWORK}/${INSIDE_NETMASK} --sport 1024:
#iptables -A inside-fw -p tcp -s ${INSIDE_NETWORK}/${INSIDE_NETMASK} --sport 1024: --dport 80 -j ACCEPT
#iptables -A inside-fw -p tcp -s ${INSIDE_NETWORK}/${INSIDE_NETMASK} --sport 1024: --dport 25 -j ACCEPT
#iptables -A inside-fw -p tcp -s ${INSIDE_NETWORK}/${INSIDE_NETMASK} --sport 1024: --dport 53 -j ACCEPT
iptables -A inside-fw -p udp -s ${INSIDE_NETWORK}/${INSIDE_NETMASK} --sport 1024: --dport 53 -j ACCEPT
#iptables -A inside-fw -p tcp -s ${INSIDE_NETWORK}/${INSIDE_NETMASK} --sport 1024: --dport 110 -j ACCEPT
#iptables -A inside-fw -p tcp -s ${INSIDE_NETWORK}/${INSIDE_NETMASK} --sport 1024: --dport 443 -j ACCEPT
iptables -N outside-fw
iptables -A outside-fw -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A outside-fw -p tcp -d ${MAILSERVER_IP} --dport 80 -j ACCEPT
iptables -A outside-fw -p tcp -d ${WEBSERVER_IP} --dport 80 -j ACCEPT
iptables -A outside-fw -p tcp -d ${MAILSERVER_IP} --dport 25 -j ACCEPT
iptables -A outside-fw -p tcp -d ${MAILSERVER_IP} --dport 110 -j ACCEPT
#====================================
#INPUT
iptables -A INPUT -i ${INSIDE_DEVICE} -j inside-in
iptables -A INPUT -i ${OUTSIDE_DEVICE} -j outside-in
iptables -A FORWARD -i ${INSIDE_DEVICE} -j inside-fw
iptables -A FORWARD -i ${OUTSIDE_DEVICE} -j outside-fw
iptables -A OUTPUT -o ${INSIDE_DEVICE} -d ${INSIDE_NETWORK}/${INSIDE_NETMASK} -j
iptables -A OUTPUT -o ${OUTSIDE_DEVICE} -j ACCEPT
#--用作代理时打开
#iptables -t nat -A PREROUTING -i ${INSIDE_DEVICE} -j inside-pre
iptables -t nat -A PREROUTING -i ${OUTSIDE_DEVICE} -j outside-pre
iptables -t nat -A POSTROUTING -o ${OUTSIDE_DEVICE} -j inside-post
iptables -A INPUT -i lo -j ACCEPT
# And, some attempt to get interactive sesions a bit more interactive
# under load:
#
iptables -A PREROUTING -t mangle -p tcp --sport ssh
iptables -A PREROUTING -t mangle -p tcp --sport ftp -j TOS --set-tos Minimize-Delay
# iptables -A PREROUTING -t mangle -p tcp --sport ftp-data -j TOS --set-tos Maximize-Throughput
# Finally, list what we have
#
#
#iptables -L
#iptables -L -n
#
#
# Some of there goes under the "Better safe than sorry" - banner.
#
#
# 使用动态IP地址,拨号连接用
#
#echo 7 > /proc/sys/net/ipv4/ip_dynaddr
# 忽略子网广播消息
#
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
# 禁用源路由地址
#
/bin/echo "0" > /proc/sys/net/ipv4/conf/all/accept_source_route
# Syncookies (if they are really needed any more?)
#
echo "1" > /proc/sys/net/ipv4/tcp_syncookies
# 过滤伪造包
#
if [ -f /proc/sys/net/ipv4/conf/all/rp_filter ]
#
#
#
fi
# 不发送 ICMP 重定向报文
#
fi
# 忽略回应地址是广播地址的主机生成的ICMP错误
#
/bin/echo "1" > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
# This is RAM dependant so be careful with this.
# The max, which is the valuehere, needs around 32M RAM to work properly.
# echo "65535" > /proc/sys/net/ipv4/ip_conntrack_max
echo "600" > /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_established
# This is commented out and will be an option when we have a "LOG_STUFF"
# config option.
# /bin/echo "1" > /proc/sys/net/ipv4/conf/all/log_martians
# Rules set, we can enable forwarding in the kernel.
#
echo "Enabling IP forwarding."