zoukankan      html  css  js  c++  java
  • iptables

     

    目录

    1. 防火墙简介
    2. iptables 与 firewalld
    3. iptables 基础 
      3.1 链的概念 
      3.2 表的概念 
      3.3 链与表的关系 
      3.4 数据通过的流程
    4. iptables 语法
    5. iptables nat表的应用

    1. 防火墙简介
    防火墙是一种应用于网络上的过滤机制,从保护对象上可分为:主机防火墙、网络防火墙,从物理上可分为:硬件防火墙、软件防火墙

    保护对象上的分类:

    主机防火墙:针对于单个主机进行防护
    网络防火墙:往往部署于网络边界,对流入以及流出的流量进行过滤
    物理上的分类:

    硬件防火墙:拥有经过特别设计的硬件及芯片,性能高、成本高
    软件防火墙:应用软件处理逻辑运行于通用硬件平台之上的防火墙,性能低、成本低
    2. firewalld 与 iptables
    2.1 异同点
    相同点:firewalld 与 iptables 都是 Linux 中防火墙的管理程序,但其实其角色主要为对于防火墙策略的管理,真正的防火墙执行者是位于内核中的 Netfilter。

    不同点:

    iptables 仅能通过命令行进行配置;而 firewalld提供了图形接口,类似windows防火墙的操作方式
    iptables 每一个单独更改意味着清除所有旧的规则,并从 /etc/sysconfig/iptables 中读取所有新的规则;而 firewalld 在有规则变动后,可以仅仅运行规则中的不同之处,即在 firewalld 运行时间内,改变设置时可以不丢失现行链接
    iptables 的配置文件在 /etc/sysconfig/iptables 中;而 firewalld 的配置文件在 /usr/lib/firewalld/ 和 /etc/firewalld/ 中的各种 XML 文件中
    iptables 没有守护进程,并不能算是真正意义上的服务;而 firewalld 有守护进程
    2.2 关闭 firewalld,开启 iptables
    2.2.1 关闭 firewalld
    关闭 firewalld 服务:syctemctl stop firewalld
    禁用 firewalld 服务:systemctl disable firewalld
    2.2.2 开启 iptables
    安装 iptables:yum -y install iptables-services
    启用 iptables:systemctl enable iptables
    开启 iptables:systemctl start iptables
    3. iptables 基础
    iptables 准确来讲并不是防火墙,真正的防火墙是运行于系统内核中的 netfilter,而 iptables 仅仅是 netfilter 的代言人,其所负责的主要功能便是与用户交互,获取到用户的要求,并转化成 netfilter 可以接受的信息。

    防火墙的实现机制
    防火墙的核心处理机制是过滤,而说到过滤,就必须具有“条件 & 动作”这两个关键要素,而在 iptables 中,这两种要素分别叫做“rule & target”,可以理解成符合 rule 的流量将会去往 target。

    匹配规则的要素
    防火墙的处理对象是网络流量,而对于网络流量来讲,标识流量的最重要的信息便是五元组,包括:S_IP, S_PORT, D_IP, DI_PORT, TCP/UDP,iptables 常用的也往往是根据五元组中的某个或某些要素进行过滤

    举个栗子
    防火墙的主要功能是过滤,那我们不妨把防火墙看成是个社区的废水处理厂,负责将社区的生活废水收集起来,经过多道工序的处理后,返还给社区。
    链:既然是要处理废水,那我们首先要在废水处理的个别关键环节上设置集中处理的“处理链”,在“处理链”中放置各种不同的过滤网、膜以及化学配方。
    表:即便是不同的处理链中,可能会用到一些相同的处理技术,比如粗滤膜、细滤膜,为了方便这些技术的复用,废水处理厂将功能类似的处理技术封装成集合,这样能够更加方便地使用

    3.1 链的概念
    3.1.1 什么是链?
    就是从报文进入到报文离开这整个期间,计算机处理报文的关键环节。
    就好比污水处理厂,污水进入前得处理一下,进入后得沉淀一下,送回时又得进行一些处理,那么这3个节点就是关键环节。

    3.1.2 为何叫做链?
    因为防火墙是对报文进行规则匹配,然后执行相应动作。但在关键环节上,往往不止一条规则,而是有大量的规则,而且这些规则都是按顺序排列的,待匹配的报文需要按顺序一个一个规则的进行匹配,直到匹配到一条规则为止,所以在每个关键环节上的匹配过程,就像是一条有顺序的链。
    好比污水处理厂,必须在个别关键环节部署处理措施,比如在进入废水厂时,先进行一批处理。处理可能包括粗滤、细滤等各种方式,而且各种方式不能随意排列,必定是按照一定的顺序依次进行,比如先粗滤,再细滤。

    3.1.3 iptables 有哪些链?
    记住,链其实就是报文处理的重要环节,所以很容易就得出有以下两个重要环节:
    * INPUT:进入主机
    * OUTPUT:离开主机

    但还有一种复杂的情况,就是报文的目标并不是本主机,而只是借道本主机,希望通过本主机去往下一台主机。
    就好比我们的这个污水处理厂只处理生活污水,并不具备能力可以完全处理工业污水,但是政府又要求我们要对工业污水进行一下简单的处理,之后再传送给工业污水处理厂。那么就容易得出以下两个重要环节(链):

    PREROUTING:路有前
    FORWARD:转发
    POSTROUTING:路由后
    ![](https://upload-images.jianshu.io/upload_images/8951510-3ff0b09b8b7893cc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    3.2 表的概念
    3.2.1 什么是表?
    在每个链上都有一堆规则,但是部分规则是相似的,那我们把一些实现相同功能的规则放在一起,就能轻松的完成复用了。
    好比污水处理厂,粗滤、细滤基本上是好多关键环节都要用到的,那何不将粗滤网与细滤网直接封装到一根管子里,要用的时候,直接整根管子接上去就能一次实现两个功能,岂不方便?

    3.2.2 iptables 提供了哪些表?
    filter:负责过滤功能,内核模块 iptables_filter
    nat:负责进行网络地址转换,内核模块 iptable_nat
    mangle:拆解报文,进行修改,重新封装,内核模块 iptable_mangle
    raw:关闭 nat 表上启用的连接追踪机制,内核模块 iptable_raw
    security:安全相关?CentOS 7 里新增的表,暂且不介绍
    ps. 运维中常用的为 filter、nat

    3.3 链与表的关系
    现在应该清楚:链是报文流转过程中的关键处理环节,表是某一些相似规则的集合

    3.3.1 链中的表
    但是由于处理环节的分工不同,每个处理环节可能具有不同的表,让我们看看5个环节中,每个环节都有哪些表

    ![](https://upload-images.jianshu.io/upload_images/8951510-8daa792da565ebac.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    3.3.2 表对应的链
    但是,实际使用中,往往是以“表”作为操作入口来对规则进行定义的,所以比起知道某个链中存在哪些表,不如明确每种表能够应用于哪些链中,这样能方便实际的使用。

    ![](https://upload-images.jianshu.io/upload_images/8951510-abdd9e9dc043e356.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    3.3.3 表的优先级
    由上可知,每个链中包含多个表。
    但是还记得规则是有顺序的吗?那问题来了,同一条链中,到底哪个表被先执行呢?

    ![表的优先级](https://upload-images.jianshu.io/upload_images/8951510-097383144d72fa25.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    3.4 规则的概念
    之前已经讲过,规则主要包含两部分,即“条件 & 动作”

    3.4.1 匹配条件
    iptables 主要是通过网络流量的五元组(某个或某些)来进行匹配,包括:
    * S_IP:源 IP
    * S_PORT:源端口
    * D_IP:目的 IP
    * D_PORT:目的端口
    * TCP/UDP:四层协议

    3.4.2 处理动作
    iptables 中称为 target

    ACCEPT:允许数据包通过。
    DROP:直接丢弃数据包。不回应任何信息,客户端只有当该链接超时后才会有反应。
    REJECT:拒绝数据包。会给客户端发送一个响应的信息 。
    SNAT:源 NAT,解决私网用户用同一个公网 IP 上网的问题。
    MASQUERADE:是 SNAT 的一种特殊形式,适用于动态的、临时会变的 IP 上。
    DNAT:目的 NAT,解决私网服务端,接收公网请求的问题。
    REDIRECT:在本机做端口映射。
    LOG:在 /etc/log/messages 中留下记录,但并不对数据包进行任何操作。
    4. iptables 常用语法
    使用 iptables 时,最常用的就是对规则、表进行“增删改查”

    4.1 查询
    4.1.1 Options
    -L:负责查询的主要选项
    -n:不进行IP地址翻译
    -v:显示详细信息,包括:命中规则的包数&字节数、缺省策略的包数&字节数、入向接口,
    -t <TABLE>:指定需要查看的表,若不加该选项,默认查询 filter 表
    --line:显示各链的规则行号
    4.1.2 简单查看
    $ iptables -L

    $ iptables -L
    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

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

    Chain OUTPUT (policy ACCEPT)
    target prot opt source destination

    Chain:所属的链
    (policy ACCEPT):该链的缺省规则
    target:对应的处理动作
    prot:对应的协议
    opt:规则对应的选项
    source:对应的源 IP 地址或网段
    destination:对应的目的 IP 地址或网段
    4.1.3 查看命中数:-v
    iptables -vL

    $ iptables -vL
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
    pkts bytes target prot opt in out source destination
    1305 102K ACCEPT all -- any any anywhere anywhere state RELATED,ESTABLISHED
    0 0 ACCEPT icmp -- any any anywhere anywhere
    0 0 ACCEPT all -- lo any anywhere anywhere
    1 52 ACCEPT tcp -- any any anywhere anywhere state NEW tcp dpt:ssh
    7 1150 REJECT all -- any any anywhere anywhere reject-with icmp-host-prohibited

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

    Chain OUTPUT (policy ACCEPT 964 packets, 134K bytes)
    pkts bytes target prot opt in out source destination

    pkts:命中规则的报文个数
    bytes:命中规则的报文大总和
    in:规则对应的入向接口
    out:规则对应的出向接口
    4.1.4 查看特定的表:-t <TABLE>
    $ iptables -t filter -L
    竟然发现跟 -L 显示的一模一样?
    翻到前面,看看 filter 表可以在哪些链中使用?是不是正好就是 INPUT、FORWARD、OUTPUT 这三条链啊!

    $ iptables -t filter -L
    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

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

    Chain OUTPUT (policy ACCEPT)
    target prot opt source destination

    4.1.5 查看特定的链
    iptables -L <CHAIN>

    $ iptables -nvL INPUT
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
    pkts bytes target prot opt in out source destination
    1256 98520 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
    0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
    0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
    1 52 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
    6 921 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

    4.1.6 显示规则行号:--line
    $ iptables -t filter -nvL --line-number
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
    num pkts bytes target prot opt in out source destination
    1 1542 121K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
    2 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
    3 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
    4 1 52 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
    5 11 1613 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

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

    Chain OUTPUT (policy ACCEPT 1142 packets, 153K bytes)
    num pkts bytes target prot opt in out source destination

    4.2 增加
    4.2.1 Options
    -I <CHAIN>:insert,插入,排序在指定链的规则的首位
    -I <CHAIN> #:插入到指定链的第 # 号规则的位置
    -A <CHAIN>:append,追加,排序在指定链的规则的末尾
    -s <S_IP>:指定源 IP
    -j <ACTION>:指定执行的动作,具体动作类型见 3.4.2
    4.2.2 插入规则到首位
    $ iptables -nvL INPUT
    Chain INPUT (policy ACCEPT 102 packets, 8892 bytes)
    pkts bytes target prot opt in out source destination
    0 0 ACCEPT all -- * * 12.12.12.12 0.0.0.0/0
    0 0 LOG all -- * * 11.11.11.11 0.0.0.0/0 LOG flags 0 level 4
    0 0 DROP all -- * * 10.10.10.10 0.0.0.0/0
    $ iptables -t filter -I INPUT -s 1.1.1.1 -j DROP
    $ iptables -nvL INPUT
    Chain INPUT (policy ACCEPT 43 packets, 3020 bytes)
    pkts bytes target prot opt in out source destination
    0 0 DROP all -- * * 1.1.1.1 0.0.0.0/0
    0 0 ACCEPT all -- * * 12.12.12.12 0.0.0.0/0
    0 0 LOG all -- * * 11.11.11.11 0.0.0.0/0 LOG flags 0 level 4
    0 0 DROP all -- * * 10.10.10.10 0.0.0.0/0

    4.2.3 插入规则到指定号码
    Chain INPUT (policy ACCEPT 279 packets, 21604 bytes)
    num pkts bytes target prot opt in out source destination
    1 0 0 DROP all -- * * 1.1.1.1 0.0.0.0/0
    2 0 0 ACCEPT all -- * * 12.12.12.12 0.0.0.0/0
    3 0 0 LOG all -- * * 11.11.11.11 0.0.0.0/0 LOG flags 0 level 4
    4 0 0 DROP all -- * * 10.10.10.10 0.0.0.0/0

    $ iptables -I INPUT 3 -s 3.3.3.3 -j ACCEPT
    $ iptables -nvL INPUT --line
    Chain INPUT (policy ACCEPT 5 packets, 388 bytes)
    num pkts bytes target prot opt in out source destination
    1 0 0 DROP all -- * * 1.1.1.1 0.0.0.0/0
    2 0 0 ACCEPT all -- * * 12.12.12.12 0.0.0.0/0
    3 0 0 ACCEPT all -- * * 3.3.3.3 0.0.0.0/0
    4 0 0 LOG all -- * * 11.11.11.11 0.0.0.0/0 LOG flags 0 level 4
    5 0 0 DROP all -- * * 10.10.10.10 0.0.0.0/0

    4.2.4 追加到末位
    $ iptables -nvL INPUT
    Chain INPUT (policy ACCEPT 50 packets, 3592 bytes)
    pkts bytes target prot opt in out source destination
    0 0 DROP all -- * * 1.1.1.1 0.0.0.0/0
    0 0 ACCEPT all -- * * 12.12.12.12 0.0.0.0/0
    0 0 ACCEPT all -- * * 3.3.3.3 0.0.0.0/0
    0 0 LOG all -- * * 11.11.11.11 0.0.0.0/0 LOG flags 0 level 4
    0 0 DROP all -- * * 10.10.10.10 0.0.0.0/0
    $ iptables -A INPUT -s 255.255.255.255 -j ACCEPT
    $ iptables -nvL INPUT
    Chain INPUT (policy ACCEPT 36 packets, 2600 bytes)
    pkts bytes target prot opt in out source destination
    0 0 DROP all -- * * 1.1.1.1 0.0.0.0/0
    0 0 ACCEPT all -- * * 12.12.12.12 0.0.0.0/0
    0 0 ACCEPT all -- * * 3.3.3.3 0.0.0.0/0
    0 0 LOG all -- * * 11.11.11.11 0.0.0.0/0 LOG flags 0 level 4
    0 0 DROP all -- * * 10.10.10.10 0.0.0.0/0
    0 0 ACCEPT all -- * * 255.255.255.255 0.0.0.0/0

    4.3 删除
    4.3.1 Options
    -D <CHAIN> <DETAILS>:根据规则的具体匹配条件与动作进行删除
    -D <CHAIN> #:根据规则的编号进行删除
    -F <CHAIN>:清空指定链上的所有规则
    -t <TABLE> -F:清空某种表在所有链上的规则
    4.3.2 根据规则的具体情况进行删除
    跟添加时的命令几乎一模一样

    $ iptables -nvL INPUT
    Chain INPUT (policy ACCEPT 78 packets, 5684 bytes)
    pkts bytes target prot opt in out source destination
    0 0 DROP all -- * * 1.1.1.1 0.0.0.0/0
    0 0 ACCEPT all -- * * 12.12.12.12 0.0.0.0/0
    0 0 ACCEPT all -- * * 3.3.3.3 0.0.0.0/0
    0 0 LOG all -- * * 11.11.11.11 0.0.0.0/0 LOG flags 0 level 4
    0 0 DROP all -- * * 10.10.10.10 0.0.0.0/0
    0 0 ACCEPT all -- * * 255.255.255.255 0.0.0.0/0
    $ iptables -D INPUT -s 1.1.1.1 -j DROP
    $ iptables -nvL INPUT
    Chain INPUT (policy ACCEPT 5 packets, 388 bytes)
    pkts bytes target prot opt in out source destination
    0 0 ACCEPT all -- * * 12.12.12.12 0.0.0.0/0
    0 0 ACCEPT all -- * * 3.3.3.3 0.0.0.0/0
    0 0 LOG all -- * * 11.11.11.11 0.0.0.0/0 LOG flags 0 level 4
    0 0 DROP all -- * * 10.10.10.10 0.0.0.0/0
    0 0 ACCEPT all -- * * 255.255.255.255 0.0.0.0/0

    4.3.3 根据规则编号进行删除
    $ iptables -nvL INPUT --line
    Chain INPUT (policy ACCEPT 32 packets, 2417 bytes)
    num pkts bytes target prot opt in out source destination
    1 0 0 ACCEPT all -- * * 12.12.12.12 0.0.0.0/0
    2 0 0 ACCEPT all -- * * 3.3.3.3 0.0.0.0/0
    3 0 0 LOG all -- * * 11.11.11.11 0.0.0.0/0 LOG flags 0 level 4
    4 0 0 DROP all -- * * 10.10.10.10 0.0.0.0/0
    5 0 0 ACCEPT all -- * * 255.255.255.255 0.0.0.0/0
    $ iptables -D INPUT 5
    $ iptables -nvL INPUT --line
    Chain INPUT (policy ACCEPT 35 packets, 2784 bytes)
    num pkts bytes target prot opt in out source destination
    1 0 0 ACCEPT all -- * * 12.12.12.12 0.0.0.0/0
    2 0 0 ACCEPT all -- * * 3.3.3.3 0.0.0.0/0
    3 0 0 LOG all -- * * 11.11.11.11 0.0.0.0/0 LOG flags 0 level 4
    4 0 0 DROP all -- * * 10.10.10.10 0.0.0.0/0

    4.3.4 清空
    iptables -F <CHAIN> # 清空链下的所有规则
    iptables -t <TABLE> -F # 清空某个表中、所有链上的规则

    4.4 修改
    说是修改,可能本以为只要在命令中输入所需修改部分的内容即可,但其实不然。想要修改,必须要像添加时一样输全了匹配规则,只是修改的部分进行更改,所以也是麻烦,有时不如删掉再写一条来得干净。

    4.4.1 Options
    -R <CHAIN> #:修改指定链中指定序号的规则
    4.4.2 修改指定链中指定序号的规则
    看一下当前 INPUT 链的规则
    $ iptables -nvL INPUT --line
    Chain INPUT (policy ACCEPT 106 packets, 8413 bytes)
    num pkts bytes target prot opt in out source destination
    1 0 0 ACCEPT all -- * * 12.12.12.12 0.0.0.0/0
    2 0 0 ACCEPT all -- * * 3.3.3.3 0.0.0.0/0
    3 0 0 LOG all -- * * 11.11.11.11 0.0.0.0/0 LOG flags 0 level 4
    4 0 0 DROP all -- * * 10.10.10.10 0.0.0.0/0

    将 3号规则改为 REJECT(仅键入待修改部分)
    怎么刚一设置完就断了?
    $ iptables -R INPUT 3 -j REJECT
    $
    Socket error Event: 32 Error: 10053.
    Connection closing...Socket close.

    Connection closed by foreign host.

    Disconnected from remote host(CentOS_7_1804_x64) at 17:23:56.

    Type `help' to learn how to use Xshell prompt.

    我们进 tty 看一下到底是怎么回事。怎么源 IP 变成 0.0.0.0 (相当于 all)了,当SSH 当然就断了!

    ![](https://upload-images.jianshu.io/upload_images/8951510-a0483036b58e1e7b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 这就是本节开始时所讲的,并不是仅键入待修改的那部分信息即可,而需要完整的输入整条规则,看看下面这个例子。 * 将 3 号规则改回原样(全面输入) 这回终于可以了
    ![](https://upload-images.jianshu.io/upload_images/8951510-085814515f7e9799.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    4.5 修改链的缺省动作
    4.5.1 Options
    -P <CHAIN> <ACTION>:修改指定链的缺省动作
    4.5.2 示例
    看看当前 INPUT 链的缺省动作
    $ iptables -nvL INPUT
    Chain INPUT (policy ACCEPT 104 packets, 9184 bytes)
    pkts bytes target prot opt in out source destination
    0 0 ACCEPT all -- * * 12.12.12.12 0.0.0.0/0
    0 0 ACCEPT all -- * * 3.3.3.3 0.0.0.0/0
    0 0 ACCEPT all -- * * 11.11.11.11 0.0.0.0/0
    0 0 DROP all -- * * 10.10.10.10 0.0.0.0/0

    改为 DROP(又得掉线了)
    $ iptables -P INPUT DROP

    上 tty 去看一眼,果然改成 DROP 了

  • 相关阅读:
    Object.keys方法
    vue数据绑定原理
    JS控制数字从指定数开始变化
    传入参数和回调取值
    Android contacts content provider学习小结
    Android输入法框架分析(1)-三大组件
    通过Android View的两种事件响应方法比较inheritance和composition
    对象间相互调用时互相控制的几种方法
    immutable和mutable对象
    关于Unicode
  • 原文地址:https://www.cnblogs.com/liuhongru/p/11207573.html
Copyright © 2011-2022 走看看