关于iptables实例,大家帮忙分析一下

包含网卡/无线网的网络问题和ADSL/校园网/宽带拨号支持及代理/共享等网络使用问题
回复
abbott
帖子: 435
注册时间: 2007-07-11 22:45

关于iptables实例,大家帮忙分析一下

#1

帖子 abbott » 2009-08-09 19:51

说明:
主机采用物理双网卡,连接私网,公网,主机网络OK。
主机上运行富有有:
openvpn 1194 1195 虚拟设备是tun0:10.8.8.*/24 tun1:10.9.9.*/24
vsftp 8021 被动模式:8001-8008
ssh 8022
以上三个服务,公网内网都可访问:

smb 仅仅vpn网络 内网可以访问。smb采用默认的端口。

nat 后端主机,运行
ftp 192.9.206.130:21
微软远程:192.9.206.130:3389
eMule 192.9.206.130:4666 udp 4668 tcp


要求防火墙的策略为:
1 内网数据 lo接口数据,vpn数据,全部通过;
2 通过nat访问内网的数据,仅仅转发内网存在的服务,其他的丢弃。
3 来自公网的访问nat本机的数据,之允许访问nat主机上运行的服务,其他的丢弃。

下面是我大致些的规则,请大侠指点一下:
# 01
#===

# Set the key parameters
EXTIF="eth0"
INIF="eth1"
INNET="192.9.206.0/24"
PATH=/sbin:/bin:/usr/sbin:/usr/bin
export PATH EXTIF INIF INNET

# Configuration Network

ifconfig $EXTIF down
ifconfig $INIF down
ifconfig $EXTIF hw ether 00:22:B0:61:A6:B2
ifconfig $EXTIF 59.72.122.49 netmask 255.255.255.0
ifconfig $INIF 192.9.206.50 netmask 255.255.255.0
ifconfig $INIF up
ifconfig $EXTIF up
route add -net 192.9.0.0 netmask 255.255.0.0 gw 192.9.206.254 dev $INIF
route add default gw 59.72.122.254 dev $EXTIF

# 02
#===
# Load modules
echo "1" > /proc/sys/net/ipv4/ip_forward
modprobe ip_tables > /dev/null 2>&1
modprobe iptable_nat > /dev/null 2>&1
modprobe ip_nat_ftp > /dev/null 2>&1
modprobe ip_nat_irc > /dev/null 2>&1
modprobe ip_conntrack > /dev/null 2>&1
modprobe ip_conntrack_irc > /dev/null 2>&1
modprobe ip_conntrack_ftp ports=8021 > /dev/null 2>&1
# if your ftp server is running on default port 21, then just delete ports=8021
# in the above line.
# 03
#===
# Flush all the default rules.
# Set the new default policy

/sbin/iptables -F
/sbin/iptables -X
/sbin/iptables -Z
/sbin/iptables -F -t nat
/sbin/iptables -X -t nat
/sbin/iptables -Z -t nat
# Flush the original rules

/sbin/iptables -P INPUT DROP
/sbin/iptables -P OUTPUT ACCEPT
/sbin/iptables -P FORWARD ACCEPT
# filter table

/sbin/iptables -t nat -P PREROUTING ACCEPT
/sbin/iptables -t nat -P POSTROUTING ACCEPT
/sbin/iptables -t nat -P OUTPUT ACCEPT
# NAT talbes
# 04
#===
# Allow trusted network
/sbin/iptables -A INPUT -i lo -j ACCEPT

if [ "$INIF" != "" ]; then
/sbin/iptables -A INPUT -i $INIF -j ACCEPT
echo "1" > /proc/sys/net/ipv4/ip_forward
/sbin/iptables -t nat -A POSTROUTING -s $INNET -o $EXTIF -j MASQUERADE
fi
# if the private network exists,
# then all all the data from private network.
# and set IP masquerade for private network.


# 05
#===
# load the allow and deny file.

if [ -f /etc/iptables.deny ]; then
sh /etc/iptables.deny
fi

if [ -f /etc/iptables.allow ]; then
sh /etc/iptables.allow
fi

# 06
#===
# Allow Services
# Allow Service
/sbin/iptables -A INPUT -p tcp -i $EXTIF --dport 8022 -j ACCEPT #SSH
/sbin/iptables -A INPUT -p tcp -i $INIF --dport 8022 -j ACCEPT #SSH
/sbin/iptables -A INPUT -p tcp -i $EXTIF --dport 8021 -j ACCEPT #FTP
/sbin/iptables -A INPUT -p tcp --dport 8000:8008 -j ACCEPT # FTP pasv pattern
/sbin/iptables -A INPUT -p udp -i $EXTIF --dport 1194 -j ACCEPT #OpenVPN
/sbin/iptables -A INPUT -p udp -i $EXTIF --dport 1195 -j ACCEPT #OpenVPN
/sbin/iptables -A INPUT -p tcp -i $EXTIF --dport 4999 -j ACCEPT # aMule
/sbin/iptables -A INPUT -p udp -i $EXTIF --dport 4999 -j ACCEPT # aMule
/sbin/iptables -A INPUT -p tcp -i $EXTIF --dport 177 -j ACCEPT # xdmcp
/sbin/iptables -A INPUT -p udp -i $EXTIF --dport 177 -j ACCEPT # xdmcp


# 07
#===
# Enable iptables works in high efficience

/sbin/iptables -A INPUT -m state --state RELATED,ESTABLISHED,NEW -j ACCEPT
/sbin/iptables -A FORWARD -m state --state ESTABLISHED,RELATED,NEW -j ACCEPT

# 08
#===
# About NAT
/sbin/iptables -t nat -A POSTROUTING -s $INNET -o $EXTIF -j MASQUERADE

# 09
#===
# Make sure OpenVPN can pass through firewall
iptables -A INPUT -i tun+ -j ACCEPT
iptables -A FORWARD -i tun+ -j ACCEPT
iptables -A FORWARD -o tun+ -j ACCEPT

# Enable the VPN client can access to internet
iptables -t nat -A POSTROUTING -s 10.8.8.0/24 -o $EXTIF -j MASQUERADE
iptables -t nat -A POSTROUTING -s 10.8.8.0/24 -o $INIF -j MASQUERADE

iptables -t nat -A POSTROUTING -s 10.9.9.0/24 -o $EXTIF -j MASQUERADE
iptables -t nat -A POSTROUTING -s 10.9.9.0/24 -o $INIF -j MASQUERADE

# 10
# Port mapping
# Enable service running on private network can be accessed via internet.
# Forward the package to private network
iptables -t nat -A PREROUTING -p tcp -d 59.72.122.49 --dport 3389 -j DNAT --to 192.9.206.130:3389 # Win Remote Desktop

iptables -t nat -A PREROUTING -p tcp -d 59.72.122.49 --dport 20 -j DNAT --to 192.9.206.130:20 # FTP
iptables -t nat -A PREROUTING -p tcp -d 59.72.122.49 --dport 21 -j DNAT --to 192.9.206.130:21 # FTP

iptables -t nat -A PREROUTING -p tcp -d 59.72.122.49 --dport 8085 -j DNAT --to 192.9.206.130:8085 # World of Warcraft
iptables -t nat -A PREROUTING -p udp -d 59.72.122.49 --dport 8085 -j DNAT --to 192.9.206.130:8085 # World of Warcraft

iptables -t nat -A PREROUTING -p udp -d 59.72.122.49 --dport 4666 -j DNAT --to 192.9.206.130:4666
iptables -t nat -A PREROUTING -p tcp -d 59.72.122.49 --dport 4668 -j DNAT --to 192.9.206.130:4668 # eMule
# for Sunlei WinXP



比较疑惑的是:
这些规则的书写顺序;
请指正一下,顺序有没有规则,有没有矛盾的地方?
回复