iptables总结
声明:朱双印个人日志-iptables学习总结详解。附原链接:https://www.zsythink.net/archives/tag/iptables/
文章目录
- iptables总结
-
- 1. 防火墙相关概念
-
- 1.1 逻辑区分
- 1.2 物理区分
- 2. iptables和netfilter
-
- 2.1 iptables和netfilter的关系
- 2.2 netfilter的作用
- 3. 报文流向
- 4. 关卡,链与表
-
- 4.1 关卡
- 4.2 链
- 4.3 表
- 5. 规则
-
- 5.1 规则查询
-
- 5.1.1 查询表中的所有链
- 5.1.2 查询表中的指定链
- 5.2 规则管理
-
- 5.2.1 规则添加
- 5.2.2 规则删除
-
- 5.2.2.1 编号删除
- 5.2.2.2 删除匹配条件和动作
- 5.2.2.3 全链删除
- 5.2.2.4 全表删除
- 5.2.3 规则修改
-
- 5.2.3.1 单规则修改
- 5.2.3.2 修改链条的默认策略
- 5.2.4 规则保存
- 5.3 匹配条件
-
- 5.3.1 使用匹配条件
- 5.3.2 基本匹配条件
- 5.3.3 扩大匹配条件
-
- 5.3.3.1 隐式扩展
- 5.3.3.2 显式扩展
- 5.4 处理动作
-
- 5.4.1 基本处理动作
- 5.4.2 扩展处理动作
- 5.5 自定义链
-
- 5.5.1 创建自定义链
- 5.5.2 删除自定义链
- 5.6 黑白名单
-
- 5.6.1 白名单
- 5.6.2 黑名单
- 5.7 网络防火墙
1. 防火墙相关概念
1.1 逻辑区分
防火墙可分为主机防火墙和网络防火墙
主机防火墙:保护单个主机
网络防火墙:位于网络入口或边缘
网络防火墙***主外***(集体)主机防火墙***主内***(个人)
1.2 物理区分
防火墙可分为硬件防火墙和软件防火墙
2. iptables和netfilter
2.1 iptables和netfilter的关系
iptables不是真正的防火墙,而是客户代理。用户通过iptables该代理将用户的安全设置执行到相应的安全框架中。这个安全框架是真正的防火墙。这个框架是netfilter
netfilter位于核心空间的防火墙真正的安全框架
iptables使用此命令行工具操作真实框架是一种命令行工具
2.2 netfilter的作用
netfilter是linux系统核心层内的数据处理模块具有以下功能:
- 网络地址转换(NAT)
- 数据包内容修改
- 数据包过滤
3. 报文流向
当我们使用防火墙功能时,根据实际情况,报纸的链可能会有所不同。如果需要报纸***转发***,所以报文不会通过input链发送到用户空间,但直接通过核心空间forward链和postrouting链转发
因此,根据上图,我们可以想象报纸的常用场景和流向
- 到本机某进程的报文:prerouting->input
- 本机转发的报文:prerouting->forward->postrouting
- 报文由本机的某个过程发出(通常是响应报文):output->postrouting
4. 关卡,链与表
4.1 关卡
有些链注定不包含某些规则,就像有些链一样自然没有某些功能,prerouting即为关卡
这张图是什么意思?它的意思是,prerouting只有链nat表、raw表和mangle所以,prerouting规则只能存储在中间nat表、raw表和mangle表中
4.2 链
在iptables中,关卡上的规则被称为“,为什么?当报纸通过这些关卡时,它必须与关卡上的规则相匹配。然而,这个关卡可能有不止一条规则,但有许多规则。当我们把这些规则串在一个链上时,就会形成一个链
4.3 表
是相同功能规则的集合。iptables定义了四种表,每种表对应不同的功能
- filter表 --负责过滤功能;iptable_filter
- nat表 --网络地址转换功能;iptable_nat
- mangle表 --拆解报文,修改并重新包装;iptable_mangle
- raw表 --关闭nat表面启用的连接跟踪机制;iptable_raw
关卡下有表,表里面有规则,规则按照从上往下的顺序形成了“链”
然后,在上述报文流向图中的关卡加上表格后,它就变成了下面的关卡
5. 规则
iptables按规则操作。所谓规则,就是如果数据包头符合这样的条件,就这样处理数据包
规则存储在信息报过滤表中,分别指定源地址、目的地址、传输协议和服务类型(HTTP、FTP和SMTP)等 该规则还规定了处理符合条件的数据***动作***,如放行(accept)、拒绝(reject)和丢弃(drop)等
if(满足条件) { 执行动作 }
主要配置防火墙作就是添加、修改和删除这些规则
5.1 规则查询
5.1.1 查询表中的所有链
# iptables -t filter -L
# iptables -t raw -L
# iptables -t mangle -L
# iptables -t nat -L
-t选项指定要操作的表(-t省略默认查询filter表),-L选项的意思是列出规则,所以上述命令的含义为列出filter表的所有规则。以下面命令的操作结果的第一条Chain(链)为例,Chain INPUT(policy )表示filter表中的第一条INPUT的链,它的默认策略为ACCEPT(即报文进入INPUT链中,当下列的规则都不满足的时候,ACCEPT)
从下面的命令执行结果可以看出,INPUT,FORWARD,OUTPUT链都拥有“过滤”的能力,所以当我们需要定义某条“过滤”的规则时,我们会在filter表中定义,但是具体在哪一条链上定义呢?比如说我们需要禁止某个IP访问我们的主机,我们则需要在INPUT链上定义;因为如果想禁止某些进入主机的报文,我们只能在PREROUTING和INPUT表中添加规则,但是PREROUTING关卡中没有filter表,所以只能在filter表的INPUT链中添加规则
/ # iptables -t filter -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:68
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:68
srvcntrl all -- 0.0.0.0/0 0.0.0.0/0
lan_access all -- 0.0.0.0/0 0.0.0.0/0
fwports all -- 0.0.0.0/0 0.0.0.0/0
firewall all -- 0.0.0.0/0 0.0.0.0/0
wan_access all -- 0.0.0.0/0 0.0.0.0/0
srvdrop all -- 0.0.0.0/0 0.0.0.0/0
srvctlext all -- 0.0.0.0/0 0.0.0.0/0
fwinput all -- 0.0.0.0/0 0.0.0.0/0
devaccrt all -- 0.0.0.0/0 0.0.0.0/0
Chain FORWARD (policy ACCEPT)
target prot opt source destination
drop_no_nat all -- 0.0.0.0/0 0.0.0.0/0
urldoor all -- 0.0.0.0/0 0.0.0.0/0
webfilter all -- 0.0.0.0/0 0.0.0.0/0
wfmode all -- 0.0.0.0/0 0.0.0.0/0
macfilter all -- 0.0.0.0/0 0.0.0.0/0
upnp all -- 0.0.0.0/0 0.0.0.0/0
algfilter all -- 0.0.0.0/0 0.0.0.0/0
ipfilter all -- 0.0.0.0/0 0.0.0.0/0
firewall all -- 0.0.0.0/0 0.0.0.0/0
portmapp all -- 0.0.0.0/0 0.0.0.0/0
dmzmapp all -- 0.0.0.0/0 0.0.0.0/0
pctrlfilter all -- 0.0.0.0/0 0.0.0.0/0
url_redir all -- 0.0.0.0/0 0.0.0.0/0
ACCRT_L3_FORWARD all -- 0.0.0.0/0 0.0.0.0/0
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain ACCRT_L3_FORWARD (1 references)
target prot opt source destination
Chain algfilter (1 references)
target prot opt source destination
Chain devaccrt (1 references)
target prot opt source destination
Chain dmzmapp (1 references)
target prot opt source destination
Chain drop_no_nat (1 references)
target prot opt source destination
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 state INVALID
Chain firewall (2 references)
target prot opt source destination
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 icmptype 8 DEVWL match:WANDEV
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 destination IP range 224.0.0.0-239.255.255.255
DROP all -- 0.0.0.0/0 0.0.0.0/0 state INVALID,NEW DEVWL match:WANDEV
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 DEVWL match:WANDEV
state NEW,RELATED,ESTABLISHED
DROP all -- 0.0.0.0/0 0.0.0.0/0 DEVWL match:WANDEV
Chain fwinput (1 references)
target prot opt source destination
Chain fwports (1 references)
target prot opt source destination
DROP udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:514
ACCEPT tcp -- 0.0.0.0/0 172.18.55.25 tcp dpt:58000
Chain ipfilter (1 references)
target prot opt source destination
Chain ipfilterinb (0 references)
target prot opt source destination
Chain ipfilterinw (0 references)
target prot opt source destination
DROP all -- 0.0.0.0/0 0.0.0.0/0
Chain ipfilteroutb (0 references)
target prot opt source destination
Chain ipfilteroutw (0 references)
target prot opt source destination
DROP all -- 0.0.0.0/0 0.0.0.0/0
Chain lan_access (1 references)
target prot opt source destination
Chain macfilter (1 references)
target prot opt source destination
Chain pctrlfilter (1 references)
target prot opt source destination
Chain portmapp (1 references)
target prot opt source destination
Chain srvcntrl (1 references)
target prot opt source destination
Chain srvctlext (1 references)
target prot opt source destination
REJECT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:53 DEVWL match:WANDEV
reject-with icmp-port-unreachable
REJECT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:67 DEVWL match:WANDEV
reject-with icmp-port-unreachable
REJECT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:68 DEVWL match:LANDEV
reject-with icmp-port-unreachable
REJECT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:137 DEVWL match:WANDEV
reject-with icmp-port-unreachable
REJECT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:138 DEVWL match:WANDEV
reject-with icmp-port-unreachable
REJECT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:161 reject-with icmp-port-unreachable
REJECT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:162 DEVWL match:WANDEV
reject-with icmp-port-unreachable
REJECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:58000 DEVWL match:LANDEV
reject-with tcp-reset
REJECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:17998 DEVWL match:WANDEV
reject-with tcp-reset
REJECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080 DEVWL match:WANDEV
reject-with tcp-reset
Chain srvdrop (1 references)
target prot opt source destination
REJECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 DEVWL match:WANDEV
reject-with tcp-reset
REJECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:21 DEVWL match:WANDEV
reject-with tcp-reset
REJECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:23 DEVWL match:WANDEV
reject-with tcp-reset
REJECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:443 DEVWL match:WANDEV
reject-with tcp-reset
Chain upnp (1 references)
target prot opt source destination
Chain url_redir (1 references)
target prot opt source destination
Chain urldoor (1 references)
target prot opt source destination
Chain wan_access (1 references)
target prot opt source destination
REJECT tcp -- 0.0.0.0/0 0.0.0.0/0 DEVWL match:WANDEV
tcp dpt:139 reject-with tcp-reset
REJECT tcp -- 0.0.0.0/0 0.0.0.0/0 DEVWL match:WANDEV
tcp dpt:23 reject-with tcp-reset
REJECT tcp -- 0.0.0.0/0 0.0.0.0/0 DEVWL match:WANDEV
tcp dpt:445 reject-with tcp-reset
Chain webfilter (1 references)
target prot opt source destination
Chain wfmode (1 references)
target prot opt source destination
5.1.2 查询表中的指定链
iptables -t filter -L INPUT
/ # iptables -t filter -L INPUT
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:68
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:68
srvcntrl all -- 0.0.0.0/0 0.0.0.0/0
lan_access all -- 0.0.0.0/0 0.0.0.0/0
fwports all -- 0.0.0.0/0 0.0.0.0/0
firewall all -- 0.0.0.0/0 0.0.0.0/0
wan_access all -- 0.0.0.0/0 0.0.0.0/0
srvdrop all -- 0.0.0.0/0 0.0.0.0/0
srvctlext all -- 0.0.0.0/0 0.0.0.0/0
fwinput all -- 0.0.0.0/0 0.0.0.0/0
devaccrt all -- 0.0.0.0/0 0.0.0.0/0
iptables -t iptables -vL INPUT
/ # iptables -t filter -vL INPUT
Chain INPUT (policy ACCEPT 31 packets, 1301 bytes)
pkts bytes target prot opt in out source destination
42 24192 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:68
0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:68
133K 19M srvcntrl all -- * * 0.0.0.0/0 0.0.0.0/0
133K 19M lan_access all -- * * 0.0.0.0/0 0.0.0.0/0
133K 19M fwports all -- * * 0.0.0.0/0 0.0.0.0/0
133K 19M firewall all -- * * 0.0.0.0/0 0.0.0.0/0
94992 13M wan_access all -- * * 0.0.0.0/0 0.0.0.0/0
94992 13M srvdrop all -- * * 0.0.0.0/0 0.0.0.0/0
94992 13M srvctlext all -- * * 0.0.0.0/0 0.0.0.0/0
94992 13M fwinput all -- * * 0.0.0.0/0 0.0.0.0/0
94992 13M devaccrt all -- * * 0.0.0.0/0 0.0.0.0/0
上述的-v选项输出后规则的各种信息如下
-
pkts:对应规则匹配到的报文的个数。
-
bytes:对应匹配到的报文包的大小总和。
-
target:规则对应的target,往往表示规则对应的”动作”,即规则匹配成功后需要采取的措施。
-
prot:表示规则对应的协议,是否只针对某些协议应用此规则。
-
opt:表示规则对应的选项。
-
in:表示数据包由哪个接口(网卡)流入,我们可以设置通过哪块网卡流入的报文需要匹配当前规则。
-
out:表示数据包由哪个接口(网卡)流出,我们可以设置通过哪块网卡流出的报文需要匹配当前规则。
-
source:表示规则对应的源头地址,可以是一个IP,也可以是一个网段。
-
destination:表示规则对应的目标地址。可以是一个IP,也可以是一个网段。
iptables -t filter --line-number -vL INPUT
/ # iptables -t filter --line-number -vL INPUT
Chain INPUT (policy ACCEPT 12 packets, 986 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:68
2 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:68
3 767 87334 srvcntrl all -- * * 0.0.0.0/0 0.0.0.0/0
4 767 87334 lan_access all -- * * 0.0.0.0/0 0.0.0.0/0
5 767 87334 fwports all -- * * 0.0.0.0/0 0.0.0.0/0
6 767 87334 firewall all -- * * 0.0.0.0/0 0.0.0.0/0
7 485 39962 wan_access all -- * * 0.0.0.0/0 0.0.0.0/0
8 485 39962 srvdrop all -- * * 0.0.0.0/0 0.0.0.0/0
9 485 39962 srvctlext all -- * * 0.0.0.0/0 0.0.0.0/0
10 485 39962 fwinput all -- * * 0.0.0.0/0 0.0.0.0/0
11 485 39962 devaccrt all -- * * 0.0.0.0/0 0.0.0.0/0
iptables -t filter --line-number -xvL INPUT
/ # iptables -t filter --line-number -xvL INPUT
Chain INPUT (policy ACCEPT 50 packets, 2130 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:68
2 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:68
3 1105 124653 srvcntrl all -- * * 0.0.0.0/0 0.0.0.0/0
4 1105 124653 lan_access all -- * * 0.0.0.0/0 0.0.0.0/0
5 1105 124653 fwports all -- * * 0.0.0.0/0 0.0.0.0/0
6 1105 124653 firewall all -- * * 0.0.0.0/0 0.0.0.0/0
7 627 47693 wan_access all -- * * 0.0.0.0/0 0.0.0.0/0
8 627 47693 srvdrop all -- * * 0.0.0.0/0 0.0.0.0/0
9 627 47693 srvctlext all -- * * 0.0.0.0/0 0.0.0.0/0
10 627 47693 fwinput all -- * * 0.0.0.0/0 0.0.0.0/0
11 627 47693 devaccrt all -- * * 0.0.0.0/0 0.0.0.0/0
5.2 规则管理
5.2.1 规则添加
网关地址192.168.1.1,测试机192.168.1.3,在192.168.1.1上添加一条规则用于将源地址为192.168.1.3的报文丢弃
iptables -t filter -I INPUT -s 192.168.1.3 -j DROP
该命令执行无回显
查看新添加的规则,可以看到对源ip为192.168…1.3的报文执行DROP操作
/ # iptables -t filter -vL INPUT
Chain INPUT (policy ACCEPT 57 packets, 2334 bytes)
pkts bytes target prot opt in out source destination
0 0 DROP all -- * * 192.168.1.3 0.0.0.0/0
0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:68
0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:68
2343 264K srvcntrl all -- * * 0.0.0.0/0 0.0.0.0/0
2343 264K lan_access all -- * * 0.0.0.0/0 0.0.0.0/0
2343 264K fwports all -- * * 0.0.0.0/0 0.0.0.0/0
2343 264K firewall all -- * * 0.0.0.0/0 0.0.0.0/0
1373 118K wan_access all -- * * 0.0.0.0/0 0.0.0.0/0
1373 118K srvdrop all -- * * 0.0.0.0/0 0.0.0.0/0
1373 118K srvctlext all -- * * 0.0.0.0/0 0.0.0.0/0
1373 118K fwinput all -- * * 0.0.0.0/0 0.0.0.0/0
1373 118K devaccrt all -- * * 0.0.0.0/0 0.0.0.0/0
此时从测试机ping 192.168.1.1,如图
5.2.2 规则删除
如果我们需要删除一条具体的规则,那么如何删除呢
5.2.2.1 编号删除
/ # iptables -t filter --line-number -vL INPUT
Chain INPUT (policy ACCEPT 25 packets, 1034 bytes)
num pkts bytes target prot opt in out source destination
1 185 15508 DROP all -- * * 192.168.1.3 0.0.0.0/0
2 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:68
3 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:68
4 4595 761K srvcntrl all -- * * 0.0.0.0/0 0.0.0.0/0
5 4595 761K lan_access all -- * * 0.0.0.0/0 0.0.0.0/0
6 4595 761K fwports all -- * * 0.0.0.0/0 0.0.0.0/0
7 4595 761K firewall all -- * * 0.0.0.0/0 0.0.0.0/0
8 3133 538K wan_access all -- * * 0.0.0.0/0 0.0.0.0/0
9 3133 538K srvdrop all -- * * 0.0.0.0/0 0.0.0.0/0
10 3133 538K srvctlext all -- * * 0.0.0.0/0 0.0.0.0/0
11 3133 538K fwinput all -- * * 0.0.0.0/0 0.0.0.0/0
12 3133 538K devaccrt all -- * * 0.0.0.0/0 0.0.0.0/0
iptables -t filter -D INPUT 1
/ # iptables -t filter --line-number -vL INPUT
Chain INPUT (policy ACCEPT 6 packets, 682 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:68
2 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:68
3 4700 774K srvcntrl all -- * * 0.0.0.0/0 0.0.0.0/0
4 4700 774K lan_access all -- * * 0.0.0.0/0 0.0.0.0/0
5 4700 774K fwports all -- * * 0.0.0.0/0 0.0.0.0/0
6 4700 774K firewall all -- * * 0.0.0.0/0 0.0.0.0/0
7 3172 540K wan_access all -- * * 0.0.0.0/0 0.0.0.0/0
8 3172 540K srvdrop all -- * * 0.0.0.0/0 0.0.0.0/0
9 3172 540K srvctlext all -- * * 0.0.0.0/0 0.0.0.0/0
10 3172 540K fwinput all -- * * 0.0.0.0/0 0.0.0.0/0
11 3172 540K devaccrt all -- * * 0.0.0.0/0 0.0.0.0/0
5.2.2.2 匹配条件与动作删除
/ # iptables -t filter --line-number -vL INPUT
Chain INPUT (policy ACCEPT 25 packets, 1034 bytes)
num pkts bytes target prot opt in out source destination
1 185 15508 DROP all -- * * 192.168.1.3 0.0.0.0/0
2 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:68
3 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:68
4 4595 761K srvcntrl all -- * * 0.0.0.0/0 0.0.0.0/0
5 4595 761K lan_access all -- * * 0.0.0.0/0 0.0.0.0/0
6 4595 761K fwports all -- * * 0.0.0.0/0 0.0.0.0/0
7 4595 761K firewall all -- * * 0.0.0.0/0 0.0.0.0/0
8 3133 538K wan_access all -- * * 0.0.0.0/0 0.0.0.0/0
9 3133 538K srvdrop all -- * * 0.0.0.0/0 0.0.0.0/0
10 3133 538K srvctlext all -- * * 0.0.0.0/0 0.0.0.0/0
11 3133 538K fwinput all -- * * 0.0.0.0/0 0.0.0.0/0
12 3133 538K devaccrt all -- * * 0.0.0.0/0 0.0.0.0/0
iptables -t filter -D INPUT -s 192.168.1.3 -j DROP
/ # iptables -t filter --line-number -vL INPUT
Chain INPUT (policy ACCEPT 7 packets, 288 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:68
2 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:68
3 5025 799K srvcntrl all -- * * 0.0.0.0/0 0.0.0.0/0
4 5025 799K lan_access all -- * * 0.0.0.0/0 0.0.0.0/0
5 5025 799K fwports all -- * * 0.0.0.0/0 0.0.0.0/0
6 5025 799K firewall all -- * * 0.0.0.0/0 0.0.0.0/0
7 3400 552K wan_access all -- * * 0.0.0.0/0 0.0.0.0/0
8 3400 552K srvdrop all -- * * 0.0.0.0/0 0.0.0.0/0
9 3400 552K srvctlext all -- * * 0.0.0.0/0 0.0.0.0/0
10 3400 552K fwinput all -- * * 0.0.0.0/0 0.0.0.0/0
11 3400 552K devaccrt all -- * * 0.0.0.0/0 0.0.0.0/0
5.2.2.3 全链删除
iptables -t 表名 -F 链名
5.2.2.4 全表删除
iptables -t 表名 -F
5.2.3 规则修改
5.2.3.1 单规则修改
iptables -t filter -R INPUT 1 -s 192.168.1.3 -j REJECT
5.2.3.2 链的默认策略修改
iptables -t filter -P FORWARD DROP
5.2.4 规则保存
service iptables restart
iptables-save > /etc/sysconfig/iptables
iptables-restore < /etc/sysconfig/iptables
5.3 匹配条件
5.3.1 匹配条件的用法
iptables -t filter -I INPUT -s 192.168.1.111,192.168.1.112 -j DROP
#iptables -t filter -L INPUT
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all -- 192.168.1.112 anywhere
DROP all -- 192.168.1.111 anywhere
wlan_access_rule_input all -- anywhere anywhere
srvcntrl all -- anywhere anywhere
lan_access all -- anywhere anywhere
fwports all -- anywhere anywhere
wan_access all -- anywhere anywhere
srvdrop all -- anywhere anywhere
fwinput all -- anywhere anywhere
firewall all -- anywhere anywhere
srvctlext all -- anywhere anywhere
devaccrt all -- anywhere anywhere
iptables -t filter -I INPUT -s 192.168.1.3 -d 192.168.1.1 -p tcp -j REJECT
#iptables -t filter -L INPUT
Chain INPUT (policy ACCEPT)
target prot opt source destination
REJECT tcp -- 192.168.1.3 192.168.1.1 reject-with icmp-port-unreachable
DROP all -- 192.168.1.112 anywhere
DROP all -- 192.168.1.111 anywhere
wlan_access_rule_input all -- anywhere anywhere
srvcntrl all -- anywhere anywhere
lan_access all -- anywhere anywhere
fwports all -- anywhere anywhere
wan_access all -- anywhere anywhere
srvdrop all -- anywhere anywhere
fwinput all -- anywhere anywhere
firewall all -- anywhere anywhere
srvctlext all -- anywhere anywhere
devaccrt all -- anywhere anywhere
iptables -t filter -A INPUT ! -s 192.168.1.3 -j ACCEPT
#iptables -t filter -L INPUT
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all -- 192.168.1.112 anywhere
DROP all -- 192.168.1.111 anywhere
wlan_access_rule_input all -- anywhere anywhere
srvcntrl all -- anywhere anywhere
lan_access all -- anywhere anywhere
fwports all -- anywhere anywhere
wan_access all -- anywhere anywhere
srvdrop all -- anywhere anywhere
fwinput all -- anywhere anywhere
firewall all -- anywhere anywhere
srvctlext all -- anywhere anywhere
devaccrt all -- anywhere anywhere
ACCEPT all -- !192.168.1.3 anywhere
5.3.2 基本匹配条件
-
s,–source address
iptables -t filter -A INPUT -s 192.168.1.3 -j DROP
iptables -t filter -A INPUT -s 192.168.1.0/24 ACCEPT
-
-d,–destination address
iptables -t filter -A INPUT -d 192.168.1.1 -j ACCEPT
-
-p,–protocol,可使用tcp,udp,icmp,icmpv6,udplite,esp,ah,sctp,mh,all
iptables -t filter -A INPUT -p tcp -j DROP
-
-i, --in-interface name
iptables -t filter -A INPUT -i eth4 -j DROP
-
-o, --out-interface name
iptables -t filter -A OUTPUT -o eth4 -j DROP
5.3.3 扩展匹配条件
centos查看扩展模块:man iptables-extensions
5.3.3.1 隐式扩展
–使用-p选项指明了特定的协议时,无需再用-m选项指明扩展模块的扩展机制
- tcp协议的扩展选项
-
-sport; --source port;匹配报文源端口,可为端口连续范围
iptables -t filter -I INPUT -s 192.168.1.3 -p --sport 22 -j REJECT
iptables -t filter -I INPUT -s 192.168.1.3 -p --sport 22:28 -j REJECT
iptables -t filter -I INPUT -s 192.168.1.3 -p --sport 28: -j REJECT
-
-dport; --destination port;匹配报文目标端口,可为连续范围
iptables -t filter -I INPUT -s 192.168.1.3 -p --dport 22,23 -j REJECT
iptables -t filter -I INPUT -s 192.168.1.3 -p --dport 22:28 -j REJECT
iptables -t filter -I INPUT -s 192.168.1.3 -p --dport 28: -j REJECT
-
–tcp-flags; --tcp的标志位,第一部分为需要匹配的标志位,第二部分为需要设为1的标志位,以***空格***分隔(三次握手中,第一次SYN=1,其余为0;第二次握手SYN=1,ACK=1,其余为0;可以用ALL表示SYN,ACK,FIN,RST,URG,PSH)
iptables -t filter -I INPUT -s 192.168.1.3 -p tcp --dport 22 --tcp-flags SYN,ACK,FIN,RST,URG,PSH, SYN -j REJECT
iptables -t filter -I INPUT -s 192.168.1.3 -p tcp --dport 22 --tcp-flags ALL SYN -j REJECT
-
–syn; --用于匹配第一次握手,相当于:-tcp-flags SYN,ACK,FIN,RST SYN
iptables -t filter -I INPUT -s 192.168.1.3 -p tcp --dport 22 --tcp-flags --syn -j REJECT
-
-
udp协议的扩展选项
-
–sport
iptables -t filter -I INPUT -p udp --sport 137 -j ACCEPT
-
–dport
iptables -t filter -I INPUT -p udp --dport 137 -j ACCEPT
-
-
icmp协议的扩展选项
- [type/code]
- 0/0 echo-reply icmp应答
- 8/0 echo-request icmp请求
iptables -t filter -I INPUT -p icmp --icmp-type 8/0 -j REJECT
iptables -t filter -I INPUT -p icmp --icmp-type 0/0 -j REJECT
iptables -t filter -I INPUT -p icmp --icmp-type "echo-request" -j REJECT
- [type/code]
5.3.3.2 显式扩展
查看扩展模块:man iptables-extensions
-
multiport –离散方式指定多端口匹配,最多匹配15个端口
iptables -t filter -I INPUT -s 192.168.1.3 -p tcp -m multiport --dport 22,36,80 -j DROP
-
iprange --指明连续的ip地址范围
iptables -t filter -I INPUT -m iprange --src-range 192.168.1.3-192.168.1.10 -j DROP
- mac --指明源mac地址,适用于:PREROUTING,FORWARD,INPUT chains
`iptables -t filter -I INPUT -p tcp --dport 22 -m mac --mac-source 04:ED:33:E3:C1:F6 -j DROP`
-
string扩展 --对报文中的应用层数据做字符串模式匹配检测
- –algo{bm|kmp}字符串匹配检测算法
- bm:Boyer-Moore
- kmp:Knuth-Pratt-Morris
- –from offset 开始偏移
- –to offset 结束偏移
iptables -t filter -I INPUT -m string --algo bm --string "OOXX" -j REJECT
- –algo{bm|kmp}字符串匹配检测算法
-
time扩展 --根据报文到达的时间与指定的时间范围进行匹配(–monthdays与–weekdays可以使用”!”取反,其他选项不能取反)
- –datastart YYYY[-MM[-DD[Thh[:mm[:ss]]]]] 日期
- –datastop YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
- –timestart hh:mm:[:ss] 时间
- –timestop hh:mm[:ss]
iptables -t filter -I OUTPUT -p tcp --dport 80 -m time --timestart 09:00:00 --timestop 18:00:00 -j REJECT
iptables -t filter -I OUTPUT -p tcp --dport 443 -m time --timestart 09:00:00 --timestop 18:00:00 -j REJECT
iptables -t filter -I OUTPUT -p tcp --dport 80 -m time --weekdays 6,7 -j REJECT
iptables -t filter -I OUTPUT -p tcp --dport 443 -m time --timestart 09:00:00 --timestop 18:00:00 --weekdays 6,7 -j REJECT
iptables -t filter -I OUTPUT -p tcp --dport 80 -m time --timestart 09:00:00 --timestop 18:00:00 monthdays 22,23 -j REJECT
-
connlimit扩展 --根据每客户端IP做并发连接书数量匹配,可防止Dos(Denial of Service)攻击
- connlinit-upto x;连接的数量小于等于x时匹配
- conlimit-above x;连接的数量大于x时匹配
iptables -t filter -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 2 -j REJECT
-
limit扩展 --基于收发报文的速率做匹配,即限制单位时间内流入的包的数量(令牌桶)
- –limit x[/second|/minute|/hour|/day] 如果为x/minute,就是每分钟最多只放行x个包,即每1/x分钟最多只放行1个包
- –limit -burst x 前x个包不受限制
iptables -t filter -I INPUT -p icmp -m limit --limit-burst 3 --limit 10/minute -j ACCEPT
-
state扩展 --根据报文的状态来匹配
- NEW --新连接的第一个包的状态为NEW
- ESTABLISHED --把NEW状态后面的包的状态理解为ESTABLISHED,表示连接已建立
- RELATED --如ftp协议的“数据连接”的报文与“命令连接”的报文是有关系的
- INVALID --一个包没有没有办法被识别,或者这个包没有任何状态
- UNTRACKED --报文无法找到相关的连接
iptables -t filter -A INPUT -m state --satate RELATED,ESTABLISHED -j ACCEPT
5.4 处理动作
–处理动作与匹配条件一样,有“基础”与“扩展”之分,同样,使用扩展动作也需要借助扩展模块,但是,扩展动作可以***直接使用***,不用像使用”扩展匹配条件”那样指定特定的模块
5.4.1 基础处理动作
- ACCEPT --允许数据包通过
- DROP --直接丢弃数据包;客户端会感觉自己的请求泥牛入海了,过了超时时间才会有反应
5.4.2 扩展处理动作
-
REJECT --拒绝数据包通过,必要时会给数据发送端一个响应的信息,客户端刚请求就会收到拒绝的信息(该信息默认为icmp-port unreachable)
iptables -t filter -I INPUT -j REJECT --reject-with icmp-host-unreachable
-
SNAT --源地址转换,解决内网用户用同一个公网地址上网的问题(SNAT和DNAT的区别是整个过程的前半段使用了SNAT还是DNAT)
iptables -t nat -A POSTROUTING -s 10.1.0.0/16 -j SNAT --to-source 192.168.1.3
-
MASQUERADE --是SNAT的一种特殊形式,适用于动态的,临时会变的ip上(不用指定映射的IP,会动态的指定为网卡上的可用ip)
iptables -t nat -I POSTROUTING -s 10.1.0.0/16 -o eno50332184 -j MASQUERADE
-
DNAT --目标地址转换,用于访问内网中的服务器(如果配置完成后不能访问,需要配置对应的SNAT规则)
iptables -t nat PREROUTING -d 192.168.1.3 -p tcp --dport 3389 -j DNAT --to-destination 10.1.0.3:3389
-
REDIRECT --在本机做端口映射
iptables -t nat -A PREROUTING -p tcp –dport 80 -j REDIRECT –to-ports 8080
-
LOG --在/var/log/messages文件中记录日志信息,然后将数据包传递给下一条规则。
iptables -t filter -I INPUT -p tcp --dport 22 -j LOG
iptables -t filter -I INPUT -p tcp --dport 22 -m state NEW -j LOG --log-prefix "want-in-from-port-22"
标签”信息
5.5 自定义链
5.5.1 创建自定义链
可以用自定义链将同一类型的规则存放到一起,方便查看和管理
如,将针对80端口的入站规则写入IN_WEB自定义链中;将针对sshd的出站规则放入到OUT_SSH自定义链中
自定义链不能直接使用,而是需要被默认链引用才能够使用
综上,自定义链创建的三个过程如下
- 创建自定义链
- 给自定义链添加规则
- 添加引用
iptables -t filter -N IN_WEB
iptables -t filter -I IN_WEB -s 192.168.1.3 -j REJECT
iptables -t filter -I INPUT -p tcp --dport 80 -j IN_WEB
iptables -t filter -E IN_WEB WEB
5.5.2 删除自定义链
删除自定义链的过程
- 删除规则
- 删除引用
- 删除自定义链
iptables -t filter -F IN_WEB
iptables -t filter -D INPUT 1
iptables -t filter -X IN_WEB
5.6 黑白名单
通过规则与默认策略的配合可以实现黑白名单机制
白名单机制更安全,黑名单机制更灵活
5.6.1 白名单
默认策略设置为DROP的缺点:在对应的链中没有设置任何规则时,这样使用默认策略为DROP是非常不明智的,因为管理员也会把自己拒之门外,即使对应的链中存在放行规则,当我们不小心使用”iptables -F”清空规则时,放行规则被删除,则所有数据包都无法进入,这个时候就相当于给管理员挖了个坑
iptables -t filter -P INPUT ACCEPT
iptables -t filter -I INPUT -p tcp --dport 22 -j ACCEPT
iptables -t filter -I INPUT -p tcp --dport 80 -j ACCEPT
iptables -t filter -A INPUT -j REJECT
5.6.2 黑名单
黑名单即为将默认策略设置为ACCEPT,添加REJECT或者DROP的规则
5.7 网络防火墙
上述的规则配置都是基于主机防火墙,如果需要配置网络防火墙,根据下图,我们需要配置的链变成了FORWARD表(POSTROUTING没有filter表)