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 ACCEPT)表示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/0Chain 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/0Chain OUTPUT (policy ACCEPT)
target     prot opt source               destinationChain ACCRT_L3_FORWARD (1 references)
target     prot opt source               destinationChain algfilter (1 references)
target     prot opt source               destinationChain devaccrt (1 references)
target     prot opt source               destinationChain dmzmapp (1 references)
target     prot opt source               destinationChain drop_no_nat (1 references)
target     prot opt source               destination
DROP       tcp  --  0.0.0.0/0            0.0.0.0/0            state INVALIDChain firewall (2 references)
target     prot opt source               destination
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0            icmptype 8 DEVWL match:WANDEVACCEPT     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:WANDEVACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            DEVWL match:WANDEVstate NEW,RELATED,ESTABLISHED
DROP       all  --  0.0.0.0/0            0.0.0.0/0            DEVWL match:WANDEVChain fwinput (1 references)
target     prot opt source               destinationChain 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:58000Chain ipfilter (1 references)
target     prot opt source               destinationChain ipfilterinb (0 references)
target     prot opt source               destinationChain ipfilterinw (0 references)
target     prot opt source               destination
DROP       all  --  0.0.0.0/0            0.0.0.0/0Chain ipfilteroutb (0 references)
target     prot opt source               destinationChain ipfilteroutw (0 references)
target     prot opt source               destination
DROP       all  --  0.0.0.0/0            0.0.0.0/0Chain lan_access (1 references)
target     prot opt source               destinationChain macfilter (1 references)
target     prot opt source               destinationChain pctrlfilter (1 references)
target     prot opt source               destinationChain portmapp (1 references)
target     prot opt source               destinationChain srvcntrl (1 references)
target     prot opt source               destinationChain 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:WANDEVreject-with icmp-port-unreachable
REJECT     udp  --  0.0.0.0/0            0.0.0.0/0            udp dpt:67 DEVWL match:WANDEVreject-with icmp-port-unreachable
REJECT     udp  --  0.0.0.0/0            0.0.0.0/0            udp dpt:68 DEVWL match:LANDEVreject-with icmp-port-unreachable
REJECT     udp  --  0.0.0.0/0            0.0.0.0/0            udp dpt:137 DEVWL match:WANDEVreject-with icmp-port-unreachable
REJECT     udp  --  0.0.0.0/0            0.0.0.0/0            udp dpt:138 DEVWL match:WANDEVreject-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:WANDEVreject-with icmp-port-unreachable
REJECT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:58000 DEVWL match:LANDEVreject-with tcp-reset
REJECT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:17998 DEVWL match:WANDEVreject-with tcp-reset
REJECT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:8080 DEVWL match:WANDEVreject-with tcp-resetChain 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:WANDEVreject-with tcp-reset
REJECT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:21 DEVWL match:WANDEVreject-with tcp-reset
REJECT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:23 DEVWL match:WANDEVreject-with tcp-reset
REJECT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:443 DEVWL match:WANDEVreject-with tcp-resetChain upnp (1 references)
target     prot opt source               destinationChain url_redir (1 references)
target     prot opt source               destinationChain urldoor (1 references)
target     prot opt source               destinationChain wan_access (1 references)
target     prot opt source               destination
REJECT     tcp  --  0.0.0.0/0            0.0.0.0/0            DEVWL match:WANDEVtcp dpt:139 reject-with tcp-reset
REJECT     tcp  --  0.0.0.0/0            0.0.0.0/0            DEVWL match:WANDEVtcp dpt:23 reject-with tcp-reset
REJECT     tcp  --  0.0.0.0/0            0.0.0.0/0            DEVWL match:WANDEVtcp dpt:445 reject-with tcp-resetChain webfilter (1 references)
target     prot opt source               destinationChain wfmode (1 references)
target     prot opt source               destination

5.1.2 查询表中的指定链

iptables -t filter -L INPUT

即在-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

加上-v选项,可以显示链的更完整的信息

/ # iptables -t filter -vL INPUT
Chain INPUT (policy ACCEPT 31 packets, 1301 bytes)pkts bytes target     prot opt in     out     source               destination42 24192 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0            udp dpt:680     0 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0            udp dpt:68133K   19M srvcntrl   all  --  *      *       0.0.0.0/0            0.0.0.0/0133K   19M lan_access  all  --  *      *       0.0.0.0/0            0.0.0.0/0133K   19M fwports    all  --  *      *       0.0.0.0/0            0.0.0.0/0133K   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

加上–line-number选项可以给规则显示行号

/ # 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

加上-x选项可以使加上-v选项显示的xxxx bytes显示为具体的数字,而不是可读性高的k,m,g等,通常可以在调试时使用

/ # 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

该命令执行无回显

-I选项指插入规则(插入到首部),-A选项插入规则到尾部。-s选项指源地址,-j选项指满足条件时执行的动作

查看新添加的规则,可以看到对源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               destination0     0 DROP       all  --  *      *       192.168.1.3          0.0.0.0/00     0 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0            udp dpt:680     0 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0            udp dpt:682343  264K srvcntrl   all  --  *      *       0.0.0.0/0            0.0.0.0/02343  264K lan_access  all  --  *      *       0.0.0.0/0            0.0.0.0/02343  264K fwports    all  --  *      *       0.0.0.0/0            0.0.0.0/02343  264K firewall   all  --  *      *       0.0.0.0/0            0.0.0.0/01373  118K wan_access  all  --  *      *       0.0.0.0/0            0.0.0.0/01373  118K srvdrop    all  --  *      *       0.0.0.0/0            0.0.0.0/01373  118K srvctlext  all  --  *      *       0.0.0.0/0            0.0.0.0/01373  118K fwinput    all  --  *      *       0.0.0.0/0            0.0.0.0/01373  118K devaccrt   all  --  *      *       0.0.0.0/0            0.0.0.0/0

此时从测试机ping 192.168.1.1,如图

注:iptables执行结果与规则的顺序有关,如对同一个条件,前一个动作为DROP,后一个动作为ACCEPT,则DROP;同理,前一个ACCEPT,后一个DROP,则DROP


5.2.2 规则删除

如果我们需要删除一条具体的规则,那么如何删除呢

  • 根据规则的编号来删除
  • 根据具体的匹配条件与动作删除规则

5.2.2.1 编号删除

首先查看规则,注意使用–line-number选项,然后删除对用编号的规则

/ # 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

然后删除编号为1的规则

iptables -t filter -D INPUT 1

-D选项指删除,该命令没有回显

再来查看一下

/ # 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

删除规则,根据-s和-j来匹配规则

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 单规则修改

将新建的“对源地址为192.168.1.3的报文丢弃”的规则进行修改,将动作改为拒绝

iptables -t filter -R INPUT 1 -s 192.168.1.3 -j REJECT

-R选项表明是对规则的修改,1为规则编号,-s指定条件,-j指定动作,该命令无回显;该命令需要指定条件与动作,如果有多个条件均需要列出


5.2.3.2 链的默认策略修改

iptables -t filter -P FORWARD DROP

使用-P选项来修改链的默认策略


5.2.4 规则保存

iptables对规则、表、链做的修改如果不保存,重启iptables或者重启主机,修改就会消失

centos 6中,使用service iptables save即可保存规则,规则默认保存在/etc/sysconfig/iptables文件中

centos 6iptables重启:

service iptables restart

另外一种保存的方法,iptables-save命令会将当前的iptables规则以“保存后的格式”输出到屏幕上,所以我们可以配合重定向保存iptables配置,如下:

iptables-save > /etc/sysconfig/iptables

我们也可以将/etc/sysconfig/iptables中饿规则重新载入,如下:

iptables-restore < /etc/sysconfig/iptables


5.3 匹配条件

5.3.1 匹配条件的用法

  • 指定匹配条件时,相同条件的多个(如都是-s),可以一次指定,用逗号隔开即可,执行完毕会有多个规则被添加

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

注意这句规则的意思是,满足不是192.168.1.3的就ACCEPT,否则,执行默认策略;也就是说192.168.1.3会执行默认策略,也就是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 扩展匹配条件

如果要使用扩展匹配条件,必须要用-m选项指定相应的扩展模块

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


5.3.3.2 显式扩展

使用显式扩展必须使用-m选项指明要调用的扩展模块名称

查看扩展模块: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


  • 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”选项就是用于指定”多长时间生成一个新令牌的”,”–limit-burst”选项就是用于指定”木桶中最多存放几个令牌的”

    • –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"

    –log-prefix可以给记录到的相关信息添加“标签”信息

    –log-level可以指定记录日志的日志级别,可用级别有emerg,alert,crit,error,warning,notice,info,debug


5.5 自定义链

5.5.1 创建自定义链

可以用自定义链将同一类型的规则存放到一起,方便查看和管理

如,将针对80端口的入站规则写入IN_WEB自定义链中;将针对sshd的出站规则放入到OUT_SSH自定义链中

自定义链不能直接使用,而是需要被默认链引用才能够使用


综上,自定义链创建的三个过程如下

  1. 创建自定义链
  2. 给自定义链添加规则
  3. 添加引用

iptables -t filter -N IN_WEB

创建自定义链,-N选项可以创建自定义链

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

添加自定义链的引用,即把-j选项的target改为自定义链

iptables -t filter -E IN_WEB WEB

-E选项指的是将自定义链改名


5.5.2 删除自定义链

删除自定义链的过程

  1. 删除规则
  2. 删除引用
  3. 删除自定义链

iptables -t filter -F IN_WEB

-F删除链中的所有规则

iptables -t filter -D INPUT 1

删除INPUT链中第一条规则(target为IN_WEB)

iptables -t filter -X IN_WEB

-X选项删除自定义链


5.6 黑白名单

通过规则与默认策略的配合可以实现黑白名单机制

白名单机制更安全,黑名单机制更灵活

5.6.1 白名单

默认策略设置为DROP的缺点:在对应的链中没有设置任何规则时,这样使用默认策略为DROP是非常不明智的,因为管理员也会把自己拒之门外,即使对应的链中存在放行规则,当我们不小心使用”iptables -F”清空规则时,放行规则被删除,则所有数据包都无法进入,这个时候就相当于给管理员挖了个坑

所以,我们如果想要使用”白名单”的机制,最好将链的默认策略保持为”ACCEPT”,然后将”拒绝所有请求”这条规则放在链的尾部,将”放行规则”放在前面,这样做,既能实现”白名单”机制,又能保证在规则被清空时,管理员还有机会连接到主机


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表)

iptables学习总结相关推荐

  1. Linux防火墙iptables学习

    http://blog.chinaunix.net/uid-9950859-id-98277.html 要在网上传输的数据会被分成许多小的数据包,我们一旦接通了网络,会有很多数据包进入,离开,或者经过 ...

  2. iptables学习笔记:使用NAT实现简单的无线AP

    之前使用的是无线路由让手机上网.学习了iptables后,尝试在非openwrt系统的Linux上实现相同功能.本文简单记录一下. 手上有块X86的板子,上面安装了Linux系统.几个月前研究了WIF ...

  3. iptables学习笔记:同一端口号同时支持tcp和udp的转发

    前段时间,某项目中遇到一个端口转发问题,虽然我无缘参与项目,但由于项目使用到的一个平台恰好是我前不久搞过的,所以最终还是找我,于是中断了正在进行的任务进行协助.他们定位到只有udp无法转发成功,而tc ...

  4. centos Iptables学习笔记

    一.防火墙,iptables和netfilter定义 防火墙:是由软件和硬件设备组合而成的一种隔离技术,它工作于网络或主机的边缘(通信报文的进出口),对于进出本网络或主机的数据包根据事先定义的检测规则 ...

  5. Netfilter和iptables学习总结

    最近一段时间一直在学习netfilter和iptables相关的知识.首先要感谢我的老大能给我时间.给我机会来学习.写这篇总结就只有一个目的,总j结近来学到的知识,理清思路,查漏补缺.看了半个多月的基 ...

  6. IPTABLES学习摘抄

    为什么80%的码农都做不了架构师?>>>    //----------------------------------------------------------------- ...

  7. 摘自ubantuer-Linux防火墙iptables学习笔记(三)iptables命令详解和举例

    网上看到这个配置讲解得还比较易懂,就转过来了,大家一起看下,希望对您工作能有所帮助. 网管员的安全意识要比空喊Linux安全重要得多. iptables -F iptables -X iptables ...

  8. 路由及iptables学习笔记

    启动linux路由功能 echo "echo 1 >/proc/sys/net/ipv4/ip_forward" >> /etc/rc.d/rc.local 或者 ...

  9. iptables学习笔记

    1.数据包的流向 数据包在主机上有三个流向: a.发往本机:从本机的内核空间流向用户空间(应用程序) b.本机发出:从本机的用户空间流向内核空间,在经过网卡流出 c.转发:从本机的一个网卡进来,从另外 ...

最新文章

  1. C++与.net的编译方式
  2. jq判断滚动条向上还是向下
  3. oracle共享时监听,Oracle监听---共享连接参数配置介绍
  4. 信息系统项目管理知识--项目成本管理
  5. kibana操作elasticsearch:查看映射关系
  6. 汇编语言——100个数中的最大数
  7. 深入了解一下PYTHON中关于SOCKETSERVER的模块-B
  8. 重磅!微信、淘宝、抖音、支付宝或将迎来“超级监管”
  9. 大数据可视化应用在哪些方面
  10. python编程框架_python编程基础框架
  11. C语言——知识点汇总
  12. 状态分布函数 详细介绍
  13. SQL server-数据库的创建
  14. nacos 未读取到合法数据,请检查导入的数据文件
  15. 如何充分利用点赞功能提升Linkedin账号曝光率和活跃度
  16. 纷享销客《快消行业CRM应用与选型指南》重磅发布
  17. 三点钟社群联合发起人Sky: 中国版“马克·扎克伯格”,用区块链激励差异化价值创造者...
  18. hibernate数据检索策略
  19. 求二维整数数组中最大子数组的和(结对作业)
  20. 《了不起的盖茨比》----走出绿灯困境

热门文章

  1. 天地图获取点击位置的经纬度
  2. CSS Position 定位
  3. 路由与交换技术实验(eNSP)
  4. php给textarea赋值,html中textarea赋值与取值问题详细讲解
  5. 计算机体系结构(一)——量化设计的基本原则
  6. printk 续行问题
  7. python程序设计丁亚涛版_《Python程序设计 主编:丁亚涛 副主编:王世好 胡继礼 阚峻岭 著 丁亚涛 编 》【摘要 书评 试读】- 京东图书...
  8. sklearn参数优化方法
  9. 天秤女天蝎男的缘与份
  10. MySQL的卸载与安装(附管理工具Navicat SQLyog)