安全:nftables:基础知识

一,policy:

1,原文档链接:

https://docs.redhat.com/zh_hans/documentation/red_hat_enterprise_linux/9/html/configuring_firewalls_and_packet_filters/assembly_creating-and-managing-nftables-tables-chains-and-rules_getting-started-with-nftables#con_basics-of-nftables-tables_assembly_creating-and-managing-nftables-tables-chains-and-rules

2,链策略

如果此链中的规则没有指定任何操作,则链策略定义 nftables 是否应该接受或丢弃数据包。
您可以在链中设置以下策略之一:

  • accept (默认)
  • drop

 

二,两种链:基本链和常规链的区别

表由链组成,链又是规则的容器。存在以下两种规则类型:

  • 基本链 :您可以使用基本链作为来自网络堆栈的数据包的入口点。
  • 常规链 :您可以将常规链用作 jump 目标来更好地组织规则。

如果要向表中添加基本链,所使用的格式取决于您的防火墙脚本:

  • 在原生语法的脚本中,使用:

    table <table_address_family> <table_name> {
      chain <chain_name> {
        type <type> hook <hook> priority <priority>
        policy <policy> ;
      }
    }
  • 在 shell 脚本中,使用:

    nft add chain <table_address_family> <table_name> <chain_name> { type <type> hook <hook> priority <priority> \; policy <policy> \; }
    为了避免 shell 将分号解释为命令的结尾,请将 \ 转义字符放在分号前面。

       这两个示例都创建 基本链。要创建 常规链,请不要在大括号中设置任何参数。

 

三,链的类型

以下是链类型以及您可以使用的地址系列和钩子的概述:

四,add和insert的区别:

add 命令在链的末尾附加新规则。
如果要在链的开头添加一条规则,请使用 nft insert 命令而不是 nft add

 

例子:

添加表和链

[root@192 ~]# nft add table inet my_table
[root@192 ~]# nft add chain inet my_table my_chain { type filter hook input priority 0 \; }

反斜线(\)用来转义,这样 shell 就不会将分号解释为命令的结尾

查看规则
[root@192 ~]# nft list ruleset
table inet my_table {
        chain my_chain {
                type filter hook input priority filter; policy accept;
        }
}

分别用add/insert命令添加规则

[root@192 ~]# nft add rule inet my_table my_chain tcp dport ssh accept
[root@192 ~]# nft insert rule inet my_table my_chain tcp dport http accept

 查看添加后的规则:

[root@192 ~]# nft list ruleset
table inet my_table {
        chain my_chain {
                type filter hook input priority filter; policy accept;
                tcp dport 80 accept
                tcp dport 22 accept
        }
}

五,索引和句柄的区别:

使用索引要注意的地方:一是 index 的值是从 0 开始的;二是 index 必须指向一个存在的规则

把规则插入到链的指定位置,有两种方法:

1、 使用 index 来指定规则的索引。
add 表示新规则添加在索引位置的规则后面,
insert 表示新规则添加在索引位置的规则前面。
index 的值从 0 开始增加。

使用insert插入规则的例子:

[root@192 ~]# nft insert rule inet my_table my_chain index 1 tcp dport nfs accept

查看效果:

[root@192 ~]# nft list ruleset
table inet my_table {
        chain my_chain {
                type filter hook input priority filter; policy accept;
                tcp dport 80 accept
                tcp dport 2049 accept
                tcp dport 22 accept
        }
}

insert插入规则例子2:

[root@192 ~]# nft insert rule inet my_table my_chain index 0 tcp dport mysql accept

查看效果:

[root@192 ~]# nft list ruleset
table inet my_table {
        chain my_chain {
                type filter hook input priority filter; policy accept;
                tcp dport 3306 accept
                tcp dport 80 accept
                tcp dport 2049 accept
                tcp dport 22 accept

add添加规则:

[root@192 ~]# nft add rule inet my_table my_chain index 0 tcp dport 1234 accept

查看效果:

[root@192 ~]# nft list ruleset
table inet my_table {
        chain my_chain {
                type filter hook input priority filter; policy accept;
                tcp dport 3306 accept
                tcp dport 1234 accept
                tcp dport 80 accept
                tcp dport 2049 accept
                tcp dport 22 accept
        }
}

 2,使用 handle 来指定规则的句柄。
add 表示新规则添加在索引位置的规则后面,
insert 表示新规则添加在索引位置的规则前面。
handle 的值可以通过参数 --handle 获取

查看句柄:

[root@192 ~]# nft  --handle list ruleset
table inet my_table { # handle 2
        chain my_chain { # handle 1
                type filter hook input priority filter; policy accept;
                tcp dport 3306 accept # handle 5
                tcp dport 1234 accept # handle 6
                tcp dport 80 accept # handle 3
                tcp dport 2049 accept # handle 4
                tcp dport 22 accept # handle 2
        }
}

用add添加规则:

[root@192 ~]# nft add rule inet my_table my_chain handle 6 tcp dport 222 accept

查看效果:

[root@192 ~]# nft  --handle list ruleset
table inet my_table { # handle 2
        chain my_chain { # handle 1
                type filter hook input priority filter; policy accept;
                tcp dport 3306 accept # handle 5
                tcp dport 1234 accept # handle 6
                tcp dport 222 accept # handle 7
                tcp dport 80 accept # handle 3
                tcp dport 2049 accept # handle 4
                tcp dport 22 accept # handle 2
        }
}

用insert命令添加规则:

[root@192 ~]# nft insert rule inet my_table my_chain handle 5 tcp dport 333 accept

查看效果:

[root@192 ~]# nft  --handle list ruleset
table inet my_table { # handle 2
        chain my_chain { # handle 1
                type filter hook input priority filter; policy accept;
                tcp dport 333 accept # handle 8
                tcp dport 3306 accept # handle 5
                tcp dport 1234 accept # handle 6
                tcp dport 222 accept # handle 7
                tcp dport 80 accept # handle 3
                tcp dport 2049 accept # handle 4
                tcp dport 22 accept # handle 2
        }
}

 3,在 nftables 中,句柄值是固定不变的,除非规则被删除,这就为规则提供了稳定的索引。
    而 index 的值是可变的,只要有新规则插入,就有可能发生变化。
    一般建议使用 handle 来插入新规则。

 

六,刷新链和删除链的区别:

删除链

和删除表一样通过delete参数

 nft delete chain 族类型 表名 链名

刷新来自链的规则

清空指定表中特定链的所有规则,但保留该链的结构和配置

nft flush chain 族类型 表名 链名

例子:

删除前:

[root@192 ~]# nft list ruleset
table inet my_table {
        chain my_chain {
                type filter hook input priority filter; policy accept;
                tcp dport 333 accept
                tcp dport 3306 accept
                tcp dport 1234 accept
                tcp dport 222 accept
                tcp dport 80 accept
                tcp dport 2049 accept
                tcp dport 22 accept
        }
}

清空链

[root@192 ~]# nft flush chain inet my_table my_chain

查看效果:

[root@192 ~]# nft list ruleset
table inet my_table {
        chain my_chain {
                type filter hook input priority filter; policy accept;
        }
}

删除链:

[root@192 ~]# nft delete chain inet my_table my_chain

查看效果:

[root@192 ~]# nft list ruleset
table inet my_table {
}

 

 

posted @ 2024-09-17 13:40  刘宏缔的架构森林  阅读(1)  评论(0编辑  收藏  举报