zoukankan      html  css  js  c++  java
  • Linux-nftables


    Linux-nftables

    https://netfilter.org/
    https://netfilter.org/projects/iptables/index.html
    https://netfilter.org/projects/nftables/index.html
    https://www.netfilter.org/projects/nftables/manpage.html
    https://wiki.nftables.org/wiki-nftables/index.php/Main_Page
    https://wiki.nftables.org/wiki-nftables/index.php/Quick_reference-nftables_in_10_minutes
    https://wiki.archlinux.org/index.php/Nftables
    https://wiki.gentoo.org/wiki/Nftables
    https://wiki.gentoo.org/wiki/Nftables/Examples
    https://kernelnewbies.org/nftables_examples

    1. Linux防火墙简介
    2. nftables介绍
    3. 为什么要使用nftables
    4. Netfilter日志框架
    5. nft命令介绍
    6. nft基础操作
    7. 更多规则选项

    1. Linux防火墙简介

    Linux系统较早使用的防火墙是(约1996年的Linux 2.0) ipfwadm 和(约1999年的Linux 2.2) ipchains。
    基于以上版本经过重新设计和大幅改进后出现了(约2001年的Linux 2.4)的netfilter。一直应用到现在。

    • 包过滤框架:netfilter (约2001年Linux 2.4) 该框架内的软件支持包过滤,网络地址[和端口]转换(NA[P]T)和其他数据包重整。是Linux内核中的一组钩子,允许内核模块向网络堆栈注册回调函数。然后,对于遍历网络堆栈内的相应挂钩的每个数据包,回调注册的回调函数。
    • 用户工具集:iptables, nftables等
      • iptables (约2001年Linux 2.4) 是用户空间命令行程序,用于配置netfilter包过滤规则集。
      • nftables (约2013年Linux 3.13) 旨在取代现有iptables的{ip,ip6,arp,eb}表框架。(iptables,ip6tables,arptables,etables用于以太网桥过滤的功能。)


    补充:firewalld也是Linux的防火墙,同时支持iptables和nftables,最新版本默认使用nftables。简单的说firewalld是基于nftfilter防火墙的用户界面工具。而iptables和nftables是命令行工具。firewalld引入区域的概念,可以动态配置,让防火墙配置及使用变得简便。详见firewalld.org

    netfilter, iptables, nftables 都是netfilter项目。nftables 用于替代 iptables。
    netfilter的功能很强大,远远超越防火墙的范围,详见项目主页。

    2. nftables介绍

    本文主要记录较新的nftables的特性及nft工具的使用。
    nftables 提供了一个新的包过滤框架,该框架基于特定于网络的虚拟机(VM),一个新的用户空间实用程序(nft)和一个用于{ip,ip6}表的兼容层。
    它使用现有的钩子,连接跟踪系统,用户空间排队组件和netfilter的日志子系统。
    它由三个主要组件组成:内核实现,libnl netlink通信和nftables用户空间前端。
    内核提供了netlink配置接口,以及运行时规则集评估,
    libnl包含用于与内核通信的低级函数,
    而nftables前端是用户通过nft进行交互的内容。

    从Linux内核3.13版本开始(2013),nftables已经成为Linux内核主线的一部分。正在逐渐替换iptables。

    3. 为什么要使用nftables

    https://developers.redhat.com/blog/2016/10/28/what-comes-after-iptables-its-successor-of-course-nftables/
    nftables对旧iptables提供的最显着的功能有:
    支持查找表 - 无需进行线性规则评估
    不再强制执行隐式规则计数器和地址/接口匹配的开销
    拥有使用额外脚本的能力。
    nftables不包含任何内置表。

    可用性:
    事务规则更新 - 所有规则都以原子方式应用
    应用程序可以订阅nfnetlink通知,以便在添加或删除新规则时接收规则更新
    nft命令行工具可以显示正在匹配的规则的实时日志,以便于规则集调试

    nftables重用了netfilter框架的许多部分,例如连接跟踪和NAT设施。它还保留了命名法和基本iptables设计的几个部分,例如表格,链条和规则。
    就像使用iptables一样,表充当链的容器,链包含单独的规则,可以执行诸如丢弃数据包,移动到下一个规则或跳转到新链的操作。

    什么被替换?
    从用户的角度来看,nftables添加了一个名为nft的新工具,它取代了iptables,arptables和ebtables中的所有其他工具。从架构的角度来看,它还取代了处理包过滤规则集的运行时评估的内核部分。

    允许监控规则更新。nft工具可用于监视规则集中的更改,例如“nft monitor”显示从内核添加和删除的每个规则。
    它还提供“跟踪模式”,系统管理员可以在此处执行“添加规则修改” prerouting ip saddr 10.2.3.4 meta nftrace set 1'。
    然后,“nft monitor trace”将显示与来自IP地址10.2.3.4的数据包匹配的每个规则。

    4. Netfilter日志框架

    Nftables 7 : Nftables Logging
    http://computer-outlines.over-blog.com/article-nftables-7-nftables-logging-123303629.html
    /proc/net/netfilter/nf_log
    从Linux 2.6.14开始,可以通过内核数据包过滤器记录的用户空间数据包进行传递。
    有两个工具:
    QUEUE et NFQUEUE:外部应用程序用于决策(SNORT IDS / IPS,NuFW,ulogd,...)
    LOG et NFLOG:用于外部日志记录(Wireshark,Syslog,...)

    NFlog是一个新的iptables和Nftables的目标是通过虚拟设备记录数据包。
    NFLOG目标会复制数据包并将其发送到指定的netlink套接字。

    $ sudo cat /proc/net/netfilter/nf_log
     0 NONE ()
     1 NONE ()
     2 NONE ()// IPv4
     3 NONE ()
     4 NONE ()// Novell IPX
     5 NONE ()
     6 NONE ()
     7 NONE ()
     8 NONE ()
     9 NONE ()
    10 NONE ()// IPv6
    11 NONE ()
    12 NONE ()
    // 行号是协议。
    比如启用IPv6 nfnetlink_log:
    echo "nfnetlink_log" > /proc/sys/net/netfilter/nf_log/10
    $ ls /proc/net/netfilter/nf_log
    /proc/net/netfilter/nf_log
    $ ls /proc/sys/net/netfilter/nf_log
    0  1  10  11  12  2  3  4  5  6  7  8  9
    $ sudo cat /proc/net/netfilter/nf_log
    ...
    10 nfnetlink_log(nfnetlink_log)

    5. nft命令介绍

    不同与iptables,nftables不包含任何内置表。还拥有使用额外脚本的能力, 拥有一些高级的类似编程语言的能力,例如定义变量和包含外部文件。
    与iptables类似的是,表层次结构:表包含链,链里是具体的过滤规则。处理规则则由 地址,接口,端口或包含当前处理数据包中的其他数据等表达式以及诸如drop, queue, continue等声明组成。
    可以用于多种地址族的过滤和处理:

    • ip: IPv4 地址
    • ip6: IPv6 地址
    • inet: IPv4 和 IPv6 地址
    • arp: 地址解析协议(ARP)地址
    • bridge: 处理桥接数据包


    注:当没有指定地址族时,默认为IP。

    特定的地址族包含钩子,对ip,ipv6,和inet地址族来说,可以应用如下钩子

    • prerouting: 刚到达并且未被处理过的数据包
    • input: 已经被接收并且已经经过prerouting钩子的传入数据包
    • forward: 如果数据包将被发送到另一个设备,他将会通过forward钩子
    • output: 从本地系统传出的数据包
    • postrouting: 仅仅在离开系统之前


    ARP地址族只能使用input和output钩子。

    $ nft -h
      -h, --help 显示帮书
      -v, --version 显示版本信息

      -c, --check 检查命令的有效性,而不实际应用更改。
      -f, --file <filename> 包含文件内容<filename>
      -i, --interactive 从命令行读取输入

      -j, --json 以JSON格式化输出
      -n, --numeric 指定一次后,以数字方式显示网络地址(默认行为)。
      指定两次以数字方式显示Internet服务(端口号)。
    指定三次以数字方式显示协议,用户ID和组ID。
      -s, --stateless 省略规则集的有状态信息。
      -N 将IP地址转换为名称。
      -a, --handle 显示规则句柄handle.
      -e, --echo Echo what has been added, inserted or replaced.
      -I, --includepath <directory> 添加<directory>目录到包含文件的搜索路径中。默认为: /usr/share
      --debug <level [,level...]> 添加调试,在level处(scanner, parser, eval, netlink, mnl, proto-ctx, segtree, all)

    # nft 操作符 操作目标 操作内容
    操作符: 增,删,改,查,清除,插入,创建
    操作目标: 表,链(),规则()
    表操作(add,delete,list,flush;
    链操作(add,delete,rename,list,flush,create);链类型(type:filter,route,nat);链钩子(hook...)
    规则(add,delete,insert;
    操作内容:...

    查询所有表名
    nft list tables

    查询某个表的内容
    nft list table 表名

        Subcmd→
    cmd↓
    表语法
    table
    链语法
    chain
    规则语法
    rule
    备注
    add 添加到末尾
    create 除非已存在同名链
    insert 插入到指定位置
    delete  
    flush  
    rename 链名称
    list  

    6. nft基础操作

    https://yq.aliyun.com/articles/531207
    1、增
    增加表:nft add table fillter
    增加链:nft add chain filter input { type filter hook input priority 0 ; } # 要和hook(钩子)相关连
    增加规则:nft add rule filter input tcp dport 22 accept
    2、删
    只需要把上面的 add 改为 delete 即可
    3、改
    更改链名用rename
    更改规则用replace
    4、查
    nft list ruleset # 列出所有规则
    nft list tables # 列出所有表
    nft list table filter # 列出filter表
    nft chain filter input # 列出filter表input链
    以上命令后面也可以加 -nn  用于不解析ip地址和端口
    加 -a 用于显示 handles

    7. 更多规则选项

    声明

    1 accept 接受 接受 包 停止处理
    2 drop 丢弃 丢弃 包 停止处理
    3 reject 拒绝 驳回 包 停止处理
    4 queue 队列 发送包 到用户空间程序 停止处理
    5 continue 继续 继续处理包  
    6 return 返回 发送到调用的规则链进行处理  
    7 jump
    <chain>
    跳跃 发送到指定的规则链进行处理 当完成时或执行了返回的声明,返回到调用的规则链
    8 goto
    <chain>
    转到 发送到指定的规则链进行处理 不返回到调用的规则链
    9 limit limit 达到接收包的匹配限制, 则根据规则处理包
    10 log log 日志记录 包 继续处理

    meta 元表达式

    66 meta rtclassid Routing realm 路由领域          
    67 length Length of the packet in bytes: meta length > 1000 以字节为单位的数据包长度:元长度> 1000          
    68 protocol ethertype protocol: meta protocol vlan ethertype协议:元协议vlan          
    69 priority TC packet priority TC包优先级          
    70 mark Packet mark 包标记          
    71 iif Input interface index 输入接口索引 76 meta oif Output interface index 输出接口索引
    72 iifname Input interface name 输入接口名称 77 oifname Output interface name 输出接口名称
    73 iiftype Input interface type 输入接口类型 78 oiftype Output interface hardware type 输出接口硬件类型
    74 skuid UID associated with originating socket 与原始套接字关联的UID 79 skgid GID associated with originating socket 与原始套接字关联的GID
    75 iifgroup Input interface group 输入接口组 80 oifgroup Output interface group 输出接口组

    ct 连接跟踪

    54 ct direction Direction of the packet relative to the connection 数据包相对于连接的方向          
    55 mark Connection mark 连接标记          
    56 expiration Connection expiration time 连接到期时间          
    57 helper Helper associated with the connection 辅助与连接相关联          
    58 l3proto Layer 3 protocol of the connection 连接的第3层协议          
    59 protocol Layer 4 protocol of the connection for the given direction 给定方向的连接的第4层协议          
    60 state State of the connection 连接状态 63 ct status Status of the connection 连接状态
    61 saddr Source address of the connection for the given direction 给定方向的连接的源地址 64 daddr Destination address of the connection for the given direction 给定方向的连接的目标地址
    62 proto-src Layer 4 protocol source for the given direction 给定方向的第4层协议源 65 proto-dst Layer 4 protocol destination for the given direction 给定方向的第4层协议目的地

    IPv4 IPv6 报头表达式

    1 ip hdrlength IP header length IP标头长度          
    2 tos Type of Service 服务类型          
    3 id IP ID IP ID          
    4 frag-off Fragmentation offset 碎片偏移          
    5 ttl Time to live 生存的时间          
    6 protocol Upper layer protocol 上层协议          
    7 checksum IP header checksum IP头校验和          
    8 version Ip Header version Ip Header版本 12 ip6 version IP header version IP标头版本
    9 saddr Source address 来源地址 13 saddr Source Address 来源地址
    10 daddr Destination address 目的地址 14 daddr Destination Address 目的地址
    11 length Total packet length 总包长度 15 length Payload length 有效载荷长度
              16 priority   优先
              17 nexthdr Next header type (Upper layer protocol number) 下一个标题类型
    (上层协议号)
              18 flowlabel Flow label 流标签
              19 hoplimit Hop limit 跳限制

    TCP UDP 报头表达式

    20 tcp sequence Sequence number 序列号          
    21 ackseq Acknowledgement number 致谢号码          
    22 doff Data offset 数据偏移          
    23 flags TCP flags TCP标志          
    24 window Window 窗口          
    25 urgptr Urgent pointer 紧急指针          
    26 sport Source port 源端口 29 udp sport Source port 源端口
    27 dport Destination port 目的端口 30 dport destination port 目的端口
    28 checksum Checksum 校验 31 checksum Checksum 校验
              32 length Total packet length 总包长度

    其他匹配项目

    33 udplite sport Source port 源端口
    34 dport destination port 目的端口
    35 cscov Checksum coverage 校验和覆盖范围
    36 checksum Checksum 校验
    37 sctp sport Source port 源端口
    38 dport destination port 目的端口
    39 vtag Verification tag 验证标签
    40 checksum Checksum 校验
    41 dccp sport Source port 源端口
    42 dport destination port 目的端口
    43 ah nexthdr Next header protocol (Upper layer protocol) 下一个标头协议(上层协议)
    44 hdrlength AH header length AH标头长度
    45 spi Security Parameter Index 安全参数索引
    46 sequence Sequence Number 序列号
    47 esp spi Security Parameter Index 安全参数索引
    48 sequence Sequence Number 序列号
    49 ipcomp nexthdr Next header protocol (Upper layer protocol) 下一个标头协议(上层协议)
    50 flags Flags
    51 cfi Compression Parameter Index 压缩参数索引
    52 icmp type icmp packet type icmp数据包类型
    53 icmpv6 type icmpv6 packet type icmpv6数据包类型
  • 相关阅读:
    iOS 新建xib文件时,最外层view的约束问题
    React native 无法弹出调试控件的问题
    从GitHub下载demo时遇到的依赖问题
    Mac 解决 Sourcetree 同步代码总需要密码的问题
    Mac 安装JRE 1.8
    正则表达式-- (.*?) 或 (.*+)
    字符串内有多个#号,每俩#号为一组,JavaScript 截取每组#号之间的字符
    Js/jQuery实时监听input输入框值变化
    Redis设置密码
    redis本机能访问 远程不能访问的问题
  • 原文地址:https://www.cnblogs.com/sztom/p/10947111.html
Copyright © 2011-2022 走看看