LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 933|回复: 11

关于iptables规则的问题!!!

[复制链接]
发表于 2003-6-4 21:02:45 | 显示全部楼层 |阅读模式
服务器:eth0 -> 210.52.*.*
        eth1 -> 192.168.102.1

要达到这样的效果:
1 永远不屏蔽eth1(让内网的用户不受任何限制)
2 只开eth0的80,21,25,110端口。
请问iptables 规则怎么写???


我尝试着这样写:
iptables -P INPUT -i eth0 ACCEPT
iptables -P INPUT 0i eth1 DROP
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
.....
但是执行第一个就报错,说什么“requires a chain and policy"
摸索了半天,要这样才行:iptables -A INPUT -i eth1
但这样不行啊,没有表达我要把eth1全部关闭的意思啊,

请各路神仙帮帮忙,先谢了。
发表于 2003-6-5 08:49:16 | 显示全部楼层
iptables -P INPUT DROP
iptables -A INPUT -i eth1 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --dport www -j ACCEPT
iptables -A INPUT -f -j ACCEPT  ;允许碎片通过
大概这样,内部可以自由访问服务器,外部只能访问80端口。其它的你自己加吧
发表于 2003-6-5 09:34:27 | 显示全部楼层
我这里有一个成功的链:

# want to do the full Sys V style init stuff.
echo "1" > /proc/sys/net/ipv4/ip_forward
LAN_IP_RANCE="192.168.1.0/24"
LAN_IP="192.168.1.1/32"
LAN_BCAST_ADRESS="192.168.1.255/32"
LOCALHOST_IP="127.0.0.1/32"
STATIC_IP="202.111.189.115/255.255.255.128"
INET_IFACE="eth0"
LAN_IFACE="eth1"
IPTABLES="/sbin/iptables"

/sbin/depmod -a
/sbin/modprobe ipt_LOG
/sbin/modprobe ipt_MASQUERADE
$IPTABLES -t nat -A POSTROUTING -o $INET_IFACE -j MASQUERADE
$IPTABLES -A FORWARD -p TCP ! --syn -m state --state NEW -j LOG --log-prefix "New not syn:"
$IPTABLES -A FORWARD -p TCP ! --syn -m state --state New -j DROP
$IPTABLES -A FORWARD -i $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A FORWARD -m limit --limit 3/minute --limit-burst 3 -j LOG --log-level DEBUG --log-prefix "IPT FORWARD packet died:"
$IPTABLES -P INPUT DROP
$IPTABLES -P OUTPUT DROP
$IPTABLES -P FORWARD DROP
$IPTABLES -N icmp_packets
$IPTABLES -N tcp_packets
$IPTABLES -N udpincoming_packets
$IPTABLES -N allowed
$IPTABLES -A allowed -p TCP --syn -j ACCEPT
$IPTABLES -A allowed -p TCP -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A allowed -p TCP -j DROP
$IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 0 -j ACCEPT
$IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 3 -j ACCEPT
$IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 5 -j ACCEPT
$IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 11 -j ACCEPT
$IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 21 -j allowed
$IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 22 -j allowed
$IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 80 -j allowed
$IPTABLES -A tcp_packets -p TCP -s 0/0 --dport 113 -j allowed
$IPTABLES -A udpincoming_packets -p UDP -s 0/0 --source-port 53 -j ACCEPT
$IPTABLES -A udpincoming_packets -p UDP -s 0/0 --source-port 123 -j ACCEPT
$IPTABLES -A udpincoming_packets -p UDP -s 0/0 --source-port 2074 -j ACCEPT
$IPTABLES -A udpincoming_packets -p UDP -s 0/0 --source-port 4000 -j ACCEPT
$IPTABLES -A udpincoming_packets -p UDP -s 0/0 --source-port 8000 -j ACCEPT

$IPTABLES -t nat -A PREROUTING -i $INET_IFACE -s 192.168.1.0/24 -j DROP
$IPTABLES -t nat -A PREROUTING -i $INET_IFACE -s 10.0.0.0/8 -j DROP
$IPTABLES -t nat -A PREROUTING -i $INET_IFACE -s 172.16.0.0/12 -j DROP

$IPTABLES -A INPUT -p TCP ! --syn -m state --state NEW -j LOG --log-prefix "New not syn:"
$IPTABLES -A INPUT -p TCP ! --syn -m state --state NEW -j DROP

$IPTABLES -A INPUT -p ICMP -i $INET_IFACE -j icmp_packets
$IPTABLES -A INPUT -p TCP -i $INET_IFACE -j tcp_packets
$IPTABLES -A INPUT -p UDP -i $INET_IFACE -j udpincoming_packets

$IPTABLES -A INPUT -p ALL -i $LAN_IFACE -d $LAN_BCAST_ADRESS -j ACCEPT
$IPTABLES -A INPUT -p ALL -d $LOCALHOST_IP -j ACCEPT
$IPTABLES -A INPUT -p ALL -d $LAN_IP -j ACCEPT
$IPTABLES -A INPUT -p ALL -d $STATIC_IP -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A INPUT -m limit --limit 3/minute --limit-burst 3 -j LOG --log-level DEBUG --log-prefix "IPT INPUT packet died:"

$IPTABLES -A OUTPUT -p tcp ! --syn -m state --state NEW -j LOG --log-prefix "NEW NOT SYN:"
$IPTABLES -A OUTPUT -p tcp ! --syn -m state --state NEW -j DROP

$IPTABLES -A OUTPUT -p ALL -s $LOCALHOST_IP -j ACCEPT
$IPTABLES -A OUTPUT -p ALL -s $LAN_IP -j ACCEPT
$IPTABLES -A OUTPUT -p ALL -s $STATIC_IP -j ACCEPT
$IPTABLES -A OUTPUT -m limit --limit 3/minute --limit-burst 3 -j LOG --log-level DEBUG --log-prefix "IPT OUTPUT packet died:"
发表于 2003-6-5 09:50:27 | 显示全部楼层
不错!拿回去研究一下~
 楼主| 发表于 2003-6-5 09:59:31 | 显示全部楼层

谢谢两位大侠的回复!!!

yingda大侠的回复好多啊,呵呵,我们局域网不需要做NAT,只是让他们可以无限制的上网就够了。

主要是防止来自外部的恶意攻击,按照stanlogin大侠的说法,我重写的规则:
iptables -F
iptables -P INPUT DROP
iptables -A INPUT -p all -d 192.168.102.0/24 -j ACCEPT
iptables -A INPUT -p all -s 192.168.102.0/24 -j ACCEPT
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 25 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 110 -j ACCEPT
iptables -A INPUT -f -j ACCEPT

有一点,我不是太懂,就是只允许碎片通过有什么意义??
还有,像我这样写,内部主机可不可以用QQ,MSN等等 即时通讯软件呢,这些软件都是udp吧?? 是不是应该还加一条呀???

期待回复中。。。。
发表于 2003-6-5 10:18:30 | 显示全部楼层
碎片就是火车的车厢,我们只能、而且只需要控制车头。

关于访问控制,不好意思,我感觉有些误导你了,我写的只是对访问那台服务器本身的控制~
要实现你的目的,大致需要这样:
首先要保证内部可上外网,这需要NAT。
其次对内部到外部的访问进行控制。
再次对外部到内部的访问进行控制。

我还没做这样的例子,所以没有现成的代码给你,yingda贴的那段代码应该不错,就算不能解决你的问题,也是不错的学习资料,结合下面这个网址看看:
http://cmpp.linuxforum.net/NetSnake/

我正好要找时间做一个相关例子,到时再贴出来(大概1、2天内吧)~
 楼主| 发表于 2003-6-5 11:33:27 | 显示全部楼层

帮我解答一下,谢谢!!!

我的局域网内主机因为不需要向外部提供任何服务,所以只需要实现
包在出去时离开firewall时转成公网IP,
包在进来时碰到firewall时转成私网IP,
这两条语句怎么写呀,

还有,现在我的规则是这样的:
iptables -F
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -A INPUT -p all -d 192.168.102.0/24 -j ACCEPT
iptables -A INPUT -p all -s 192.168.102.0/24 -j ACCEPT
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 25 -j ACCEPT
#iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 110 -j ACCEPT
iptables -A INPUT -f -j ACCEPT

iptables -A INPUT -i lo -j ACCEPT
这个时候,我的服务器可以ping通它自己的eth0,eth1,lo,但是!,但是它ping不通外网!!!它不能上网,那就隔屁了呀,请问是不是少了条规则呀,
这样加是可以的:iptables -A INPUT -i eth0 -j ACCEPT
但这样就失去屏蔽外网恶意攻击的意义了呀。各路神仙帮帮忙,先谢了
 楼主| 发表于 2003-6-5 11:40:36 | 显示全部楼层

补充

稍瑞,刚才说错了,服务器ping不通eth0,不能和外面建立任何联系,包括telnet,ftp internet上的任何节点都不行。
发表于 2003-6-5 12:02:17 | 显示全部楼层
ping 也是需要往复的,没有INPUT ACCEPT当然不行。
别着急,我也在做这东西,等我测试好了就贴出来~
 楼主| 发表于 2003-6-5 12:51:22 | 显示全部楼层

解决问题的暂行办法:

加iptables -A INPUT -i lo -j ACCEPT  让,服务器ping通127.0.0.1
加iptables -A INPUT -p icmp -j ACCEPT 让服务器ping通外网的任意节点

在上面对应于每个端口控制时,加一条语句:
iptables -A INPUT -p tcp --sport 21 -j ACCEPT
现在我的规则如下:
iptables -F
iptables --table nat --flush
iptables --table nat --delete-chain
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -A INPUT -p all -d 192.168.102.0/24 -j ACCEPT
iptables -A INPUT -p all -s 192.168.102.0/24 -j ACCEPT
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --sport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --sport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 25 -j ACCEPT
iptables -A INPUT -p tcp --sport 25 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --sport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 110 -j ACCEPT
iptables -A INPUT -p tcp --sport 110 -j ACCEPT
iptables -A INPUT -f -j ACCEPT

iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p icmp -j ACCEPT

经过我检查:
服务器可以ping通任何节点,亦可访问外网节点的80,21,25,110.
局域网内主机可以ping通任何节点,亦可访问服务器或外网节点的80,21,25,110
外网节点可以ping通服务器,也可以访问服务器的80,21,25,110

但局域网内的主机还没有检查能否使用QQ等使用udp协议的情况

稍等,检查后再贴出来。

to: stanlogin 期待你的文章啊,,,
还有,能不能帮我检查一下我的规则是否有漏洞??我马上就要交差了:)
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表