|
|
- #本脚本环境为eth0外网,eth1内网;
- #!/bin/sh
- #外网网卡
- EXT_IF="eth0"
- FW_IP="XX.XX.XX.XX"
- #内网网卡
- INT_IF="eth1"
- LAN_IP="192.168.0.1"
- LAN_IP_RANGE="192.168.0.0/255.255.255.0"
- #加阅模块,一般已内建
- #Module loading.
- #echo "modprobe modules"
- #modprode ip_tables
- #modprode ip_nat_ftpiptables -P INPUT DROP
- iptables -P FORWARD DROP
- iptables -P OUTPUT DROP
- ###-----------------------------------------------------------------###
- #过虑蠕虫病毒
- #444/445/69/135/139
- ###-----------------------------------------------------------------###
- iptables -A FORWARD -p tcp --dport 4444 -j DROP
- iptables -A FORWARD -p udp --dport 4444 -j DROP
- iptables -A FORWARD -p tcp --dport 445 -j DROP
- iptables -A FORWARD -p udp --dport 445 -j DROP
- iptables -A FORWARD -p tcp --dport 69 -j DROP
- iptables -A FORWARD -p udp --dport 69 -j DROP
- iptables -A FORWARD -p tcp --dport 135 -j DROP
- iptables -A FORWARD -p udp --dport 135 -j DROP
- iptables -A FORWARD -p tcp --dport 139 -j DROP
- iptables -A FORWARD -p udp --dport 139 -j DROP
- #允许ping localhost,ping 192.168.0.1/2
- #allow loopback access
- iptables -A INPUT -p icmp -i lo -j ACCEPT
- iptables -A OUTPUT -p icmp -o lo -j ACCEPT
- #打开内对内连接
- #iptables -A INPUT -i lo -j ACCEPT
- #允许代理和内网客户机相互传输数据(包括ping)
- #allow ping LAN
- iptables -A INPUT -p ALL -i $INT_IF -s $LAN_IP_RANGE -j ACCEPT
- iptables -A OUTPUT -p ALL -o $INT_IF -d $LAN_IP_RANGE -j ACCEPT
- #允许外网的网卡与内网相互通讯.接受数据只接受响应封包,否则不予放行.发送数据没有限制.
- iptables -A INPUT -p ALL -i $INT_IF -s $LAN_IP_RANGE -j ACCEPT
- iptables -A INPUT -p ALL -i $INT_IF -s $LAN_IP_RANGE -j ACCEPT
- #拒绝外部使用内网进行欺骗
- #deny local cheat
- iptables -A INPUT -i $EXT_IF -s 192.168.0.0/16 -j DROP
- iptables -A INPUT -i $EXT_IF -s 10.0.0.0/8 -j DROP
- iptables -A INPUT -i $EXT_IF -s 172.16.0.0/12 -j DROP
- iptables -A INPUT -i $EXT_IF -s 127.0.0.0/8 -j DROP
- #从LAN进入防火墙主机的dhcp封包,不于放行,只有防火墙担任DHCP时才放行
- #deny DHCP_packets from LAN
- iptables -A INPUT -p udp -i $INT_IF --dport 67 --sport 68 -j DROP
- ###-----------------------------------------------------------------------------------###
- #配置向外方向的TCP规则,其中,--state ESTABLISHED ,NEW参数指定要检查哪个状态.
- #ESTABLISHED标志匹配属于已有的TCP连接的封包.
- #NEW标志指定试图创建一条新的TCP连接的第一个封包,这条规则指明属于新建的和已建立的
- #TCP连接的封包将会通过eth0端口向外发送.
- ###-----------------------------------------------------------------------------------###
- iptables -A OUTPUT -o $EXT_IF -p tcp -m state --state ESTABLISHED,NEW -j ACCEPT
- ###----------------------------------------------------------------------------------###
- #配置封包从一个端口转发到另一个端口
- ###----------------------------------------------------------------------------------###
- iptables -A FORWARD -i $INT_IF -j ACCEPT
- # same to above 和上面的规则功能相同
- #iptables -A FORWARD -i $EXT_IF -m state --state ESTABLISHED,RELATED -j ACCEPT
- ###-------------------------------------------------------------------------------------###
- #检查到达外部网络接口的封包状态.属于已有TCP连接的封包都允许通过
- # 从WAN到LAN的封包仅放行回应封包
- ###-------------------------------------------------------------------------------------###
- iptables -A INPUT -i $EXT_IF -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
-
- # 限制过滤规则的比对频率为每分钟平均流量三个封包(超过上限的封包将暂停比对),
- #并将瞬间流量设定为一次最多处理三个封包(超过上限的封包将丢弃不予处理),
- #这类封包通常是黑客用来进行阻断式攻击
- iptables -A INPUT -m limit --limit 3/minute --limit-burst 3 -j LOG --log-level INFO --log-prefix "IPT INPUT packets died:"
- ###-------------------------------------------------------------------------###
- #不管来自哪里的ip碎片都进行控制,允许每秒通过100个碎片
- ###-------------------------------------------------------------------------###
- iptables -A FORWARD -f -m limit --limit 100/s --limit-burst 100 -j ACCEPT
- ###-------------------------------------------------------------------------###
- #icmp包通过的控制,防止icmp黑客攻击
- ###-------------------------------------------------------------------------###
- iptables -A FORWARD -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
- ###-------------------------------------------------------------------------###
- # 防止DDOS
- ###-------------------------------------------------------------------------###
- #iptables -A FORWARD -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j ACCEPT
- ###-------------------------------------------------------------------------###
- # UDP包一律放行
- # allow UDP
- ###-------------------------------------------------------------------------###
- iptables -A FORWARD -p udp -d $LAN_IP_RANGE -i $EXT_IF -j ACCEPT
- ###-------------------------------------------------------------------------###
- #根据mac屏蔽主机上网
- ###-------------------------------------------------------------------------###
- #iptables -t nat -I PREROUTING -m mac --mac-source 4C:00:10:D8:57:F3 -j DROP
- #modprode ip_conntrack
- #modprobe ip_conntrack_ftp
- #启用转发(forward)功能
- echo "enabling IP FORWARDING......"
- echo "1" > /proc/sys/net/ipv4/ip_forward
- #规则初始化,设置默认都为drop
- echo "enabling iptables rules"
- #reset the default policies in the tables
- iptables -F
- iptables -X
- iptables -F -t mangle
- iptables -X -t mangle
- iptables -F -t nat
- iptables -X -t nat
- iptables -Z -t nat
- #set policies
- ###-----------------------------------------------------###
- # 开放内部主机可以telnet至外部主 telnet port 23
- ###-----------------------------------------------------###
- #没必要打开23端口
- #iptables -A OUTPUT -o $EXT_IF -p tcp -s $FW_IP --sport 1024:65535 -d any/0 --dport 23 -j ACCEPT
- #iptables -A INPUT -i $EXT_IF -p tcp ! --syn -s any/0 --sport 23 -d $FW_IP --dport 1024:65535 -j ACCEPT
- ###-----------------------------------------------------###
- # 开放邮包转送通道 open SMTP port 25
- ###-----------------------------------------------------###
- #以下是别人可以送信给你
- iptables -A INPUT -i $EXT_IF -p tcp -s any/0 --sport 1024:65535 -d $FW_IP --dport 25 -j ACCEPT
- iptables -A OUTPUT -o $EXT_IF -p tcp ! --syn -s $FW_IP --sport 25 -d any/0 --dport 1024:65535 -j ACCEPT
- #以下是你可以送信给别人
- iptables -A OUTPUT -o $EXT_IF -p tcp -s $FW_IP --sport 1024:65535 -d any/0 --dport 25 -j ACCEPT
- iptables -A INPUT -i $EXT_IF -p tcp ! --syn -s any/0 --sport 25 -d $FW_IP --dport 1024:65525 -j ACCEPT
- ###-----------------------------------------------------###
- # 开放对外离线下载信件的通道 POP3 port 110
- ###-----------------------------------------------------###
- iptables -A OUTPUT -o $EXT_IF -p tcp -s $FW_IP --sport 1024:65535 -d any/0 --dport 110 -j ACCEPT
- iptables -A INPUT -i $EXT_IF -p tcp ! --syn -s any/0 --sport 110 -d $FW_IP --dport 1024:65535 -j ACCEPT
- ###-----------------------------------------------------###
- # 开放浏览网页的通道 http port 80
- ###-----------------------------------------------------###
- iptables -A OUTPUT -o $EXT_IF -p tcp -s $FW_IP --sport 1024:65535 -d any/0 --dport 80 -j ACCEPT
- iptables -A INPUT -i $EXT_IF -p tcp ! --syn -s any/0 --sport 80 -d $FW_IP --dport 1024:65535 -j ACCEPT
- ###-----------------------------------------------------###
- # 开放查询外部网络的DNS主机 DNS port:53
- ###-----------------------------------------------------###
- #第一次会用udp封包来查询
- iptables -A OUTPUT -o $EXT_IF -p udp -s $FW_IP --sport 1024:65535 -d any/0 --dport 53 -j ACCEPT
- iptables -A INPUT -i $EXT_IF -p udp -s any/0 --sport 53 -d $FW_IP --dport 1024:65535 -j ACCEPT
- #若有错误,会改用tcp包来查询
- iptables -A OUTPUT -o $EXT_IF -p tcp -s $FW_IP --sport 1024:65535 -d any/0 --dport 53 -j ACCEPT
- iptables -A INPUT -i $EXT_IF -p tcp ! --syn -s any/0 --sport 53 -d $FW_IP --dport 1024:65535 -j ACCEPT
- #开放这台主机上的DNS和外部的DNS主机互动查询:使用udp
- iptables -A OUTPUT -o $EXT_IF -p udp -s $FW_IP --sport 53 -d any/0 --dport 53 -j ACCEPT
- iptables -A INPUT -i $EXT_IF -p udp -s any/0 --sport 53 -d $FW_IP --dport 53 -j ACCEPT
- #开放这台主机上的DNS和外部的DNS主机互动查询:使用udp
- iptables -A OUTPUT -o EXT_IF -p tcp -s $FW_IP --sport 53 -d any/0 --dport 53 -j ACCEPT
- iptables -A INPUT -i EXT_IF -p tcp ! --syn -s any/0 --sport 53 -d $FW_IP --dport 53 -j ACCEPT
- ###------------------------------------------------------------------------###
- #开放内部主机可以SSH至外部的主机 SSH port:22
- ###------------------------------------------------------------------------###
- iptables -A OUTPUT -o $EXT_IF -p tcp -s $FW_IP --sport 1024:65535 -d any/0 --dport 22 -j ACCEPT
- iptables -A INPUT -i $EXT_IF -p tcp ! --syn -s any/0 --sport 22 -d $FW_IP --dport 1024:65535 -j ACCEPT
- #以下是SSH protocol比较不同的地方
- iptables -A OUTPUT -o $EXT_IF -p tcp -s $FW_IP --sport 1020:1023 -d any/0 --dport 22 -j ACCEPT
- iptables -A INPUT -i $EXT_IF -p tcp ! --syn -s any/0 --sport 22 -d $FW_IP --dport 1020:1023 -j ACCEPT
- ###------------------------------------------------------------------------###
- ###开放内部网络,可以ftp至外部主机
- ###------------------------------------------------------------------------###
- #以下是打开命令 channel 21
- iptables -A OUTPUT -o $EXT_IF -p tcp -s $FW_IP --sport 1024:65535 -d any/0 --dport 21 -j ACCEPT
- iptables -A INPUT -i $EXT_IF -p tcp ! --syn -s any/0 --sport 21 -d $FW_IP --dport 1024:65535 -j ACCEPT
-
- #以下是打开资料 channel 20
- iptables -A INPUT -i $EXT_IF -p tcp -s any/0 --sport 20 -d $FW_IP --dport 1024:65535 -j ACCEPT
- iptables -A OUTPUT -o $EXT_IF -p tcp ! --syn -s $FW_IP --sport 1024:65535 -d any/0 --dport 20 -j ACCEPT
- #以下是打开 passive mode FTP 资料通道
- iptables -A OUTPUT -o $EXT_IF -p tcp -s $FW_IP --sport 1024:65535 -d any/0 --dport 1024:65535 -j ACCEPT
- iptables -A INPUT -i $EXT_IF -p tcp ! --syn -s any/0 --sport 1024:65535 -d $FW_IP --dport 1024:65535 -j ACCEPT
- #-------------------------------------NAT------------------------------------------------
- #透明代理设定:将WWW服务转向squid
- iptables -t nat -A PREROUTING -i eth1 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 3128
- ###-------------------------------------------------------------------------###
- #启动内部对外部转址:源网络地址转换SNAT
- ###-------------------------------------------------------------------------###
- iptables -t nat -A POSTROUTING -o $EXT_IF -s $LAN_IP_RANGE -j SNAT --to $FW_IP
- ###-------------------------------------------------------------------------###
- #启动外部对内部转址(设置内网WWWW服务器映射)DNAT
- ###-------------------------------------------------------------------------###
- iptables -t nat -A PREROUTING -i $EXT_IF -p tcp -d $FW_IP --dport 80 -j DNAT --to 192.168.0.16:80
复制代码 |
|