iptables_超解

 

 

 

 

 

 

查询:

 

 

-t选项,指定要操作的表,使用-L选项,查看-t选项对应的表的规则,-L选项的意思是,列出规则,所以,上述命令的含义为列出filter表的所有规则
显示出了3条链INPUT链、FORWARD链、OUTPUT链,每条链中都有自己的规则,
上图中可以看出,INPUT链、FORWARD链、OUTPUT链都拥有"过滤"的能力,所以,当我们要定义某条"过滤"的规则时,
我们会在filter表中定义,但是具体在哪条""上定义规则呢?这取决于我们的工作场景。
比如,我们需要禁止某个IP地址访问我们的主机,我们则需要在INPUT链上定义规则。
报文发往本机时,会经过PREROUTING链与INPUT链,所以,如果我们想要禁止某些报文发往本机,我们只能在PREROUTING链和INPUT链中定义规则,
但是PREROUTING链并不存在于filter表中,所以,我们只能在INPUT链中定义

注意:其他表可以类似查询

iptables -t raw -L

iptables -t mangle -L

iptables -t nat -L

[root@#quan#Better ~]$iptables -t raw -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@#quan#Better ~]$iptables -t mangle -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

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         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
[root@#quan#Better ~]$iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination     

 

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

只查看指定表中的指定链的规则,比如,我们只查看filter表中INPUT链的规则,示例如下(注意大小写)

[root@#quan#Better ~]$iptables -L INPUT
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED 
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh 
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited 

 

查看详细一点的信息可以加-v

[root@#quan#Better ~]$iptables -vL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  452  131K ACCEPT     all  --  any    any     anywhere             anywhere            state RELATED,ESTABLISHED 
    0     0 ACCEPT     icmp --  any    any     anywhere             anywhere            
   14   840 ACCEPT     all  --  lo     any     anywhere             anywhere            
    0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere            state NEW tcp dpt:ssh 
   12   936 REJECT     all  --  any    any     anywhere             anywhere            reject-with icmp-host-prohibited 

解析;

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

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

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

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

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

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

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

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

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

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

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

加-n 进行域名解析

[root@#quan#Better ~]$iptables -nL INPUT
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

iptable --line-numbers -t 表名 -L  表示查看表的所有规则,并且显示规则的序号

[root@#quan#Better ~]$iptables --line-numbers -L INPUT  
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED 
2    ACCEPT     icmp --  anywhere             anywhere            
3    ACCEPT     all  --  anywhere             anywhere            
4    ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh 
5    REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited 
[root@#quan#Better ~]$iptables --line-numbers -t filter  -L
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED 
2    ACCEPT     icmp --  anywhere             anywhere            
3    ACCEPT     all  --  anywhere             anywhere            
4    ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh 
5    REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination    

 

 

规则管理

 在iptables的中,匹配条件是报文的"源地址"、"目标地址"、"源端口"、"目标端口"等最常用的动作有ACCEPT(接受)、DROP(丢弃)、REJECT(拒绝)

 使用iptables -F INPUT命令清空filter表INPUT链中的规则

 

 

 增加规则

[root@#quan#Better ~]$iptables -t filter -I INPUT -s 192.168.31.42 -j DROP
[root@#quan#Better ~]$iptables -nvL INPUT
Chain INPUT (policy ACCEPT 62 packets, 11448 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       all  --  *      *       192.168.31.42        0.0.0.0/0           

-t选项指定了要操作的表,此处指定了操作filter表,与之前的查看命令一样,不使用-t选项指定表时,默认为操作filter表。

-I选项,指明将"规则"插入至哪个链中,-I表示insert,即插入的意思,所以-I INPUT表示将规则插入于INPUT链中

-s选项,指明"匹配条件"中的"源地址",即如果报文的源地址属于-s对应的地址,,-s为source之意,表示源地址

-j选项,指明当"匹配条件"被满足时,所对应的动作,动作被称之为"target",所以,上图中taget字段对应的动作为DROP。

测试!!!!

 

 

 

实验实验

在追加一条接受规则

[root@#quan#Better ~]$iptables -A INPUT -s 192.168.31.66 -j ACCEPT
[root@#quan#Better ~]$iptables -nvL INPUT
Chain INPUT (policy ACCEPT 10 packets, 720 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  147 11688 DROP       all  --  *      *       192.168.31.42        0.0.0.0/0           
    0     0 ACCEPT     all  --  *      *       192.168.31.66        0.0.0.0/0   

-A选项,表示在对应的链中"追加规则",-A为append之意,所以,-A INPUT则表示在INPUT链中追加规则,而之前示例中使用的-I选项则表示在链中"插入规则",它们都是添加一条规则,只是-A表示在链的尾部追加规则,-I表示在链的首部插入规则而已。

使用-j选项,指定当前规则对应的动作为ACCEPT。

 

 

 经过测试,第二条规则是没有生效的

如果报文已经被前面的规则匹配到,iptables则会对报文执行对应的动作,即使后面的规则也能匹配到当前报文,很有可能也没有机会再对报文执行相应的动作了,就以上图为例,报文先被第一条规则匹配到了,于是当前报文被拒绝了,因为报文已经被拒绝了,所以,即使上图中的第二条规则即使能够匹配到刚才"拒绝"的报文,也没有机会再对刚才的报文进行通过操作了。这就是iptables的工作机制。

使用--line-number选项可以列出规则的序号

[root@#quan#Better ~]$iptables --line-number  -nvL INPUT
Chain INPUT (policy ACCEPT 138 packets, 38872 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1      447 36360 DROP       all  --  *      *       192.168.31.42        0.0.0.0/0           
2        0     0 ACCEPT     all  --  *      *       192.168.31.66        0.0.0.0/0  

添加规则时,指定新增规则的编号,这样我们就能在任意位置插入规则了()

 

 

 注意:

一定要使用-I选项进行插入规则操作,-I INPUT 2表示在INPUT链中新增规则,新增的规则的编号为2

删除规则

方法一:根据规则的编号去删除规则

[root@#quan#Better ~]$iptables --line-number  -nvL INPUT
Chain INPUT (policy ACCEPT 57 packets, 25372 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     all  --  *      *       192.168.31.66        0.0.0.0/0           
2      741 60484 DROP       all  --  *      *       192.168.31.42        0.0.0.0/0           
3        0     0 ACCEPT     all  --  *      *       192.168.31.66        0.0.0.0/0           
[root@#quan#Better ~]$iptables -t filter -D INPUT 3
[root@#quan#Better ~]$iptables --line-number  -nvL INPUT
Chain INPUT (policy ACCEPT 17 packets, 8256 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     all  --  *      *       192.168.31.66        0.0.0.0/0           
2      766 62496 DROP       all  --  *      *       192.168.31.42        0.0.0.0/0     

 

方法二:根据具体的匹配条件与动作删除规则  略

 

注意:

删除指定表中某条链中的所有规则的命令,iptables -t 表名 -F 链名

-F选项为flush之意,即冲刷指定的链,即删除指定链中的所有规则,

注意,此操作相当于删除操作,在没有保存iptables规则的情况下,请慎用。

其实,-F选项不仅仅能清空指定链上的规则,其实它还能清空整个表中所有链上的规则,不指定链名,只指定表名即可删除表中的所有规则,命令如下

iptables -t 表名 -F

不过再次强调,在没有保存iptables规则时,请勿随便清空链或者表中的规则,除非你明白你在干什么

修改规则

强烈建议是把原来的规则先删除在添加

 可以修改默认动作,但是没必要

当报文没有被链中的任何规则匹配到时,或者,当链中没有任何规则时,防火墙会按照默认动作处理报文,我们可以修改指定链的默认策略,使用如下命令即可。

[root@#quan#Better ~]$iptables -nvL
Chain INPUT (policy ACCEPT 2868 packets, 1121K bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     all  --  *      *       192.168.31.66        0.0.0.0/0           
 8852  726K DROP       all  --  *      *       192.168.31.42        0.0.0.0/0           

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT 5135 packets, 333K bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@#quan#Better ~]$iptables -t filter -P FORWARD DROP
[root@#quan#Better ~]$iptables -nvL
Chain INPUT (policy ACCEPT 6 packets, 432 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     all  --  *      *       192.168.31.66        0.0.0.0/0           
 8878  728K DROP       all  --  *      *       192.168.31.42        0.0.0.0/0           

Chain FORWARD (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT 3 packets, 408 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@#quan#Better ~]$

 

iptables详解(3):iptables规则管理

使用-t指定要操作的表,使用-P选项指定要修改的链,-P FORWARD DROP表示将表中FORWRD链的默认策略改为DROP。

保存规则

C6中

[root@#quan#Better ~]$service iptables save

救可以了

当我们对规则进行了修改以后,如果想要修改永久生效,必须使用service iptables save保存规则,当然,如果你误操作了规则,但是并没有保存,
那么使用service iptables restart命令重启iptables以后,规则会再次回到上次保存/etc/sysconfig/iptables文件时的模样。

 

 

C7

使用iptables-save命令

使用iptables-save并不能保存当前的iptables规则,但是可以将当前的iptables规则以"保存后的格式"输出到屏幕上。

所以,我们可以使用iptables-save命令,再配合重定向,将规则重定向到/etc/sysconfig/iptables文件中即可。

iptables-save > /etc/sysconfig/iptables

我们也可以将/etc/sysconfig/iptables中的规则重新载入为当前的iptables规则,但是注意,未保存入/etc/sysconfig/iptables文件中的修改将会丢失或者被覆盖。

使用iptables-restore命令可以从指定文件中重载规则,示例如下

iptables-restore < /etc/sysconfig/iptables

再次提醒:重载规则时,现有规则将会被覆盖。

 

匹配条件总结

使用-s选项作为匹配条件,可以匹配报文的源地址

[root@#quan#Better ~]$iptables -t filter -F INPUT
[root@#quan#Better ~]$iptables -t filter -I INPUT -s 192.168.31.22,192.168.31.66 -j DROP  #可以逗号隔开,实现多个源地址匹配
[root@#quan#Better ~]$iptables -nvL INPUT
Chain INPUT (policy ACCEPT 58 packets, 4204 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       all  --  *      *       192.168.31.66        0.0.0.0/0           
    0     0 DROP       all  --  *      *       192.168.31.22        0.0.0.0/0           
[root@#quan#Better ~]$iptables -t filter -I INPUT -s  192.168.1.0/24 -j DROP  #也可以使用便捷方式去匹配一个网段
[root@#quan#Better ~]$iptables -nvL INPUT
Chain INPUT (policy ACCEPT 6 packets, 432 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       all  --  *      *       192.168.1.0/24       0.0.0.0/0           
    0     0 DROP       all  --  *      *       192.168.31.66        0.0.0.0/0           
    0     0 DROP       all  --  *      *       192.168.31.22        0.0.0.0/0    
[root@#quan#Better ~]$iptables -t filter -I INPUT ! -s  192.168.2.0/24 -j DROP  #匹配不是哪个网段可以加!,取反
[root@#quan#Better ~]$iptables -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   45  5287 ACCEPT     all  --  *      *       192.168.31.0/24      0.0.0.0/0           
   83  8390 DROP       all  --  *      *      !192.168.2.0/24       0.0.0.0/0           
    0     0 DROP       all  --  *      *       192.168.1.0/24       0.0.0.0/0           
    0     0 DROP       all  --  *      *       192.168.31.66        0.0.0.0/0           
    0     0 DROP       all  --  *      *       192.168.31.22        0.0.0.0/0        

没有其他可以匹配的规则,于是,就会去匹配当前链的默认动作(默认策略),而上例中,INPUT链的默认动作为ACCEPT

匹配条件:目标IP地址

使用-d选项指定"目标地址"作为匹配条件

[root@#quan#Better ~]$iptables -t filter -F INPUT
[root@#quan#Better ~]$iptables -t filter -I INPUT -s 192.168.1.11 -d 192.168.2.222 -j DROP
#如果是来自192.168.1.11 想到达192.168.2.222的报文将要抛弃
[root@#quan#Better ~]$iptables -vnL INPUT
Chain INPUT (policy ACCEPT 121 packets, 54164 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       all  --  *      *       192.168.1.11         192.168.2.222       
[root@#quan#Better ~]$iptables -t filter -F INPUT
[root@#quan#Better ~]$iptables -t filter -I INPUT  -d 192.168.2.222 -j DROP
#凡是要到达192.168.2.222主机的,无论来自哪个IP都是淘汰
[root@#quan#Better ~]$iptables -vnL INPUT
Chain INPUT (policy ACCEPT 10 packets, 676 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       all  --  *      *       0.0.0.0/0            192.168.2.222  

匹配条件:协议类型

使用-p选项,指定需要匹配的报文的协议类型

[root@#quan#Better ~]$iptables -t filter -F INPUT
[root@#quan#Better ~]$iptables -t filter -I INPUT -s 192.168.31.42 -p tcp -j REJECT
#拒绝来自192.168.31.42 主机 类型为tcp的报文
[root@#quan#Better ~]$iptables -vnL INPUT
Chain INPUT (policy ACCEPT 28 packets, 9056 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    1    60 REJECT     tcp  --  *      *       192.168.31.42        0.0.0.0/0           reject-with icmp-port-unreachable 
测试
[root@MiWiFi-R1CM-srv ~]# ssh 192.168.31.66  #ssh 使用的是tcp 服务器拒绝
ssh: connect to host 192.168.31.66 port 22: Connection refused
[root@MiWiFi-R1CM-srv ~]# ping  192.168.31.66  #ping使用的是icmp协议
PING 192.168.31.66 (192.168.31.66) 56(84) bytes of data.
64 bytes from 192.168.31.66: icmp_seq=1 ttl=64 time=0.403 ms
64 bytes from 192.168.31.66: icmp_seq=2 ttl=64 time=0.396 ms

,-p选项都支持匹配哪些协议呢?我们总结一下

centos6中,-p选项支持如下协议类型

tcp, udp, udplite, icmp, esp, ah, sctp

centos7中,-p选项支持如下协议类型

tcp, udp, udplite, icmp, icmpv6,esp, ah, sctp, mh

当不使用-p指定协议类型时,默认表示所有类型的协议都会被匹配到,与使用-p all的效果相同。

 

匹配条件:网卡接口

使用 -i 选项去匹配报文是通过哪块网卡流入本机的

理论解析

 

 

 

 

-i选项是用于判断报文是从哪个网卡流入的,那么,-i选项只能用于上图中的PREROUTING链、INPUT链、FORWARD链,这是-i选项的特殊性,因为它只是用于判断报文是从哪个网卡流入的,所以只能在上图中"数据流入流向"的链中与FORWARD链中存在,而上图中的"数据发出流向"经过的链中,是不可能使用-i选项的,比如上图中的OUTPUT链与POSTROUTING链,他们都不能使用-i选项。

-i选项只能用于PREROUTING链、INPUT链、FORWARD链,那么-o选项只能用于FORWARD链、OUTPUT链、POSTROUTING链。

因为-o选项是用于匹配报文将由哪个网卡"流出"的,所以,-o选项只能用于FORWARD链、OUTPUT链、POSTROUTING链中。FORWARD链它能同时使用-i选项与-o选项。

eth1      Link encap:Ethernet  HWaddr 00:0C:29:30:61:38  
          inet addr:192.168.31.66  Bcast:192.168.31.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe30:6138/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:133652 errors:0 dropped:0 overruns:0 frame:0
          TX packets:47581 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:28850952 (27.5 MiB)  TX bytes:4723438 (4.5 MiB)


[root@#quan#Better ~]$iptables -t filter -I INPUT -i eth1 -p icmp -j DROP
[root@#quan#Better ~]$iptables -vnL INPUT
Chain INPUT (policy ACCEPT 55 packets, 4332 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       icmp --  eth1   *       0.0.0.0/0            0.0.0.0/0  
测试
[root@MiWiFi-R1CM-srv ~]# ping 192.168.31.66
PING 192.168.31.66 (192.168.31.66) 56(84) bytes of data.
^C
--- 192.168.31.66 ping statistics ---
5 packets transmitted, 0 received, 100% packet loss, time 4836ms

扩展模块udp

udp扩展模块,这个扩展模块中能用的匹配条件比较少,只有两个,就是--sport与--dport,即匹配报文的源端口与目标端口tcp模块中也有这两个选项,名称都一模一样

只不过udp扩展模块的--sport与--dport是用于匹配UDP协议报文的源端口与目标端口

[root@#quan#Better ~]$iptables -F
[root@#quan#Better ~]$iptables -t filter -I INPUT -p udp -m udp --dport 137 -j ACCEPT
[root@#quan#Better ~]$iptables -t filter -I INPUT -p udp -m udp --dport 138 -j ACCEPT
[root@#quan#Better ~]$iptables -vnL  INPUT
Chain INPUT (policy ACCEPT 24 packets, 1728 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0           udp dpt:138 
    0     0 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0           udp dpt:137 
#开放samba服务的137与138这两个UDP端口

icmp扩展

ICMP协议的全称为Internet Control Message Protocol,翻译为互联网控制报文协议,它主要用于探测网络上的主机是否可用,目标是否可达,网络是否通畅,路由是否可用等

 

虽然ping请求报文与ping回应报文都属于ICMP类型的报文,但是如果在概念上细分的话,它们所属的类型还是不同的,我们发出的ping请求属于类型8的icmp报文,

而对方主机的ping回应报文则属于类型0的icmp报文,根据应用场景的不同,icmp报文被细分为如下各种类型

 

 

 

 

 

 所有表示"目标不可达"的icmp报文的type码为3,而"目标不可达"又可以细分为多种情况,是网络不可达呢?还是主机不可达呢?再或者是端口不可达呢?所以,为了更加细化的区分它们,icmp对每种type又细分了对应的code,用不同的code对应具体的场景,  所以,我们可以使用type/code去匹配具体类型的ICMP报文

[root@#quan#Better ~]$iptables -F
[root@#quan#Better ~]$iptables -t filter -I INPUT -p icmp -j REJECT
[root@#quan#Better ~]$iptables -vnL  INPUT
Chain INPUT (policy ACCEPT 8 packets, 576 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-port-unreachable 
[root@#quan#Better ~]$ping baidu.com
PING baidu.com (220.181.38.148) 56(84) bytes of data.
^C
--- baidu.com ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1610ms
#这样子我们ping不了别人,别人也ping不了我

只想要ping通别人,但是不想让别人ping通我们

[root@#quan#Better ~]$iptables -F
[root@#quan#Better ~]$iptables -t filter -I INPUT -p icmp -m icmp --icmp-type 8/0 -j REJECT
[root@#quan#Better ~]$iptables -vnL  INPUT
Chain INPUT (policy ACCEPT 10 packets, 720 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           icmp type 8 code 0 reject-with icmp-port-unreachable 
[root@#quan#Better ~]$ping baidu.com
PING baidu.com (39.156.69.79) 56(84) bytes of data.
64 bytes from 39.156.69.79: icmp_seq=1 ttl=50 time=40.6 ms
64 bytes from 39.156.69.79: icmp_seq=2 ttl=50 time=41.5 ms
64 bytes from 39.156.69.79: icmp_seq=3 ttl=50 time=40.8 ms
64 bytes from 39.156.69.79: icmp_seq=4 ttl=50 time=40.9 ms

#使用"-m icmp"表示使用icmp扩展,因为上例中使用了"-p icmp",所以"-m icmp"可以省略,使用"--icmp-type"选项表示根据具体的type与code去匹配对应的icmp报文,
#而上图中的"--icmp-type 8/0"表示icmp报文的type为8,code为0才会被匹配到,也就是只有ping请求类型的报文才能被匹配到,所以,
#别人对我们发起的ping请求将会被拒绝通过防火墙,而我们之所以能够ping通别人,是因为别人回应我们的报文的icmp type为0,code也为0,
#所以无法被上述规则匹配到,所以我们可以看到别人回应我们的信息。

 

 

扩展模块state

state是可以译为状态,state模块可以让iptables实现"连接追踪"机制。既然是"连接追踪",则必然要有"连接"。

在TCP/IP协议簇中,UDP和ICMP是没有所谓的连接的,但是对于state模块来说,tcp报文、udp报文、icmp报文都是有连接状态的,我们可以这样认为,对于state模块而言,只要两台机器在"你来我往"的通信,就算建立起了连接

"连接"其中的报文可以分为5种状态,报文状态可以为NEW、ESTABLISHED、RELATED、INVALID、UNTRACKED

NEW:连接中的第一个包,状态就是NEW,我们可以理解为新连接的第一个包的状态为NEW。

ESTABLISHED:我们可以把NEW状态包后面的包的状态理解为ESTABLISHED,表示连接已建立

RELATED:RELATED译为关系
就是类似FTP服务端会建立两个进程,数据连接和命令连接,具体传输哪些数据,是由命令去控制的,所以,"数据连接"中的报文与"命令连接"是有"关系"的。

那么,"数据连接"中的报文可能就是RELATED状态,因为这些报文与"命令连接"中的报文有关系

INVALID:如果一个包没有办法被识别,或者这个包没有任何状态,那么这个包的状态就是INVALID,我们可以主动屏蔽状态为INVALID的报文。

UNTRACKED:报文的状态为untracked时,表示报文未被追踪,当报文的状态为Untracked时通常表示无法找到相关的连接。

 

如果报文的状态为ESTABLISHED,那么报文肯定是之前发出的报文的回应

将状态为RELATED或ESTABLISHED的报文都放行表示只有回应我们的报文能够通过防火墙,如果是别人主动发送过来的新的报文,则无法通过防火墙

[root@#quan#Better ~]$iptables -F
[root@#quan#Better ~]$iptables -t filter -I INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 
[root@#quan#Better ~]$iptables -t filter -A INPUT -j REJECT 
[root@#quan#Better ~]$iptables -vnL  
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  372 53543 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-port-unreachable 

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 8 packets, 903 bytes)
 pkts bytes target     prot opt in     out     source               destination     
#当前主机IP为66,当放行ESTABLISHED与RELATED状态的包以后,并没有影响通过本机远程ssh到IP为42的主机上,但是无法从42上使用22端口主动连接到66上。
#对于其他端口与IP来说,也是相同的,可以从66主动发送报文,并且能够收到响应报文,但是其他主机并不能主动向66发起请求
[root@#quan#Better ~]$ssh 192.168.31.42
root@192.168.31.42's password: 
Permission denied, please try again.
root@192.168.31.42's password: 
Last login: Fri Nov 22 12:50:14 2019 from 192.168.31.66
[root@MiWiFi-R1CM-srv ~]# exit

[root@MiWiFi-R1CM-srv ~]# ssh 192.168.31.66
ssh: connect to host 192.168.31.66 port 22: Connection refused

 

扩展匹配条件

想要使用iptables设置一条规则,拒绝来自192.168.1.146的ssh请求,我们就可以拒绝146上的报文能够发往本机的22号端口,这个时候,就需要用到"目标端口"选项。

使用选项--dport可以匹配报文的目标端口,--dport意为destination-port,即表示目标端口。

注意,与之前的选项不同,--dport前有两条"横杠",而且,使用--dport选项时,必须事先指定了使用哪种协议,即必须先使用-p选项

[root@#quan#Better ~]$iptables -t filter -I INPUT -s 192.168.31.42 -p tcp -m tcp --dport 22 -j REJECT

[root@#quan#Better ~]$iptables -vnL INPUT
Chain INPUT (policy ACCEPT 6 packets, 432 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     tcp  --  *      *       192.168.31.42        0.0.0.0/0           tcp dpt:22 reject-with icmp-port-unreachable 

#使用--dport之前,我们使用-m选项,指定了对应的扩展模块为tcp,也就是说,如果想要使用--dport这个扩展匹配条件,则必须依靠某个扩折模块完成,
#上例中,这个扩展模块就是tcp扩展模块,最终,我们使用的是tcp扩展模块中的dport扩展匹配条件。

也就是说

在使用扩展匹配条件之前,需要指定相应的扩展模块才行。

-m tcp表示使用tcp扩展模块,--dport表示tcp扩展模块中的一个扩展匹配条件,可用于匹配报文的目标端口。

-p tcp与 -m tcp并不冲突,-p用于匹配报文的协议,-m 用于指定扩展模块的名称,正好,这个扩展模块也叫tcp。

源端口"的扩展匹配条件为--sport

使用--sport可以判断报文是否从指定的端口发出,即匹配报文的源端口是否与指定的端口一致,--sport表示source-port

[root@#quan#Better ~]$iptables -t filter -I INPUT -s 192.168.31.42 -p tcp -m tcp --sport 22 -j REJECT
[root@#quan#Better ~]$iptables -vnL INPUT
Chain INPUT (policy ACCEPT 12 packets, 880 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     tcp  --  *      *       192.168.31.42        0.0.0.0/0           tcp spt:22 reject-with icmp-port-unreachable 
    0     0 REJECT     tcp  --  *      *       192.168.31.42        0.0.0.0/0           tcp dpt:22 reject-with icmp-port-unreachable 

悉知:

--sport和--dsport,都能够指定一个端口范围

--dport 22:25表示目标端口为22到25之间的所有端口,即22端口、23端口、24端口、25端口

 

[root@#quan#Better ~]$iptables -t filter -I INPUT -s 192.168.31.42 -p tcp -m tcp --sport 33:35 -j REJECT
[root@#quan#Better ~]$iptables -vnL INPUT
Chain INPUT (policy ACCEPT 6 packets, 432 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     tcp  --  *      *       192.168.31.42        0.0.0.0/0           tcp spts:33:35 reject-with icmp-port-unreachable 
    0     0 REJECT     tcp  --  *      *       192.168.31.42        0.0.0.0/0           tcp spt:22 reject-with icmp-port-unreachable 
    0     0 REJECT     tcp  --  *      *       192.168.31.42        0.0.0.0/0           tcp dpt:22 reject-with icmp-port-unreachable 

 

 

但是无法同时指定多个离散的、不连续的端口,如果想要同时指定多个离散的端口,需要借助另一个扩展模块,"multiport"模块

[root@#quan#Better ~]$iptables -t filter -I INPUT -s 192.168.31.42 -p tcp -m multiport --sport 11,33:35 -j REJECT
[root@#quan#Better ~]$iptables -vnL INPUT
Chain INPUT (policy ACCEPT 8 packets, 576 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     tcp  --  *      *       192.168.31.42        0.0.0.0/0           multiport sports 11,33:35 reject-with icmp-port-unreachable 

 

 

 

 

 

传说中的黑白名单的机制???

报文在经过iptables的链时,会匹配链中的规则,遇到匹配的规则时,就执行对应的动作,如果链中的规则都无法匹配到当前报文,则使用链的默认策略(默认动作),链的默认策略通常设置为ACCEPT或者DROP。

链的默认策略为ACCEPT时,链中的规则对应的动作应该为DROP或者REJECT,表示只有匹配到规则的报文才会被拒绝,没有被规则匹配到的报文都会被默认接受,这就是"黑名单"机制。

 

当链的默认策略为DROP时,链中的规则对应的动作应该为ACCEPT,表示只有匹配到规则的报文才会被放行,没有被规则匹配到的报文都会被默认拒绝,这就是"白名单"机制。

 

如果使用白名单机制,我们就要把所有人都当做坏人,只放行好人。

如果使用黑名单机制,我们就要把所有人都当成好人,只拒绝坏人。

白名单机制似乎更加安全一些,黑名单机制似乎更加灵活一些

动作总结:

一些常用动作,比如ACCEPT、DROP、REJECT等。其实,"动作"与"匹配条件"一样,也有"基础"与"扩展"之分。同样,使用扩展动作也需要借助扩展模块,但是,扩展动作可以直接使用,不用像使用"扩展匹配条件"那样指定特定的模块。

ACCEPT与DROP都属于基础动作。而REJECT则属于扩展动作

动作"也有自己的选项,我们可以在使用动作时,设置对应的选项,以reject解析

REJECT动作的常用选项为--reject-with

使用--reject-with选项,可以设置提示信息,当对方被拒绝时,会提示对方为什么被拒绝。

可用值如下

icmp-net-unreachable

icmp-host-unreachable

icmp-port-unreachable,

icmp-proto-unreachable

icmp-net-prohibited

icmp-host-pro-hibited

icmp-admin-prohibited

当不设置任何值时,默认值为icmp-port-unreachable。
[root@#quan#Better ~]$iptables -t filter -I INPUT -p tcp --dport 22 -j ACCEPT
[root@#quan#Better ~]$iptables -t filter -A INPUT -j REJECT
[root@#quan#Better ~]$iptables -vnL  INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  109  8776 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:22 
    4   432 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-port-unreachable 

测试:
[root@MiWiFi-R1CM-srv ~]# ping 192.168.31.66
PING 192.168.31.66 (192.168.31.66) 56(84) bytes of data.
From 192.168.31.66 icmp_seq=1 Destination Port Unreachable
From 192.168.31.66 icmp_seq=2 Destination Port Unreachable
#默认提示信息为icmp-port-unreachable,即端口不可达之意。

经过以下修改
[root@#quan#Better ~]$iptables -t filter -I INPUT 2 -j REJECT --reject-with icmp-host-unreachable
[root@#quan#Better ~]$iptables -vnL  INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  345 28184 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:22 
   10   892 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-unreachable 
   62  6076 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-port-unreachable 

在测试
[root@MiWiFi-R1CM-srv ~]# ping 192.168.31.66
PING 192.168.31.66 (192.168.31.66) 56(84) bytes of data.
From 192.168.31.66 icmp_seq=1 Destination Host Unreachable
From 192.168.31.66 icmp_seq=2 Destination Host Unreachable
From 192.168.31.66 icmp_seq=3 Destination Host Unreachable

#Destination Port Unreachable提示信息变为了  Destination Host Unreachable

动作LOG

LOG动作只负责记录匹配到的报文的相关信息,不负责对报文的其他处理

 

 

 

自定义链,方便管理

情景:

如果INPUT链中存放了200条规则,这200条规则有针对httpd服务的,有针对sshd服务的,有针对私网IP的,有针对公网IP的,假如,我们突然想要修改针对httpd服务的相关规则,

难道我们还要从头看一遍这200条规则,找出哪些规则是针对httpd

[root@#quan#Better ~]$iptables -F
#
"-t filter"表示操作的表为filter表,与之前的示例相同,省略-t选项时,缺省操作的就是filter表。
#"-N IN_WEB"表示创建一个自定义链,自定义链的名称为"IN_WEB"
[root@#quan#Better ~]$iptables -t filter -N INPUT_WEB
[root@#quan#Better ~]$iptables -vnL
Chain INPUT (policy ACCEPT 24 packets, 2248 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 11 packets, 1192 bytes)
 pkts bytes target     prot opt in     out     source               destination         
#引用计数为0 (0 references),也就是说,这条自定义链还没有被任何默认链所引用,所以,即使INPUT_WEB中配置了规则,也不会生效
Chain INPUT_WEB (0 references) #表示没有被引用,注意的是,自定义链并不能直接使用,而是需要被默认链引用才能够使用
 pkts bytes target     prot opt in     out     source               destination         
[root@#quan#Better ~]$

自定义一条链,链名叫INPUT_WEB,我们可以将所有针对80端口的入站规则都写入到这条自定义链中,当以后想要修改针对web服务的入站规则时,就直接修改IN_WEB链中的规则就好了

[root@#quan#Better ~]$iptables -t filter -I INPUT_WEB -s 192.168.31.42 -j ACCEPT
[root@#quan#Better ~]$iptables -t filter -I INPUT_WEB -s 192.168.31.88 -j DROP
[root@#quan#Better ~]$iptables -vnL  INPUT_WEB
Chain INPUT_WEB (0 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       all  --  *      *       192.168.31.88        0.0.0.0/0           
    0     0 ACCEPT     all  --  *      *       192.168.31.42        0.0.0.0/0  
INPUT_WEB链是为了针对web服务的入站规则而创建的,那么这些规则应该去匹配入站的报文,所以,我们应该用INPUT链去引用它。
当然,自定义链在哪里创建,应该被哪条默认链引用,取决于实际的工作场景,因为此处示例的规则是匹配入站报文,所以在INPUT链中引用自定义链

 

[root@#quan#Better ~]$iptables -I INPUT -p tcp --dport 80 -j INPUT_WEB
[root@#quan#Better ~]$iptables -vnL  
Chain INPUT (policy ACCEPT 18 packets, 1536 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 INPUT_WEB  tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:80 

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 14 packets, 1552 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT_WEB (1 references)#引用计数已经变为1,表示这条自定义链已经被引用了1次,自定义链还可以引用其他的自定义链
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       all  --  *      *       192.168.31.88        0.0.0.0/0           
    0     0 ACCEPT     all  --  *      *       192.168.31.42        0.0.0.0/0       

述规则中的"-j INPUT_WEB"表示:访问80端口的tcp报文将由自定义链"INPUT_WEB"中的规则进行处理,没错,在之前的示例中,我们使用"-j"选项指定动作,而此处,我们将"动作"替换为了"自定义链",当"-j"对应的值为一个自定义链时,就表示被当前规则匹配到的报文将交由对应的自定义链处理,具体怎样处理,取决于自定义链中的规则

[root@#quan#Better ~]$iptables -E INPUT_WEB WWW  #使用-E 可以修改自定义的链的名字
[root@#quan#Better ~]$iptables -vnL  
Chain INPUT (policy ACCEPT 6 packets, 432 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 WWW        tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:80 

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 3 packets, 392 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain WWW (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       all  --  *      *       192.168.31.88        0.0.0.0/0           
    0     0 ACCEPT     all  --  *      *       192.168.31.42        0.0.0.0/0           
[root@#quan#Better ~]$iptables -X WWW  #删除自定义链
iptables: Too many links.             #因为被其他默认链引用所有,删除不了
[root@#quan#Better ~]$iptables -D INPUT 1   #先除去引用自定义链的规则
[root@#quan#Better ~]$iptables -X WWW
iptables: Directory not empty.          #因为不是空的自定义链,需要清空
[root@#quan#Better ~]$iptables -vnL  
Chain INPUT (policy ACCEPT 29 packets, 9076 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 27 packets, 2332 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain WWW (0 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       all  --  *      *       192.168.31.88        0.0.0.0/0           
    0     0 ACCEPT     all  --  *      *       192.168.31.42        0.0.0.0/0           
[root@#quan#Better ~]$iptables -t filter -F WWW #先清空自定义链的规则,
[root@#quan#Better ~]$iptables -vnL  
Chain INPUT (policy ACCEPT 6 packets, 432 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 4 packets, 496 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain WWW (0 references)
 pkts bytes target     prot opt in     out     source               destination         
[root@#quan#Better ~]$iptables -X WWW #删除,因为-X只能删除空的,未被引用的自定义链

  

 

 

 

最后的警告:

1、规则的顺序非常重要。
  如果报文已经被前面的规则匹配到,IPTABLES则会对报文执行对应的动作,通常是ACCEPT或者REJECT,报文被放行或拒绝以后,即使后面的规则也能匹配到刚才放行或拒绝的报文,
  也没有机会再对报文执行相应的动作了(前面规则的动作为LOG时除外),所以,针对相同服务的规则,更严格的规则应该放在前面。
2、当规则中有多个匹配条件时,条件之间默认存在""的关系。   如果一条规则中包含了多个匹配条件,那么报文必须同时满足这个规则中的所有匹配条件,报文才能被这条规则匹配到。 3、在不考虑1的情况下,应该将更容易被匹配到的规则放置在前面。   比如,你写了两条规则,一条针对sshd服务,一条针对web服务。   假设,一天之内,有20000个请求访问web服务,有200个请求访问sshd服务,   那么,应该将针对web服务的规则放在前面,针对sshd的规则放在后面,因为访问web服务的请求频率更高。   如果将sshd的规则放在前面,当报文是访问web服务时,sshd的规则也要白白的验证一遍,由于访问web服务的频率更高,白白耗费的资源就更多。   如果web服务的规则放在前面,由于访问web服务的频率更高,所以无用功会比较少。   换句话说就是,在没有顺序要求的情况下,不同类别的规则,被匹配次数多的、匹配频率高的规则应该放在前面。 4、当IPTABLES所在主机作为网络防火 墙时,在配置规则时,应着重考虑方向性,双向都要考虑,从外到内,从内到外。 5、在配置IPTABLES白名单时,往往会将链的默认策略设置为ACCEPT,通过在链的最后设置REJECT规则实现白名单机制,而不是将链的默认策略设置为DROP,
  如果将链的默认策略设置为DROP,当链中的规则被清空时,管理员的请求也将会被DROP掉。

 

 

 

posted @ 2019-11-22 13:11  linux——quan  阅读(410)  评论(0编辑  收藏  举报