随笔-260  评论-285  文章-1  trackbacks-0

 

实现路由、内网NAT上网、防火墙功能

这里是一个很好很全的模板,我修改了下,就用到我的DEBIAN下了。这是修改后版本

要点说明如下:

#这是内网网口

INTIF="eth0"

INTNET="192.168.1.0/24"

# 自己的IP地址

INTIP="192.168.1.17/24"

#外网网口

EXTIF="ppp0"

# LOAD需要的模块,只用执行一次

#/sbin/depmod -a

#/sbin/modprobe ip_tables

#/sbin/modprobe ip_conntrack

#/sbin/modprobe ip_conntrack_ftp

#/sbin/modprobe ip_conntrack_irc

#/sbin/modprobe iptable_nat

#/sbin/modprobe ip_nat_ftp

#/sbin/modprobe ip_nat_irc

# 打开路由转发功能

echo " Enabling IP forwarding..."

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

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

# 全局地址

UNIVERSE="0.0.0.0/0"

# 清除原有规则,并将默认规则设为禁止所有访问

iptables -P INPUT DROP

iptables -F INPUT

iptables -P OUTPUT DROP

iptables -F OUTPUT

iptables -P FORWARD DROP

iptables -F FORWARD

iptables -F -t nat

# Flush the user chain.. if it exists

if [ "`iptables -L | grep drop-and-log-it`" ]; then

iptables -F drop-and-log-it

fi

# Delete all User-specified chains

iptables -X

# Reset all IPTABLES counters

iptables -Z

# Creating a DROP chain

iptables -N drop-and-log-it

#iptables -A drop-and-log-it -j LOG --log-level info

iptables -A drop-and-log-it -j REJECT

echo -e " - Loading INPUT rulesets"

#######################################################################

# INPUT: Incoming traffic from various interfaces. All rulesets are

# already flushed and set to a default policy of DROP.

#

# 自己可以访问自己

iptables -A INPUT -i lo -s $UNIVERSE -d $UNIVERSE -j ACCEPT

# 从公司连进来

iptables -A INPUT -s 210.21.224.54 -p tcp -m multiport --dport 22,4080 -j ACCEPT # from company

# 允许本地到任何地方

iptables -A INPUT -i $INTIF -s $INTNET -d $UNIVERSE -j ACCEPT

# 防虚拟本地地址攻击,抄过来的

iptables -A INPUT -i $EXTIF -s $INTNET -d $UNIVERSE -j drop-and-log-it

# 允许所有已经建立连接的通讯包

iptables -A INPUT -s $UNIVERSE -m state --state ESTABLISHED,RELATED -j ACCEPT

# 允许DHCP服务端口

iptables -A INPUT -i $INTIF -p tcp --sport 68 --dport 67 -j ACCEPT

iptables -A INPUT -i $INTIF -p udp --sport 68 --dport 67 -j ACCEPT

# BT、电驴、WEB、SSL端口

iptables -A INPUT -i $EXTIF -p tcp -s $UNIVERSE --dport 80 -j ACCEPT

iptables -A INPUT -i $EXTIF -p tcp -s $UNIVERSE --dport 6881:6890 -j ACCEPT # for bt

iptables -A INPUT -i $EXTIF -p tcp -s $UNIVERSE --dport 21558 -j ACCEPT # for module

iptables -A INPUT -i $EXTIF -p udp -s $UNIVERSE --dport 21562 -j ACCEPT # for module

iptables -A INPUT -i $EXTIF -p tcp -s $UNIVERSE --dport 443 -j ACCEPT # for SSL

# 其它全部拒绝

iptables -A INPUT -s $UNIVERSE -d $UNIVERSE -j drop-and-log-it

echo -e " - Loading OUTPUT rulesets"

#######################################################################

# OUTPUT: Outgoing traffic from various interfaces. All rulesets are

# already flushed and set to a default policy of DROP.

#

# 允许所有外出流量,我没看懂模板里为什么要定义很多条规则,似乎允许所有就可以了

iptables -A OUTPUT -j ACCEPT

echo -e " - Loading FORWARD rulesets"

#######################################################################

# FORWARD: Enable Forwarding and thus IPMASQ

#

# 允许路由所有已经建立了连接的进来的流量

echo " - FWD: Allow all connections OUT and only existing/related IN"

iptables -A FORWARD -i $EXTIF -o $INTIF -m state --state ESTABLISHED,RELATED -j ACCEPT

# 允许路由所有由内向外的建立连接请求及数据流量

iptables -A FORWARD -i $INTIF -o $EXTIF -j ACCEPT

# 禁止其它流量

iptables -A FORWARD -j drop-and-log-it

echo " - NAT: Enabling SNAT (MASQUERADE) functionality on $EXTIF"

#

# NAT:将所有内网地址伪装为外网地址,即让内网可以上网

iptables -t nat -A POSTROUTING -o $EXTIF -j MASQUERADE

# 导出到一个规则文件,下次启动再LOAD进来

iptables-save > /etc/firewall-rules




2007.11.24更新:下载

删除全部应该用iptables -F;

加入了获得PPP0的IP地址,并用到规则中去;

加入了QQ视频及VNCViewer的端口映射到内网一台机器的规则。




搬家了,影评和好玩的事跑这去了:qqmovie.qzone.qq.com
posted on 2007-11-15 14:45 云中漫步者 阅读(919) 评论(0)  编辑  收藏 所属分类: 猎奇