在之前的文章中,我们已经总结过,iptables为我们预定义了4张表,它们分别是raw表、mangle表、nat表、filter表,不同的表拥有不同的功能。

filter负责过滤功能,比如允许哪些IP地址访问,拒绝哪些IP地址访问,允许访问哪些端口,禁止访问哪些端口,filter表会根据我们定义的规则进行过滤,filter表应该是我们最常用到的表了,所以此处,我们以filter表为例,开始学习怎样实际操作iptables。

1,查询filter表中的所有规则:

[root@bogon /]# iptables -t filter -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
ACCEPT     all  --  anywhere             anywhere            
INPUT_direct  all  --  anywhere             anywhere            
INPUT_ZONES_SOURCE  all  --  anywhere             anywhere            
INPUT_ZONES  all  --  anywhere             anywhere            
DROP       all  --  anywhere             anywhere             ctstate INVALID
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
ACCEPT     all  --  anywhere             anywhere            
FORWARD_direct  all  --  anywhere             anywhere            
FORWARD_IN_ZONES_SOURCE  all  --  anywhere             anywhere            
FORWARD_IN_ZONES  all  --  anywhere             anywhere            
FORWARD_OUT_ZONES_SOURCE  all  --  anywhere             anywhere            
FORWARD_OUT_ZONES  all  --  anywhere             anywhere            
DROP       all  --  anywhere             anywhere             ctstate INVALID
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
OUTPUT_direct  all  --  anywhere             anywhere                   

参数说明:

-t 表示查询的表的名字 

-L 是列出之意 list

其他表中的查询

我们可以使用iptables -t filter -L命令列出filter表中的所有规则,那么举一反三,我们也可以查看其它表中的规则,示例如下。

iptables -t raw -L

iptables -t mangle -L

iptables -t nat -L

其实,我们可以省略-t filter,当没有使用-t选项指定表时,默认为操作filter表,即iptables -L表示列出filter表中的所有规则。

当我们要定义某条"过滤"的规则时,我们会在filter表中定义,但是具体在哪条"链"上定义规则呢?这取决于我们的工作场景。比如,我们需要禁止某个IP地址访问我们的主机,我们则需要在INPUT链上定义规则。因为,我们在理论总结中已经提到过,报文发往本机时,会经过PREROUTING链与INPUT链(如果你没有明白,请回顾前文),所以,如果我们想要禁止某些报文发往本机,我们只能在PREROUTING链和INPUT链中定义规则,但是PREROUTING链并不存在于filter表中,换句话说就是,PREROUTING关卡天生就没有过滤的能力,所以,我们只能在INPUT链中定义,当然,如果是其他工作场景,可能需要在FORWARD链或者OUTPUT链中定义过滤规则。

2,查询某一表中某一个链中的规则

查询filter表中INPUT链的规则:

[root@bogon /]# iptables -L INPUT
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
ACCEPT     all  --  anywhere             anywhere            
INPUT_direct  all  --  anywhere             anywhere            
INPUT_ZONES_SOURCE  all  --  anywhere             anywhere            
INPUT_ZONES  all  --  anywhere             anywhere            
DROP       all  --  anywhere             anywhere             ctstate INVALID
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
[root@bogon /]# 

-v 会显示比较详细的信息

[root@bogon /]# iptables -L INPUT -v
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
 3318   14M ACCEPT     all  --  any    any     anywhere             anywhere             ctstate RELATED,ESTABLISHED
    2   136 ACCEPT     all  --  lo     any     anywhere             anywhere            
   28  2158 INPUT_direct  all  --  any    any     anywhere             anywhere            
   28  2158 INPUT_ZONES_SOURCE  all  --  any    any     anywhere             anywhere            
   28  2158 INPUT_ZONES  all  --  any    any     anywhere             anywhere            
    0     0 DROP       all  --  any    any     anywhere             anywhere             ctstate INVALID
   27  2106 REJECT     all  --  any    any     anywhere             anywhere             reject-with icmp-host-prohibited
[root@bogon /]# 

每一列的含义:

pkts:对应规则匹配到的报文的个数。

bytes:对应匹配到的报文包的大小总和。

target:规则对应的target,往往表示规则对应的"动作",即规则匹配成功后需要采取的措施。

prot:表示规则对应的协议,是否只针对某些协议应用此规则。

opt:表示规则对应的选项。

in:表示数据包由哪个接口(网卡)流入,我们可以设置通过哪块网卡流入的报文需要匹配当前规则。

out:表示数据包由哪个接口(网卡)流出,我们可以设置通过哪块网卡流出的报文需要匹配当前规则。

source:表示规则对应的源头地址,可以是一个IP,也可以是一个网段。

destination:表示规则对应的目标地址。可以是一个IP,也可以是一个网段。

细心如你一定发现了,上图中的源地址与目标地址都为anywhere,看来,iptables默认为我们进行了名称解析,但是在规则非常多的情况下如果进行名称解析,效率会比较低,所以,在没有此需求的情况下,我们可以使用-n选项,表示不对IP地址进行名称反解,直接显示IP地址,示例如下。

[root@bogon /]# iptables -L INPUT -nv
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
 3428   14M ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
    2   136 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
   28  2158 INPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
   28  2158 INPUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
   28  2158 INPUT_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID
   27  2106 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
[root@bogon /]# 

如果你习惯了查看有序号的列表,你在查看iptables表中的规则时肯定会很不爽,没有关系,满足你,使用--line-numbers即可显示规则的编号,示例如下:

[root@bogon /]# iptables -nvL INPUT --line-number
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1     3518   14M ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
2        2   136 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
3       28  2158 INPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
4       28  2158 INPUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
5       28  2158 INPUT_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
6        0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID
7       27  2106 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
[root@bogon /]# 

我知道你目光如炬,你可能早就发现了,表中的每个链的后面都有一个括号,括号里面有一些信息,如下图红色标注位置,那么这些信息都代表了什么呢?我们来看看。

policy:表示当前链的默认策略,policy ACCEPT表示上图中INPUT的链的默认动作为ACCEPT,换句话说就是,默认接受通过INPUT关卡的所有请求,所以我们在配置INPUT链的具体规则时,应该将需要拒绝的请求配置到规则中,说白了就是"黑名单"机制,默认所有人都能通过,只有指定的人不能通过,当我们把INPUT链默认动作设置为接受(ACCEPT),就表示所有人都能通过这个关卡,此时就应该在具体的规则中指定需要拒绝的请求,就表示只有指定的人不能通过这个关卡,这就是黑名单机制,但是,你一定发现了,上图中所显示出的规则,大部分都是接受请求(ACCEPT),并不是想象中的拒绝请求(DROP或者REJECT),这与我们所描述的黑名单机制不符啊,按照道理来说,默认动作为接受,就应该在具体的规则中配置需要拒绝的人,但是上图中并不是这样的,之所以出现上图中的情况,是因为IPTABLES的工作机制导致到,上例其实是利用了这些"机制",完成了所谓的"白名单"机制,并不是我们所描述的"黑名单"机制,我们此处暂时不用关注这一点,之后会进行详细的举例并解释,此处我们只要明白policy对应的动作为链的默认动作即可,或者换句话说,我们只要理解,policy为链的默认策略即可。

packets:表示当前链(上例为INPUT链)默认策略匹配到的包的数量,0 packets表示默认策略匹配到0个包。

bytes:表示当前链默认策略匹配到的所有包的大小总和。

 

posted on 2020-12-31 15:10  EZgod  阅读(351)  评论(0编辑  收藏  举报