iptables 认识 第二章

一、四表五链

netfilter 通过四表五链两个维度来定义数据包过滤规则。

#上面图片中 raw 表不在postrouting 链中,请注意

上图中的五个位置也被称为五个钩子函数(hook functions),也叫五个规则链。

1.PREROUTING (路由前)

2.INPUT (数据包流入口)

3.FORWARD (转发管卡)

4.OUTPUT(数据包出口)

5.POSTROUTING(路由后)

 这是netfilter规定的五个规则链,任何一个数据包,只要经过本机,必将经过这五个链中的其中一个链。

iptables包含4个表,5个链。其中表是按照对数据包的操作区分的,链是按照不同的Hook点来区分的,表和链实际上是netfilter的两个维度。      

(1)4表:

   filter,nat,mangle,raw,默认表是filter(没有指定表的时候就是filter表)

    filter:一般的过滤功能

    nat:用于nat功能(端口映射,地址映射等)

    mangle:用于对特定数据包的修改

    raw:有限级最高,设置raw时一般是为了不再让iptables做数据包的链接跟踪处理,提高性能

(2)5链:PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING。

    PREROUTING:数据包进入路由表之前

    INPUT:通过路由表后目的地为本机

    FORWARDING:通过路由表后,目的地不为本机

    OUTPUT:由本机产生,向外转发

    POSTROUTING:发送到网卡接口之前。

二、各功能分别实现的位置

流入:PREROUTING --->INPUT
流出:OUTPUT-----> POSTROUTING
转发: PREROUTING ---> FORWARD ----POSTOUTING
filter : INPUT ,OUTPUT ,FORWARD
nat    : PREROUTING(DNAT) ,OUTPUT ,POSTROUTING (SNAT)
mangle : PREROUTING ,INPUT ,OUTPUT ,FORWARD ,POSTROUTING 
raw    : PREROUTING ,OUTPUT ,POSTROUTING
功能的优先级次序: raw -->mangle --> nat--> filter
1.filter表——三个链:INPUT、FORWARD、OUTPUT
作用:过滤数据包  内核模块:iptables_filter.
2.Nat表——三个链:PREROUTING、POSTROUTING、OUTPUT
作用:用于网络地址转换(IP、端口) 内核模块:iptable_nat
3.Mangle表——五个链:PREROUTING、POSTROUTING、INPUT、OUTPUT、FORWARD
作用:修改数据包的服务类型、TTL、并且可以配置路由实现QOS内核模块:iptable_mangle(不经常使用)
4.Raw表——两个链:OUTPUT、PREROUTING
作用:决定数据包是否被状态跟踪机制处理  内核模块:iptable_raw
iptables :规则管理工具 
        添加, 删除,修改,显示等;
        
        规则和链有计数器:
            pkts;有规则或链所匹配到的报文个数
            bytes: 由规则或链匹配到的所有报文大小之和
            
iptables 命令:
       iptables [-t table] {-A|-C|-D} chain rule-specification

       iptables [-t table] -I chain [rulenum] rule-specification

       iptables [-t table] -R chain rulenum rule-specification

       iptables [-t table] -D chain rulenum

       iptables [-t table] -S [chain [rulenum]]

       iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options...]

       iptables [-t table] -N chain

       iptables [-t table] -X [chain]

       iptables [-t table] -P chain target

       iptables [-t table] -E old-chain-name new-chain-name
                    
    
    
    语法:   iptables [-t table]      SUBCOMMAND CHAIN  CRETERIA -j TARGET
    -t table: 默认为 filter
            filter ,nat ,mangle ,raw 
      chain ;        
            -F:flush ,清空规则链,省略链,表示清空指定表上的所有链
            -N:    new, 创建新的自定义规则链,    
            -X:drop ,删除用户自定义的空的规则链;
            -Z:zero ,置零,置零规则计数器
            -P:policy ,设置默认策略,对filter 表中的链而言,默认通常ACCEPT,DROP ,REJECT
            -E: 重命名自定义链 ,应用计数不为0 的自定义链,无法改名,也无法删除
            
            
        规则管理:
            -A ;append ,将新规则追加至指定链的尾部
            -I ;insert  ,将新规则追加至指定链的指定位置
            -D :delete ,删除指定链上的指定规则
                        两种指定方式:
                         指定匹配条件
                         指定规则编号
            -R:replace,替换指定链上的指定规则
        查看:
            -L :list ,列出指定链上的所有规则
            -n :numberic ,以数字格式显示地址和端口
            -v :verbose ,显示详细信息
                -vv ,-vvv
            --line-numbers;显示规则编号
            -x :exactly,显示计数器结果的精确值
        目标; 
            -j ;TARGET ,jump ,跳转 指定的 TARGET 
                ACCEPT :接受
                DROP :丢弃
                REJECT :拒绝
                RETURN :返回调用链
                REDIRECT :端口重定向
                LOG :记录日志
                MARK ;做防火墙标记
                DNAT :目标地址转换
                MASQUERADE ;地址伪装
                .....
                自定义链;由自定义链上的规则进行匹配检查
                
        centos 6: man iptables
        centos 7: man iptables-extensions
[root@localhost /]# iptables -t filter -N test_public    #添加自定义链
[root@localhost /]# 
[root@localhost /]# iptables -t filter -L
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 test_public (0 references)
target     prot opt source               destination         
[root@localhost /]# 
[root@localhost /]# iptables -X -t filter    #删除用户自定义没有引用的空链
[root@localhost /]# 
[root@localhost /]# iptables -t filter -L
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  
[root@localhost /]# systemctl restart firewalld    #重启iptables 服务
[root@localhost /]# iptables  -L -n          #查看iptables 
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_direct  all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0           
INPUT_ZONES  all  --  0.0.0.0/0            0.0.0.0/0           
DROP       all  --  0.0.0.0/0            0.0.0.0/0            ctstate INVALID
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
[root@localhost /]# iptables -t filter -P FORWARD DROP   修改某一个链上的默认策略
[root@localhost /]# 
[root@localhost /]# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy DROP)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@nginx etc]# iptables -t filter -A INPUT -d 10.2.61.22 -p tcp -j ACCEPT    #允许任意地址通过TCP 地址访问本机
[root@nginx etc]# 
[root@nginx etc]# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            10.2.61.22          

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@nginx etc]# 
[root@nginx etc]# iptables -t filter -D OUTPUT 1    #删除指定链上编号为1 的规则
[root@nginx etc]# iptables -L -n -v --line-number
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1       58  4140 ACCEPT     tcp  --  *      *       0.0.0.0/0            10.2.61.22          

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

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        4   448 ACCEPT     tcp  --  *      *       10.2.61.22           0.0.0.0/0           
[root@nginx etc]# 
[root@nginx etc]# iptables -P INPUT DROP    #指定默认策略为 DROP
[root@nginx etc]# iptables -P OUTPUT DROP
[root@nginx etc]# iptables -L -n
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            10.2.61.22          

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  10.2.61.22           0.0.0.0/0           
[root@nginx etc]# 
[root@nginx etc]# iptables -t filter -A INPUT -p icmp -d 10.2.61.22 -j ACCEPT   #允许ping 包进来
[root@nginx etc]# iptables -L -n
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            10.2.61.22          
ACCEPT     icmp --  0.0.0.0/0            10.2.61.22          

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  10.2.61.22           0.0.0.0/0           
[root@nginx etc]# iptables -t filter -A OUTPUT -p icmp -s 10.2.61.22 -j ACCEPT   #允许响应ping 包出去 
[root@nginx etc]# iptables -L -n
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            10.2.61.22          
ACCEPT     icmp --  0.0.0.0/0            10.2.61.22          

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  10.2.61.22           0.0.0.0/0           
ACCEPT     icmp --  10.2.61.22           0.0.0.0/0           
[root@nginx etc]# 
[root@nginx etc]# iptables -A INPUT -i ens192 -j ACCEPT         #指定出入接口匹配
[root@nginx etc]# iptables -A OUTPUT -o ens192 -j ACCEPT

[root@nginx etc]# iptables -L -n -v
Chain INPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination      #限制了流入流出
859 62296 ACCEPT tcp -- * * 0.0.0.0/0 10.2.61.22
3 244 ACCEPT all -- ens192 * 0.0.0.0/0 0.0.0.0/0


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


Chain OUTPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
557 56796 ACCEPT tcp -- * * 10.2.61.22 0.0.0.0/0
3 244 ACCEPT all -- * ens192 0.0.0.0/0 0.0.0.0/0
[root@nginx etc]#

 
原文地址:https://www.cnblogs.com/zy09/p/10384718.html