最阳春的防火墙!
时间:2007-02-03 来源:ppzlyg
[root@linux ~]# mkdir -p /usr/local/ppz/iptables
[root@linux ~]# cd /usr/local/ppz/iptables
[root@linux iptables]# vi iptables.rule
#!/bin/bash
# 请先输入您的相关参数,不要输入错误了!
EXTIF="eth1" # 这个是可以连上 Public IP 的网络接口
INIF="eth0" # 内部 LAN 的连接接口;若无请填 ""
INNET="192.168.1.0/24" # 内部 LAN 的网域,若没有内部 LAN 请设定为 ""
export EXTIF INIF INNET
# 第一部份,针对本机的防火墙设定!
# 1. 先设定好核心的网络功能:
echo "1" > /proc/sys/net/ipv4/tcp_syncookies
echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
for i in /proc/sys/net/ipv4/conf/*/rp_filter; do
echo "1" > $i
done
for i in /proc/sys/net/ipv4/conf/*/log_martians; do
echo "1" > $i
done
for i in /proc/sys/net/ipv4/conf/*/accept_source_route; do
echo "0" > $i
done
for i in /proc/sys/net/ipv4/conf/*/accept_redirects; do
echo "0" > $i
done
for i in /proc/sys/net/ipv4/conf/*/send_redirects; do
echo "0" > $i
done
# 2. 清除规则、设定预设政策及开放 lo 与相关的设定值
PATH=/sbin:/usr/sbin:/bin:/usr/bin; export PATH
iptables –F #清除所有的已订定的规则
iptables –X #杀掉所有使用者 "自订" 的 chain (应该说的是 tables )啰;
iptables –Z #将所有的 chain 的计数与流量统计都归零
#将本机的 INPUT 设定为 DROP ,其它设定为 ACCEPT
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state RELATED -j ACCEPT
#RELATED :这个最常用!表示这个封包是与我们主机发送出去的封包有关
iptables -A INPUT -m state --state INVALID -j DROP
INVALID :无效的封包,例如数据破损的封包状态
# 3. 启动额外的防火墙 script 模块
if [ -f /usr/local/virus/iptables/iptables.deny ]; then
sh /usr/local/virus/iptables/iptables.deny
fi
if [ -f /usr/local/virus/iptables/iptables.allow ]; then
sh /usr/local/virus/iptables/iptables.allow
fi
if [ -f /usr/local/virus/httpd-err/iptables.http ]; then
sh /usr/local/virus/httpd-err/iptables.http
fi
iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT
#ESTABLISHED:已经联机成功的联机状态;
# 4. 允许某些类型的 ICMP 封包进入
AICMP="0 3 3/4 4 11 12 14 16 18"
for tyicmp in $AICMP
do
iptables -A INPUT -i $EXTIF -p icmp --icmp-type $tyicmp -j ACCEPT
done
# 5. 允许某些服务的进入,请依照您自己的环境开启
# iptables -A INPUT -p TCP -i $EXTIF --dport 22 -j ACCEPT # SSH
# iptables -A INPUT -p TCP -i $EXTIF --dport 25 -j ACCEPT # SMTP
# iptables -A INPUT -p UDP -i $EXTIF --sport 53 -j ACCEPT # DNS
# iptables -A INPUT -p TCP -i $EXTIF --sport 53 -j ACCEPT # DNS
# iptables -A INPUT -p TCP -i $EXTIF --dport 80 -j ACCEPT # WWW
# iptables -A INPUT -p TCP -i $EXTIF --dport 110 -j ACCEPT # POP3
# iptables -A INPUT -p TCP -i $EXTIF --dport 443 -j ACCEPT # HTTPS
# 第二部份,针对后端主机的防火墙设定!
# 1. 先加载一些有用的模块
modules="ip_tables iptable_nat ip_nat_ftp ip_nat_irc ip_conntrack
ip_conntrack_ftp ip_conntrack_irc"
for mod in $modules
do
testmod='lsmod | grep "${mod} "'
if [ "$testmod" == "" ]; then
modprobe $mod
fi
done
# 2. 清除 NAT table 的规则吧!
iptables -F -t nat
iptables -X -t nat
iptables -Z -t nat
iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -P POSTROUTING ACCEPT
iptables -t nat -P OUTPUT ACCEPT
# 3. 开放成为路由器,且为 IP 分享器!
if [ "$INIF" != "" ]; then
iptables -A INPUT -i $INIF -j ACCEPT
echo "1" > /proc/sys/net/ipv4/ip_forward
if [ "$INNET" != "" ]; then
for innet in $INNET
do
iptables -t nat -A POSTROUTING -s $innet -o $EXTIF -j MASQUERADE
done
fi
fi
# 如果你的 MSN 一直无法联机,或者是某些网站 OK 某些网站不 OK,
# 可能是 MTU 的问题,那你可以将底下这一行给他取消批注来启动 MTU 限制范围
# iptables -A FORWARD -p tcp -m tcp --tcp-flags SYN,RST SYN -m tcpmss \
# --mss 1400:1536 -j TCPMSS --clamp-mss-to-pmtu
# 4. 内部服务器的设定:
# iptables -t nat -A PREROUTING -p tcp -i $EXTIF --dport 80 \
# -j DNAT --to 192.168.1.210:80
特别留意上面程序代码的特殊字体部分,基本上,你只要修改一下最上方的接口部分, 应该就能够运作这个防火墙了。不过因为每个人的环境都不相同, 因此你在设定完成后,依旧需要测试一下才行喔!不然,出了问题不要怪我啊!.... 再来看一下关于 iptables.allow 的内容是如何?假如我要让一个 140.116.44.0/24 这个网域的所有主机来源可以进入我的主机的话,那么这个档案的内容可以写成这样:
[root@linux iptables]# vi iptables.allow
#!/bin/bash
# 底下则填写你允许进入本机的其它网域或主机啊!
iptables -A INPUT -i $EXTIF -s 140.116.44.0/24 -j ACCEPT
# 底下则是关于抵挡的档案设定法!
[root@linux iptables]# vi iptables.deny
#!/bin/bash
# 底下填写的是『你要抵挡的那个咚咚!』
iptables -A INPUT -i $EXTIF -s 140.116.44.254 -j DROP
[root@linux iptables]# chmod 700 iptables.*
将这三个档案的权限设定为 700 且只属于 root 的权限后,就能够直接执行 iptables.rule 啰! 不过要注意的是,在上面的案例当中,鸟哥预设将所有的服务的通道都是关闭的! 所以你必须要到本机防火墙的第 5 步骤处将一些批注符号 (#) 解开才行。 同样的,如果有其它更多的 port 想要开启时,一样需要增加额外的规则才行喔!
不过,还是如同前面我们所说的,这个 firewall 仅能提供基本的安全防护,其它的相关问题还需要再测试测试呢! 此外,如果你希望一开机就自动执行这个 script 的话,请将这个档案的完整档名写入 /etc/rc.d/rc.local 当中,有点像底下这样:
[root@linux ~]# vi /etc/rc.d/rc.local
.....其它省略.....
# 1. Firewall
/usr/local/ppz/iptables/iptables.rule
.....其它省略.....
补:
范例:将要由 eth1 传送出去的封包,封包来源改为 192.168.200.250
[root@linux ~]# iptables -t nat -A POSTROUTING -o eth1 \
> -j SNAT --to 192.168.200.250
范例:同上,但封包来源为 192.168.200.210~220
[root@linux ~]# iptables -t nat -A POSTROUTING -o eth1 \
> -j SNAT --to 192.168.200.210-192.168.200.210
范例:将连接到 eth1 接口的 port 80 传导到内部的 192.168.1.210
[root@linux ~]# iptables -t nat -A PREROUTING -p tcp -i eth1 \
> --dport 80 -j DNAT --to 192.168.1.210:80
-j REDIRECT --to-ports <port number>
# 这个也挺常见的,基本上,就是进行本机上面 port 的转换就是了!
# 不过,特别留意的是,这个动作仅能够在 nat table 的 PREROUTING 以及
# OUTPUT 链上面实行而已喔!
范例:将要求与 80 联机的封包转递到 8080 这个 port
[root@linux ~]# iptables -t nat -A PREROUTING -p tcp --dport 80 \
> -j REDIRECT --to-ports 8080
# 这玩意最容易在您使用了非正规的 port 来进行某些 well known 的协议,
# 例如使用 8080 这个 port 来启动 WWW ,但是别人都以 port 80 来联机,
# 所以,您就可以使用上面的方式来将对方对您主机的联机传递到 8080 啰!
因为 DNS 的来源是 port 53 ,因此要接受来自 port 53 的封包就成为了:
iptables -A INPUT -p udp --sport 53 -j ACCEPT
iptables -A INPUT -p tcp --sport 53 -j ACCEPT
学习更新中------