免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2424 | 回复: 0
打印 上一主题 下一主题

IPTABLES中文手册 [2008年12月02日] [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-02 14:49 |只看该作者 |倒序浏览

                                file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
6.4.3.5. Owner match
基于包的生成者(也就是所有者,或称作拥有者,owner)的ID来匹配包,owner可以是启动
进程的用户的ID,或用户所在的组的ID,或进程的ID,或会话的ID。这个扩展原本只是为了
说明iptables可以做什么,现在发展到实用阶段了。但要注意,此扩展只能用在OUTPUT中,
原因显而易见:我们几乎不可能得到发送端例程的ID的任何信息,或者在去往真正目的地的
路上哪儿有路由。甚至在 OUTPUT链里,这也不是十分可靠,因为有些包根本没有owner,比
如 ICMP responses,所以它们从不会被这个match抓到:)
Table 6-12. Owner match options
6.4.3.6. State match
状态匹配扩展要有内核里的连接跟踪代码的协助,因为它是从连接跟踪机制中得到包的状态
的。这样我们就可以了解连接所处的状态。它几乎适用于所有的协议,包括那些无状态的协
议,如ICMP和UDP。针对每个连接都有一个缺省的超时值,如果连接的时间超过了这个值,那
么这个连接的记录就被会从连接跟踪的记录数据库中删除,也就是说连接就不再存在了。这
个match必须有-m state作为前提才能使用。状态机制的详细内容在章节状态机制 中。
Example iptables -A INPUT -p tcp -m multiport --port 22,53,80,110
Explanation 同端口多端口匹配,意思就是它匹配的是那种源端口和目的端口是同一个端口
的包,比如:端口80到端口80的包,110到110的包等。使用方法和源端口多端
口匹配一样。
Match --uid-owner
Example iptables -A OUTPUT -m owner --uid-owner 500
Explanation 按生成包的用户的ID(UID)来匹配外出的包。使用这个匹配可以做这样一些
事,比如,阻止除root外的用户向防火墙外建立新连接,或阻止除用户http外
的任何人使用HTTP端口发送数据。
Match --gid-owner
Example iptables -A OUTPUT -m owner --gid-owner 0
Explanation 按生成包的用户所在组的ID(GID)来匹配外出的包。比如,我们可以只让属
于network组的用户上Internet,而其他用户都不行;或者只允许http组的成员
能从HTTP端口发送数据。
Match --pid-owner
Example iptables -A OUTPUT -m owner --pid-owner 78
Explanation 按生成包的进程的ID(GID)来匹配外出的包。比如,我们可以只允许PID为94
的进程(http进程当然不能是多线程的)使用http端口。这个匹配使用起来有
一点难度,因为你要知道进程的ID号。当然,你也可以写一个小小的脚本,先
从ps的输出中得到PID,再添加相应的规则,这儿有个例子Pid-owner.txt。
Match --sid-owner
Example iptables -A OUTPUT -m owner --sid-owner 100
Explanation 按生成包的会话的ID(SID)来匹配外出的包。一个进程以及它的子进程或它
的多个线程都有同一个SID。比如,所有的HTTPD进程的SID和它的父进程一样
(最初的 HTTPD进程),即使HTTPD是多线程的(现在大部分都是,比如
Apache和Roxen)也一样。这里有个脚本Sid-owner.txt可以反映出这一点。
Iptables 指南 1.1.19 页码,42/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
Table 6-13. State matches
6.4.3.7. TOS match
根据TOS字段匹配包,必须使用-m tos才能装入。TOS是IP头的一部分,其含义是Type Of
Service,由8个二进制位组成,包括一个3 bit的优先权子字段(现在已被忽略),4 bit的
TOS子字段和1 bit未用位(必须置0)。它一般用来把当前流的优先权和需要的服务(比如,
最小延时、最大吞吐量等)通知路由器。但路由器和管理员对这个值的处理相差很大,有的
根本就不理会,而有的就会尽量满足要求。
Table 6-14. TOS matches
6.4.3.8. TTL match
Match --state
Example iptables -A INPUT -m state --state RELATED,ESTABLISHED
Explanation 指定要匹配包的的状态,当前有4种状态可用:INVALID,ESTABLISHED,NEW和
RELATED。 INVALID意味着这个包没有已知的流或连接与之关联,也可能是它
包含的数据或包头有问题。ESTABLISHED意思是包是完全有效的,而且属于一
个已建立的连接,这个连接的两端都已经有数据发送。NEW表示包将要或已经
开始建立一个新的连接,或者是这个包和一个还没有在两端都有数据发送的连
接有关。RELATED说明包正在建立一个新的连接,这个连接是和一个已建立的
连接相关的。比如,FTP data transfer,ICMP error 和一个TCP或UDP连接相
关。注意NEW状态并不在试图建立新连接的TCP包里寻找SYN标记,因此它不应
该不加修改地用在只有一个防火墙或在不同的防火墙之间没有启用负载平衡的
地方。具体如何使用,你就再看看章节状态机制吧:)
Match --tos
Example iptables -A INPUT -p tcp -m tos --tos 0x16
Explanation 根据TOS字段匹配包。这个match常被用来mark包,以便后用,除此之外,它还
常和iproute2或高级路由功能一起使用。它的参数可以是16进制数,也可以是
十进制数,还可以是相应的名字(用 iptables -m tos -h能查到)。到写这
篇文章时,有以下参数可用:
Minimize-Delay 16 (0x10),要求找一条路径使延时最小,一些标准服务如
telnet、SSH、FTP-control 就需要这个选项。
Maximize-Throughput 8 (0x08),要求找一条路径能使吞吐量最大,标准服务
FTP-data能用到这个。
Maximize-Reliability 4 (0x04),要求找一条路径能使可靠性最高,使用它
的有BOOTP和TFTP。
Minimize-Cost 2 (0x02),要求找一条路径能使费用最低,一般情况下使用这
个选项的是一些视频音频流协议,如RTSP(Real Time Stream Control
Protocol)。
Normal-Service 0 (0x00),一般服务,没有什么特殊要求。
Iptables 指南 1.1.19 页码,43/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
根据IP头里的TTL (Time To Live,即生存期)字段来匹配包,此必须由-m ttl装入。TTL
field是一个字节(8个二进制位),一旦经过一个处理它的路由器,它的值就减去1它的值。
当该字段的值减为0时,报文就被认为是不可转发的,数据报就被丢弃,并发送ICMP报文通知
源主机,不可转发的报文被丢弃。这也有两种情况,一是传输期间生存时间为0,使用类型为
11代码是0的ICMP报文;二是在数据报重组期间生存时间为0,使用类型为11代码是1的ICMP报
文。这个match只是根据TTL匹配包,而对其不做任何更改,所以在它之后可使用任何类型的
match。
Table 6-15. TTL matches
6.4.4. 针对非正常包的匹配
这个匹配没有任何参数,也不需要显式地装载。注意这应该被看作是一个实验性的匹配,它
不总是能正常工作的,对有些不正常的包(unclean package,就是所谓的脏包)或问题,它
是视而不见的。这个match 试图匹配那些好象畸形或不正常的包,比如包头错或校验和错,
等等。它可能常用来DROP错误的连接、检查有错的流,但要知道这样做也可能会中断合法的
连接。
6.5. Targets/Jumps
target/jump决定符合条件的包到何处去,语法是--jump target或-j target。(译者注:本
文中,原作者把target细分为两类,即Target和Jump。它们唯一的区别是jump的目标是一个
在同一个表内的链,而target的目标是具体的操作。)我们会先接触到两个基本的target,
就是ACCEPT和DROP。
前面提到过用户自定义链要用到-N命令。下面我们在filter表中建一个名为tcp_packets的
链:
iptables -N tcp_packets
然后再把它作为jump的目标:
iptables -A INPUT -p tcp -j tcp_packets
这样我们就会从INPUT链跳入tcp_packets链,开始在tcp_packets中的旅行。如果到达了
tcp_packets链的结尾(也就是未被链中的任何规则匹配),则会退到INPUT链的下一条规则
继续它的旅行。如果在子链中被ACCEPT了,也就相当于在父链中被ACCEPT了,那么它不会再
经过父链中的其他规则。但要注意这个包能被其他表的链匹配,过程可查看章节 表和链。
Match --ttl
Example iptables -A OUTPUT -m ttl --ttl 60
Explanation 根据TTL的值来匹配包,参数的形式只有一种,就是十进制数值。它可以被用
来调试你的局域网,比如解决LAN内的主机到Internet上的主机的连接问题,
或找出 Trojan(Trojan)可能的入口。这个match的用处相对有限,但它其实
是很有用的,这就看你的想象力如何了。比如可以用它来发现那些TTL具有错
误缺省值的机子(这可能是实现TCP/IP栈功能的那个程序本身的错误,或者是
配置有问题)。
Iptables 指南 1.1.19 页码,44/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
target指定我们要对包做的操作,比如DROP和ACCEPT,还有很多,我们后面会介绍。不同的
target有不同的结果。一些target会使包停止前景,也就是不再继续比较当前链中的其他规
则或父链中的其他规则,最好的例子就是DROP和ACCEPT。而另外一些target在对包做完操作
之后,包还会继续和其他的规则比较,如LOG,ULOG和TOS。它们会对包进行记录、mangle,
然后让包通过,以便匹配这条链中的其他规则。有了这样的target,我们就可以对同一个包既
改变它的TTL又改变它的TOS。有些target必须要有准确的参数(如TOS需要确定的数值),有
些就不是必须的,但如果我们想指定也可以(如日志的前缀,伪装使用的端口,等等)。本
节我们会尽可能全面地介绍每一个target。现在我们就来看看有哪几种target。
6.5.1. ACCEPT target
这个target没有任何选项和参数,使用也很简单,指定-j ACCEPT即可。一旦包满足了指定的
匹配条件,就会被ACCEPT,并且不会再去匹配当前链中的其他规则或同一个表内的其他规
则,但它还要通过其他表中的链,而且在那儿可能会百DROP也说不准哦。
6.5.2. DNAT target
这个target是用来做目的网络地址转换的,就是重写包的目的IP地址。如果一个包被匹配
了,那么和它属于同一个流的所有的包都会被自动转换,然后就可以被路由到正确的主机或
网络。DNAT target是非常有用的。比如,你的Web服务器在LAN内部,而且没有可在Internet
上使用的真实IP地址,那就可以使用这个 target让防火墙把所有到它自己HTTP端口的包转发
给LAN内部真正的Web服务器。目的地址也可以是一个范围,这样的话,DNAT会为每一个流随
机分配一个地址。因此,我们可以用这个target做某种类型地负载平衡。
注意,DANT target只能用在nat表的PREROUTING和OUTPUT链中,或者是被这两条链调用的链
里。但还要注意的是,包含DANT target的链不能被除此之外的其他链调用,如
POSTROUTING。
Table 6-16. DNAT target
因为DNAT要做很多工作,所以我要再罗嗦一点。我们通过一个例子来大致理解一下它是如何
工作的。比如,我想通过Internet连接发布我们的网站,但是HTTP server在我们的内网里,
而且我们对外只有一个合法的IP,就是防火墙那个对外的IP——$INET_IP。防火墙还有一个
内网的IP——$LAN_IP,HTTP server的IP是$HTTP_IP (这当然是内网的了)。为了完成我们
Option --to-destination
Example iptables -t nat -A PREROUTING -p tcp -d 15.45.23.67 --dport 80 -j
DNAT --to-destination 192.168.1.1-192.168.1.10
Explanation 指定要写入IP头的地址,这也是包要被转发到的地方。上面的例子就是把所有
发往地址15.45.23.67的包都转发到一段LAN使用的私有地址中,即
192.168.1.1到 192.168.1.10。如前所述,在这种情况下,每个流都会被随机
分配一个要转发到的地址,但同一个流总是使用同一个地址。我们也可以只指
定一个IP地址作为参数,这样所有的包都被转发到同一台机子。我们还可以在
地址后指定一个或一个范围的端口。比如:--to-destination
192.168.1.1:80或 --to-destination 192.168.1.1:80-100。SNAT的语法和这
个target的一样,只是目的不同罢了。要注意,只有先用--protocol指定了
TCP或 UDP协议,才能使用端口。
Iptables 指南 1.1.19 页码,45/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
的设想,要做的第一件事就是把下面的这个简单的规则加入到nat表的PREROUTING链中:
iptables -t nat -A PREROUTING --dst $INET_IP -p tcp --dport 80 -j DNAT \ --todestination
$HTTP_IP
现在,所有从Internet来的、到防火墙的80端口去的包都会被转发(或称做被DNAT )到在内
网的HTTP服务器上。如果你在Internet上试验一下,一切正常吧。再从内网里试验一下,完
全不能用吧。这其实是路由的问题。下面我们来好好分析这个问题。为了容易阅读,我们把
在外网上访问我们服务器的那台机子的IP地址记为$EXT_BOX。
1. 包从地址为$EXT_BOX的机子出发,去往地址为$INET_IP 的机子。
2. 包到达防火墙。
3. 防火墙DNAT(也就是转发)这个包,而且包会经过很多其他的链检验及处理。
4. 包离开防火墙向$HTTP_IP前进。
5. 包到达HTTP服务器,服务器就会通过防火墙给以回应,当然,这要求把防火墙作为HTTP
到达$EXT_BOX的网关。一般情况下,防火墙就是HTTP服务器的缺省网关。
6. 防火墙再对返回包做Un-DNAT(就是照着DNAT的步骤反过来做一遍),这样就好像是防
火墙自己回复了那个来自外网的请求包。
7. 返回包好象没经过这么复杂的处理、没事一样回到$EXT_BOX。
现在,我们来考虑和HTTP服务器在同一个内网(这里是指所有机子不需要经过路由器而可以
直接互相访问的网络,不是那种把服务器和客户机又分在不同子网的情况)的客户访问它时
会发生什么。我们假设客户机的IP为$LAN_BOX,其他设置同上。
1. 包离开$LAN_BOX,去往$INET_IP。
2. 包到达防火墙。
3. 包被DNAT,而且还会经过其他的处理。但是包没有经过SNAT 的处理,所以包还是使用它
自己的源地址,就是$LAN_BOX(译者注:这就是IP 传输包的特点,只根据目的地的不
同改变目的地址,但不因传输过程中要经过很多路由器而随着路由器改变其源地址,除
非你单独进行源地址的改变。其实这一步的处理和对外来包的处理是一样的,只不过内
网包的问题就在于此,所以这里交待一下原因)。
4. 包离开防火墙,到达HTTP服务器。
5. HTTP服务器试图回复这个包。它在路由数据库中看到包是来自同一个网络的一台机子,
因此它会把回复包直接发送到请求包的源地址(现在是回复包的目的地址),也就是
$LAN_BOX。
6. 回复包到达客户机,但它会很困惑,因为这个包不是来自它访问的那台机子。这样,它
就会把这个包扔掉而去等待“真正”的回复包。
针对这个问题有个简单的解决办法,因为这些包都要进入防火墙,而且它们都去往需要做
DNAT才能到达的那个地址,所以我们只要对这些包做SNAT操作即可。比如,我们来考虑上面
的例子,如果对那些进入防火墙而且是去往地址为$HTTP_IP、端口为80的包做SNAT操作,那
Iptables 指南 1.1.19 页码,46/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
么这些包就好象是从$LAN_IP来的了,也就是说,这些包的源地址被改为$LAN_IP了。这样,
HTTP服务器就会把回复包发给防火墙,而防火墙会再对包做 Un-DNAT操作,并把包发送到客
户机。解决问题的规则如下:
iptables -t nat -A POSTROUTING -p tcp --dst $HTTP_IP --dport 80 -j SNAT \ --tosource
$LAN_IP
要记住,按运行的顺序POSTROUTING链是所有链中最后一个,因此包到达这条链时,已经被做
过DNAT操作了,所以我们在规则里要基于内网的地址$HTTP_IP(包的目的地)来匹配包。
对上面的例子应该考虑再全面些,现在还有一个问题没解决,就是防火墙自己要访问HTTP服
务器时会发生什么,能正常访问吗?你觉得呢:)很可惜,现在的配置还是不行,仔细想想就
明白了。我们这里讨论的基础都是假设机子访问的是HTTP服务器的外网地址,但这个外网地
址其实就是防火墙对外的地址,所以当防火墙访问这个外网地址时,就是访问它自己。防火
墙上如果有HTTP服务,那客户机就会看到页面内容,不过这不是它想看到的(它想要的在
DNAT上了),如果没有HTTP服务,客户就只能收到错误信息了。前面给出的规则之所以不起
作用是因为从防火墙发出的请求包不会经过那两条链。还记得防火墙自己发出的包经过哪些
链吧:)我们要在nat表的OUTPUT链中添加下面的规则:
iptables -t nat -A OUTPUT --dst $INET_IP -p tcp --dport 80 -j DNAT \ --todestination
$HTTP_IP
有了最后这条规则,一切都正常了。和HTTP服务器不在同一个网的机子能正常访问服务了,
和它在一个网内的机子也可以正常访问服务了,防火墙本身也能正常访问服务了,没有什么
问题了。这种心情,套用《大话西游》里的一句话,就是“世界又清净了”。(不要说你不
知道什么是《大话西游》)
警告:我们刚才写的这条规则会对日志产生很大影响,这种影响应该说是很不好
的。因为来自 Internet包在防火墙内先后经过了DNAT和SNAT处理,才能到达HTTP
服务器(上面的例子),所以HTTP服务器就认为包是防火墙发来的,而不知道真正
的源头是其他的IP。这样,当它记录服务情况时,所有访问记录的源地址都是防火
墙的IP而不是真正的访问源。我们如果想根据这些记录来了解访问情况就不可能
了。因此上面提供的“简单办法”并不是一个明智的选择,但它确实可以解决“能
够访问”的问题,只是没有考虑到日志而已。
其他的服务也有类似的问题。比如,你在LAN内建立了SMTP服务器,那你就要设置
防火墙以便能转发SMTP的数据流。这样你就创建了一个开放的SMTP中继服务器,随
之而来的就是日志的问题了。
一定要注意,这里所说的问题只是针对没有建立DMZ或类似结构的网络,并且内网
的用户访问的是服务器的外网地址而言的。(译者注:因为如果建立了DMZ,或者
服务器和客户机又被分在不同的子网里,那就不需要这么麻烦了。因为所有访问的
源头都不在服务器所在的网里,所以就没必要做SNAT去改变包的源地址了,从而记
录也就不是问题了。如果内网客户是直接访问服务器的内网地址那就更没事了)
较好的解决办法是为你的LAN在内网建立一台单独的DNS服务器(译者注:这样,内
网的客户使用网站名访问HTTP服务器时,DNS就可以把它解析成内网地址。客户机
就可以直接去访问HTTP服务器的内网地址了,从而避免了通过防火墙的操作,而且
包的源地址也可以被HTTP服务器的日志使用,也就没有上面说的日志问题了。),
或者干脆建立DMZ得了(这是最好的办法,但你要有钱哦,因为用的设备多啊)。
我想大家应该能明白这些规则只说明了数据包是如何恰当地被DNAT和SNAT的。除此
Iptables 指南 1.1.19 页码,47/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
6.5.3. DROP target
顾名思义,如果包符合条件,这个target就会把它丢掉,也就是说包的生命到此结束,不会
再向前走一步,效果就是包被阻塞了。在某些情况下,这个target会引起意外的结果,因为
它不会向发送者返回任何信息,也不会向路由器返回信息,这就可能会使连接的另一方的
sockets因苦等回音而亡:) 解决这个问题的较好的办法是使用REJECT target,(译者注:因
为它在丢弃包的同时还会向发送者返回一个错误信息,这样另一方就能正常结束),尤其是
在阻止端口扫描工具获得更多的信息时,可以隐蔽被过滤掉的端口等等(译者注:因为扫描
工具扫描一个端口时,如果没有返回信息,一般会认为端口未打开或被防火墙等设备过滤掉
了)。还要注意如果包在子链中被DROP了,那么它在主链里也不会再继续前进,不管是在当
前的表还是在其他表里。总之,包死翘翘了。
6.5.4. LOG target
这个target是专门用来记录包地有关信息的。这些信息可能是非法的,那就可以用来除错。
LOG会返回包的有关细节,如IP头的大部分和其他有趣的信息。这个功能是通过内核的日志工
具完成的,一般是syslogd。返回的信息可用dmesg阅读,或者可以直接查看syslogd的日志文
件,也可以用其他的什么程序来看。LOG对调试规则有很大的帮助,你可以看到包去了哪里、
经过了什么规则的处理,什么样的规则处理什么样的包,等等。当你在生产服务器上调试一
个不敢保证100%正常的规则集时,用LOG代替DROP是比较好的(有详细的信息可看,错误就容
易定位、解决了),因为一个小小的语法错误就可能引起严重的连接问题,用户可不喜欢这
样哦。如果你想使用真正地扩展日志地话,可能会对ULOG target有些兴趣,因为它可以把日
志直接记录到MySQL databases或类似的数据库中。
LOG现在有5个选项,你可以用它们指定需要的信息类型或针对不同的信息设定一些值以便在
记录中使用。选项如下:
Table 6-17. LOG target options
之外,在 filter表中还需要其他的规则(在FORWARD链里),以允许特定的包也能
经过前面写的(在POSTROUTING链和 OUTPUT链里的)规则。千万不要忘了,那些包
在到达FORWARD链之前已经在PREROUTING链里被DNAT过了,也就是说它们的目的地
址已被改写,在写规则时要注意这一点。
注意,如果在控制台得到的信息不是你想要的,那不是iptables或Netfilter的问
题,而是 syslogd 配置文件的事,这个文件一般都是/etc/syslog.conf。有关这
个问题的更多信息请查通过man syslog.conf查看。
Option --log-level
Example iptables -A FORWARD -p tcp -j LOG --log-level debug
Explanation 告诉iptables和 syslog使用哪个记录等级。记录等级的详细信息可以查看文
件syslog.conf,一般来说有以下几种,它们的级别依次是:debug,info,
notice,warning,warn,err,error,crit,alert, emerg,panic。其
中,error和err、warn和warning、panic和emerg分别是同义词,也就是说作
用完全一样的。注意这三种级别是不被赞成使用的,换句话说,就是不要使用
它们(因为信息量太大)。信息级别说明了被记录信息所反映的问题的严重程
度。所有信息都是通过内核的功能被记录的,也就是说,先在文件
Iptables 指南 1.1.19 页码,48/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
6.5.5. MARK target
用来设置mark值,这个值只能在本地的mangle表里使用,不能用在其他任何地方,就更不用
说路由器或另一台机子了。因为mark比较特殊,它不是包本身的一部分,而是在包穿越计算
机的过程中由内核分配的和它相关联的一个字段。它可以和本地的高级路由功能联用,以使
不同的包能使用不同的队列要求,等等。如果你想在传输过程中也有这种功能,还是用TOS
target吧。有关高级路由的更多信息,可以查看Linux Advanced Routing and Traffic
Control HOW-TO。
Table 6-18. MARK target options
syslog.conf里设置kern.=info /var/log/iptables,然后再让所有关于
iptables的LOG信息使用级别info,就可以把所有的信息存入文
件/var/log/iptables内。注意,其中也可能会有其他的信息,它们是内核中使
用info 这个等级的其他部分产生的。有关日志的详细信息,我建议你看看
syslog和syslog.conf的帮助,还有HOWTO,等等。
Option --log-prefix
Example iptables -A INPUT -p tcp -j LOG --log-prefix "INPUT packets"
Explanation 告诉iptables在记录的信息之前加上指定的前缀。这样和grep或其他工具一起
使用时就容易追踪特定的问题,而且也方便从不同的规则输出。前缀最多能有
29个英文字符,这已经是包括空白字符和其他特殊符号的总长度了。
Option --log-tcp-sequence
Example iptables -A INPUT -p tcp -j LOG --log-tcp-sequence
Explanation 把包的TCP序列号和其他日志信息一起记录下来。TCP序列号可以唯一标识一个
包,在重组时也是用它来确定每个分组在包里的位置。注意,这个选项可能会
带来危险,因为这些记录被未授权的用户看到的话,可能会使他们更容易地破
坏系统。其实,任何iptables的输出信息都增加了这种危险。(译者注:现
在,我深刻理解了什么是“言多必失”,什么是“沉默是金”)
Option --log-tcp-options
Example iptables -A FORWARD -p tcp -j LOG --log-tcp-options
Explanation 记录TCP包头中的字段大小不变的选项。这对一些除错是很有价值的,通过它
提供的信息,可以知道哪里可能出错,或者哪里已经出了错。
Option --log-ip-options
Example iptables -A FORWARD -p tcp -j LOG --log-ip-options
Explanation 记录IP包头中的字段大小不变的选项。这对一些除错是很有价值的,还可以用
来跟踪特定地址的包。
Option --set-mark
Example iptables -t mangle -A PREROUTING -p tcp --dport 22 -j MARK --setmark
2
Explanation 设置mark值,这个值是一个无符号的整数。比如,我们对一个流或从某台机子
发出的所有的包设置了mark值,就可以利用高级路由功能来对它们进行流量控
制等操作了。
Iptables 指南 1.1.19 页码,49/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
6.5.6. MASQUERADE target
这个target和SNAT target的作用是一样的,区别就是它不需要指定--to-source 。
MASQUERADE是被专门设计用于那些动态获取IP地址的连接的,比如,拨号上网、DHCP连接
等。如果你有固定的IP地址,还是用SNAT target吧。
伪装一个连接意味着,我们自动获取网络接口的IP地址,而不使用--to-source 。当接口停
用时,MASQUERADE不会记住任何连接,这在我们kill掉接口时是有很大好处的。如果我们使
用SNAT target,连接跟踪的数据是被保留下来的,而且时间要好几天哦,这可是要占用很多
连接跟踪的内存的。一般情况下,这种处理方式对于拨号上网来说是较好的(这有利于已有
那连接继续使用)。如果我们被分配给了一个不同于前一次的IP,不管怎样已有的连接都要
丢失,但或多或少地还是有一些连接记录被保留了(真是白痴,是吧)。
即使你有静态的IP,也可以使用MASQUERADE,而不用SNAT 。不过,这不是被赞成的,因为它
会带来额外的开销,而且以后还可能引起矛盾,比如它也许会影响你的脚本,使它们不能
用。
注意,MASQUERADE和SNAT一样,只能用于nat表的 POSTROUTING链,而且它只有一个选项(不
是必需的):
Table 6-19. MASQUERADE target
6.5.7. MIRROR target
这个target是实验性的,它只是一个演示而已,不建议你使用它,因为它可能引起循环,除
此之外,还可能引起严重的DoS。这个target的作用是颠倒IP头中的源目地址,然后再转发
包。这会引起很有趣的事,一个骇客最后攻破的可能就是他自己的机子。看来,使用这个
target至少可以使我们的机子更强壮:) 我们如果对机子A的80端口使用了MIRROR,会发生什
么呢?假设有来自yahoo.com的机子B 想要访问A的HTTP服务,那他得到的将是yahoo的主页,
因为请求是来自yahoo的。
注意,MIRROR只能用在INPUT、 FORWARD、 PREROUTING链和被它们调用的自定义链中。还要
注意,如果外出的包是因 MIRROR target发出的,则它们是不会被filter、nat或mangle表内
的链处理的,这可能引起循环或其他问题。比如,一台机子向另一台配置了MIRROR且TTL值为
255的机子发送一个会被认为是欺骗的数据包,同时这台机子也欺骗自己的数据包,以使它被
认为好像是来自第三个使用了MIRROR 的机子。这样,那个包就会不间断地往来很多次,直到
TTL为0。如果两台机子之间只有一个路由器,这个包就会往返240-255次。对骇客来说,这是
不坏的情况,因为他只要发送一个1500字节的数据(也就是一个包),就可以消耗你的连接
的380K字节。对于骇客或者叫做脚本小子(不管我们把他们称作什么)来说,这可是很理想
的情况。
Option --to-ports
Example iptables -t nat -A POSTROUTING -p TCP -j MASQUERADE --to-ports 1024-
31000
Explanation 在指定TCP或UDP的前提下,设置外出包能使用的端口,方式是单个端口,如--
to-ports 1025,或者是端口范围,如--to- ports 1024-3000。注意,在指定
范围时要使用连字号。这改变了SNAT中缺省的端口选择,详情请查看 SNAT
target。
Iptables 指南 1.1.19 页码,50/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
6.5.8. QUEUE target
这个target为用户空间的程序或应用软件管理包队列。它是和iptables之外的程序或工具协
同使用的,包括网络计数工具,高级的数据包代理或过滤应用,等等。讨论程序的编码已超
出了本文的范围。即使讨论,也要花很多时间,而且在这样一篇文章之内也无法说清有关
Netfilter和iptables的编程。具体的信息请查看Netfilter Hacking HOW-TO。
6.5.9. REDIRECT target
在防火墙所在的机子内部转发包或流到另一个端口。比如,我们可以把所有去往端口HTTP的
包REDIRECT到HTTP proxy(例如squid),当然这都发生在我们自己的主机内部。本地生成的
包都会被映射到127.0.0.1。换句话说,这个target把要转发的包的目的地址改写为我们自己
机子的IP。我们在做透明代理(LAN内的机子根本不需要知道代理的存在就可以正常上网)
时,这个target可是起了很大作用的。
注意,它只能用在nat表的PREROUTING、OUTPUT链和被它们调用的自定义链里。 REDIRECT只
有一个选项:
Table 6-20. REDIRECT target
6.5.10. REJECT target
REJECT和DROP基本一样,区别在于它除了阻塞包之外,还向发送者返回错误信息。现在,此
target还只能用在INPUT、FORWARD、OUTPUT和它们的子链里,而且包含 REJECT的链也只能被
它们调用,否则不能发挥作用。它只有一个选项,是用来控制返回的错误信息的种类的。虽
然有很多种类,但如果你有TCP/IP方面的基础知识,就很容易理解它们。
Table 6-21. REJECT target
Option --to-ports
Example iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --toports
8080
Explanation 在指定TCP或UDP协议的前提下,定义目的端口,方式如下:
1、不使用这个选项,目的端口不会被改变。
2、指定一个端口,如--to-ports 8080
3、指定端口范围,如--to-ports 8080-8090
Option --reject-with
Example iptables -A FORWARD -p TCP --dport 22 -j REJECT --reject-with tcpreset
Explanation 告诉REJECT target应向发送者返回什么样的信息。一旦包满足了设定的条
件,就要发送相应的信息,然后再象DROP一样无情地抛弃那些包。可用的信息
类型有:1、icmp-net-unreachable 2、icmp-host-unreachable 3、 icmp-
Iptables 指南 1.1.19 页码,51/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
6.5.11. RETURN target
顾名思义,它使包返回上一层,顺序是:子链——>父链——>缺省的策略。具体地说,就是
若包在子链中遇到了RETURN,则返回父链的下一条规则继续进行条件的比较,若是在父链
(或称主链,比如INPUT)中遇到了RETURN,就要被缺省的策略(一般是ACCEPT或DROP)操作
了。(译者注:这很象C语言中函数返回值的情况)
我们来举个例子说明一下,假设一个包进入了INPUT链,匹配了某条target为--jump
EXAMPLE_CHAIN规则,然后进入了子链EXAMPLE_CHAIN。在子链中又匹配了某条规则,恰巧
target是--jump RETURN,那包就返回INPUT链了。如果在INPUT链里又遇到了--jump
RETURN,那这个包就要交由缺省的策略来处理了。
6.5.12. SNAT target
这个target是用来做源网络地址转换的,就是重写包的源IP地址。当我们有几个机子共享一
个Internet 连接时,就能用到它了。先在内核里打开ip转发功能,然后再写一个SNAT规则,
就可以把所有从本地网络出去的包的源地址改为Internet连接的地址了。如果我们不这样做
而是直接转发本地网的包的话,Internet上的机子就不知道往哪儿发送应答了,因为在本地
网里我们一般使用的是IANA组织专门指定的一段地址,它们是不能在Internet上使用的。
SNAT target的作用就是让所有从本地网出发的包看起来都是从一台机子发出的,这台机子一
般就是防火墙。
SNAT只能用在nat表的POSTROUTING链里。只要连接的第一个符合条件的包被SNAT了,那么这
个连接的其他所有的包都会自动地被SNAT,而且这个规则还会应用于这个连接所在流的所有数
据包。
Table 6-22. SNAT target
port-unreachable 4、icmp-proto-unreachable 5、icmp-net-prohibited
6、icmp-host-prohibited 。其中缺省的是port-unreachable。你可以在附录
ICMP类型中看到更多的信息。还有一个类型——echo-reply,它只能和匹配
ICMP ping包的规则联用。最后一个类型是tcp-reset,(显然,只能用于TCP
协议)它的作用是告诉REJECT返回一个TCP RST包(这个包以文雅的方式关闭
TCP连接,有关它的详细信息在RFC 793 - Transmission Control Protocol
里)给发送者。正如iptables的 man page中说的,tcp-reset主要用来阻塞身
份识别探针(即113/tcp,当向被破坏的邮件主机发送邮件时,探针常被用
到,否则它不会接受你的信)。
Option --to-source
Example iptables -t nat -A POSTROUTING -p tcp -o eth0 -j SNAT --to-source
194.236.50.155-194.236.50.160:1024-32000
Explanation 指定源地址和端口,有以下几种方式:
1、单独的地址。
2、一段连续的地址,用连字符分隔,如194.236.50.155-194.236.50.160,这
样可以实现负载平衡。每个流会被随机分配一个IP,但对于同一个流使用的是
Iptables 指南 1.1.19 页码,52/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
6.5.13. TOS target
TOS是用来设置IP头中的Type of Service字段的。这个字段长一个字节,可以控制包的路由
情况。它也是iproute2及其子系统可以直接使用的字段之一。值得注意的是,如果你有几个
独立的防火墙和路由器,而且还想在他们之间利用包的头部来传递路由信息,TOS是唯一的办
法。前面说过,MARK是不能用来传递这种信息的。如果你需要为某个包或流传递路由信息,
就要使用TOS字段,它也正是为这个而被开发的。
Internet上有很多路由器在这一方面并没有做好工作,因此,在发送包之前改变其TOS没有什
么大用处。最好的情况是路由器根本不理它,最坏的情况是路由器会根据TOS处理,但都是错
误的。然而,如果你是在一个很大的WAN或LAN里,而且有很多路由器,TOS还是能有很好的作
为的。总的来说,基于TOS的值给包以不同的路由和参数还是可能的,即使在网络里是受限制
的(译者注:大不了不起作用就是了)。
TOS target只有一个选项:
Table 6-23. TOS target
同一个IP。
3、在指定-p tcp 或 -p udp的前提下,可以指定源端口的范围,如
194.236.50.155:1024-32000,这样包的源端口就被限制在1024-32000了。
注意,如果可能,iptables总是想避免任何的端口变更,换句话说,它总是尽
力使用建立连接时所用的端口。但是如果两台机子使用相同的源端口,
iptables 将会把他们的其中之一映射到另外的一个端口。如果没有指定端口
范围, 所有的在512以内的源端口会被映射到512以内的另一个端口,512和
1023之间的将会被映射到 1024内,其他的将会被映射到大于或对于1024的端
口,也就是说是同范围映射。还要注意,这种映射和目的端口无关。因此,如
果客户想和防火墙外的HTTP服务器联系,它是不会被映射到FTP control所用
的端口的。
TOS只能用来设置具体的或者说是特定的值(这些预定义的值在内核源码的include
文件——Linux/ip.h中),原因是很多的,但不管怎么说,你不要使用其他的值就是
了。当然,我们也有办法突破这个限制,就是使用一个名为FTOS的patch,你可在
由Matthew G. Marsh维护的站点 Paksecured Linux Kernel patches得到它,但要
小心使用哦。除了非常特殊、极端的情况,我们是不应该使用预定义以外的值的。
注意,这个target只能在mangle表内使用。
还要注意,在一些老版(1.2.2或更低)的iptables中包含的这个target在设置了
TOS之后,不会调整包的校验和,这样包会被认为是错误的并要求重发。而且,这
很可能会导致更多的mangle操作,从而使整个连接无法工作。(译者注:好在我们
现在不会再用这么老的版本了:) )
Option --set-tos
Example iptables -t mangle -A PREROUTING -p TCP --dport 22 -j TOS --set-tos
Iptables 指南 1.1.19 页码,53/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
6.5.14. TTL target
TTL可以修改IP头中Time To Live字段的值。它有很大的作用,我们可以把所有外出包的Time
To Live值都改为一样的,比如64,这是Linux的默认值。有些ISP不允许我们共享连接(他们
可以通过TTL的值来区分是不是有多个机子使用同一个连接),如果我们把TTL都改为一样的
值,他们就不能再根据TTL来判断了。
关于任何设置Linux的TTL默认值,请参阅附录其他资源和链接 内的ip-sysctl.txt。
TTL只能在mangle表内使用,它有3个选项:
Table 6-24. TTL target
0x10
Explanation 设置TOS的值,值的形式可以是名字或者使相应的数值(十进制或16进制
的)。一般情况下,建议你使用名字而不使用数值形式,因为以后这些数值可
能会有所改变,而名字一般是固定的。TOS字段有8个二进制位,所以可能的值
是0-255(十进制)或0x00-0xFF(16进制)。如前所述,你最好使用预定义的
值,它们是:
1、Minimize-Delay 16 (0x10),要求找一条路径使延时最小,一些标准服务
如telnet、SSH、FTP- control 就需要这个选项。
2、Maximize-Throughput 8 (0x08),要求找一条路径能使吞吐量最大,标准
服务FTP-data能用到这个。
3、Maximize-Reliability 4 (0x04),要求找一条路径能使可靠性最高,使用
它的有BOOTP和TFTP。
4、Minimize-Cost 2 (0x02),要求找一条路径能使费用最低,一般情况下使
用这个选项的是一些视频音频流协议,如RTSP(Real Time Stream Control
Protocol)。
5、Normal-Service 0 (0x00),一般服务,没有什么特殊要求。这个值也是大
部分包的缺省值。
完整的列表可以通过命令iptables -j TOS -h
得到。在1.2.5版时,这个列表就已经是完整的了,而且会保持很长一段时
间。
这个target需要patch-o-matic里的名为TTL的patch,可从
http://www.netfilter.org获得。此站点的FAQ是开始学习iptables和Netfilter的
好地方。
Option --ttl-set
Example iptables -t mangle -A PREROUTING -i eth0 -j TTL --ttl-set 64
Explanation 设置TTL的值。这个值不要太大,也不要太小,大约64就很好。值太大会影响
网络,而且有点不道德,为什么这样说呢?如果有些路由器的配置不太正确,
Iptables 指南 1.1.19 页码,54/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
6.5.15. ULOG target
ULOG可以在用户空间记录被匹配的包的信息,这些信息和整个包都会通过netlink socket被
多播。然后,一个或多个用户空间的进程就会接受它们。换句话说,ULOG是至今iptables和
Netfilter下最成熟、最完善的日志工具,它包含了很多更好的工具用于包的记录。这个
target可以是我们把信息记录到MySQL或其他数据库中。这样,搜索特定的包或把记录分组就
很方便了。你可以在ULOGD project page里找到ULOGD用户空间的软件。
Table 6-25. ULOG target
包的TTL又非常大,那它们就会在这些路由器之间往返很多次,值越大,占用
的带宽越多。这个target就可以被用来限制包能走多远,一个比较恰当的距离
是刚好能到达DNS服务器。
Option --ttl-dec
Example iptables -t mangle -A PREROUTING -i eth0 -j TTL --ttl-dec 1
Explanation 设定TTL要被减掉的值,比如--ttl-dec 3。假设一个进来的包的TTL是53,那
么当它离开我们这台机子时,TTL就变为49了。为什么不是50呢?因为经过我
们这台机子,TTL本身就要减1,还要被TTL target再减3,当然总共就是减去4
了。使用这个 target可以限制“使用我们的服务的用户”离我们有多远。比
如,用户总是使用比较近的DNS,那我们就可以对我们的DNS服务器发出的包进
行几个--ttl-dec。(译者注:意思是,我们只想让距离DNS服务器近一些的用
户访问我们的服务)当然,用--set-ttl控制更方便些。
Option --ttl-inc
Example iptables -t mangle -A PREROUTING -i eth0 -j TTL --ttl-inc 1
Explanation 设定TTL要被增加的值,比如--ttl-inc 4。假设一个进来的包的TTL是53,那
么当它离开我们这台机子时,TTL应是多少呢?答案是56,原因同--ttl-dec。
使用这个选项可以使我们的防火墙更加隐蔽,而不被trace-routes发现,方法
就是设置--ttl-inc 1。原因应该很简单了,包每经过一个设备,TTL就要自动
减1,但在我们的防火墙里这个1又被补上了,也就是说,TTL的值没变,那么
trace-routes就会认为我们的防火墙是不存在的。Trace-routes让人又爱又
恨,爱它是因为在连接出问题时,它可以给我们提供极有用的信息,告诉我们
哪里有毛病;恨它是由于它也可以被黑客或骇客用来收集目标机器的资料。怎
么使用它呢?这里有个很好的例子,请看脚本Ttl-inc.txt。
Option --ulog-nlgroup
Example iptables -A INPUT -p TCP --dport 22 -j ULOG --ulog-nlgroup 2
Explanation 指定向哪个netlink组发送包,比如-- ulog-nlgroup 5。一个有32个netlink
组,它们被简单地编号位1-32。默认值是1。
Option --ulog-prefix
Example iptables -A INPUT -p TCP --dport 22 -j ULOG --ulog-prefix "SSH
connection attempt: "
Explanation 指定记录信息的前缀,以便于区分不同的信息。使用方法和 LOG的prefix一
样,只是长度可以达到32个字符。
Option --ulog-cprange
Example iptables -A INPUT -p TCP --dport 22 -j ULOG --ulog-cprange 100
Explanation 指定每个包要向“ULOG在用户空间的代理”发送的字节数,如--ulog-cprange
Iptables 指南 1.1.19 页码,55/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
Chapter 7. 防火墙配置实例 rc.firewall
在本章里,我们将要建立一个防火墙,并且详细地说明了如何去阅读、理解它。在这个例子
中,我们使用的是最基本的配置,对其工作方式和我们在里面做了些什么都有深入的解释。
这个例子应该能在某些方面给你提供基本的思路,比如,如何解决不同的问题(当然是网络
方面的),再如,在真正把脚本应用于工作之前应该考虑些什么,等等。对本例中的变量值
做些修改就可能在实际的网络中使用,但不建议你这样做,因为你的网络配置和我在例子中
使用的情况可能不一样哦。但只要你有了这个基本的防火墙规则集,很可能只需要少量的调
整就可以把它用于实际了。
7.1. 关于rc.firewall
好,既然你能从头看到这儿,就说明你已经做好一切准备来检查这个脚本了。例子
rc.firewall.txt(代码在附录示例脚本的代码里)很大,但没有多少注释。我建议你先大致
看看它的内容,留个印象,再来仔细地阅读本章(要有耐心哦)。
7.2. rc.firewall详解
7.2.1. 参数配置
本小节要对照着rc.firewall脚本代码来看。
rc.firewall.txt的第一小节是配置选项,包含的都是一些至关重要的信息,它们是随着你的
网络的不同而改变的。比如,每个网络的IP地址都不一样,所有要把它放在这儿。$INET_IP
的值应该是在Internet上能使用的才可以,如果你有$INET_IP的话。如果没有,你就要看看
rc.DHCP.firewall.txt这种配置方法了,里面有很多有趣的东西。变量 $INET_IFACE应该指
向连接Internet的真实设备,比如eth0、eth1、ppp0、tr0等等。
这个脚本里没有包含任何DHCP或PPPoE的选项,所以这两节是空白的。其他空白的部分,也是
100,表示把整个包的前100个字节拷贝到用户空间记录下来,其中包含了这个
包头,还有一些包的引导数据。默认值是0,表示拷贝整个包,不管它有多
大。
Option --ulog-qthreshold
Example iptables -A INPUT -p TCP --dport 22 -j ULOG --ulog-qthreshold 10
Explanation 告诉ULOG在向用户空间发送数据以供记录之前,要在内核里收集的包的数量
(译者注:就像集装箱),如--ulog-qthreshold 10。这表示先在内核里积聚
10个包,再把它们发送到用户空间里,它们会被看作同一个netlink的信息,
只是由好几部分组成罢了。默认值是1,这是为了向后兼容,因为以前的版本
不能处理分段的信息。
可能有效率更高的方法来建立规则集,但这个脚本就是为易读而写的,所以每个人
都能理解它,即使没有多少BASH脚本编程的知识。
Iptables 指南 1.1.19 页码,56/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
这样的原因。之所以保留这些空白,是为了更容易区分这些结构相同而内容不同的脚本。如
果你需要这些部分,可以从其他脚本拷贝过来,或者你自己写了:)
Local Area Network小节包含的是LAN必须的信息,如连接到LAN的网卡的IP、LAN所用的地址
段等。
Localhost configuration小节里的信息在99%的情况下都不要改变,因为我们总是使用
127.0.0.1作为地址,也总是把接口命名为lo。紧随其后的是IPTables Configuration,里面
只有一个变量,即$IPTABLES。它指定的是iptables程序的准确位置,如果是自己编译安装的
话,一般都是/usr/local/sbin/iptables。但更多的发行版都把程序放在另外的地方,
如 /usr/sbin/iptables,等等。
7.2.2. 外部模块的装载
首先,我们要使用命令/sbin/depmod -a使module dependencies files保持最新,然后,再
装载脚本需要的模块。我们应该始终避免装入不需要的模块,如果可能,还要尽力避免装入
无所事事的模块,除非你确实需要它们。这样做主要是为了安全,因为每增加一个模块都要
花费额外的努力以增加新的规则(这样就容易出漏洞哦)。比如,如果你想支持LOG、REJECT
和MASQUERADE target,不要把相应的功能静态地编译进内核,我们使用以下模块来完成:
/sbin/insmod ipt_LOG
/sbin/insmod ipt_REJECT
/sbin/insmod ipt_MASQUERADE
接下来的一行是装载ipt_owner模块,它的作用是“只允许特定的用户创建特定的连接”。在
这个例子中,我没有使用到它,但你可能会用到。比如,你可能只允许root建立FTP 和HTTP
连接访问redhat.com,而其他用户都不可以。你也可以只允许你自己使用的用户名和root才
能访问 Internet,这样别人会很烦的,但你的安全性在某些方面会有所提高哦,比如,把你
当作发起攻击的跳板的情况。关于ipt_owner的更多信息,可以看看章节规则是如何练成的里
的Owner match 。
在这儿我们也可以为状态匹配安装扩展模块。状态匹配和连接跟踪的所有扩展模块的名字都
是这样的: ip_conntrack_*和ip_nat_* 。连接跟踪的helper是一些特殊的模块,正是它们
告诉了内核怎样恰当地跟踪特殊的连接。没有这些helper,内核在处理特殊连接的时候,就
不知道该查看些什么东西。NAT helper就是连接跟踪helper的扩展,它会告诉内核在包里找
什么、如何转换它们,这样连接才能真正工作起来。比如, FTP是一个复杂的协议,它利用
包的有效数据部分来发送连接信息。如果一台需要被NAT的机子(译者注:也就是说,机子在
一个内网里)连接Internet上的FTP服务器,它就会把自己的内网IP地址放在包的数据区内发
送出去,以使FTP服务器能连接到那个地址。但私有地址不能在LAN外使用,所以FTP服务器不
知道用它做什么,连接就会断掉了。FTP NAT helper能完成这些连接中所有的地址转换工
作,因此FTP服务器就知道该往哪儿连了。同样的事情也发生在DCC的文件传输(这里指的是
发送)和聊天上,为了建立连接,IP地址和端口都需要利用IRC协议的数据区发送,而且还要
做一些转换工作。没有这些helper的话,FTP和IRC只有一部分工作是正常的,但另一部分根
本就无法工作。例如,你可以通过DCC接收文件,但就是不能发送。这个问题的原因在于DCC
是如何建立连接的。当DCC想发送文件时,会告诉接收者你要发送文件,并让它知道要连接到
注意,本文使用的脚本都是用类似命令装入模块,这可能会引起装载失败(有错误
信息显示)。原因是多方面的,但如果较基本的模块也失败的话,那最大的可能是
哪个模块或相应的功能已被静态地编译进内核了。进一步的信息可以看看附录常见
问题与解答中的模块装载问题。
Iptables 指南 1.1.19 页码,57/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
什么地方。如果没有helper,这个DCC连接最终会断开,因为接收者收到的是内网的地址。这
样,当它按那个地址连接时,其实就连到和它在同一内网的机子了。那为什么可以接收呢?
因为发送者给你的是可在 Internet上使用的IP地址(大部分情况下,IRC服务器都有真实的
IP地址)。
在这个例子中,我们在这儿装载支持FTP和IRC协议的模块。有关连接跟踪和nat的详细信息,
请查看附录 常见问题与解答。在patch-o-matic中,还有H.323 conntrack helper等其他象
NAT helper的模块。但为了使用它们,你需要使用patch-o-matic提供的补丁,还需要编译内
核。详细的操作信息可以查看章节准备阶段 。
7.2.3. proc的设置
我们可以使用下面的语句打开IP转发功能(IP forwarding):
echo "1" > /proc/sys/net/ipv4/ip_forward
万一你使用的是SLIP、PPP或DHCP,也就是说你是动态获取IP的,那还要用下面的命令打开
ip_dynaddr:
echo "1" > /proc/sys/net/ipv4/ip_dynaddr
如果你还要打开其他的proc选项,也是用类似的方法,但有关那些选项的具体介绍以不是本
文的内容,你可以看看其他相关的文章。在附录其他资源和链接里就有一些介绍了内核proc
系统的短小精干的文章。如果你在本文中找不到想要的资料,就可以到附录其他资源和链接
去看看,你会有所收获的。
如果你在通过防火墙使用mIRC DCC时遇到了问题,但和其他IRC客户沟通很正常,
看看附录常见问题与解答里的 关于mIRC DCC的问题 吧。
注意,为了能对FTP和IRC协议做网络地址转换,需要装载ip_nat_ftp和
ip_nat_irc。在装载NAT模块之前,你还要载入ip_conntrack_ftp和
ip_conntrack_irc模块。NAT模块和conntrack模块以相同的方式被使用,但NAT模
块使我们能对这两个协议做NAT。
注意,何时何地打开这个功能才算合适是值得好好考虑的一个问题。在本文所用的
脚本中,我都是在创建IP过滤器(在本文里就是指iptables的过滤规则)之前打开
它的。这可能引起这样一种情况,就是在一小段时间内(时间的长短随脚本的复杂
程度和机子的性能高低而变化,可能只有一毫秒,也可能会长达一分钟),防火墙
可以转发任何包(译者注:因为这时防火墙的过滤规则还没有被装入)。这种情况
又会导致安全方面的问题,不怀好意的人可能会趁此通过防火墙破坏我们的网络。
也就是说,我们应该在创建所有防火墙的规则之后再打开IP转发功能,我这样做只
是为了保正所有脚本的兼容性。(译者注:我们在实际应用中一定要注意这一点,
尽量不要先开IP转发功能)
在本文所用的脚本中还包含了一个名为Non-Required proc configuration(非必
需的proc设置)的小节。当有什么工作不象你所想象的那么正常时,可以来这儿看
看,它能提供给你最基本的一些信息,但在你真正弄明白它们的含义之前不要进行
改动。
Iptables 指南 1.1.19 页码,58/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
7.2.4. 规则位置的优化
本节简要地描述了针对脚本rc.firewall.txt,我将如何选择、使用内建的链链和自定义的链。
我选择过的一些路径从这个或那个角度看可能是错误的,我会指出这些情况和问题发生在何
时何地。这里还对章节 表和链 做了简要的回顾,希望能给你一点儿提醒,以使你能想起在
实际应用中包是如何表和链的。
为了尽可能地少占用CPU,我们已经替换了所有不同的自定义链,与此同时,我把主要的精力
放在了安全性和易读性上。我不让TCP包去经历ICMP、UDP和TCP规则的洗礼,而是简单地匹配
所有的TCP包,然后让它去一个自定义链中旅行。这种方法并不比让它经历所有的规则开销
大。下图可以解释在Netfilter中,外来的包是如何被处理的(相对于章节表和链的深入讨
论,这个图形太粗糙了)。我希望通过上面的解释和下面的图形能让大家明白写这个脚本的
目的,详细的注释在后面几节。
利用这个图形,我们可以弄清楚脚本的目的。整个脚本基于这样一种假设,我们有一个局域
网,一个防火墙及一个Internet连接,且有一个静态IP地址(相对的是动态地址,它们使用
的连接是DHCP、PPP、 SLIP,等等),还想把防火墙作为Internet上的一台服务器来运行某
些服务。我们完全信任局域网,因此不能阻塞任何从局域网发出的数据传输。还有一个要优
先考虑的事,我们只允许那些被明确说明为可以接受的数据通过。为了做到这一点,我们就
要把缺省策略设为DROP。这样,那些没有被明确标识为允许进入的数据就都被阻塞了。
在上面的假设里,我们想让局域网能够访问Internet。因为局域网是被完全信任的,所以我
们应该允许所有来自局域网的数据通过。但Internet是不被信任的,所以我们想阻塞从
Internet向我们的局域网发起的连接。根据上面的所有假设,我们来考虑考虑需要做什么、
不需要做什么以及我们想做什么。
首先,我们解决的是局域网要能连接到Internet的问题。那我们就要对所有数据包做NAT操
作,因为局域网内的机子都没有真实的IP地址。NAT是在PREROUTING链中完成的,这也是脚本
最后创建的那个规则所在的链。这意味着我们必须要在FORWARD链中做过滤工作,否则我们就
是允许所有外部的机子都能完全访问局域网了。因为我们完全信任局域网,所以允许所有由
内向外的数据通过。由于我们假设Internet上的机子都不可以访问局域网内的机子,所以要
阻塞所有由外向内的连接,但已经建立的或相关的连接除外,因为它们只是用来回应内网对
外网的访问,而不是建立对内网的新连接。
Iptables 指南 1.1.19 页码,59/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
由于资金有限,我们的防火墙只提供了有限的几个服务:HTTP、 FTP、SSH和IDENTD。因此,
我们要在INPUT链里允许这些协议通过,还要在 OUTPUT链里允许返回的数据通过。我们除了
完全信任局域网,也信任loopback和它的IP地址,因此我们要有相应的规则来允许所有来自
局域网和loopback的数据通过。但是我们不会允许一些特殊的包或包头通过,也不会接受
Internet上某一段IP的访问。比如,网段 10.0.0.0/8是为局域网而保留的,一般来说,我们
不允许来自它们的包进入,因为这样的包90%都是用来进行欺骗的。不过,在实现这条标准之
前,还要注意一个问题,就是有一些ISP在他们的网络里使用的恰恰就是这些地址。 在附录
常见问题与解答里有这个问题的进一步说明。
因为我们在防火墙上运行FTP服务,而且想让包经历最少的规则,所以要把处理established
和related状态的规则放到INPUT链的顶部。基于同样的原因,我们把这些规则分到子链中。
这样,包就可以尽量少地穿越规则,从而节省时间,也可以降低网络的冗余。
在这个脚本里,我们依据不同的协议(如TCP、 UDP或ICMP)把包分到子链中。用来匹配 TCP
包的链叫做tcp_packets,它可以匹配所有我们允许通过的TCP端口和子协议(如FTP、HTTP
等)。我们还要建立一个名为allowed的子链,以便在真正接受“那些使用有效端口来访问防
火墙的TCP包”之前,对它们进行附加的检查。至于ICMP包,自有称作 icmp_packets的链来
处理。在决定如何建立这个链时,我考虑到如果我们同意接受ICMP包的类型和代码,就没有
必要对它们做附加的检查,所以直接接受它们就行了。最后,UDP包由谁处理呢?当然就是
udp_packets了。如果包是那种允许被接收的类型,就直接放行了。
因为我们的网络很小,所以防火墙也要作为工作站来用。这就要求我们要允许一些特殊的协
议能和它通信,比如speak freely和ICQ。
现在,我们来考虑考虑OUTPUT链。因为很信任防火墙,所以我们允许几乎所有离开它的包通
过,而没有阻塞任何用户和协议。但我们也不想让人利用这台机子进行IP欺骗,因此我们只
放行那些从防火墙本身的IP发出的包。为了实现这一点,我们很可能在ACCEPT链中加入这样
一条规则:如果包是由防火墙的IP发出的,就放行,否则,它们就会被OUTPUT链的缺省策略
DROP掉。
Iptables 指南 1.1.19 页码,60/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
7.2.5. 缺省策略的设置
在开始写其他规则之前,我们先要用下面的语句建立缺省的策略:
iptables [-P {chain} {policy}]
每一条链的策略都是用来处理那些在相应的链里没被规则匹配的包。也就是说,如果有一个
包没有被规则集中的任何规则匹配,那策略就有用武之地了。
7.2.6. 自定义链的设置
现在,你对我们的防火墙应该已经有了一个很清晰的印象,心动了吧。心动不如行动,让我
们把它变为现实吧。这一节我们就要小心仔细地创建所有自定义链和链内的规则。
如前所述,我们要建立这几条自定义链:icmp_packets、tcp_packets、udp_packets 和
allowed,其中allowed链是由tcp_packets链调用的。所有进入$INET_IFACE的ICMP包都会被
重定向到icmp_packets链,TCP包是到 tcp_packets链,那UDP包自然就是udp_packets链了,
详细的解释都在 INPUT chain里。创建自定义链的命令还记得吗?很简单哦,只要使用选项-
N ,再指定链的名字即可(不要忘了,新建的链都是空的),如下:
iptables [-N chain]
在下面的几节里,我们会详尽地介绍上面创建的每一条链,以使你了解它们包含哪些规则、
有什么作用。
7.2.6.1. bad_tcp_packets链
这条链包含的规则检查进入包(incoming packet)的包头是否不正常或有没有其他问题,并
进行相应地处理。但事实上,我们使用它只是为了过滤掉一些特殊的包:没有设置SYN位但又
是NEW状态的TCP包,还有那些设置了SYN/ACK但也被认为是NEW状态的TCP包。这条链可以用来
检查所有可能的不一致的东西,比如上面的包或者XMAS port-scans等。我们还可以为
INVALID状态的包增加一条规则的。
如果你想完全了解无SYN位的NEW状态(NEW not SYN),可以去附录常见问题与解答里看看未
设置SYN的NEW状态包一节,它介绍了未设置SYN的NEW状态包通过其他规则的情况。在某些情
况下可以允许这种包通过,但99%的情况是我们不想让它们通过。因此,我们会先记录这种
包,然后再扔掉它们。
我们拒绝SYN/ACK包以NEW状态进入的原因也是非常简单的,深入的说明在附录常见问题与解
答的NEW状态的SYN/ACK包 里。基本上,我们这样做是出于对其他主机的好意,因为我们为他
们预防了序列号预测攻击(sequence number prediction)。
要谨慎地设置其他表里的链的策略,因为它们不是用来过滤包的,这就可能引起很
怪异的行为发生。
Iptables 指南 1.1.19 页码,61/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
7.2.6.2. allowed链
如果包是从$INET_IFACE进入的,而且是TCP包,那它就要经过tcp_packets 链的检验。如果
这个连接就是冲着被允许通过的端口来的,我们还要对它进行一些检查,以确定是否真的要
接受它。这些“最后的审判”都是在allowed链里进行的。
首先,我们看看这个包是否是SYN包,如果是,它很可能是新连接的第一个包,我们当然接受
了。如果不是,那就看看包是否来自某个ESTABLISHED或RELATED状态的连接,是的话,就接
受。ESTABLISHED状态的连接是那种在两个方向上都有流量存在的连接。依据状态机制的观
点,这个连接一定处于是ESTABLISHED状态的,因为我们现在能看到这个包,说明以前肯定收
到了相应的SYN包。最后一条规则将DROP所有其他的包。当包到达最后这条规则,就几乎意味
着所有连接都不会有双向的交流,也就是说,我们不会回应 SYN包。当试图用非SYN包开始新
的连接时,包也会走到这条规则。不用SYN包建立新连接没有什么实际的用处,当然,端口扫
描要排除在外。就我知道的而言,现在没有什么有用的TCP/ IP程序会使用SYN包以外的包来
打开一个TCP连接。因此,我们要把这样的包DROP掉,我有99%的把握说它们是端口扫描用
的。
7.2.6.3. 处理TCP的链
tcp_packets链指定了哪些端口可从Internet访问。但我们还要对进入的包做更多的检查,因
此,每个包都会被发送到上面提到的allowed链。
-A tcp_packets告诉iptables要在哪条链里增加规则,规则被放在指定链的末尾。-p TCP指
定要匹配的是TCP包,-s 0/0说明要匹配的源地址是从网络掩码为0.0.0.0的地址0.0.0.0开始
的,换句话说,就是所有的地址。这实际上是默认值,我写出来只是尽可能使你更明白。--
dport 21指定目的端口,也就是说如果包是发往端口21的,就会被匹配。如果所有的标准都
匹配了,包就要被送往allowed链。
TCP的21号端口也是允许访问的,也就是FTP的控制端口,它可以控制FTP连接,前面提到过,
我还允许所有RELATED状态的连接通过。这样,我们也就可以使用PASSIVE(主动)和ACTIVE
(被动)的连接了,当然,要事先装载ip_conntrack_ftp模块。如果我们不想再提供 FTP服
务,就卸载ip_conntrack_ftp模块,并把$IPTABLES -A tcp_packets -p TCP -s 0/0 --
dport 21 -j allowed 这一行从文件 rc.firewall.txt里删掉。
22号端口是SSH使用的。如果你允许外网的如何人都能通过telnet(使用23号端口)访问你的
机子,那你还是使用SSH吧,它的安全性要好很多。注意,你操作的是防火墙,把任何访问权
分配给除你自己之外的人都不是什么好主意。防火墙总是应该尽量少地暴露自己。
80是HTTP端口,也就是说你在防火墙上运行了网页服务。如果你不提供网页服务,就删掉这
条规则吧。
最后我们还提供了IDENTD服务,端口是113。这个服务对某些协议是必须的,如IRC。注意,
如果你NAT一些在内网里的主机的话,软件oidentd也值得一用,它会把 IDENTD请求中继给内
网里正确的机子。
如果没有匹配上面任何一条规则,包就会被送回tcp_packets链的父链,也就是把它发到
tcp_packets链的那条规则所在的链。如果你想打开更多的端口,只要对tcp_packets链里的
任何一行使用“复制、粘贴大法”,再修改一下端口号即可。
Iptables 指南 1.1.19 页码,62/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
7.2.6.4. 处理UDP的链
如果我们在INPUT链中遇到了UDP包,就把它发送到udp_packets链。在那里,我们只处理UDP
包,所以要用-p UDP来指定相应的协议。我们接受来自任何地址的包,故有-s 0/0,这其实
就是源地址选项的默认值,但为了更明确,我们还是把它写出来了。此外,我们只接受发往
特定端口的包,这些端口是我们想对Internet开放的。注意,我们不需要依据发送端的源端
口来决定是否打开某个端口,这个工作是由状态机制完成的。如果我们想运行某个使用 UDP
端口的服务(如DNS),只要开放相应的端口,其他的端口不需要打开。那些处于
ESTABLISHED状态、正在进入防火墙的包在到达包含--state ESTABLISHED,RELATED的规则
(这是 INPUT链里那些“处理来自Internet的包的规则”中的第一条规则)之后就会被接受
了。
我们不接受外来的以53号端口为目的的UDP包,也就是说,我们不想接受外来的DNS查询。其
实,规则已经写好了,我只是把它给注释掉了。如果你想把防火墙作为一台允许Internet访
问的DNS 服务器,那就把注释符号去掉。
就我个人而言,我会打开123号端口,它对应的协议是network time protocol,简称NTP 。
我们可以利用这个协议与某台具有精确时间的时间服务器联系,以设置本机的时间。你们中
的大部分可能用不到此协议,所以我也把它注释掉了,虽然我已经写出了规则。
我打开了2074号端口,它是某些实时的多媒体应用程序使用的。比如speak freely ,你可以
用这个程序通过音箱、麦克风或耳麦与其他人进行实时交谈。如果你不需要,就把这条规则
注释掉吧。
端口4000相应的协议是ICQ协议,由ICQ使用,世界上使用最广泛的聊天程序之一,“地球人
都知道”。Linux上至少有2-3种不同的ICQ克隆。我想不必解释为什么要开放这个端口了吧。
(译者注:国产的聊天程序,常见的是QQ(端口8000),现在又有了UC(端口3001)等。)
如果你正在经历日志满天飞的苦恼,这里有两个额外的规则可以使用,当然,这要看是因为
什么引起的了。我们这里的第一条规则会阻塞目的端口是135到139的广播包。大部分
Microsoft使用者会用到NetBIOS 或SMB,而它们就使用这些广播包。这条规则可以阻塞所有
位于外网的那些Microsoft Network产生的广播包,我们的日志也就可以简洁一些了。第二条
规则也是解决日志问题,不过问题的产生者变了,这回是外网的DHCP查询。如果你的外网是
由非交换的以太网组成的,在那里客户机可以通过DHCP 得到IP地址,也就是说,如果外网里
会有很多DHCP查询广播包,那就启用这条规则吧。
7.2.6.5. 处理ICMP的链
现在,我们该决定可以接受哪些ICMP类型了。在INPUT链中,如果ICMP包是从eth0 (即本例
的Internet接口)进入的,我们就要把它重定向到icmp_packets 链(前面提到过),以检查是
否是可以接受的类型。目前,我只接受三种ICMP包:ICMP Echo requests,TTL equals 0
during transit和TTL equals 0 during reassembly。默认不接受其他任何ICMP类型的原因
是,几乎所有其他类型的ICMP包都是RELATED状态的,也就是说它们都会被处理 RELATED状态
的规则放行。
注意,我把最后这两条规则也注释掉了,因为有些人可能想看看相关的记录。如果
你正经历“合法日志过多”的痛苦,就试试丢弃那些包吧。其实,在INPUT链中,
就在这两条有关日志的规则之前,还有更多这种类型的规则。
如果一个ICMP包是用来回应“已经存在的包或流”的,那它就是与那些流相关的,
也就是说,它的状态是RELATED。更多的信息在章节状态机制里。
Iptables 指南 1.1.19 页码,63/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
现在来解释一下我为什么只接受上面提到的三种ICMP包。Echo Request用来请求echo
reply,这个操作主要被用来ping其他的机子,以确定那些机子是否可用。如果没有这一条规
则,其他机子将不能通过ping来确定我们是否可用。注意,有些人倾向于删掉此规则,只是
因为他们不想被Internet看到。删掉这个规则将会使任何来自Internet的、对我们防火墙的
ping都无效,因为防火墙对他们完全没有回应。
允许超时(Time Exceeded,如TTL equals 0 during transit传输期间生存时间为0和TTL
equals 0 during reassembly在数据报组装期间生存时间为0)信息进入,我们就可以追踪从
本地到某台主机的路径,或者在包的TTL为0时,我们能得到回应信息。比如,在我们追踪到
某台主机的路径时,会以 TTL = 1的包开始。当它得到第一个路由时,TTL减为0,我们也会
得到第一个路由返回的超时信息。然后是TTL = 2的包,我们就会得到第二个路由器返回的超
时信息。如此,直到得到我们的目的主机返回的信息。这样,我们就可以从路径上的每一台
主机得到一个回应,从而我们可以看到路径上的每一台主机,也就可以知道路径是断在哪台
机子了。
完整的ICMP类型列表在附录ICMP类型里。关于ICMP类型的更多信息与用法,我建议你看看下
面的文章:
􀁺 Ralph Walden的The Internet Control Message Protocol。
􀁺 J. Postel的RFC 792 - Internet Control Message Protocol。
7.2.7. INPUT链
我写的INPUT链大部分是使用其他链来完成这个艰难的工作的。这样做,我们就不需要从
iptables 装载太多的规则(译者注:这是针对装载INPUT链的规则说的,因为这时其他规则
已经装载好了),而且它在较慢的机子上也可以工作得很好,但另一方面,这样的机子在高
负载时还是会丢弃很多包(译者注:机子慢,就是不行)。之所以能做到这一点,是因为对
于大量不同的包,我们通过检查特定的细节来确定它们的类别,再把这些包发送到相应的自
定义链去处理。这样,我们可以分割规则集使之包含很少的规则,每个包要经历的规则也就
少了。从而,在过滤包时,防火墙的开销也就小多了。
首先,我们要检查进入的tcp包的形态是否是不正常的或我们不想要的。这个工作是这样完成
的,我们把所有的tcp包送到bad_tcp_packets链,由其中的规则进行检查,具体的描述在小
节bad_tcp_packets链里。
然后,我们开始处理被信任的网络的数据传输。这包括所有来自“连接内网的网卡”的流
量,所有来自和发往loopback的流量(要注意,和loopback相对应的IP地址包括了所有分配
给防火墙的地址,其中也包括连接Internet的地址)。我们把处理LAN的流量的规则放在防火
墙的上部,因为我们的局域网产生的流量要远远多于Internet连接。这样,规则会更有效
率,防火墙就能以较小的开销去匹配包,从而网络阻塞的可能性也就减小了,而且也便于我
们查看经过防火墙的包主要是什么类型。
注意,我阻塞了所有我不想接受的ICMP包,这对你的网络来说可能会有问题,但在
我这里,一切工作正常。
Iptables 指南 1.1.19 页码,64/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
下面的一些规则会处理来自Internet的信息,在接触这些规则之前,有一个相关的规则,我
们可用它来减少一些开销。这是一个处理状态的规则,它允许所有处于状态ESTABLISHED或
RELATED且发往 Internet接口的包进入。在allowed链中有一个与此类似的规则(译者注:实
在是多余,建议大家把它拿掉吧)。顺序上,当然是INPUT链里的规则先处理包了。然而,在
allowed链里保留--state ESTABLISHED,RELATED规则还是有一些原因的,比如,方便某些人
想剪切此功能,粘贴到其他地方。
在INPUT链里,我们会把从$INET_IFACE进入的所有TCP包发往tcp_packets链,类似地,把UDP
包发往udp_packets链,把 ICMP包发往icmp_packets链。一般说来,防火墙遇到的最多的包
是TCP包,其次是 UDP包,最后是ICMP包。但要注意,这只是一般情况,对你可能不适用。一
样的规则因为顺序不同,或者说逻辑不同,效率会有很大的差别。如果规则集写得不好,即
使只有100条规则,而且有100mbit的网卡,就算是Pentium III的机子也会吃不消的。所以你
自己写规则集时一定要注意这一点。
这里有一条被注释掉了规则,万一在我们的防火墙外部有一些Microsoft网络,我们可以启用
它来解除日志过多的烦恼。Microsoft的客户机有个坏习惯,就是向地址224.0.0.0/8发送大
量的多播包。因此我们要有这条规则来阻塞那些包,以免我们的日志被它们填满。还记得
吗?udp_packets链里也有两条类似的规则。忘了的话,就到处理UDP的链看看吧。
在其他的包被INPUT链的策略处理之前,我们会把它们记录下来,以便查找可能的问题或
bug:它可能就是我们不想允许它进入的那种包,也可能是对我们做了什么坏事的用户,还可
能是防火墙的问题,如我们阻塞了应该被放行的包。我们要了解所有的情况,这样问题才能
得以解决。我们每分钟最多记录3个包,因为我们可不想让日志里记载的都是废话。为了容易
辨别包的来源,我们还对所有的记录设置了前缀。
所有没被上面的规则处理的包都会被策略DROP掉。策略的设置在本章的小节缺省策略的设置
里,距离我们已经很远喽。
7.2.8. FORWARD链
在本例中,FORWARD链包含的规则很少。首先,我们会把所有的包发往bad_tcp_packets 链。
此链我们前面提到过多次,它可以被多条链调用,其实它也就是为这个目的而设计的。
之后就是FORWARD链的主要规则了。第一个允许所有来自$LAN_IFACE的数据通过,没有任何限
制,也就是说,我们的LAN可自由地访问Internet。第二个允许ESTABLISHED和RELATED状态的
包能通过防火墙。换句话说,就是所有对我们的内网发出的连接的回应都可以返回局域网。
为了使我们的内网能访问Internet,这些规则是必须的,因为我们在前面已经把FORWARD链的
策略设为DROP了。这样设置规则也是很聪明的,因为它在保证局域网可以访问Internet的同
时阻止了Internet对局域网的访问。
最后我们也有一个处理日志的规则,用来记录没被上面任何规则匹配的包。这样的包很可能
是形态不正常的或者是其他问题,比如可能是黑客攻击。这个规则与INPUT链中的那个类似,
只是前缀不同,这里用的是:"IPT FORWARD packet died: "。前缀主要用来分离日志的记
录,便于我们查找包的来源和包头的一些信息。
7.2.9. OUTPUT链
除了我几乎没有人把防火墙还当作工作站来使用,但正因为这样,我允许几乎所有从防火墙
Iptables 指南 1.1.19 页码,65/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
的IP(包括 LOCALHOST_IP,$LAN_IP或$STATIC_IP )出发的数据,而阻塞其他情况。因为其
他任何情况都可能被人以某种方式欺骗。最后的规则还是用来记录那些要被策略DROP掉的
包。这样,我们就可以了解它们,继而可以对产生的问题(可能是具有威胁性的错误,或者
是用来进行欺骗的包)采取行动。
7.2.10. PREROUTING链
顾名思义,PREROUTING链(nat表的)是在路由之前做网络地址转换工作的。然后,包再经过
路由,就会被送到filter表的INPUT或FORWARD链。我们在这里讨论这个链的唯一原因是,我
们觉得有责任再次指出你不应该在此链中做任何过滤。PREROUTING链只会匹配流的第一个
包,也就是说,这个流的所有其他的包都不会被此链检查。事实上,在这个脚本中,我们根
本没有用到PREROUTING 链。如果你想对一些包做DNAT操作,例如,你把web server放在了局
域网内,这里就是你放置规则的地方。有关PREROUTING链的详细信息在章节表和链中。
7.2.11. POSTROUTING链
我们最后的任务应该是构造网络地址转换,对吧?至少对我来说是的。我们在nat表的
POSTROUTING里只加入了一条规则,它会对所有从Internet接口(对我来说,这是eth0)发出
的包进行NAT操作。在所有的例子脚本里,都有一些变量,它们要给以正确的配置。选项-t指
定要在那个表里插入规则,这里是nat表。命令-A说明我们要把规则添加到 POSTROUTING链末
尾。-o $INET_IFACE指定要匹配所有从接口INET_IFACE出去的包,这里我们使用的是eth0。
最后,我们把target设置为SNAT。这样,所有匹配此规则的包都会由SNAT target处理,之
后,它们的源地址就是Internet接口的地址了。不要忘了SNAT可是一定要有IP地址的,用--
to-source 来设置哦。
在这个脚本中,我们选择SNAT而不用MASQUERADE是有原因的。主要的原因是我们的防火墙有
静态IP地址,使用SNAT会更快更有效。还有一个原因是我们要在这个例子中展示它的作用以
及怎样使用它。如果你没有静态的IP地址,要想实现SNAT,还是使用MASQUERADE为好,因为
它简单易用,而且它可以自动获得IP地址。当然,计算机的消耗会多一点,但如果你使用
DHCP,这样做是很值得的。如果你想了解MASQUERADE target的表现,应该看看脚本
rc.DHCP.firewall.txt。
Chapter 8. 例子简介
本章的目的是对指南提到的每个脚本都给以简单明了的说明,以及提供一个关于这些脚本的
框架,描述它们提供的服务。这些脚本不是任何情况都能用的,它们可能并不符合你的意
图。也就是说,为了能满足你的需要,还是要取决于你自己。在这方面,下面的内容可能会
给你很大的帮助。第一小节介绍了这些脚本的结构,你会发现我们在这些脚本里使用的处理
方式还是比较容易的。
千万注意,PREROUTING链只能做网络地址转换,不能被用来做任何过滤,因为每个
流只有第一个包才会经过此链。
Iptables 指南 1.1.19 页码,66/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
8.1. rc.firewall.txt脚本的结构
本指南所有的脚本都是依据一个特定的结构来写的。理由嘛,就是这样可以使它们彼此相
似,便于我们查找不同之处。本章将要对这个结构做一个很好的说明,而且还会简单地阐述
这些脚本为什么会按照现在这种样子来写,以及我为什么选择一直使用这种结构。
8.1.1. 脚本结构
这就是本指南所有脚本遵循的脚本结构。如果有不同于此的地方,可能就是我出错了,除非
我特意说明为什么要打破这种结构。
1. Configuration —— 首先是一个配置选项区,里面的变量在脚本中会用到。几乎任何
脚本(shell-script)的第一部分都是配置选项区。
1. Internet —— 有关Internet连接的配置。如果我们没有任何Internet连接,这
一部分就可以跳过去。注意,相比我们列出来的,这一部分可能会包含更多小
节,虽然我们这里只有了了几个,但足够应对我们已有的各种Internet连接了。
1. DHCP —— 如果脚本用到了DHCP,我们就要在此添加相应的配置。
2. PPPoE —— 如果想把脚本用于PPPoE连接,就要在此添加相应的配置。
2. LAN —— 如果防火墙后有局域网,就要使用这里的配置了。大部分情况下都会用
到这儿,因为局域网几乎总是存在的。
3. DMZ —— 对非军事区(DMZ zone)的配置。大部分脚本不会用到这个设置,因为
这些脚本针对的主要是一些普通的家庭网络,或小企业的网络。
4. Localhost —— 本地(local-host)的有关设置。虽然我把它们写成变量的形
式,但一般不会被改变,也不应该有什么理由要改变它们。
5. iptables —— 有关iptables的设置。大部分情况下,这里只设置一个变量,用
来指向iptables程序的位置。
6. Other —— 如果还有什么信息,首先应该把它们放在相应的小节里,实在没有相
应的小节,就放这儿吧。
2. Module loading —— 脚本应该维护一个模块列表。它分为两部分,第一部分包含必需
的模块,同时第二部分要包含不必要的模块的列表。
1. Required modules —— 这里装载的是必要的模块,它们可能会提高安全性或为
管理者、客户增加某些服务。
注意,即使我选择了这种结构,你也不一定非要用它,对你来说,它可能并不是最
好的。我选择它只是因为它易读,而且能很好地符合我的逻辑。
注意,这些模块可能会提高安全性,或为管理者、客户添加某些服务,还
有一些模块不是必需的,但它们可能也被加入了列表。不过,在本例中,
我已经注意了这个问题。
Iptables 指南 1.1.19 页码,67/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
2. Non-required modules —— 这里列出的是不必要的模块,所以它们都被注释掉
了。如果你用到了它们提供的功能,就可以启用它们。
3. proc configuration —— 这儿关心的是有关proc系统的设置。如果一些选项是必须
的,我们就启用它,如果不是,就把它注释掉。大部分有用的proc配置都列在这儿了,
但远远不是全部的。
1. Required proc configuration —— 包含了使脚本能正常工作的所有必需的proc
配置,它也可以包含一些能提高安全性或为管理者、客户增加特定服务的选项。
2. Non-required proc configuration —— 这里提到的选项不是必需的,虽然它们
可能很有用。因此,我把它们都注释掉了。当然,这里并没有包括所有这样的选
项。
4. rules set up —— 现在,应该添加规则了。我把所有的规则都明确地分配到了与表、
链相应的小节里。所有自定义的规则都写在系统内建的链之前(译者注:当然要写在前
面了,因为后面要调用它们哦)。另外,我是按照命令iptables -L输出的顺序来安排
此脚本里表与链的出现顺序的(译者注:这样,便于我们查看哦)。
1. Filter table —— 首先是filter表,而且我们先要设置策略。
1. Set policies —— 为所有系统内建的链设置策略。通常,我会设置
DROP,对于允许使用的服务或流会在后面明确地给以ACCEPT。这样,我们就
可以方便地排除所有我们不想让人们使用的端口。
2. Create user specified chains —— 在这里创建所有以后会用到的自定义
链。如果没有事先建立好,后面是不能使用它们的,所以我们要尽早地建立
这些链。
3. Create content in user specified chains —— 建立自定义链里使用的
规则。其实你也可以在后面的某个地方写这些规则,之所以写在这儿,唯一
的原因是这样做规则和链会离得近些,便于我们查看。
4. INPUT chain —— 创建INPUT链的规则。
5. FORWARD chain —— 为FORWARD链创建规则。
6. OUTPUT chain —— 为OUTPUT链创建规则。其实,在这里要建的规则很少。
2. nat table —— 在处理完filter表之后,该设置nat表了。我们这样做是有一定
原因的。首先,我们不想太早地打开转发机制(译者注:注意,filter表设定的
是过滤机制,而不是转发)和NAT功能,因为它们可能导致数据会在错误的时间
(也就是这样的时刻:我们打开了NAT,但过滤规则还没有运行)通过防火墙。还
有,我把nat表看作是围绕filter表的一个层。也就是说,filter表是核心, nat
表是它外部的一个层,mangle表是第二层。从某些观点来看,这可能有点不对,
但也八九不离十了。
1. Set policies —— 与filter一样,我们先来设置策略。一般说来,缺省的
策略,即ACCEPT,就很好。这个表不应该被用来做任何过滤,而且我们也不
从这里开始,我就是遵循iptables -L的输出格式来创建规则的,这
的唯一原因就是为了便于阅读,避免混淆。
Iptables 指南 1.1.19 页码,68/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
应该在这儿丢弃任何包,因为对我们假设的网络情况来说,可能会发生一些
难以应付的事情。我把策略设为ACCEPT,因为没有什么原因不这样做。
2. Create user specified chains —— 在这儿创建nat表会用到的自定义
链。一般情况下,我没有任何规则要在这儿建立,但我还是保留了这个小
节,以防万一罢了。注意,在被系统内建链调用之前一定要建好相应的自定
义链。
3. Create content in user specified chains —— 建立自定义链的规则。
4. PREROUTING chain —— 要对包做DNAT操作的话,就要用到此链了。大部分
脚本不会用到这条链,或者是把里面的规则注释掉了,因为我们不想在不了
解它的情况下就在防火墙上撕开一个大口子,这会对我们的局域网造成威
胁。当然,也有一些脚本默认使用了这条链,因为那些脚本的目的就是提供
这样的服务。
5. POSTROUTING chain —— 如果使用SNAT操作,就要在此建立规则。你可能
有一个或多个局域网需要防火墙的保护,而我就是依据这样的情况来写此脚
本的,所以这个脚本中使用的 POSTROUTING链是相当实用的。大部分情况
下,我们会使用SNAT target,但有些情况,如PPPoE,我们不得不使用
MASQUERADE target。
6. OUTPUT chain —— 不管什么脚本都几乎不会用到这个链。迄今为止,我还
没有任何好的理由使用它,如果你有什么理由用它了的话,麻烦你把相应的
规则也给我一份,我会把它加到本指南里的。
3. mangle table —— 最后要做的就是处理mangle表了。通常,我不会使用这个
表,因为一般情况下,它不会被任何人要到,除非他们有什么特殊的需要,比如
为了隐藏一条连接后的多台机子,我们要统一设置TTL或TOS等。在这个脚本里,
此表是空白的。但在此指南中还是有个小小的例子说明了mangle表的用处。
1. Set policies —— 设置策略。这里的情形和nat表几乎完全相同。这里不
应该做过滤,也不应该丢弃任何包。在任何脚本里我都不会把mangle表的策
略设为其他的值,也不鼓励你这样做。
2. Create user specified chains —— 建立自定义链。我几乎不会用到这个
链,所以没有建立任何规则。保留此小节,只是已备后用。
3. Create content in user specified chains —— 建立自定义链的规则。
4. PREROUTING —— 本指南的所有脚本都未在此链建立规则。
5. INPUT chain —— 本指南的所有脚本都未在此链建立规则。
6. FORWARD chain —— 本指南的所有脚本都未在此链建立规则。
7. OUTPUT chain —— 本指南的所有脚本都未在此链建立规则。
8. POSTROUTING chain —— 本指南的所有脚本都未在此链建立规则。
这应该可以较详细地解释每个脚本的结构是怎样的以及它们为什么要使用这个结构了。
Iptables 指南 1.1.19 页码,69/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
8.2. rc.firewall.txt
脚本rc.firewall.txt是核心,第七章防火墙配置实例 rc.firewall对它已经做了很详细的解
释,其他的脚本都是以它为基础得到的。这个脚本主要是针对具有两个连接的家庭网络而设
计的,如一个局域网连接,一个Internet连接。我们假设的情况是你有一个静态IP地址,不
需要DHCP,PPP, SLIP或其他什么协议为你动态分配IP。如果你想要的恰恰是使用这些协议
的脚本,就到rc.DHCP.firewall.txt看看吧。
脚本rc.firewall.txt要完全发挥作用,系统必需要有下面列出的功能,你可以把它们编译进内
核,也可以编译成模块。如果你改变了脚本,就要加入相应的功能模块或把它们编进内核。
􀁺 CONFIG_NETFILTER
􀁺 CONFIG_IP_NF_CONNTRACK
􀁺 CONFIG_IP_NF_IPTABLES
􀁺 CONFIG_IP_NF_MATCH_LIMIT
􀁺 CONFIG_IP_NF_MATCH_STATE
􀁺 CONFIG_IP_NF_FILTER
注意,上面的描述其实还是非常简单的,应该被看作一个摘要,它简要地解释了脚
本为什么要按照这种松散的结构来写。千万注意,我可没有说过这种结构是唯一
的、最好的。
Iptables 指南 1.1.19 页码,70/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
􀁺 CONFIG_IP_NF_NAT
􀁺 CONFIG_IP_NF_TARGET_LOG
8.3. rc.DMZ.firewall.txt
脚本rc.DMZ.firewall.txt所针对的情况是这样的:有一个可信任的内网,一个DMZ,还有一
个Internet连接。这里的DMZ是通过设置一对一的NAT 操作得到的,它需要IP别名(就是在一
块网卡上设置多个IP地址)的支持。我们还有其他的方法来实现 DMZ:如果你有一个整个的
网段,可划分子网,然后把某个子网分给DMZ,再为防火墙配置相应的内网与外网 IP地址
(译者注:第一种方法是针对有多个网段的情况,即内网一个网段,DMZ一个网段,第二种方
法是把一个网段划分成几个子网,这样就和第一种情况一样了)。注意,这种方法会多消耗
两个IP,一个是网络地址,一个是广播地址(译者注:具体细节请上网搜索子网划分的相关
信息,这个指南并不包含此类信息)。以上两种方法用哪一个就要你自己决定了。本指南会
给你实现防火墙与NAT的手段或叫做技术,但具体如何去做,没有完全的说明,因为这已经超
出本文的范围了。
这个脚本需要以下模块,也可能它们已被编译进内核了。
􀁺 CONFIG_NETFILTER
􀁺 CONFIG_IP_NF_CONNTRACK
􀁺 CONFIG_IP_NF_IPTABLES
Iptables 指南 1.1.19 页码,71/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
􀁺 CONFIG_IP_NF_MATCH_LIMIT
􀁺 CONFIG_IP_NF_MATCH_STATE
􀁺 CONFIG_IP_NF_FILTER
􀁺 CONFIG_IP_NF_NAT
􀁺 CONFIG_IP_NF_TARGET_LOG
从图中可以看出,此脚本假设你有两个内网,一个是可信任的内网,使用地址
192.168.0.0/24,另一个是 DMZ(我们正是对它做一对一的NAT),使用地址
192.168.1.0/24。如果有人从Internet向我们的DNS_IP发送一个包,我们就要对它使用DNAT,
之后,此包的目的地址就指向DMZ 里的DNS服务器了,它也就可以到达真正的DNS服务器。否
则,DNS服务器不会看到这个包,也就没有应答之说了。下面是实现上述DNAT功能的语句:
$IPTABLES -t nat -A PREROUTING -p TCP -i $INET_IFACE -d
$DNS_IP \
--dport 53 -j DNAT --to-destination $DMZ_DNS_IP
我们可以看出,这个规则要放在nat表的PREROUTING链中,包要满足的条件是:使用 TCP协议
且使用53号端口,从接口$INET_IFACE进入,而且要以$DNS_IP为目的。被匹配的包要交给DNAT
target来处理,它会把包的目的地址改为由--to-destination指定的地址$DMZ_DNS_IP。这就
是 DNAT的工作流程。当相应的应答包被发送到防火墙时,会自动地被un-DNAT。
现在,你应该完全可以读懂这个脚本了。如果有什么你不明白的东西在脚本的其他部分没有
被用到,那可能就是我的错误了,要告诉我哦。
8.4. rc.DHCP.firewall.txt
Iptables 指南 1.1.19 页码,72/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
脚本rc.DHCP.firewall.txt适用于那些使用DHCP、PPP或SLIP连接Internet的情况,它和原始
的脚本rc.firewall.txt几乎一样,主要的区别在于这里不再使用变量STATIC_IP。原因很简
单了,就是它不能和动态的IP一起使用。此脚本相对于原始脚本的改变是很少的,但还是有
一些人发信问我做了什么改变。经过大家的考验,这个脚本应该是一个很好的解决方案了。
它需要如下功能模块。
􀁺 CONFIG_NETFILTER
􀁺 CONFIG_IP_NF_CONNTRACK
􀁺 CONFIG_IP_NF_IPTABLES
􀁺 CONFIG_IP_NF_MATCH_LIMIT
􀁺 CONFIG_IP_NF_MATCH_STATE
􀁺 CONFIG_IP_NF_FILTER
􀁺 CONFIG_IP_NF_NAT
􀁺 CONFIG_IP_NF_TARGET_MASQUERADE
􀁺 CONFIG_IP_NF_TARGET_LOG
我做的改变主要是删除了变量STATIC_IP以及和它相关的所有东西。以前,主要的过滤工作是
基于变量STATIC_IP的,现在是INET_IFACE 了。也就是说,在这个脚本里,我们不再把-d
$STATIC_IP作为过滤的条件,而是用 -i $INET_IFACE。这几乎是唯一的改变,也是必需的改
Iptables 指南 1.1.19 页码,73/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
变。
可还是有一些问题要考虑。现在,我们不能再在INPUT链依据某些条件,比如--in-interface
$LAN_IFACE --dst $INET_IP来进行过滤(译者注:因为这时已无固定的INET_IP)。这强迫
我们只能基于Internet接口进行包的过滤,在这种情况下,内网必须访问那个可变的
Internet的IP。这会出现一些问题,有个例子可以说明这一点,就是我们在防火墙上运行
HTTP服务。如果我们访问这个网站(其中,主页包含了一个指向HTTP服务器的静态连接,这
可能是某个动态的DNS解决方案),问题就暴露了。经过NAT操作的机子会向DNS查询HTTP服务
器的IP,然后再试着访问这个IP。万一我们是基于接口和IP做的过滤,这台机子就不能访问
到HTTP了,因为INPUT链会 DROP掉这个包(译者注:还是因为Internet接口的IP不固定)。
在某种情况下,这也会发生在你有静态IP的时候,但在那种情况下,我们可以增加一条规
则,以检查LAN接口的包是否是发往INET_IP的,若是,则ACCEPT。
如果你看过以前的内容,得到或写一个可以获取动态IP的脚本可能会是个解决问题的好办
法。比如,我们可以写一个脚本,它紧随着Internet连接的启动而运行,而且它能从命令
ifconfig的输出中提取IP,再把这个IP赋给某个变量。较好的办法是使用一些程序自带的脚
本,如 pppd带的脚本ip-up。也有一些网站,如linuxguruz.org,提供了很多有用的脚本,
你可以在附录其他资源和链接找到它的链接。
还有一种方法可获得IP,就是在脚本里加上类似这样的语句:
INET_IP=`ifconfig $INET_IFACE | grep inet | cut -d : -f 2 |
\
cut -d ' ' -f 1`
上面这句话的作用是从ifconfig的输出里提取接口$INET_IFACE的IP,再赋给$INET_IP。更好
的办法是使用脚本 retreiveip.txt。但要注意,这个方法可能会引起一些不正常的情况,比
如使防火墙和内网之间已有的连接停止。下面就说明一下最常见的问题。
1. 如果这个脚本的代码是在另一个脚本内运行的,而那个脚本又是由PPP daemon启动的,
就会因为NEW not SYN rules(具体信息查看未设置SYN的NEW状态包)的原因而挂起所
以当前活动的连接。如果你删掉那个规则,可能会没有事,但还是不保险。
2. 如果你不想改动已有的规则,而又要添加或删除规则,还要不损害已有的规则,这就没
法做了。比如,你又想阻塞所有局域网里的机子访问防火墙,又想让它们能控制防火墙
上的PPP daemon,如果不删除那个用来阻塞的规则,怎么能完成这样的事?
3. 事情可能也不必这么复杂,就像上面说的,这会导致一些安全问题。但如果这个脚本能
保持简单,维持规则的顺序与发现问题都是很容易的。
8.5. rc.UTIN.firewall.txt
这个脚本比rc.firewall.txt的安全性要差一点。我明确地建议你尽可能使用后者,
因为前者的开放性大了点,所以外部攻击的威胁就大了。
Iptables 指南 1.1.19 页码,74/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
脚本rc.UTIN.firewall.txt适用于这样的情况:我们不信任任何与防火墙连接的网络,包括
内网。我们只允许内网使用POP3、HTTP 和FTP。至于从Internet来的连接,权限和其他脚本
一样。
此脚本需要以下功能模块。
􀁺 CONFIG_NETFILTER
􀁺 CONFIG_IP_NF_CONNTRACK
􀁺 CONFIG_IP_NF_IPTABLES
􀁺 CONFIG_IP_NF_MATCH_LIMIT
􀁺 CONFIG_IP_NF_MATCH_STATE
􀁺 CONFIG_IP_NF_FILTER
􀁺 CONFIG_IP_NF_NAT
􀁺 CONFIG_IP_NF_TARGET_LOG
这个脚本遵循的原则是不要相信任何人,包括我们自己的员工。这是个令人悲痛的现实,大
部分破坏和攻击确实是来自我们内部的。这个脚本只是在加强防火墙方面给了你一个例子。
它和 rc.firewall.txt并没有太多的不同,只是少了一些允许通行的规则。
Iptables 指南 1.1.19 页码,75/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
8.6. rc.test-iptables.txt
这个脚本用来测试iptables里所有的链。当然,这要根据你的配置情况做些操作,如打开
ip_forwarding或是设置masquerading,等等。只要你的安装了基本的 iptables,就可以使
用它。其实这个脚本只使用了LOG,以便能记录所有的ping请求与应答。通过这种方式,我们
就可以了解哪些链被穿越了以及被穿越的顺序。使用方法如下,先运行这个脚本,再发布一
个ping命令,如:
ping -c 1 host.on.the.internet
然后用命令tail -n 0 -f /var/log/messages就可看到用了哪些链以及是什么顺序,除非记
录因某些原因被替换了。
8.7. rc.flush-iptables.txt
rc.flush-iptables.txt不应该被称作脚本,它只是重置并清空所有的表、链。它先把filter
表的INPUT、OUTPUT和 FORWARD链的策略设为缺省的ACCEPT,然后是nat表的PREROUTING 、
POSTROUTING和OUTPUT链。我们这样做就不必为被关闭的连接和没有通过的包而操心。这个脚
本就是为防火墙设置和除错用的,因此我们只关心打开所有的东西并恢复它们的缺省值就行
了。
之后,我们清空filter表里所有的链,紧接着是NAT表的。这样,就不会有什么不应该存在的
规则了。这个做完后就该删除filter表和NAT表里的自定义链了。这时,脚本的工作就应该完
成了。当然,如果你用到了mangle表,可以在这个脚本里添加相应的清空规则。(译者注:
其实作者已经这样做了)
8.8. Limit-match.txt
这个脚本是用来测试limit match的,也会让你明白limit match是如何工作的。装入这个脚
本,再用不同的时间间隔发送ping数据包,可以看出哪个包可以通过,这些包又是以什么频
率通过的。你应该可以看出,在limit的burst值再次到达之前,所有的echo replies都会被
阻塞。
此脚本仅仅是为测试而写的。也就是说,不要使用类似这样的规则,它记录某一类
包的所有信息,这会很快地占满你的日志分区,而且它会成为一个有效的DoS攻
击。它还可能导致在最初的DoS攻击之后,无法记录真正的攻击信息。
最后再说明一下,有些人写信建议我把这个脚本放到rc.firewall脚本里面,而且
是用Red Hat Linux脚本的语法,这样当rc.firewall启动时,这个脚本也可以启
动。但我不会这样做的,因为这是一个指南,主要是用来学习iptables的使用方法
的,不应该有过多的shell脚本特有的语法。加入shell脚本特有的语法会使阅读的
难度大大增加,这就远离了我的初衷。这个指南是按照易读的标准来写的,以后我
会继续这样做。
Iptables 指南 1.1.19 页码,76/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
8.9. Pid-owner.txt
这个脚本说明了如何使用PID owner match。它其实什么都没做,但你可以运行一下,命令
iptables -L -v的输出会说明它确实匹配了些东西。
8.10. Sid-owner.txt
说明SID owner match如何使用的一个例子。同样,它也是什么都没做,但你可以运行一下,
命令iptables -L -v的输出会说明它确实匹配了些东西。
8.11. Ttl-inc.txt
一个小小的例子,说明了如何隐藏我们的防火墙或路由器,以使跟踪路由程序看不到,这样
就可以对可能的攻击者隐藏很多信息。
8.12. Iptables-save ruleset
这只是一个输出的例子,它在规则的保存与恢复里被用来说明 iptables-save命令是如何使
用的。所以,它没有任何用处,只是一个参考而已。
附录 A. 常用命令详解
A.1. 查看当前规则集的命令
查看当前正在使用的规则集是一个十分常用的操作,使用iptables的什么命令还记得吗?我
们可是在规则是如何练成的这一章里介绍过啊,虽然到时说得简单了点。再复习一下吧,命
令语法如下:
iptables -L
这个命令会尽可能地以易读的形式显示当前正在使用的规则集。比如,它会尽量用文
件/etc/services里相应的名字表示端口号,用相应的DNS记录表示IP地址。但后者可能会导致
一些问题,例如,它想尽力把LAN的IP地址(如192.168.1.1)解析成相应的名字。但
192.168.0.0/16这个网段是私有的,也就是说,它只能用在局域网里,而不能在Internet里使
用,所以它不会被Internet上的DNS服务器解析。因此,当解析这个地址时,命令就好像停在
那儿了。为了避免这种情况的发生,我们就要使用选项:
iptables -L -n
Iptables 指南 1.1.19 页码,77/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
如果你想看看每个策略或每条规则、每条链的简单流量统计,可以在上面的命令后再加一个
verbose标志,如下:
iptables -L -n -v
不要忘了,iptables -L命令还可以查看nat表和mangle表的内容哦(更不要忘了,默认的表
是filter),只需要使用-t选项,比如我们只想看nat表的规则,就用下面的命令:
iptables -L -t nat
在/proc里,可能还有一些文件你会感兴趣。比如,你可以在连接跟踪记录表里看到当前有哪
些连接。这个表包含了当前的所有连接,你还可以通过它了解到每个连接处于什么状态。要
注意,这个表是不能编辑的,即使可以,也不应该更改它。可以用下面的命令查看这个表:
cat /proc/net/ip_conntrack | less
此命令会显示当前所有被跟踪的连接,但要读懂那些记录可是有些难度哦。
A.2. 修正和清空iptables的命令
即使你把iptables弄的一塌糊涂,我们也有非常有效的命令来处理,而不必重新启动计算
机。我接到过很多关于这个问题的询问,所以我想最好在这儿回答一下。如果你增加的规则
有问题,要想删掉它,只要把命令中的-A改为-D即可。这样,iptables就会找到那个错误的
规则并删掉它,但如果在你的规则里有好几条同样的规则,它只能删掉找到的第一条。如果
你不想这样的事情发生,那就试试用序号来删除。如,你想删除INPUT链的第10条规则,可以
使用 iptables -D INPUT 10。
还有一种情况,就是要清空整个链,这就要使用选项-F。比如,我们要清空整个 INPUT链,
使用的命令就是iptables -F INPUT。但是要注意,选项-F并不改变链的缺省策略。所以,如
果被我们清空的那条INPUT链的策略是DROP,它还是会阻塞所有的包。那怎么才能重置策略
呢?还记得策略DROP是如何设置的吧,还是用那个方法啊。比如,我们把INPUT链的策略改为
ACCEPT,就用iptables -P INPUT ACCEPT。
我已经写了一个用来清空并重置iptables的脚本,叫做rc.flush- iptables.txt(附录里有
它的代码),在你写自己的防火墙脚本时,很可能会用到。但如果你在mangle表里乱试乱改
而导致问题的话,这个脚本就帮不上忙了。因为在脚本rc.firewall.txt里,我没有用到mangle
表,所以在 rc.flush-iptables.txt里也就没有添加相应的恢复功能。
附录 B. 常见问题与解答
B.1. 模块装载问题
装载模块时,你可能会遇到几个问题,比如,有错误提示说明没有你指定名字的那种模块:
insmod: iptable_filter: no module by
that name found
Iptables 指南 1.1.19 页码,78/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
这个提示是无关紧要的,因为那些模块很有可能已经被静态地编译进内核了。当你遇到这个
信息时,这是你应该首先想到的。至于是否真的如我们所想,最简单的测试方就是敲一个用
到那个模块功能的命令试试。对于上面的情况,可能是filter表没有装入,从而就没有相应
的功能,当然不能使用filter表了。为了检查 filter表是否装入,可以用下面的命令来试
试:
iptables -t filter -L
这个命令会输出filter表里所有的链,或者是运行失败,给出错误提示信息。如果一切正
常,输出结果类似下面的情况,当然,这还要看你是否已经在filter表里加入了规则(译者
注:在这个例子里,表是空的)。
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
如果你确实没有装载filter表,得到的就是如下信息:
iptables
v1.2.5: can't initialize iptables table `filter': Table \
does not exist (do you need to insmod?)
Perhaps iptables or your kernel needs to be upgraded.
这个问题就有些严重了,从此提示中我们能得到两个信息:第一,我们确实没有把相应的功
能编译进内核里;第二,在模块一般应在的目录中没有找到这个模块。这意味着问题是,你
或者忘记了装载想用的模块,或者没有用depmod -a命令更新模块数据库,或者没有把相应的
功能编译进内核(不论是静态的还是作为模块)。当然还可能是其他原因,但这些是主要
的,不管怎样,大部分原因是很容易解决的。比如,第一个问题可以简单地通过在内核源码
目录里运行make modules_install 命令来解决,这当然是有前提的,就是源码已经编译
(compile)而且模块已经构建(build)。第二个问题的解决办法也很简单,只要运行一下
depmod -a命令,之后再看看能否正常工作即可。第三个问题有点超出我们的范围了,而且这
个问题或多或少会让你感到发晕。更多的信息可以在Linux文档计划里找到。
在运行iptables时,你还可能得到另外一个错误信息:
iptables: No chain/target/match by that name
这说明你要用的链或target、或match不存在,原因有很多,但最普遍的是你拼错了名字。当
你想使用一个不可用的模块时也会产生这种错误。模块之所以不可用,可能是因为你没有装
载正确的模块,或者内核里不包含那个模块,或者是iptables自动装载模块时失败了。通
常,你不止应该考虑上面提到的所有解决办法,还要考虑规则中target的拼写错误,或者其
他的原因。
B.2. 未设置SYN的NEW状态包
Iptables 指南 1.1.19 页码,79/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
iptables有个“特点”没有被很好地给以说明,所以很多人(当然,也包括我)都忽视了
它。这个“特点”就是:如果你使用状态NEW,那么未设置SYN的包也会通过防火墙。之所以
有这个特点,是因为在某些情况下,我们想把那样的包看作某个(比如是和另一个防火墙有
关的)已处于ESTABLISHED状态的连接的一部分。这个特点使拥有两个或更多的防火墙协同工
作成为可能,而且可使数据在服务器间无丢失的传输,如辅助防火墙可以接受子网的防火墙
的操作。但它也会导致这样的事情:状态 NEW会允许几乎所有的TCP连接进入,而不管是否有
3次握手。为了处理这个问题,我们需要在防火墙的 INPUT链、OUTPUT链和FORWARD链加入如
下规则(译者注:此规则作者称为“NEW not SYN rules”,下一小节还会提到):
$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
注意,这个规则用于microsoft的TCP/IP(微软实现的TCP/IP就是不行,至少现在不行)产生
的包时还是有些问题。如果包是由microsoft的产品生成的,且被标为状态NEW,那么就会被
此规则记录然后丢弃。看起来规则工作很正常啊,是吧。但问题就出在这儿了,因为连接无
法中断了。这个问题出现在关闭连接时,在最后一个包即FIN/ACK包发出后,Netfilter的状
态机制就会关闭连接、删除连接跟踪表里的相应记录。但就在这时,Microsoft那不完善的程
序会发送另外一个包,这个包就是那种未设置SYN且被认为是NEW状态的包,因此它就会被上
面的规则匹配。换句话说,就是对这个规则不需要过于关注,如果你很在意它,就在规则里
加入选项--log-headers吧。这样,你就可以把包头记录下来,从而可以更好地了解相应的
包。
对于这个规则,还有一些已知的问题。比如,某个连接(比如是从LAN发出的)已经连接到防
火墙,而且有个脚本要在启动PPP时激活。当你启动PPP连接时,刚才提到的那个连接可能就
会被干掉(be killed)。当然,这只会在特定的情况下才能发生,就是你把conntrack和nat
作为模块运行,并且每次运行那个脚本时这两个模块都要被装入和卸载。如果你在防火墙之
外的机子上运行telnet,而且又通过这个telnet连接运行脚本rc.firewall.txt,也会导致上面
的问题。为了能简单地表达这个问题,你先准备一个telnet连接,或其他的流连接,再运行
连接跟踪模块,然后装入上面的规则,最后,试着用telnet client或daemon发送一些数据。
效果应该出来了,连接跟踪代码会认为这个连接是非法的,因为在此之前,它没有看到任何
方向有包发出,更为严重的是现在连接上有了未设置SYN的包,因为刚才由telnet client或
daemon发出的包肯定不是这个连接的第一个包。因此,上面的规则就起作用了,也就是说,
这个包会被记录下来,然后被无情地扔掉,从而连接就会中断。
B.3. NEW状态的SYN/ACK包
某些,TCP欺骗攻击所用的技术叫做序列号预测(Sequence Number Prediction)。在这类攻
击中,攻击者利用另一台机子的IP访问攻击对象(译者注:这就是为什么叫欺骗的原因了,
攻击者是想假冒另一台被攻击对象信任的机子,以达到欺骗攻击对象的目的),然后再试着
预测攻击对象使用什么序列号。
我们来看看典型的使用序列号预测技术的欺骗是如何实现的,参与者:攻击者[A]
(attacker)试图假装另一台机子[O](other host)向受害者[V](victim)发送数据。
警告,在Netfilter/iptables项目中,这个特点所拥有的行为缺少文档说明,更明
确的说,在你的防火墙上,它是一个很不安全的因素。
Iptables 指南 1.1.19 页码,80/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
1. [A]以[O]的IP为源地址向[V]发SYN。
2. [V]向[O]回应SYN/ACK。
3. 现在,若[O]以RST回应这个未知的SYN/ACK,攻击就失败了,但如果[O]已经没有这个能
力了呢?比如它早已被另外的攻击(如SYN flood)降服,或者被关闭,或者它的RST包
被防火墙拒绝。
4. 如果[O]没能破坏这条连接,而且[A]猜对了序列号,那它就能以[O]的身份和[V]交谈
了。
只要我们没能在第三步以RST回应那个未知的SYN/ACK包,[V]就会被攻击,而且我们还会被连
累(译者注:因为我们本身也被攻击了,而且还可能会成为攻击者的替罪羊被起诉,呜呜,
好惨)。所以,为安全起见,我们应该以正确的方式向[V]发送一个RST包。如果我们使用类
似“NEW not SYN rules”(译者注:在上一小节中)的规则,SYN/ACK包就可以被丢弃了。
因此,我们在bad_tcp_packets链中加入了如下规则:
iptables -A
bad_tcp_packets -p tcp --tcp-flags SYN,ACK SYN,ACK \
-m state --state NEW -j REJECT --reject-with tcp-reset
这样,你想成为上面那个[O]的机会就很少了(译者注:作者好幽默啊,我们可不想成为被别
人利用的对象),而且这条规则在绝大部分情况下是安全的,不会有什么副作用,但多个防
火墙要协同工作的情况要除外。那种情况下,防火墙之间会经常传递、接受包或流,有了这
条规则,有些连接可能会被阻塞,即使是合法的连接。这条规则的存在还产生了另外一问
题,就是有几个portscan(端口扫描器)会看到我们的防火墙,但好在仅此而已。
B.4. 使用私有IP地址的ISP
我的一位朋友告诉我说有些事我完全忘记了,从那时起,我就把这一节加上了。你刚上网时
连接的网络是 ISP提供的,但某些愚蠢的ISP在那个网络里使用的是私有地址,而那是IANA专
门分配给局域网使用的。Swedish Internet Service Provider和电话垄断企业Telia就是这
样做的,例如在DNS服务器上,他们使用的IP地址段就是10.x.x.x。我们最容易遇到的问题
是,在这个脚本里,为了防止被欺骗,不允许从10.x.x.x发出的连接来访问我们。不幸的
是,对于上面的例子,为了DNS能正常地被访问,我们不得不把规则的放宽松一些。也就是
说,我们或者在刚才提到的那条防止欺骗的规则上面增加一条规则(如下),或者是把那条
规则注释掉:
/usr/local/sbin/iptables -t nat -I PREROUTING -i
eth1 -s \
10.0.0.1/32 -j ACCEPT
我愿意对这些ISP再多费些唇舌。这些IP地址不是为了让你象这样愚蠢的使用而分配给你的,
至少我知道不是这样的。对于一个大集团的站点或者是我们自己的家庭网络来说,这样用是
很合适的,但你不能只因为你们的一些原因就强迫我们把自己公示于天下。
Iptables 指南 1.1.19 页码,81/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
B.5. 放行DHCP数据
一旦你了解DHCP是如何工作的,就会知道这其实是一个很简单的任务。但你必须小心处理到
底让谁进入、不让谁进入。首先,我们要明白DHCP是工作在UDP协议之上的,所以,UDP协议
是我们期望的第一个条件。其次,我们应该检查是从那个接口接收和发送请求的。例如,如
果我们设置了DHCP使用接口eth0,那就要阻塞 eth1上的DHCP请求。为了让规则再详细些,我
们只需打开(allow)DHCP实际使用的UDP端口,一般都是67和 68。这两个端口是标准定义,
我们就用它们来匹配被允许的包。现在,规则应该是这个样子的:
$IPTABLES -I INPUT -i $LAN_IFACE -p udp --dport 67:68 --sport \
67:68 -j ACCEPT
注意,现在我们能够接受所有来自和发往UDP端口67、68的数据,好像不太安全,但这并不是
多大的问题,因为这条规则只允许从67或68端口连接的主机才能访问。当然,此规则还可以
更严谨一些,但也应该足够接受所有的DHCP请求和更新,而不至于需要在防火墙上开一个大
洞。如果你很在意现在的规则是否很宽松,你当然可以写一个限制条件更紧的。
B.6. 关于mIRC DCC的问题
mIRC使用一个特殊的设定,它可以使mIRC连接穿过防火墙,也可以使DCC连接能在防火墙不了
解它的情况下正常工作。如果此选项和iptables还有ip_conntrack_irc模块与ip_nat_irc模
块一起使用,那mIRC就不能工作了。问题在于mIRC会自动对包进行NAT操作,这样当包到达防
火墙后,防火墙就完全不知道该对包做什么了,也不知道该怎么做。如果是防火墙来处理,
它只是简单地用自己的IP去询问IRC服务器,然后用那个地址发送DCC请求。mIRC不希望防火
墙自作聪明地以这种方式代替自己来处理这个包。
打开“I am behind a firewall”(我在防火墙后)这个配置选项并且使用
ip_conntrack_irc和 ip_nat_irc模块,会导致Netfilter建立包含“Forged DCC send
packet”的记录。
最简单的解决办法是不要选中mIRC的那个选项而让iptables来做这些工作。意思就是要明确
地告诉mIRC,它不是在防火墙后面的。
附录 C. ICMP类型
这是一个完整的ICMP类型的列表:
Table C-1. ICMP类型
TYPE CODE Description Query Error
0 0 Echo Reply——回显应答(Ping应答) x
3 0 Network Unreachable——网络不可达 x
3 1 Host Unreachable——主机不可达 x
Iptables 指南 1.1.19 页码,82/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
3 2 Protocol Unreachable——协议不可达 x
3 3 Port Unreachable——端口不可达 x
3 4 Fragmentation needed but no frag. bit set—
—需要进行分片但设置不分片比特
x
3 5 Source routing failed——源站选路失败 x
3 6 Destination network unknown——目的网络未知 x
3 7 Destination host unknown——目的主机未知 x
3 8 Source host isolated (obsolete)——源主机被
隔离(作废不用)
x
3 9 Destination network administratively
prohibited——目的网络被强制禁止
x
3 10 Destination host administratively
prohibited——目的主机被强制禁止
x
3 11 Network unreachable for TOS——由于服务类型
TOS,网络不可达
x
3 12 Host unreachable for TOS——由于服务类型
TOS,主机不可达
x
3 13 Communication administratively prohibited
by filtering——由于过滤,通信被强制禁止
x
3 14 Host precedence violation——主机越权 x
3 15 Precedence cutoff in effect——优先中止生效 x
4 0 Source quench——源端被关闭(基本流控制)
5 0 Redirect for network——对网络重定向
5 1 Redirect for host——对主机重定向
5 2 Redirect for TOS and network——对服务类型
和网络重定向
5 3 Redirect for TOS and host——对服务类型和主
机重定向
8 0 Echo request——回显请求(Ping请求) x
9 0 Router advertisement——路由器通告
10 0 Route solicitation——路由器请求
11 0 TTL equals 0 during transit——传输期间生存
时间为0
x
11 1 TTL equals 0 during reassembly——在数据报
组装期间生存时间为0
x
12 0 IP header bad (catchall error)——坏的IP首
部(包括各种差错)
x
12 1 Required options missing——缺少必需的选项 x
13 0 Timestamp request (obsolete)——时间戳请求
(作废不用)
x
14 Timestamp reply (obsolete)——时间戳应答
(作废不用)
x
15 0 Information request (obsolete)——信息请求x
Iptables 指南 1.1.19 页码,83/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
附录 D. 其他资源和链接
这里有一些资源的链接,我从这些地方获得了不少信息,相信对你应该也很有帮助:
􀁺 ip-sysctl.txt ——来自内核2.4.14,一篇关于IP网络控制参数的短小精干的参考文
章。
􀁺 The Internet Control Message Protocol ——一篇很好的详细介绍ICMP协议的文章,
作者是Ralph Walden。
􀁺 RFC 792 - Internet Control Message Protocol ——ICMP的权威文件,如果你想找关
于ICMP协议的信息,这是你应该首先想到的地方。作者:J. Postel。
􀁺 RFC 793 - Transmission Control Protocol ——TCP的权威文件,从1981年开始,它
就成为TCP的规范了。只要你想学习TCP,就一定要读读这篇技术性很强的文章。作者:
J. Postel
􀁺 ip_dynaddr.txt ——来自内核2.4.14,关于通过sysctl和proc文件系统设置
ip_dynaddr 的参考文章。
􀁺 iptables.8 ——iptables 1.2.4的帮助,这是HTML版本的。在你读写iptables规则
时,这是一个很好的参考,你应该把它带在身边。
􀁺 Firewall rules table ——由Stuart Clark给出的一个小小的PDF文件,里面是防火墙
配置的参考样式,对你书写自己的防火墙规则很有帮助。
􀁺 http://www.netfilter.org/ ——Netfilter和iptables的官方网站,是每一个打算在
linux里配置iptables和 Netfilter的人必到之处。
􀁺 http://www.netfilter.org/documentation/index.html#FAQ ——官方的Netfilter
Frequently Asked Questions,是开始了解iptables和Netfilter的好去处。
􀁺 http://www.netfilter.org/unreliable-guides/packet-filtering-HOWTO/index.html
——非常好的包过滤基础指南,介绍了如何使用iptables进行包过滤。作者是iptables
和Netfilter的核心开发者之一Rusty Russell。
􀁺 http://www.netfilter.org/unreliable-guides/NAT-HOWTO/index.html ——介绍网络
地址转换的很好的指南。作者是iptables和Netfilter的核心开发者之一Rusty
Russell。
􀁺 http://www.netfilter.org/unreliable-guides/netfilter-hacking-
HOWTO/index.html ——只有很少的文章介绍如何在Netfilter和iptables 的用户空
间、内核空间里编写代码,这是其中一篇。作者还是Rusty Russell。
(作废不用)
16 0 Information reply (obsolete)——信息应答
(作废不用)
x
17 0 Address mask request——地址掩码请求x
18 0 Address mask reply——地址掩码应答x
Iptables 指南 1.1.19 页码,84/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
􀁺 http://www.linuxguruz.org/iptables/ ——很好的资源链接网页,里包含了Internet
上大部分关于iptables的链接,尤其是它还包含了很多为不同用处而写的iptables脚本
的链接。
􀁺 http://www.islandsoft.net/veerapen.html ——这篇文章讨论了iptables自动增强坚
固性的可能,以及如何通过很少的改动使你的计算机能自动地把敌对站点加入iptables
的一个特殊的“禁止列表”。
􀁺 /etc/protocols ——此文件是从Slackware发行版中抽取的。你可以利用此文件找到协议
所对应的协议号,如IP、ICMP或TCP对应的号码。
􀁺 /etc/services ——此文件也是从Slackware发行版中抽取的。它非常值得一读,你可以
大致了解什么协议使用什么端口。
􀁺 Internet Engineering Task Force ——IETF是制定和维护互联网标准的最大的组织之
一,很多大企业集团和个人都是它的成员,他们共同工作是为了确保Internet的互操作
性。
􀁺 Linux Advanced Routing and Traffic Control HOW-TO ——此站点主要讨论Linux高
级路由和流量控制,这个HOW-TO是关于Linux高级路由的最大的也是最好的一篇文章。
作者是Bert Hubert。
􀁺 Paksecured Linux Kernel patches ——此站点包含了Matthew G. Marsh写的所有内核
补丁,FTOS patch就在这儿。
􀁺 ULOGD project page ——ULOGD的站点。
􀁺 The Linux Documentation Project ——有关Linux的文档的极好(可以说是最好)的
站点。有关Linux的很多较大的文档这儿都有,如果TLDP里没有,你就要好好地在网络
上搜索一下了。如果你想了解多一些,就去看看吧。
􀁺 http://kalamazoolinux.org/presentations/20010417/conntrack.html ——这篇文章
里有一个极其精彩的例子,它是用来展示conntrack模块以及它在Netfilter里的工作
的。如果你想多看一些有关conntrack的文章,这一篇应该是必读的。
􀁺 http://www.docum.org/ ——此站点包含了全部有关CBQ(Class Based Queue)、tc和
ip命令的资料,这是很少的几个这样的站点中的一个。此站点由Stef Coene维护。
􀁺 http://lists.samba.org/mailman/listinfo/netfilter ——Netfilter的官方邮件列
表,非常有用哦。万一你遇到了一些问题,而这篇文章或这里提到的一些链接解决不
了,它就是你的救世主了。
当然,资源不止我上面提到的这些,还有iptables的源码和文档,及很多可以帮助你的朋
友。
附录 E. 鸣谢
很多朋友在我写这篇文章时给了我热心的帮助,我要感谢他们:
􀁺 Fabrice Marie,对我糟糕的语法和拼写做了大量的订正,还用make文件等工具把这篇
Iptables 指南 1.1.19 页码,85/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
指南转换成了DocBook。
􀁺 Marc Boucher,在状态匹配代码的使用方面给了我很多帮助。
􀁺 Frode E. Nyboe,大幅度改善了rc.firewall的规则,当我要重写这个规则集、把多个表
的遍历(the multiple table traversing)引入同一份文件时,给了我很多灵感。
􀁺 Chapman Brad, Alexander W. Janssen,开始时,我对包如何穿越nat和filter 表的理
解是错误的,是他们使我了解到这一点的,而且他们还给了我正确的顺序。
􀁺 Michiel Brandenburg, Myles Uyema,帮我解决了一些状态匹配代码,并让它正常问
题。
􀁺 Kent `Artech' Stahre,帮我绘制图形,还帮我查错。
􀁺 Anders 'DeZENT' Johansson,提示我有些古怪的ISP在Internet上使用保留的网址,至
少对他来说遇到了这样的情况。
􀁺 Jeremy `Spliffy' Smith,提示我有些内容容易使大家糊涂,还帮我进行了测试和查
错。
还有很多人,我和他们进行过讨论,也请教过他们,这里不能一一提及了。
Appendix F. History
Version 1.1.19 (21 May 2003)
.
By: Oskar Andreasson
Contributors: Peter van Kampen, Xavier Bartol, Jon Anderson, Thorsten Bremer
and Spanish Translation Team.
Version 1.1.18 (24 Apr 2003)
.
By: Oskar Andreasson
Contributors: Stuart Clark, Robert P. J. Day,
Mark Orenstein and Edmond Shwayri.
Version 1.1.17 (6 Apr 2003)
.
By: Oskar Andreasson
Contributors: Geraldo Amaral Filho, Ondrej Suchy, Dino Conti,
Robert P. J. Day,
Velev Dimo, Spencer Rouser, Daveonos, Amanda Hickman, Olle Jonsson and
Bengt Aspvall.
Version 1.1.16 (16 Dec 2002)
.
By: Oskar Andreasson
Contributors: Clemens Schwaighower, Uwe Dippel and Dave Wreski.
Iptables 指南 1.1.19 页码,86/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
Version 1.1.15 (13 Nov 2002)
.
By: Oskar Andreasson
Contributors: Mark Sonarte, A. Lester Buck, Robert P. J. Day, Togan Muftuoglu,
Antony Stone, Matthew F. Barnes and Otto Matejka.
Version 1.1.14 (14 Oct 2002)
.
By: Oskar Andreasson
Contributors: Carol Anne, Manuel Minzoni, Yves Soun, Miernik, Uwe Dippel,
Dave Klipec and Eddy L O Jansson.
Version 1.1.13 (22 Aug 2002)
http://iptables- tutorial.haringstad.com
By: Oskar Andreasson
Contributors: Tons of people reporting bad HTML version.
Version 1.1.12 (19 Aug 2002)
http://www.netfilter.org/tutorial/
By: Oskar Andreasson
Contributors: Peter Schubnell, Stephen J. Lawrence, Uwe Dippel, Bradley
Dilger, Vegard Engen, Clifford Kite, Alessandro Oliveira, Tony Earnshaw,
Harald Welte, Nick Andrew and Stepan Kasal.
Version 1.1.11 (27 May 2002)
http://www.netfilter.org/tutorial/
By: Oskar Andreasson
Contributors: Steve Hnizdur, Lonni Friedman, Jelle Kalf, Harald Welte,
Valentina Barrios and Tony Earnshaw.
Version 1.1.10 (12 April 2002)
http://www.boingworld.com/workshops/linux/iptables-tutorial/
By: Oskar Andreasson
Contributors: Jelle Kalf, Theodore Alexandrov, Paul Corbett, Rodrigo
Rubira Branco, Alistair Tonner, Matthew G. Marsh, Uwe Dippel, Evan
Nemerson and Marcel J.E. Mol.
Version 1.1.9 (21 March 2002)
http://www.boingworld.com/workshops/linux/iptables-tutorial/
By: Oskar Andreasson
Contributors: Vince Herried, Togan Muftuoglu, Galen Johnson, Kelly Ashe, Janne
Johansson, Thomas Smets, Peter Horst, Mitch Landers, Neil Jolly, Jelle Kalf,
Jason Lam and Evan Nemerson.
Version 1.1.8 (5 March 2002)
http://www.boingworld.com/workshops/linux/iptables-tutorial/
By: Oskar Andreasson
Version 1.1.7 (4 February 2002)
http://www.boingworld.com/workshops/linux/iptables-tutorial/
By: Oskar Andreasson
Contributors: Parimi Ravi, Phil Schultz, Steven McClintoc, Bill Dossett,
Dave Wreski, Erik Sj鰈und, Adam Mansbridge, Vasoo Veerapen, Aladdin and
Iptables 指南 1.1.19 页码,87/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
Rusty Russell.
Version 1.1.6 (7 December 2001)
http://people.unix-fu.org/andreasson/
By: Oskar Andreasson
Contributors: Jim Ramsey, Phil Schultz, G鰎an B錱e, Doug Monroe, Jasper
Aikema, Kurt Lieber, Chris Tallon, Chris Martin, Jonas Pasche, Jan
Labanowski, Rodrigo R. Branco, Jacco van Koll and Dave Wreski.
Version 1.1.5 (14 November 2001)
http://people.unix-fu.org/andreasson/
By: Oskar Andreasson
Contributors: Fabrice Marie, Merijn Schering and Kurt Lieber.
Version 1.1.4 (6 November 2001)
http://people.unix-fu.org/andreasson
By: Oskar Andreasson
Contributors: Stig W. Jensen, Steve Hnizdur, Chris Pluta and Kurt Lieber.
Version 1.1.3 (9 October 2001)
http://people.unix-fu.org/andreasson
By: Oskar Andreasson
Contributors: Joni Chu, N.Emile Akabi- Davis and Jelle Kalf.
Version 1.1.2 (29 September 2001)
http://people.unix-fu.org/andreasson
By: Oskar Andreasson
Version 1.1.1 (26 September 2001)
http://people.unix-fu.org/andreasson
By: Oskar Andreasson
Contributors: Dave Richardson.
Version 1.1.0 (15 September 2001)
http://people.unix-fu.org/andreasson
By: Oskar Andreasson
Version 1.0.9 (9 September 2001)
http://people.unix-fu.org/andreasson
By: Oskar Andreasson
Version 1.0.8 (7 September 2001)
http://people.unix-fu.org/andreasson
By: Oskar Andreasson
Version 1.0.7 (23 August 2001)
http://people.unix-fu.org/andreasson
By: Oskar Andreasson
Contributors: Fabrice Marie.
Version 1.0.6
http://people.unix-fu.org/andreasson
By: Oskar Andreasson
Iptables 指南 1.1.19 页码,88/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
Version 1.0.5
http://people.unix-fu.org/andreasson
By: Oskar Andreasson
Contributors: Fabrice Marie.
Appendix G. GNU Free Documentation
License
Version 1.1, March 2000
Copyright (C) 2000 Free Software Foundation, Inc. 59 Temple Place,
Suite 330, Boston, MA 02111-1307 USA Everyone is permitted to copy and
distribute verbatim copies of this license document, but changing it is
not allowed.
0. PREAMBLE
The purpose of this License is to make a manual, textbook, or other written
document "free" in the sense of freedom: to assure everyone the effective freedom
to copy and redistribute it, with or without modifying it, either commercially or
noncommercially. Secondarily, this License preserves for the author and publisher
a way to get credit for their work, while not being considered responsible for
modifications made by others.
This License is a kind of "copyleft", which means that derivative works of the
document must themselves be free in the same sense. It complements the GNU
General Public License, which is a copyleft license designed for free software.
We have designed this License in order to use it for manuals for free software,
because free software needs free documentation: a free program should come with
manuals providing the same freedoms that the software does. But this License is
not limited to software manuals; it can be used for any textual work, regardless
of subject matter or whether it is published as a printed book. We recommend this
License principally for works whose purpose is instruction or reference.
1. APPLICABILITY AND DEFINITIONS
This License applies to any manual or other work that contains a notice placed by
the copyright holder saying it can be distributed under the terms of this
License. The "Document", below, refers to any such manual or work. Any member of
the public is a licensee, and is addressed as "you".
Iptables 指南 1.1.19 页码,89/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
A "Modified Version" of the Document means any work containing the Document or a
portion of it, either copied verbatim, or with modifications and/or translated
into another language.
A "Secondary Section" is a named appendix or a front-matter section of the
Document that deals exclusively with the relationship of the publishers or
authors of the Document to the Document's overall subject (or to related matters)
and contains nothing that could fall directly within that overall subject. (For
example, if the Document is in part a textbook of mathematics, a Secondary
Section may not explain any mathematics.) The relationship could be a matter of
historical connection with the subject or with related matters, or of legal,
commercial, philosophical, ethical or political position regarding them.
The "Invariant Sections" are certain Secondary Sections whose titles are
designated, as being those of Invariant Sections, in the notice that says that
the Document is released under this License.
The "Cover Texts" are certain short passages of text that are listed, as Front-
Cover Texts or Back-Cover Texts, in the notice that says that the Document is
released under this License.
A "Transparent" copy of the Document means a machine-readable copy, represented
in a format whose specification is available to the general public, whose
contents can be viewed and edited directly and straightforwardly with generic
text editors or (for images composed of pixels) generic paint programs or (for
drawings) some widely available drawing editor, and that is suitable for input to
text formatters or for automatic translation to a variety of formats suitable for
input to text formatters. A copy made in an otherwise Transparent file format
whose markup has been designed to thwart or discourage subsequent modification by
readers is not Transparent. A copy that is not "Transparent" is called "Opaque".
Examples of suitable formats for Transparent copies include plain ASCII without
markup, Texinfo input format, LaTeX input format, SGML or XML using a publicly
available DTD, and standard-conforming simple HTML designed for human
modification. Opaque formats include PostScript, PDF, proprietary formats that
can be read and edited only by proprietary word processors, SGML or XML for which
the DTD and/or processing tools are not generally available, and the machinegenerated
HTML produced by some word processors for output purposes only.
The "Title Page" means, for a printed book, the title page itself, plus such
following pages as are needed to hold, legibly, the material this License
requires to appear in the title page. For works in formats which do not have any
title page as such, "Title Page" means the text near the most prominent
appearance of the work's title, preceding the beginning of the body of the text.
2. VERBATIM COPYING
You may copy and distribute the Document in any medium, either commercially or
noncommercially, provided that this License, the copyright notices, and the
license notice saying this License applies to the Document are reproduced in all
Iptables 指南 1.1.19 页码,90/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
copies, and that you add no other conditions whatsoever to those of this License.
You may not use technical measures to obstruct or control the reading or further
copying of the copies you make or distribute. However, you may accept
compensation in exchange for copies. If you distribute a large enough number of
copies you must also follow the conditions in section 3.
You may also lend copies, under the same conditions stated above, and you may
publicly display copies.
3. COPYING IN QUANTITY
If you publish printed copies of the Document numbering more than 100, and the
Document's license notice requires Cover Texts, you must enclose the copies in
covers that carry, clearly and legibly, all these Cover Texts: Front-Cover Texts
on the front cover, and Back-Cover Texts on the back cover. Both covers must also
clearly and legibly identify you as the publisher of these copies. The front
cover must present the full title with all words of the title equally prominent
and visible. You may add other material on the covers in addition. Copying with
changes limited to the covers, as long as they preserve the title of the Document
and satisfy these conditions, can be treated as verbatim copying in other
respects.
If the required texts for either cover are too voluminous to fit legibly, you
should put the first ones listed (as many as fit reasonably) on the actual cover,
and continue the rest onto adjacent pages.
If you publish or distribute Opaque copies of the Document numbering more than
100, you must either include a machine-readable Transparent copy along with each
Opaque copy, or state in or with each Opaque copy a publicly-accessible computernetwork
location containing a complete Transparent copy of the Document, free of
added material, which the general network-using public has access to download
anonymously at no charge using public-standard network protocols. If you use the
latter option, you must take reasonably prudent steps, when you begin
distribution of Opaque copies in quantity, to ensure that this Transparent copy
will remain thus accessible at the stated location until at least one year after
the last time you distribute an Opaque copy (directly or through your agents or
retailers) of that edition to the public.
It is requested, but not required, that you contact the authors of the Document
well before redistributing any large number of copies, to give them a chance to
provide you with an updated version of the Document.
4. MODIFICATIONS
You may copy and distribute a Modified Version of the Document under the
conditions of sections 2 and 3 above, provided that you release the Modified
Version under precisely this License, with the Modified Version filling the role
Iptables 指南 1.1.19 页码,91/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
of the Document, thus licensing distribution and modification of the Modified
Version to whoever possesses a copy of it. In addition, you must do these things
in the Modified Version:
A. Use in the Title Page (and on the covers, if any) a title distinct from that
of the Document, and from those of previous versions (which should, if there
were any, be listed in the History section of the Document). You may use the
same title as a previous version if the original publisher of that version
gives permission.
B. List on the Title Page, as authors, one or more persons or entities
responsible for authorship of the modifications in the Modified Version,
together with at least five of the principal authors of the Document (all of
its principal authors, if it has less than five).
C. State on the Title page the name of the publisher of the Modified Version,
as the publisher.
D. Preserve all the copyright notices of the Document.
E. Add an appropriate copyright notice for your modifications adjacent to the
other copyright notices.
F. Include, immediately after the copyright notices, a license notice giving
the public permission to use the Modified Version under the terms of this
License, in the form shown in the Addendum below.
G. Preserve in that license notice the full lists of Invariant Sections and
required Cover Texts given in the Document's license notice.
H. Include an unaltered copy of this License.
I. Preserve the section entitled "History", and its title, and add to it an
item stating at least the title, year, new authors, and publisher of the
Modified Version as given on the Title Page. If there is no section entitled
"History" in the Document, create one stating the title, year, authors, and
publisher of the Document as given on its Title Page, then add an item
describing the Modified Version as stated in the previous sentence.
J. Preserve the network location, if any, given in the Document for public
access to a Transparent copy of the Document, and likewise the network
locations given in the Document for previous versions it was based on. These
may be placed in the "History" section. You may omit a network location for
a work that was published at least four years before the Document itself, or
if the original publisher of the version it refers to gives permission.
K. In any section entitled "Acknowledgements" or "Dedications", preserve the
section's title, and preserve in the section all the substance and tone of
each of the contributor acknowledgements and/or dedications given therein.
L. Preserve all the Invariant Sections of the Document, unaltered in their text
and in their titles. Section numbers or the equivalent are not considered
part of the section titles.
Iptables 指南 1.1.19 页码,92/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
M. Delete any section entitled "Endorsements". Such a section may not be
included in the Modified Version.
N. Do not retitle any existing section as "Endorsements" or to conflict in
title with any Invariant Section.
If the Modified Version includes new front-matter sections or appendices that
qualify as Secondary Sections and contain no material copied from the Document,
you may at your option designate some or all of these sections as invariant. To
do this, add their titles to the list of Invariant Sections in the Modified
Version's license notice. These titles must be distinct from any other section
titles.
You may add a section entitled "Endorsements", provided it contains nothing but
endorsements of your Modified Version by various parties--for example, statements
of peer review or that the text has been approved by an organization as the
authoritative definition of a standard.
You may add a passage of up to five words as a Front-Cover Text, and a passage of
up to 25 words as a Back-Cover Text, to the end of the list of Cover Texts in the
Modified Version. Only one passage of Front-Cover Text and one of Back-Cover Text
may be added by (or through arrangements made by) any one entity. If the Document
already includes a cover text for the same cover, previously added by you or by
arrangement made by the same entity you are acting on behalf of, you may not add
another; but you may replace the old one, on explicit permission from the
previous publisher that added the old one.
The author(s) and publisher(s) of the Document do not by this License give
permission to use their names for publicity for or to assert or imply endorsement
of any Modified Version.
5. COMBINING DOCUMENTS
You may combine the Document with other documents released under this License,
under the terms defined in section 4 above for modified versions, provided that
you include in the combination all of the Invariant Sections of all of the
original documents, unmodified, and list them all as Invariant Sections of your
combined work in its license notice.
The combined work need only contain one copy of this License, and multiple
identical Invariant Sections may be replaced with a single copy. If there are
multiple Invariant Sections with the same name but different contents, make the
title of each such section unique by adding at the end of it, in parentheses, the
name of the original author or publisher of that section if known, or else a
unique number. Make the same adjustment to the section titles in the list of
Invariant Sections in the license notice of the combined work.
In the combination, you must combine any sections entitled "History" in the
various original documents, forming one section entitled "History"; likewise
combine any sections entitled "Acknowledgements", and any sections entitled
Iptables 指南 1.1.19 页码,93/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
"Dedications". You must delete all sections entitled "Endorsements."
6. COLLECTIONS OF DOCUMENTS
You may make a collection consisting of the Document and other documents released
under this License, and replace the individual copies of this License in the
various documents with a single copy that is included in the collection, provided
that you follow the rules of this License for verbatim copying of each of the
documents in all other respects.
You may extract a single document from such a collection, and distribute it
individually under this License, provided you insert a copy of this License into
the extracted document, and follow this License in all other respects regarding
verbatim copying of that document.
7. AGGREGATION WITH INDEPENDENT WORKS
A compilation of the Document or its derivatives with other separate and
independent documents or works, in or on a volume of a storage or distribution
medium, does not as a whole count as a Modified Version of the Document, provided
no compilation copyright is claimed for the compilation. Such a compilation is
called an "aggregate", and this License does not apply to the other selfcontained
works thus compiled with the Document, on account of their being thus
compiled, if they are not themselves derivative works of the Document.
If the Cover Text requirement of section 3 is applicable to these copies of the
Document, then if the Document is less than one quarter of the entire aggregate,
the Document's Cover Texts may be placed on covers that surround only the
Document within the aggregate. Otherwise they must appear on covers around the
whole aggregate.
8. TRANSLATION
Translation is considered a kind of modification, so you may distribute
translations of the Document under the terms of section 4. Replacing Invariant
Sections with translations requires special permission from their copyright
holders, but you may include translations of some or all Invariant Sections in
addition to the original versions of these Invariant Sections. You may include a
translation of this License provided that you also include the original English
version of this License. In case of a disagreement between the translation and
the original English version of this License, the original English version will
prevail.
Iptables 指南 1.1.19 页码,94/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
9. TERMINATION
You may not copy, modify, sublicense, or distribute the Document except as
expressly provided for under this License. Any other attempt to copy, modify,
sublicense or distribute the Document is void, and will automatically terminate
your rights under this License. However, parties who have received copies, or
rights, from you under this License will not have their licenses terminated so
long as such parties remain in full compliance.
10. FUTURE REVISIONS OF THIS LICENSE
The Free Software Foundation may publish new, revised versions of the GNU Free
Documentation License from time to time. Such new versions will be similar in
spirit to the present version, but may differ in detail to address new problems
or concerns. See http://www.gnu.org/copyleft/.
Each version of the License is given a distinguishing version number. If the
Document specifies that a particular numbered version of this License "or any
later version" applies to it, you have the option of following the terms and
conditions either of that specified version or of any later version that has been
published (not as a draft) by the Free Software Foundation. If the Document does
not specify a version number of this License, you may choose any version ever
published (not as a draft) by the Free Software Foundation.
How to use this License for your
documents
To use this License in a document you have written, include a copy of the License
in the document and put the following copyright and license notices just after
the title page:
Copyright (c) YEAR YOUR NAME. Permission is granted to copy, distribute
and/or modify this document under the terms of the GNU Free
Documentation License, Version 1.1 or any later version published by
the Free Software Foundation; with the Invariant Sections being LIST
THEIR TITLES, with the Front-Cover Texts being LIST, and with the Back-
Cover Texts being LIST. A copy of the license is included in the
section entitled "GNU Free Documentation License".
If you have no Invariant Sections, write "with no Invariant Sections" instead of
saying which ones are invariant. If you have no Front-Cover Texts, write "no
Front-Cover Texts" instead of "Front-Cover Texts being LIST"; likewise for Back-
Cover Texts.
If your document contains nontrivial examples of program code, we recommend
Iptables 指南 1.1.19 页码,95/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
releasing these examples in parallel under your choice of free software license,
such as the GNU General Public License, to permit their use in free software.
Appendix H. GNU General Public License
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc. 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA Everyone is permitted to
copy and distribute verbatim copies of this license document, but
changing it is not allowed.
0. Preamble
The licenses for most software are designed to take away your freedom to share
and change it. By contrast, the GNU General Public License is intended to
guarantee your freedom to share and change free software--to make sure the
software is free for all its users. This General Public License applies to most
of the Free Software Foundation's software and to any other program whose authors
commit to using it. (Some other Free Software Foundation software is covered by
the GNU Library General Public License instead.) You can apply it to your
programs, too.
When we speak of free software, we are referring to freedom, not price. Our
General Public Licenses are designed to make sure that you have the freedom to
distribute copies of free software (and charge for this service if you wish),
that you receive source code or can get it if you want it, that you can change
the software or use pieces of it in new free programs; and that you know you can
do these things.
To protect your rights, we need to make restrictions that forbid anyone to deny
you these rights or to ask you to surrender the rights. These restrictions
translate to certain responsibilities for you if you distribute copies of the
software, or if you modify it.
For example, if you distribute copies of such a program, whether gratis or for a
fee, you must give the recipients all the rights that you have. You must make
sure that they, too, receive or can get the source code. And you must show them
these terms so they know their rights.
We protect your rights with two steps: (1) copyright the software, and (2) offer
you this license which gives you legal permission to copy, distribute and/or
modify the software.
Also, for each author's protection and ours, we want to make certain that
everyone understands that there is no warranty for this free software. If the
software is modified by someone else and passed on, we want its recipients to
Iptables 指南 1.1.19 页码,96/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
know that what they have is not the original, so that any problems introduced by
others will not reflect on the original authors' reputations.
Finally, any free program is threatened constantly by software patents. We wish
to avoid the danger that redistributors of a free program will individually
obtain patent licenses, in effect making the program proprietary. To prevent
this, we have made it clear that any patent must be licensed for everyone's free
use or not licensed at all.
The precise terms and conditions for copying, distribution and modification
follow.
1. TERMS AND CONDITIONS FOR COPYING,
DISTRIBUTION AND MODIFICATION
1. This License applies to any program or other work which contains a notice
placed by the copyright holder saying it may be distributed under the terms
of this General Public License. The "Program", below, refers to any such
program or work, and a "work based on the Program" means either the Program
or any derivative work under copyright law: that is to say, a work
containing the Program or a portion of it, either verbatim or with
modifications and/or translated into another language. (Hereinafter,
translation is included without limitation in the term "modification".) Each
licensee is addressed as "you".
Activities other than copying, distribution and modification are not covered
by this License; they are outside its scope. The act of running the Program
is not restricted, and the output from the Program is covered only if its
contents constitute a work based on the Program (independent of having been
made by running the Program). Whether that is true depends on what the
Program does.
2. You may copy and distribute verbatim copies of the Program's source code as
you receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice and
disclaimer of warranty; keep intact all the notices that refer to this
License and to the absence of any warranty; and give any other recipients of
the Program a copy of this License along with the Program.
You may charge a fee for the physical act of transferring a copy, and you
may at your option offer warranty protection in exchange for a fee.
3. You may modify your copy or copies of the Program or any portion of it, thus
forming a work based on the Program, and copy and distribute such
modifications or work under the terms of Section 1 above, provided that you
also meet all of these conditions:
1. You must cause the modified files to carry prominent notices stating
that you changed the files and the date of any change.
Iptables 指南 1.1.19 页码,97/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
2. You must cause any work that you distribute or publish, that in whole
or in part contains or is derived from the Program or any part thereof,
to be licensed as a whole at no charge to all third parties under the
terms of this License.
3. If the modified program normally reads commands interactively when run,
you must cause it, when started running for such interactive use in the
most ordinary way, to print or display an announcement including an
appropriate copyright notice and a notice that there is no warranty (or
else, saying that you provide a warranty) and that users may
redistribute the program under these conditions, and telling the user
how to view a copy of this License. (Exception: if the Program itself
is interactive but does not normally print such an announcement, your
work based on the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If identifiable
sections of that work are not derived from the Program, and can be
reasonably considered independent and separate works in themselves, then
this License, and its terms, do not apply to those sections when you
distribute them as separate works. But when you distribute the same sections
as part of a whole which is a work based on the Program, the distribution of
the whole must be on the terms of this License, whose permissions for other
licensees extend to the entire whole, and thus to each and every part
regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest your
rights to work written entirely by you; rather, the intent is to exercise
the right to control the distribution of derivative or collective works
based on the Program.
In addition, mere aggregation of another work not based on the Program with
the Program (or with a work based on the Program) on a volume of a storage
or distribution medium does not bring the other work under the scope of this
License.
4. You may copy and distribute the Program (or a work based on it, under
Section 2) in object code or executable form under the terms of Sections 1
and 2 above provided that you also do one of the following:
A. Accompany it with the complete corresponding machine-readable source
code, which must be distributed under the terms of Sections 1 and 2
above on a medium customarily used for software interchange; or,
B. Accompany it with a written offer, valid for at least three years, to
give any third party, for a charge no more than your cost of physically
performing source distribution, a complete machine-readable copy of the
corresponding source code, to be distributed under the terms of
Sections 1 and 2 above on a medium customarily used for software
interchange; or,
C. Accompany it with the information you received as to the offer to
distribute corresponding source code. (This alternative is allowed only
for noncommercial distribution and only if you received the program in
Iptables 指南 1.1.19 页码,98/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
object code or executable form with such an offer, in accord with
Subsection b above.)
The source code for a work means the preferred form of the work for making
modifications to it. For an executable work, complete source code means all
the source code for all modules it contains, plus any associated interface
definition files, plus the scripts used to control compilation and
installation of the executable. However, as a special exception, the source
code distributed need not include anything that is normally distributed (in
either source or binary form) with the major components (compiler, kernel,
and so on) of the operating system on which the executable runs, unless that
component itself accompanies the executable.
If distribution of executable or object code is made by offering access to
copy from a designated place, then offering equivalent access to copy the
source code from the same place counts as distribution of the source code,
even though third parties are not compelled to copy the source along with
the object code.
5. You may not copy, modify, sublicense, or distribute the Program except as
expressly provided under this License. Any attempt otherwise to copy,
modify, sublicense or distribute the Program is void, and will automatically
terminate your rights under this License. However, parties who have received
copies, or rights, from you under this License will not have their licenses
terminated so long as such parties remain in full compliance.
6. You are not required to accept this License, since you have not signed it.
However, nothing else grants you permission to modify or distribute the
Program or its derivative works. These actions are prohibited by law if you
do not accept this License. Therefore, by modifying or distributing the
Program (or any work based on the Program), you indicate your acceptance of
this License to do so, and all its terms and conditions for copying,
distributing or modifying the Program or works based on it.
7. Each time you redistribute the Program (or any work based on the Program),
the recipient automatically receives a license from the original licensor to
copy, distribute or modify the Program subject to these terms and
conditions. You may not impose any further restrictions on the recipients'
exercise of the rights granted herein. You are not responsible for enforcing
compliance by third parties to this License.
8. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot distribute so
as to satisfy simultaneously your obligations under this License and any
other pertinent obligations, then as a consequence you may not distribute
the Program at all. For example, if a patent license would not permit
royalty-free redistribution of the Program by all those who receive copies
directly or indirectly through you, then the only way you could satisfy both
it and this License would be to refrain entirely from distribution of the
Program.
Iptables 指南 1.1.19 页码,99/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
If any portion of this section is held invalid or unenforceable under any
particular circumstance, the balance of the section is intended to apply and
the section as a whole is intended to apply in other circumstances.
It is not the purpose of this section to induce you to infringe any patents
or other property right claims or to contest validity of any such claims;
this section has the sole purpose of protecting the integrity of the free
software distribution system, which is implemented by public license
practices. Many people have made generous contributions to the wide range of
software distributed through that system in reliance on consistent
application of that system; it is up to the author/donor to decide if he or
she is willing to distribute software through any other system and a
licensee cannot impose that choice.
This section is intended to make thoroughly clear what is believed to be a
consequence of the rest of this License.
If the distribution and/or use of the Program is restricted in certain
countries either by patents or by copyrighted interfaces, the original
copyright holder who places the Program under this License may add an
explicit geographical distribution limitation excluding those countries, so
that distribution is permitted only in or among countries not thus excluded.
In such case, this License incorporates the limitation as if written in the
body of this License.
9. The Free Software Foundation may publish revised and/or new versions of the
General Public License from time to time. Such new versions will be similar
in spirit to the present version, but may differ in detail to address new
problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free programs
whose distribution conditions are different, write to the author to ask for
permission. For software which is copyrighted by the Free Software
Foundation, write to the Free Software Foundation; we sometimes make
exceptions for this. Our decision will be guided by the two goals of
preserving the free status of all derivatives of our free software and of
promoting the sharing and reuse of software generally.
11. NO WARRANTY
BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE
PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE
STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE
PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
Iptables 指南 1.1.19 页码,100/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND
PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE,
YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE
OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA
OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
END OF TERMS AND CONDITIONS
2. How to Apply These Terms to Your New
Programs
If you develop a new program, and you want it to be of the greatest possible use
to the public, the best way to achieve this is to make it free software which
everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest to attach
them to the start of each source file to most effectively convey the exclusion of
warranty; and each file should have at least the "copyright" line and a pointer
to where the full notice is found.
Copyright (C)  
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Also add information on how to contact you by electronic and paper mail.
Iptables 指南 1.1.19 页码,101/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
If the program is interactive, make it output a short notice like this when it
starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author Gnomovision
comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is
free software, and you are welcome to redistribute it under certain
conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate parts
of the General Public License. Of course, the commands you use may be called
something other than `show w' and `show c'; they could even be mouse-clicks or
menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your school,
if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a
sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the progra
m
`Gnomovision' (which makes passes at compilers)
written by James Hacker.
, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may consider
it more useful to permit linking proprietary applications with the library. If
this is what you want to do, use the GNU Library General Public License instead
of this License.
附录 I. 示例脚本的代码
I.1. rc.firewall脚本代码
#!/bin/sh
#
# rc.firewall - Initial SIMPLE IP Firewall script for Linux 2.4.x and iptables
#
# Copyright (C) 2001 Oskar Andreasson
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
Iptables 指南 1.1.19 页码,102/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
#
# You should have received a copy of the GNU General Public License
# along with this program or from the site that you downloaded it
# from; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA
#
###########################################################################
#
# 1. Configuration options.
#
#
# 1.1 Internet Configuration.
#
INET_IP="194.236.50.155"
INET_IFACE="eth0"
INET_BROADCAST="194.236.50.255"
#
# 1.1.1 DHCP
#
#
# 1.1.2 PPPoE
#
#
# 1.2 Local Area Network configuration.
#
# your LAN's IP range and localhost IP. /24 means to only use the first 24
# bits of the 32 bit IP address. the same as netmask 255.255.255.0
#
LAN_IP="192.168.0.2"
LAN_IP_RANGE="192.168.0.0/16"
LAN_IFACE="eth1"
#
# 1.3 DMZ Configuration.
#
#
# 1.4 Localhost Configuration.
#
LO_IFACE="lo"
LO_IP="127.0.0.1"
#
# 1.5 IPTables Configuration.
#
IPTABLES="/usr/sbin/iptables"
#
# 1.6 Other Configuration.
#
###########################################################################
#
# 2. Module loading.
#
Iptables 指南 1.1.19 页码,103/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
#
# Needed to initially load modules
#
/sbin/depmod -a
#
# 2.1 Required modules
#
/sbin/modprobe ip_tables
/sbin/modprobe ip_conntrack
/sbin/modprobe iptable_filter
/sbin/modprobe iptable_mangle
/sbin/modprobe iptable_nat
/sbin/modprobe ipt_LOG
/sbin/modprobe ipt_limit
/sbin/modprobe ipt_state
#
# 2.2 Non-Required modules
#
#/sbin/modprobe ipt_owner
#/sbin/modprobe ipt_REJECT
#/sbin/modprobe ipt_MASQUERADE
#/sbin/modprobe ip_conntrack_ftp
#/sbin/modprobe ip_conntrack_irc
#/sbin/modprobe ip_nat_ftp
#/sbin/modprobe ip_nat_irc
###########################################################################
#
# 3. /proc set up.
#
#
# 3.1 Required proc configuration
#
echo "1" > /proc/sys/net/ipv4/ip_forward
#
# 3.2 Non-Required proc configuration
#
#echo "1" > /proc/sys/net/ipv4/conf/all/rp_filter
#echo "1" > /proc/sys/net/ipv4/conf/all/proxy_arp
#echo "1" > /proc/sys/net/ipv4/ip_dynaddr
###########################################################################
#
# 4. rules set up.
#
######
# 4.1 Filter table
#
#
# 4.1.1 Set policies
#
Iptables 指南 1.1.19 页码,104/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
$IPTABLES -P INPUT DROP
$IPTABLES -P OUTPUT DROP
$IPTABLES -P FORWARD DROP
#
# 4.1.2 Create userspecified chains
#
#
# Create chain for bad tcp packets
#
$IPTABLES -N bad_tcp_packets
#
# Create separate chains for ICMP, TCP and UDP to traverse
#
$IPTABLES -N allowed
$IPTABLES -N tcp_packets
$IPTABLES -N udp_packets
$IPTABLES -N icmp_packets
#
# 4.1.3 Create content in userspecified chains
#
#
# bad_tcp_packets chain
#
$IPTABLES -A bad_tcp_packets -p tcp --tcp-flags SYN,ACK SYN,ACK \
-m state --state NEW -j REJECT --reject-with tcp-reset
$IPTABLES -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j LOG \
--log-prefix "New not syn:"
$IPTABLES -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j DROP
#
# allowed chain
#
$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
#
# TCP rules
#
$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
#
# UDP ports
#
#$IPTABLES -A udp_packets -p UDP -s 0/0 --destination-port 53 -j ACCEPT
#$IPTABLES -A udp_packets -p UDP -s 0/0 --destination-port 123 -j ACCEPT
$IPTABLES -A udp_packets -p UDP -s 0/0 --destination-port 2074 -j ACCEPT
$IPTABLES -A udp_packets -p UDP -s 0/0 --destination-port 4000 -j ACCEPT
#
Iptables 指南 1.1.19 页码,105/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
# In Microsoft Networks you will be swamped by broadcasts. These lines
# will prevent them from showing up in the logs.
#
#$IPTABLES -A udp_packets -p UDP -i $INET_IFACE -d $INET_BROADCAST \
#--destination-port 135:139 -j DROP
#
# If we get DHCP requests from the Outside of our network, our logs will
# be swamped as well. This rule will block them from getting logged.
#
#$IPTABLES -A udp_packets -p UDP -i $INET_IFACE -d 255.255.255.255 \
#--destination-port 67:68 -j DROP
#
# ICMP rules
#
$IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 8 -j ACCEPT
$IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 11 -j ACCEPT
#
# 4.1.4 INPUT chain
#
#
# Bad TCP packets we don't want.
#
$IPTABLES -A INPUT -p tcp -j bad_tcp_packets
#
# Rules for special networks not part of the Internet
#
$IPTABLES -A INPUT -p ALL -i $LAN_IFACE -s $LAN_IP_RANGE -j ACCEPT
$IPTABLES -A INPUT -p ALL -i $LO_IFACE -s $LO_IP -j ACCEPT
$IPTABLES -A INPUT -p ALL -i $LO_IFACE -s $LAN_IP -j ACCEPT
$IPTABLES -A INPUT -p ALL -i $LO_IFACE -s $INET_IP -j ACCEPT
#
# Special rule for DHCP requests from LAN, which are not caught properly
# otherwise.
#
$IPTABLES -A INPUT -p UDP -i $LAN_IFACE --dport 67 --sport 68 -j ACCEPT
#
# Rules for incoming packets from the internet.
#
$IPTABLES -A INPUT -p ALL -d $INET_IP -m state --state ESTABLISHED,RELATED \
-j ACCEPT
$IPTABLES -A INPUT -p TCP -i $INET_IFACE -j tcp_packets
$IPTABLES -A INPUT -p UDP -i $INET_IFACE -j udp_packets
$IPTABLES -A INPUT -p ICMP -i $INET_IFACE -j icmp_packets
#
# If you have a Microsoft Network on the outside of your firewall, you may
# also get flooded by Multicasts. We drop them so we do not get flooded by
# logs
#
Iptables 指南 1.1.19 页码,106/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
#$IPTABLES -A INPUT -i $INET_IFACE -d 224.0.0.0/8 -j DROP
#
# Log weird packets that don't match the above.
#
$IPTABLES -A INPUT -m limit --limit 3/minute --limit-burst 3 -j LOG \
--log-level DEBUG --log-prefix "IPT INPUT packet died: "
#
# 4.1.5 FORWARD chain
#
#
# Bad TCP packets we don't want
#
$IPTABLES -A FORWARD -p tcp -j bad_tcp_packets
#
# Accept the packets we actually want to forward
#
$IPTABLES -A FORWARD -i $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
#
# Log weird packets that don't match the above.
#
$IPTABLES -A FORWARD -m limit --limit 3/minute --limit-burst 3 -j LOG \
--log-level DEBUG --log-prefix "IPT FORWARD packet died: "
#
# 4.1.6 OUTPUT chain
#
#
# Bad TCP packets we don't want.
#
$IPTABLES -A OUTPUT -p tcp -j bad_tcp_packets
#
# Special OUTPUT rules to decide which IP's to allow.
#
$IPTABLES -A OUTPUT -p ALL -s $LO_IP -j ACCEPT
$IPTABLES -A OUTPUT -p ALL -s $LAN_IP -j ACCEPT
$IPTABLES -A OUTPUT -p ALL -s $INET_IP -j ACCEPT
#
# Log weird packets that don't match the above.
#
$IPTABLES -A OUTPUT -m limit --limit 3/minute --limit-burst 3 -j LOG \
--log-level DEBUG --log-prefix "IPT OUTPUT packet died: "
######
# 4.2 nat table
#
#
# 4.2.1 Set policies
Iptables 指南 1.1.19 页码,107/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
#
#
# 4.2.2 Create user specified chains
#
#
# 4.2.3 Create content in user specified chains
#
#
# 4.2.4 PREROUTING chain
#
#
# 4.2.5 POSTROUTING chain
#
#
# Enable simple IP Forwarding and Network Address Translation
#
$IPTABLES -t nat -A POSTROUTING -o $INET_IFACE -j SNAT --to-source $INET_IP
#
# 4.2.6 OUTPUT chain
#
######
# 4.3 mangle table
#
#
# 4.3.1 Set policies
#
#
# 4.3.2 Create user specified chains
#
#
# 4.3.3 Create content in user specified chains
#
#
# 4.3.4 PREROUTING chain
#
#
# 4.3.5 INPUT chain
#
#
# 4.3.6 FORWARD chain
#
#
# 4.3.7 OUTPUT chain
#
#
# 4.3.8 POSTROUTING chain
#
Iptables 指南 1.1.19 页码,108/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
I.2. rc.DMZ.firewall脚本代码
#!/bin/sh
#
# rc.DMZ.firewall - DMZ IP Firewall script for Linux 2.4.x and iptables
#
# Copyright (C) 2001 Oskar Andreasson
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program or from the site that you downloaded it
# from; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA
#
###########################################################################
#
# 1. Configuration options.
#
#
# 1.1 Internet Configuration.
#
INET_IP="194.236.50.152"
HTTP_IP="194.236.50.153"
DNS_IP="194.236.50.154"
INET_IFACE="eth0"
#
# 1.1.1 DHCP
#
#
# 1.1.2 PPPoE
#
#
# 1.2 Local Area Network configuration.
#
# your LAN's IP range and localhost IP. /24 means to only use the first 24
# bits of the 32 bit IP address. the same as netmask 255.255.255.0
#
LAN_IP="192.168.0.1"
LAN_IFACE="eth1"
#
# 1.3 DMZ Configuration.
#
Iptables 指南 1.1.19 页码,109/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
DMZ_HTTP_IP="192.168.1.2"
DMZ_DNS_IP="192.168.1.3"
DMZ_IP="192.168.1.1"
DMZ_IFACE="eth2"
#
# 1.4 Localhost Configuration.
#
LO_IFACE="lo"
LO_IP="127.0.0.1"
#
# 1.5 IPTables Configuration.
#
IPTABLES="/usr/sbin/iptables"
#
# 1.6 Other Configuration.
#
###########################################################################
#
# 2. Module loading.
#
#
# Needed to initially load modules
#
/sbin/depmod -a
#
# 2.1 Required modules
#
/sbin/modprobe ip_tables
/sbin/modprobe ip_conntrack
/sbin/modprobe iptable_filter
/sbin/modprobe iptable_mangle
/sbin/modprobe iptable_nat
/sbin/modprobe ipt_LOG
/sbin/modprobe ipt_limit
/sbin/modprobe ipt_state
#
# 2.2 Non-Required modules
#
#/sbin/modprobe ipt_owner
#/sbin/modprobe ipt_REJECT
#/sbin/modprobe ipt_MASQUERADE
#/sbin/modprobe ip_conntrack_ftp
#/sbin/modprobe ip_conntrack_irc
#/sbin/modprobe ip_nat_ftp
#/sbin/modprobe ip_nat_irc
###########################################################################
#
# 3. /proc set up.
#
Iptables 指南 1.1.19 页码,110/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
#
# 3.1 Required proc configuration
#
echo "1" > /proc/sys/net/ipv4/ip_forward
#
# 3.2 Non-Required proc configuration
#
#echo "1" > /proc/sys/net/ipv4/conf/all/rp_filter
#echo "1" > /proc/sys/net/ipv4/conf/all/proxy_arp
#echo "1" > /proc/sys/net/ipv4/ip_dynaddr
###########################################################################
#
# 4. rules set up.
#
######
# 4.1 Filter table
#
#
# 4.1.1 Set policies
#
$IPTABLES -P INPUT DROP
$IPTABLES -P OUTPUT DROP
$IPTABLES -P FORWARD DROP
#
# 4.1.2 Create userspecified chains
#
#
# Create chain for bad tcp packets
#
$IPTABLES -N bad_tcp_packets
#
# Create separate chains for ICMP, TCP and UDP to traverse
#
$IPTABLES -N allowed
$IPTABLES -N icmp_packets
#
# 4.1.3 Create content in userspecified chains
#
#
# bad_tcp_packets chain
#
$IPTABLES -A bad_tcp_packets -p tcp --tcp-flags SYN,ACK SYN,ACK \
-m state --state NEW -j REJECT --reject-with tcp-reset
$IPTABLES -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j LOG \
--log-prefix "New not syn:"
$IPTABLES -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j DROP
#
Iptables 指南 1.1.19 页码,111/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
# allowed chain
#
$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
#
# ICMP rules
#
# Changed rules totally
$IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 8 -j ACCEPT
$IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 11 -j ACCEPT
#
# 4.1.4 INPUT chain
#
#
# Bad TCP packets we don't want
#
$IPTABLES -A INPUT -p tcp -j bad_tcp_packets
#
# Packets from the Internet to this box
#
$IPTABLES -A INPUT -p ICMP -i $INET_IFACE -j icmp_packets
#
# Packets from LAN, DMZ or LOCALHOST
#
#
# From DMZ Interface to DMZ firewall IP
#
$IPTABLES -A INPUT -p ALL -i $DMZ_IFACE -d $DMZ_IP -j ACCEPT
#
# From LAN Interface to LAN firewall IP
#
$IPTABLES -A INPUT -p ALL -i $LAN_IFACE -d $LAN_IP -j ACCEPT
#
# From Localhost interface to Localhost IP's
#
$IPTABLES -A INPUT -p ALL -i $LO_IFACE -s $LO_IP -j ACCEPT
$IPTABLES -A INPUT -p ALL -i $LO_IFACE -s $LAN_IP -j ACCEPT
$IPTABLES -A INPUT -p ALL -i $LO_IFACE -s $INET_IP -j ACCEPT
#
# Special rule for DHCP requests from LAN, which are not caught properly
# otherwise.
#
$IPTABLES -A INPUT -p UDP -i $LAN_IFACE --dport 67 --sport 68 -j ACCEPT
#
# All established and related packets incoming from the internet to the
Iptables 指南 1.1.19 页码,112/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
# firewall
#
$IPTABLES -A INPUT -p ALL -d $INET_IP -m state --state ESTABLISHED,RELATED \
-j ACCEPT
#
# In Microsoft Networks you will be swamped by broadcasts. These lines
# will prevent them from showing up in the logs.
#
#$IPTABLES -A INPUT -p UDP -i $INET_IFACE -d $INET_BROADCAST \
#--destination-port 135:139 -j DROP
#
# If we get DHCP requests from the Outside of our network, our logs will
# be swamped as well. This rule will block them from getting logged.
#
#$IPTABLES -A INPUT -p UDP -i $INET_IFACE -d 255.255.255.255 \
#--destination-port 67:68 -j DROP
#
# If you have a Microsoft Network on the outside of your firewall, you may
# also get flooded by Multicasts. We drop them so we do not get flooded by
# logs
#
#$IPTABLES -A INPUT -i $INET_IFACE -d 224.0.0.0/8 -j DROP
#
# Log weird packets that don't match the above.
#
$IPTABLES -A INPUT -m limit --limit 3/minute --limit-burst 3 -j LOG \
--log-level DEBUG --log-prefix "IPT INPUT packet died: "
#
# 4.1.5 FORWARD chain
#
#
# Bad TCP packets we don't want
#
$IPTABLES -A FORWARD -p tcp -j bad_tcp_packets
#
# DMZ section
#
# General rules
#
$IPTABLES -A FORWARD -i $DMZ_IFACE -o $INET_IFACE -j ACCEPT
$IPTABLES -A FORWARD -i $INET_IFACE -o $DMZ_IFACE -m state \
--state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A FORWARD -i $LAN_IFACE -o $DMZ_IFACE -j ACCEPT
$IPTABLES -A FORWARD -i $DMZ_IFACE -o $LAN_IFACE -m state \
--state ESTABLISHED,RELATED -j ACCEPT
#
# HTTP server
#
Iptables 指南 1.1.19 页码,113/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
$IPTABLES -A FORWARD -p TCP -i $INET_IFACE -o $DMZ_IFACE -d $DMZ_HTTP_IP \
--dport 80 -j allowed
$IPTABLES -A FORWARD -p ICMP -i $INET_IFACE -o $DMZ_IFACE -d $DMZ_HTTP_IP \
-j icmp_packets
#
# DNS server
#
$IPTABLES -A FORWARD -p TCP -i $INET_IFACE -o $DMZ_IFACE -d $DMZ_DNS_IP \
--dport 53 -j allowed
$IPTABLES -A FORWARD -p UDP -i $INET_IFACE -o $DMZ_IFACE -d $DMZ_DNS_IP \
--dport 53 -j ACCEPT
$IPTABLES -A FORWARD -p ICMP -i $INET_IFACE -o $DMZ_IFACE -d $DMZ_DNS_IP \
-j icmp_packets
#
# LAN section
#
$IPTABLES -A FORWARD -i $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
#
# Log weird packets that don't match the above.
#
$IPTABLES -A FORWARD -m limit --limit 3/minute --limit-burst 3 -j LOG \
--log-level DEBUG --log-prefix "IPT FORWARD packet died: "
#
# 4.1.6 OUTPUT chain
#
#
# Bad TCP packets we don't want.
#
$IPTABLES -A OUTPUT -p tcp -j bad_tcp_packets
#
# Special OUTPUT rules to decide which IP's to allow.
#
$IPTABLES -A OUTPUT -p ALL -s $LO_IP -j ACCEPT
$IPTABLES -A OUTPUT -p ALL -s $LAN_IP -j ACCEPT
$IPTABLES -A OUTPUT -p ALL -s $INET_IP -j ACCEPT
#
# Log weird packets that don't match the above.
#
$IPTABLES -A OUTPUT -m limit --limit 3/minute --limit-burst 3 -j LOG \
--log-level DEBUG --log-prefix "IPT OUTPUT packet died: "
######
# 4.2 nat table
#
#
# 4.2.1 Set policies
#
Iptables 指南 1.1.19 页码,114/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
#
# 4.2.2 Create user specified chains
#
#
# 4.2.3 Create content in user specified chains
#
#
# 4.2.4 PREROUTING chain
#
$IPTABLES -t nat -A PREROUTING -p TCP -i $INET_IFACE -d $HTTP_IP --dport 80 \
-j DNAT --to-destination $DMZ_HTTP_IP
$IPTABLES -t nat -A PREROUTING -p TCP -i $INET_IFACE -d $DNS_IP --dport 53 \
-j DNAT --to-destination $DMZ_DNS_IP
$IPTABLES -t nat -A PREROUTING -p UDP -i $INET_IFACE -d $DNS_IP --dport 53 \
-j DNAT --to-destination $DMZ_DNS_IP
#
# 4.2.5 POSTROUTING chain
#
#
# Enable simple IP Forwarding and Network Address Translation
#
$IPTABLES -t nat -A POSTROUTING -o $INET_IFACE -j SNAT --to-source $INET_IP
#
# 4.2.6 OUTPUT chain
#
######
# 4.3 mangle table
#
#
# 4.3.1 Set policies
#
#
# 4.3.2 Create user specified chains
#
#
# 4.3.3 Create content in user specified chains
#
#
# 4.3.4 PREROUTING chain
#
#
# 4.3.5 INPUT chain
#
#
# 4.3.6 FORWARD chain
#
#
# 4.3.7 OUTPUT chain
#
Iptables 指南 1.1.19 页码,115/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
#
# 4.3.8 POSTROUTING chain
#
I.3. rc.UTIN.firewall脚本代码
#!/bin/sh
#
# rc.firewall - UTIN Firewall script for Linux 2.4.x and iptables
#
# Copyright (C) 2001 Oskar Andreasson
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program or from the site that you downloaded it
# from; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA
#
###########################################################################
#
# 1. Configuration options.
#
#
# 1.1 Internet Configuration.
#
INET_IP="194.236.50.155"
INET_IFACE="eth0"
INET_BROADCAST="194.236.50.255"
#
# 1.1.1 DHCP
#
#
# 1.1.2 PPPoE
#
#
# 1.2 Local Area Network configuration.
#
# your LAN's IP range and localhost IP. /24 means to only use the first 24
# bits of the 32 bit IP address. the same as netmask 255.255.255.0
#
LAN_IP="192.168.0.2"
LAN_IP_RANGE="192.168.0.0/16"
Iptables 指南 1.1.19 页码,116/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
LAN_IFACE="eth1"
#
# 1.3 DMZ Configuration.
#
#
# 1.4 Localhost Configuration.
#
LO_IFACE="lo"
LO_IP="127.0.0.1"
#
# 1.5 IPTables Configuration.
#
IPTABLES="/usr/sbin/iptables"
#
# 1.6 Other Configuration.
#
###########################################################################
#
# 2. Module loading.
#
#
# Needed to initially load modules
#
/sbin/depmod -a
#
# 2.1 Required modules
#
/sbin/modprobe ip_tables
/sbin/modprobe ip_conntrack
/sbin/modprobe iptable_filter
/sbin/modprobe iptable_mangle
/sbin/modprobe iptable_nat
/sbin/modprobe ipt_LOG
/sbin/modprobe ipt_limit
/sbin/modprobe ipt_state
#
# 2.2 Non-Required modules
#
#/sbin/modprobe ipt_owner
#/sbin/modprobe ipt_REJECT
#/sbin/modprobe ipt_MASQUERADE
#/sbin/modprobe ip_conntrack_ftp
#/sbin/modprobe ip_conntrack_irc
#/sbin/modprobe ip_nat_ftp
#/sbin/modprobe ip_nat_irc
###########################################################################
#
# 3. /proc set up.
#
Iptables 指南 1.1.19 页码,117/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
#
# 3.1 Required proc configuration
#
echo "1" > /proc/sys/net/ipv4/ip_forward
#
# 3.2 Non-Required proc configuration
#
#echo "1" > /proc/sys/net/ipv4/conf/all/rp_filter
#echo "1" > /proc/sys/net/ipv4/conf/all/proxy_arp
#echo "1" > /proc/sys/net/ipv4/ip_dynaddr
###########################################################################
#
# 4. rules set up.
#
######
# 4.1 Filter table
#
#
# 4.1.1 Set policies
#
$IPTABLES -P INPUT DROP
$IPTABLES -P OUTPUT DROP
$IPTABLES -P FORWARD DROP
#
# 4.1.2 Create userspecified chains
#
#
# Create chain for bad tcp packets
#
$IPTABLES -N bad_tcp_packets
#
# Create separate chains for ICMP, TCP and UDP to traverse
#
$IPTABLES -N allowed
$IPTABLES -N tcp_packets
$IPTABLES -N udp_packets
$IPTABLES -N icmp_packets
#
# 4.1.3 Create content in userspecified chains
#
#
# bad_tcp_packets chain
#
$IPTABLES -A bad_tcp_packets -p tcp --tcp-flags SYN,ACK SYN,ACK \
-m state --state NEW -j REJECT --reject-with tcp-reset
$IPTABLES -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j LOG \
--log-prefix "New not syn:"
$IPTABLES -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j DROP
Iptables 指南 1.1.19 页码,118/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
#
# allowed chain
#
$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
#
# TCP rules
#
$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
#
# UDP ports
#
#$IPTABLES -A udp_packets -p UDP -s 0/0 --source-port 53 -j ACCEPT
#$IPTABLES -A udp_packets -p UDP -s 0/0 --source-port 123 -j ACCEPT
$IPTABLES -A udp_packets -p UDP -s 0/0 --source-port 2074 -j ACCEPT
$IPTABLES -A udp_packets -p UDP -s 0/0 --source-port 4000 -j ACCEPT
#
# In Microsoft Networks you will be swamped by broadcasts. These lines
# will prevent them from showing up in the logs.
#
#$IPTABLES -A udp_packets -p UDP -i $INET_IFACE -d $INET_BROADCAST \
#--destination-port 135:139 -j DROP
#
# If we get DHCP requests from the Outside of our network, our logs will
# be swamped as well. This rule will block them from getting logged.
#
#$IPTABLES -A udp_packets -p UDP -i $INET_IFACE -d 255.255.255.255 \
#--destination-port 67:68 -j DROP
#
# ICMP rules
#
$IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 8 -j ACCEPT
$IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 11 -j ACCEPT
#
# 4.1.4 INPUT chain
#
#
# Bad TCP packets we don't want.
#
$IPTABLES -A INPUT -p tcp -j bad_tcp_packets
#
# Rules for special networks not part of the Internet
#
$IPTABLES -A INPUT -p ALL -i $LO_IFACE -s $LO_IP -j ACCEPT
Iptables 指南 1.1.19 页码,119/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
$IPTABLES -A INPUT -p ALL -i $LO_IFACE -s $LAN_IP -j ACCEPT
$IPTABLES -A INPUT -p ALL -i $LO_IFACE -s $INET_IP -j ACCEPT
#
# Rules for incoming packets from anywhere.
#
$IPTABLES -A INPUT -p ALL -d $INET_IP -m state --state ESTABLISHED,RELATED \
-j ACCEPT
$IPTABLES -A INPUT -p TCP -j tcp_packets
$IPTABLES -A INPUT -p UDP -j udp_packets
$IPTABLES -A INPUT -p ICMP -j icmp_packets
#
# If you have a Microsoft Network on the outside of your firewall, you may
# also get flooded by Multicasts. We drop them so we do not get flooded by
# logs
#
#$IPTABLES -A INPUT -i $INET_IFACE -d 224.0.0.0/8 -j DROP
#
# Log weird packets that don't match the above.
#
$IPTABLES -A INPUT -m limit --limit 3/minute --limit-burst 3 -j LOG \
--log-level DEBUG --log-prefix "IPT INPUT packet died: "
#
# 4.1.5 FORWARD chain
#
#
# Bad TCP packets we don't want
#
$IPTABLES -A FORWARD -p tcp -j bad_tcp_packets
#
# Accept the packets we actually want to forward
#
$IPTABLES -A FORWARD -p tcp --dport 21 -i $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -p tcp --dport 80 -i $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -p tcp --dport 110 -i $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
#
# Log weird packets that don't match the above.
#
$IPTABLES -A FORWARD -m limit --limit 3/minute --limit-burst 3 -j LOG \
--log-level DEBUG --log-prefix "IPT FORWARD packet died: "
#
# 4.1.6 OUTPUT chain
#
#
# Bad TCP packets we don't want.
#
$IPTABLES -A OUTPUT -p tcp -j bad_tcp_packets
Iptables 指南 1.1.19 页码,120/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
#
# Special OUTPUT rules to decide which IP's to allow.
#
$IPTABLES -A OUTPUT -p ALL -s $LO_IP -j ACCEPT
$IPTABLES -A OUTPUT -p ALL -s $LAN_IP -j ACCEPT
$IPTABLES -A OUTPUT -p ALL -s $INET_IP -j ACCEPT
#
# Log weird packets that don't match the above.
#
$IPTABLES -A OUTPUT -m limit --limit 3/minute --limit-burst 3 -j LOG \
--log-level DEBUG --log-prefix "IPT OUTPUT packet died: "
######
# 4.2 nat table
#
#
# 4.2.1 Set policies
#
#
# 4.2.2 Create user specified chains
#
#
# 4.2.3 Create content in user specified chains
#
#
# 4.2.4 PREROUTING chain
#
#
# 4.2.5 POSTROUTING chain
#
#
# Enable simple IP Forwarding and Network Address Translation
#
$IPTABLES -t nat -A POSTROUTING -o $INET_IFACE -j SNAT --to-source $INET_IP
#
# 4.2.6 OUTPUT chain
#
######
# 4.3 mangle table
#
#
# 4.3.1 Set policies
#
#
# 4.3.2 Create user specified chains
#
#
# 4.3.3 Create content in user specified chains
#
Iptables 指南 1.1.19 页码,121/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
#
# 4.3.4 PREROUTING chain
#
#
# 4.3.5 INPUT chain
#
#
# 4.3.6 FORWARD chain
#
#
# 4.3.7 OUTPUT chain
#
#
# 4.3.8 POSTROUTING chain
#
I.4. rc.DHCP.firewall脚本代码
#!/bin/sh
#
# rc.firewall - DHCP IP Firewall script for Linux 2.4.x and iptables
#
# Copyright (C) 2001 Oskar Andreasson
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program or from the site that you downloaded it
# from; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA
#
###########################################################################
#
# 1. Configuration options.
#
#
# 1.1 Internet Configuration.
#
INET_IFACE="eth0"
#
# 1.1.1 DHCP
#
Iptables 指南 1.1.19 页码,122/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
#
# Information pertaining to DHCP over the Internet, if needed.
#
# Set DHCP variable to no if you don't get IP from DHCP. If you get DHCP
# over the Internet set this variable to yes, and set up the proper IP
# address for the DHCP server in the DHCP_SERVER variable.
#
DHCP="no"
DHCP_SERVER="195.22.90.65"
#
# 1.1.2 PPPoE
#
# Configuration options pertaining to PPPoE.
#
# If you have problem with your PPPoE connection, such as large mails not
# getting through while small mail get through properly etc, you may set
# this option to "yes" which may fix the problem. This option will set a
# rule in the PREROUTING chain of the mangle table which will clamp
# (resize) all routed packets to PMTU (Path Maximum Transmit Unit).
#
# Note that it is better to set this up in the PPPoE package itself, since
# the PPPoE configuration option will give less overhead.
#
PPPOE_PMTU="no"
#
# 1.2 Local Area Network configuration.
#
# your LAN's IP range and localhost IP. /24 means to only use the first 24
# bits of the 32 bit IP address. the same as netmask 255.255.255.0
#
LAN_IP="192.168.0.2"
LAN_IP_RANGE="192.168.0.0/16"
LAN_IFACE="eth1"
#
# 1.3 DMZ Configuration.
#
#
# 1.4 Localhost Configuration.
#
LO_IFACE="lo"
LO_IP="127.0.0.1"
#
# 1.5 IPTables Configuration.
#
IPTABLES="/usr/sbin/iptables"
#
# 1.6 Other Configuration.
#
###########################################################################
#
Iptables 指南 1.1.19 页码,123/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
# 2. Module loading.
#
#
# Needed to initially load modules
#
/sbin/depmod -a
#
# 2.1 Required modules
#
/sbin/modprobe ip_conntrack
/sbin/modprobe ip_tables
/sbin/modprobe iptable_filter
/sbin/modprobe iptable_mangle
/sbin/modprobe iptable_nat
/sbin/modprobe ipt_LOG
/sbin/modprobe ipt_limit
/sbin/modprobe ipt_MASQUERADE
#
# 2.2 Non-Required modules
#
#/sbin/modprobe ipt_owner
#/sbin/modprobe ipt_REJECT
#/sbin/modprobe ip_conntrack_ftp
#/sbin/modprobe ip_conntrack_irc
#/sbin/modprobe ip_nat_ftp
#/sbin/modprobe ip_nat_irc
###########################################################################
#
# 3. /proc set up.
#
#
# 3.1 Required proc configuration
#
echo "1" > /proc/sys/net/ipv4/ip_forward
#
# 3.2 Non-Required proc configuration
#
#echo "1" > /proc/sys/net/ipv4/conf/all/rp_filter
#echo "1" > /proc/sys/net/ipv4/conf/all/proxy_arp
#echo "1" > /proc/sys/net/ipv4/ip_dynaddr
###########################################################################
#
# 4. rules set up.
#
######
# 4.1 Filter table
#
#
# 4.1.1 Set policies
#
Iptables 指南 1.1.19 页码,124/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
$IPTABLES -P INPUT DROP
$IPTABLES -P OUTPUT DROP
$IPTABLES -P FORWARD DROP
#
# 4.1.2 Create userspecified chains
#
#
# Create chain for bad tcp packets
#
$IPTABLES -N bad_tcp_packets
#
# Create separate chains for ICMP, TCP and UDP to traverse
#
$IPTABLES -N allowed
$IPTABLES -N tcp_packets
$IPTABLES -N udp_packets
$IPTABLES -N icmp_packets
#
# 4.1.3 Create content in userspecified chains
#
#
# bad_tcp_packets chain
#
$IPTABLES -A bad_tcp_packets -p tcp --tcp-flags SYN,ACK SYN,ACK \
-m state --state NEW -j REJECT --reject-with tcp-reset
$IPTABLES -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j LOG \
--log-prefix "New not syn:"
$IPTABLES -A bad_tcp_packets -p tcp ! --syn -m state --state NEW -j DROP
#
# allowed chain
#
$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
#
# TCP rules
#
$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
#
# UDP ports
#
$IPTABLES -A udp_packets -p UDP -s 0/0 --source-port 53 -j ACCEPT
if [ $DHCP == "yes" ] ; then
$IPTABLES -A udp_packets -p UDP -s $DHCP_SERVER --sport 67 \
--dport 68 -j ACCEPT
fi
Iptables 指南 1.1.19 页码,125/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
#$IPTABLES -A udp_packets -p UDP -s 0/0 --source-port 53 -j ACCEPT
#$IPTABLES -A udp_packets -p UDP -s 0/0 --source-port 123 -j ACCEPT
$IPTABLES -A udp_packets -p UDP -s 0/0 --source-port 2074 -j ACCEPT
$IPTABLES -A udp_packets -p UDP -s 0/0 --source-port 4000 -j ACCEPT
#
# In Microsoft Networks you will be swamped by broadcasts. These lines
# will prevent them from showing up in the logs.
#
#$IPTABLES -A udp_packets -p UDP -i $INET_IFACE \
#--destination-port 135:139 -j DROP
#
# If we get DHCP requests from the Outside of our network, our logs will
# be swamped as well. This rule will block them from getting logged.
#
#$IPTABLES -A udp_packets -p UDP -i $INET_IFACE -d 255.255.255.255 \
#--destination-port 67:68 -j DROP
#
# ICMP rules
#
$IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 8 -j ACCEPT
$IPTABLES -A icmp_packets -p ICMP -s 0/0 --icmp-type 11 -j ACCEPT
#
# 4.1.4 INPUT chain
#
#
# Bad TCP packets we don't want.
#
$IPTABLES -A INPUT -p tcp -j bad_tcp_packets
#
# Rules for special networks not part of the Internet
#
$IPTABLES -A INPUT -p ALL -i $LAN_IFACE -s $LAN_IP_RANGE -j ACCEPT
$IPTABLES -A INPUT -p ALL -i $LO_IFACE -j ACCEPT
#
# Special rule for DHCP requests from LAN, which are not caught properly
# otherwise.
#
$IPTABLES -A INPUT -p UDP -i $LAN_IFACE --dport 67 --sport 68 -j ACCEPT
#
# Rules for incoming packets from the internet.
#
$IPTABLES -A INPUT -p ALL -i $INET_IFACE -m state --state ESTABLISHED,RELATED \
-j ACCEPT
$IPTABLES -A INPUT -p TCP -i $INET_IFACE -j tcp_packets
$IPTABLES -A INPUT -p UDP -i $INET_IFACE -j udp_packets
$IPTABLES -A INPUT -p ICMP -i $INET_IFACE -j icmp_packets
#
Iptables 指南 1.1.19 页码,126/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
# If you have a Microsoft Network on the outside of your firewall, you may
# also get flooded by Multicasts. We drop them so we do not get flooded by
# logs
#
#$IPTABLES -A INPUT -i $INET_IFACE -d 224.0.0.0/8 -j DROP
#
# Log weird packets that don't match the above.
#
$IPTABLES -A INPUT -m limit --limit 3/minute --limit-burst 3 -j LOG \
--log-level DEBUG --log-prefix "IPT INPUT packet died: "
#
# 4.1.5 FORWARD chain
#
#
# Bad TCP packets we don't want
#
$IPTABLES -A FORWARD -p tcp -j bad_tcp_packets
#
# Accept the packets we actually want to forward
#
$IPTABLES -A FORWARD -i $LAN_IFACE -j ACCEPT
$IPTABLES -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
#
# Log weird packets that don't match the above.
#
$IPTABLES -A FORWARD -m limit --limit 3/minute --limit-burst 3 -j LOG \
--log-level DEBUG --log-prefix "IPT FORWARD packet died: "
#
# 4.1.6 OUTPUT chain
#
#
# Bad TCP packets we don't want.
#
$IPTABLES -A OUTPUT -p tcp -j bad_tcp_packets
#
# Special OUTPUT rules to decide which IP's to allow.
#
$IPTABLES -A OUTPUT -p ALL -s $LO_IP -j ACCEPT
$IPTABLES -A OUTPUT -p ALL -s $LAN_IP -j ACCEPT
$IPTABLES -A OUTPUT -p ALL -o $INET_IFACE -j ACCEPT
#
# Log weird packets that don't match the above.
#
$IPTABLES -A OUTPUT -m limit --limit 3/minute --limit-burst 3 -j LOG \
--log-level DEBUG --log-prefix "IPT OUTPUT packet died: "
######
Iptables 指南 1.1.19 页码,127/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
# 4.2 nat table
#
#
# 4.2.1 Set policies
#
#
# 4.2.2 Create user specified chains
#
#
# 4.2.3 Create content in user specified chains
#
#
# 4.2.4 PREROUTING chain
#
#
# 4.2.5 POSTROUTING chain
#
if [ $PPPOE_PMTU == "yes" ] ; then
$IPTABLES -t nat -A POSTROUTING -p tcp --tcp-flags SYN,RST SYN \
-j TCPMSS --clamp-mss-to-pmtu
fi
$IPTABLES -t nat -A POSTROUTING -o $INET_IFACE -j MASQUERADE
#
# 4.2.6 OUTPUT chain
#
######
# 4.3 mangle table
#
#
# 4.3.1 Set policies
#
#
# 4.3.2 Create user specified chains
#
#
# 4.3.3 Create content in user specified chains
#
#
# 4.3.4 PREROUTING chain
#
#
# 4.3.5 INPUT chain
#
#
# 4.3.6 FORWARD chain
#
#
# 4.3.7 OUTPUT chain
#
Iptables 指南 1.1.19 页码,128/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
#
# 4.3.8 POSTROUTING chain
#
I.5. rc.flush-iptables脚本代码
#!/bin/sh
#
# rc.flush-iptables - Resets iptables to default values.
#
# Copyright (C) 2001 Oskar Andreasson
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program or from the site that you downloaded it
# from; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA
#
# Configurations
#
IPTABLES="/usr/sbin/iptables"
#
# reset the default policies in the filter table.
#
$IPTABLES -P INPUT ACCEPT
$IPTABLES -P FORWARD ACCEPT
$IPTABLES -P OUTPUT ACCEPT
#
# reset the default policies in the nat table.
#
$IPTABLES -t nat -P PREROUTING ACCEPT
$IPTABLES -t nat -P POSTROUTING ACCEPT
$IPTABLES -t nat -P OUTPUT ACCEPT
#
# reset the default policies in the mangle table.
#
$IPTABLES -t mangle -P PREROUTING ACCEPT
$IPTABLES -t mangle -P OUTPUT ACCEPT
#
# flush all the rules in the filter and nat tables.
#
$IPTABLES -F
$IPTABLES -t nat -F
$IPTABLES -t mangle -F
Iptables 指南 1.1.19 页码,129/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
#
# erase all chains that's not default in filter and nat table.
#
$IPTABLES -X
$IPTABLES -t nat -X
$IPTABLES -t mangle -X
I.6. rc.test-iptables脚本代码
#!/bin/bash
#
# rc.test-iptables - test script for iptables chains and tables.
#
# Copyright (C) 2001 Oskar Andreasson
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program or from the site that you downloaded it
# from; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA
#
#
# Filter table, all chains
#
iptables -t filter -A INPUT -p icmp --icmp-type echo-request \
-j LOG --log-prefix="filter INPUT:"
iptables -t filter -A INPUT -p icmp --icmp-type echo-reply \
-j LOG --log-prefix="filter INPUT:"
iptables -t filter -A OUTPUT -p icmp --icmp-type echo-request \
-j LOG --log-prefix="filter OUTPUT:"
iptables -t filter -A OUTPUT -p icmp --icmp-type echo-reply \
-j LOG --log-prefix="filter OUTPUT:"
iptables -t filter -A FORWARD -p icmp --icmp-type echo-request \
-j LOG --log-prefix="filter FORWARD:"
iptables -t filter -A FORWARD -p icmp --icmp-type echo-reply \
-j LOG --log-prefix="filter FORWARD:"
#
# NAT table, all chains except OUTPUT which don't work.
#
iptables -t nat -A PREROUTING -p icmp --icmp-type echo-request \
-j LOG --log-prefix="nat PREROUTING:"
iptables -t nat -A PREROUTING -p icmp --icmp-type echo-reply \
-j LOG --log-prefix="nat PREROUTING:"
iptables -t nat -A POSTROUTING -p icmp --icmp-type echo-request \
-j LOG --log-prefix="nat POSTROUTING:"
iptables -t nat -A POSTROUTING -p icmp --icmp-type echo-reply \
Iptables 指南 1.1.19 页码,130/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
-j LOG --log-prefix="nat POSTROUTING:"
iptables -t nat -A OUTPUT -p icmp --icmp-type echo-request \
-j LOG --log-prefix="nat OUTPUT:"
iptables -t nat -A OUTPUT -p icmp --icmp-type echo-reply \
-j LOG --log-prefix="nat OUTPUT:"
#
# Mangle table, all chains
#
iptables -t mangle -A PREROUTING -p icmp --icmp-type echo-request \
-j LOG --log-prefix="mangle PREROUTING:"
iptables -t mangle -A PREROUTING -p icmp --icmp-type echo-reply \
-j LOG --log-prefix="mangle PREROUTING:"
iptables -t mangle -I FORWARD 1 -p icmp --icmp-type echo-request \
-j LOG --log-prefix="mangle FORWARD:"
iptables -t mangle -I FORWARD 1 -p icmp --icmp-type echo-reply \
-j LOG --log-prefix="mangle FORWARD:"
iptables -t mangle -I INPUT 1 -p icmp --icmp-type echo-request \
-j LOG --log-prefix="mangle INPUT:"
iptables -t mangle -I INPUT 1 -p icmp --icmp-type echo-reply \
-j LOG --log-prefix="mangle INPUT:"
iptables -t mangle -A OUTPUT -p icmp --icmp-type echo-request \
-j LOG --log-prefix="mangle OUTPUT:"
iptables -t mangle -A OUTPUT -p icmp --icmp-type echo-reply \
-j LOG --log-prefix="mangle OUTPUT:"
iptables -t mangle -I POSTROUTING 1 -p icmp --icmp-type echo-request \
-j LOG --log-prefix="mangle POSTROUTING:"
iptables -t mangle -I POSTROUTING 1 -p icmp --icmp-type echo-reply \
-j LOG --log-prefix="mangle POSTROUTING:"
Iptables 指南 1.1.19 页码,131/131
file://H:\new\iptables-tutorial-1-1-19\iptables-tutorial-1-1-19\ipta... 2004-3-1
               
               
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/87203/showart_1675555.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP