当前位置:   article > 正文

iptables与nftables,ufw与firewalld以及netfilter详解_nftables和iptables

nftables和iptables

简介

名称解释

netfilter:netfilter可以理解为linux内核处理网络堆栈的一个框架,提供了钩子函数用于其它内核模块(iptables和nftables)具体实现网络数据处理方法;

iptables与nftables:iptables与nftables可以理解为具有相同功能的内核模块,作用是实现了netfilter提供的钩子函数,用于真正处理网络数据包,我们称之为防火墙软件,nftables的出现是用于替换iptables;

ufw,firewalld,iptalbes配置工具“iptables”和nftables配置工具“nft”:ufw,firewalld,iptables(这里指的是iptables配置工具,不同于iptables内核模块),nft等,这些软件是属于用户工具,用于管理iptables和nftables防火墙内核模块的,他们的作用是相同的,但执行命令和管理方式有差异。

iptables与nftables的区别

iptables与nftables同属于netfilter组织,两者都是用于实现netfilter框架用于处理网络数据包的钩子函数,iptables应用时间早于nftables,nftables在较新的系统中作为默认的防火墙软件,有替换iptables的趋势。下面举例说明不同版本的系统使用的防火墙软件:

系统名称版本防火墙软件

ubuntu

20.04iptables
ubuntu22.04

nftables

centos7iptables
centos8nftables

ufw,firewalld,iptables(管理工具)与iptables的关系

在较旧的操作系统中,如ubuntu20.04,centos7等,ufw,firewalld,iptables(管理工具)有相同的作用,都是用于配置iptables防火墙内核模块。

在ubuntu上我们常用ufw作为防火墙配置工具,在centos上用firewalld作为防火墙配置工具,实际上,不管是使用ufw,firewalld,还是执行iptables配置命令,最终都是作用于iptables防火墙内核模块,只是他们执行的命令和管理方式不同。

在ubuntu20.04系统上启用ufw并使用ufw执行防火墙命令时,产生的结果可以通过执行iptables命令(iptables -L)查看;在centos7上使用firewalld管理防火墙时,命令执行的结果同样也可以通过执行iptables命令(iptables -L)查看,实际上ufw和firewalld是对iptables配置工具进行了高级封装,使得管理员在配置防火墙时可以通过执行相对简单,更容易理解的ufw或firewall-cmd命令配置防火墙,而不是通过执行复杂难懂的iptables命令配置防火墙,相当于把更简单易懂的ufw或firewall-cmd命令转换为复杂难懂的iptables命令配置iptables防火墙

ufw,firewalld,nft(nftables配置工具)与nftables的关系

在较新的系统中,如ubuntu22.04,centos8等,ufw,firewalld,nft(nftables配置工具)有相同的作用,都是用于配置nftables防火墙内核模块。

在ubuntu22.04系统中,启用ufw并执行防火墙配置命令后,我们不仅可以通过nft命令查看防火墙规则,也可以使用iptables配置工具查看配置的防火墙规则。在这里我们也许会疑惑,新的系统中默认使用nftables作为防火墙软件,那么ufw软件对nft管理工具进行高级封装,对nftables防火墙进行配置,所以ufw和nft都可以用于配置和查看nftables防火墙规则,这个好理解,但为什么iptables配置工具也可以管理nftables防火墙呢?

为解决上述疑惑,我们进入ubuntu22.04系统进行确认

  1. # 在ubuntu22.04上确认iptables的真实软件
  2. ubuntu@22.04:~$ which iptables
  3. /usr/sbin/iptables
  4. ubuntu@22.04:~$ ll /usr/sbin/iptables
  5. lrwxrwxrwx 1 root root 26 Aug 28 07:13 /usr/sbin/iptables -> /etc/alternatives/iptables*
  6. ubuntu@22.04:~$ ll /etc/alternatives/iptables
  7. lrwxrwxrwx 1 root root 22 Aug 28 07:13 /etc/alternatives/iptables -> /usr/sbin/iptables-nft*
  8. ubuntu@22.04:~$ ll /usr/sbin/iptables-nft
  9. lrwxrwxrwx 1 root root 17 Apr 26 22:40 /usr/sbin/iptables-nft -> xtables-nft-multi*
  10. ubuntu@22.04:~$

通过上述命令,我们可以发现,在ubuntu22.04上,iptables的真实应用程序是“xtables-nft-multi”,这是一款兼容iptables命令的nftables管理工具,也就是说用户可以通过执行iptables命令管理nftables防火墙,iptables和nft命令之间的差异对用户透明,由“xtables-nft-multi”负责将iptables命令翻译为nft命令。

上面我们说到了ufw软件对nft进行了高级封装,这里是一个误区,实际上ufw仍然是对iptables软件进行高级封装,在这里,实际上是对“xtables-nft-multi”进行了封装,这也就不难理解为什么通过iptables命令能够查看到ufw配置的防火墙规则了。

firewalld又是怎么回事呢,其实也不是说ubuntu必须用ufw,centos必须用firewalld,在ubuntu系统上同样可以使用firewalld管理防火墙,在ubuntu22.04系统中,安装的firewalld(version1.1.1)可以直接配置nftables防火墙,通过nft命令可以查看具体配置信息,但通过iptables命令无法查看配置。

其实我们不需要太关心firewalld或ufw是怎么进行高级封装的,也不需要关心iptables或nft命令是怎么配置防火墙的,我们应当关心的是防火墙的配置是怎么作用于网络数据包的。

运行时配置和永久配置

不管是iptables还是nftables防火墙都分为运行时配置和永久配置。

运行时配置是指正应用于系统,对网络数据包进行动作的配置,当系统重启或服务重新加载配置时会丢失运行时配置;永久配置是记录在防火墙管理软件的配置文件中,当系统重启或防火墙管理软件重新加载配置文件时,从永久配置文件中读取配置信息并作为防火墙的运行时配置。

netfilter架构示意图

netfilter钩子函数及iptables内置链

netfilter的钩子函数与iptables的内置链

NF_IP_PRE_ROUTING(iptables链:PREROUTING):进入网络堆栈后,任何传入流量都会很快触发此钩子函数。在做出有关将数据包发送到何处的任何路由决策之前执行此钩子函数。

NF_IP_LOCAL_IN(iptables链:INPUT):如果传入数据包的目的地是本地系统,则在路由传入数据包后会触发此钩子函数。

NF_IP_FORWARD(iptables链:FORWARD):如果传入数据包要转发到另一台主机,则在路由传入数据包后会触发此钩子函数。

NF_IP_LOCAL_OUT(iptables链:OUTPUT):任何本地创建的出站流量一旦到达网络堆栈就会触发此钩子函数。

NF_IP_POST_ROUTING(iptables链:POSTROUTING):在路由发生之后且在线路上发送出去之前,任何传出或转发的流量都会触发此挂钩。

iptalbes表与链

iptables有多个表,用于管理不同功能的链,不同的表执行优先级不同,一个表可以包含多个链,相同的链也可以存在于多个表中

iptables处理数据包的规则(表与链)

Tables↓/Chains→PREROUTINGINPUTFORWARDOUTPUTPOSTROUTING
(routing decision)
raw
(connection tracking enabled)
mangle
nat (DNAT)
(routing decision)
filter
security
nat (SNAT)

当数据包触发netfilter钩子时,相关链将按照上表中从上到下列出的方式进行处理。数据包将触发的钩子(列)取决于它是传入还是传出数据包、所做的路由决策以及数据包是否通过过滤标准。

  • 发往本地系统的传入数据包PREROUTING->INPUT
  • 发往另一台主机的传入数据包PREROUTING-> FORWARD->POSTROUTING
  • 本地生成的数据包OUTPUT->POSTROUTING

iptables常用命令演示

命令参考

  1. root@ubuntu:~# iptables -h
  2. iptables v1.8.4
  3. Usage: iptables -[ACD] chain rule-specification [options]
  4. iptables -I chain [rulenum] rule-specification [options]
  5. iptables -R chain rulenum rule-specification [options]
  6. iptables -D chain rulenum [options]
  7. iptables -[LS] [chain [rulenum]] [options]
  8. iptables -[FZ] [chain] [options]
  9. iptables -[NX] chain
  10. iptables -E old-chain-name new-chain-name
  11. iptables -P chain target [options]
  12. iptables -h (print this help information)
  13. Commands:
  14. Either long or short options are allowed.
  15. --append -A chain Append to chain
  16. --check -C chain Check for the existence of a rule
  17. --delete -D chain Delete matching rule from chain
  18. --delete -D chain rulenum
  19. Delete rule rulenum (1 = first) from chain
  20. --insert -I chain [rulenum]
  21. Insert in chain as rulenum (default 1=first)
  22. --replace -R chain rulenum
  23. Replace rule rulenum (1 = first) in chain
  24. --list -L [chain [rulenum]]
  25. List the rules in a chain or all chains
  26. --list-rules -S [chain [rulenum]]
  27. Print the rules in a chain or all chains
  28. --flush -F [chain] Delete all rules in chain or all chains
  29. --zero -Z [chain [rulenum]]
  30. Zero counters in chain or all chains
  31. --new -N chain Create a new user-defined chain
  32. --delete-chain
  33. -X [chain] Delete a user-defined chain
  34. --policy -P chain target
  35. Change policy on chain to target
  36. --rename-chain
  37. -E old-chain new-chain
  38. Change chain name, (moving any references)
  39. Options:
  40. --ipv4 -4 Nothing (line is ignored by ip6tables-restore)
  41. --ipv6 -6 Error (line is ignored by iptables-restore)
  42. [!] --protocol -p proto protocol: by number or name, eg. `tcp'
  43. [!] --source -s address[/mask][...]
  44. source specification
  45. [!] --destination -d address[/mask][...]
  46. destination specification
  47. [!] --in-interface -i input name[+]
  48. network interface name ([+] for wildcard)
  49. --jump -j target
  50. target for rule (may load target extension)
  51. --goto -g chain
  52. jump to chain with no return
  53. --match -m match
  54. extended match (may load extension)
  55. --numeric -n numeric output of addresses and ports
  56. [!] --out-interface -o output name[+]
  57. network interface name ([+] for wildcard)
  58. --table -t table table to manipulate (default: `filter')
  59. --verbose -v verbose mode
  60. --wait -w [seconds] maximum wait to acquire xtables lock before give up
  61. --wait-interval -W [usecs] wait time to try to acquire xtables lock
  62. default is 1 second
  63. --line-numbers print line numbers when listing
  64. --exact -x expand numbers (display exact values)
  65. [!] --fragment -f match second or further fragments only
  66. --modprobe=<command> try to insert modules using this command
  67. --set-counters PKTS BYTES set the counter during insert/append
  68. [!] --version -V print package version.

包过滤示例

iptables的默认表为filter

示例1:发往本地系统的传入数据包:PREROUTING->INPUT

主机A:192.168.21.135

主机B:192.168.21.136

主机A不配置iptables规则的情况下,主机B ping 主机A可以通

  1. # 主机hosta
  2. # 不配置iptables规则,默认为filter表
  3. root@hosta:~# iptables -nvL
  4. Chain INPUT (policy ACCEPT 995 packets, 65404 bytes)
  5. pkts bytes target prot opt in out source destination
  6. Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
  7. pkts bytes target prot opt in out source destination
  8. Chain OUTPUT (policy ACCEPT 735 packets, 91416 bytes)
  9. pkts bytes target prot opt in out source destination
  10. root@hosta:~#
  11. # 主机hostb
  12. # 可以ping通主机A
  13. [root@hostb ~]# ping 192.168.21.135 -c 2
  14. PING 192.168.21.135 (192.168.21.135) 56(84) bytes of data.
  15. 64 bytes from 192.168.21.135: icmp_seq=1 ttl=64 time=0.614 ms
  16. 64 bytes from 192.168.21.135: icmp_seq=2 ttl=64 time=0.945 ms
  17. --- 192.168.21.135 ping statistics ---
  18. 2 packets transmitted, 2 received, 0% packet loss, time 1002ms
  19. rtt min/avg/max/mdev = 0.614/0.779/0.945/0.167 ms
  20. [root@hostb ~]#

主机A配置iptables禁用icmp协议后,主要B不能ping通主机A

  1. # 主机A
  2. # -A 在未尾追加规则
  3. # -p 协议
  4. # -s 源地址
  5. # -d 目标地址
  6. # -j 动作
  7. # 以下命令禁止192.168.21.136的主机ping 192.168.21.135的主机
  8. root@hosta:~# iptables -A INPUT -p icmp -s 192.168.21.136 -d 192.168.21.135 -j DROP
  9. # 查看iptables规则
  10. root@hosta:~# iptables -nvL
  11. Chain INPUT (policy ACCEPT 28 packets, 1624 bytes)
  12. pkts bytes target prot opt in out source destination
  13. 0 0 DROP icmp -- * * 192.168.21.136 192.168.21.135
  14. Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
  15. pkts bytes target prot opt in out source destination
  16. Chain OUTPUT (policy ACCEPT 15 packets, 1172 bytes)
  17. pkts bytes target prot opt in out source destination
  18. root@hosta:~#
  19. # 主机B
  20. # 不能ping通主机A
  21. [root@hostb ~]# ping 192.168.21.135 -c 2
  22. PING 192.168.21.135 (192.168.21.135) 56(84) bytes of data.
  23. --- 192.168.21.135 ping statistics ---
  24. 2 packets transmitted, 0 received, 100% packet loss, time 1001ms
  25. [root@hostb ~]#
  26. # 其它主机
  27. # 可以ping通主机A
  28. C:\Users\49285>ping 192.168.21.135
  29. 正在 Ping 192.168.21.135 具有 32 字节的数据:
  30. 来自 192.168.21.135 的回复: 字节=32 时间<1ms TTL=64
  31. 来自 192.168.21.135 的回复: 字节=32 时间=1ms TTL=64
  32. 来自 192.168.21.135 的回复: 字节=32 时间<1ms TTL=64
  33. 来自 192.168.21.135 的回复: 字节=32 时间<1ms TTL=64
  34. 192.168.21.135 的 Ping 统计信息:
  35. 数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
  36. 往返行程的估计时间(以毫秒为单位):
  37. 最短 = 0ms,最长 = 1ms,平均 = 0ms
  38. C:\Users\49285>

在上面的示例中,我们是将规则添加到了filter表的INPUT链中,filter包含“INPUT”,“FORWARD”,“OUTPUT”链,试想一下,如果我们将规则添加到“OUTPUT”链中,能达到同样的效果吗?下面我们来演示一下

  1. # 主机A
  2. # 清除之前的规则
  3. root@hosta:~# iptables -D INPUT -p icmp -s 192.168.21.136 -d 192.168.21.135 -j DROP
  4. # 在filter表中的OUTPUT链中添加规则
  5. root@hosta:~# iptables -A OUTPUT -p icmp -s 192.168.21.136 -d 192.168.21.135 -j DROP
  6. # 查看iptables规则
  7. root@hosta:~# iptables -nvL
  8. Chain INPUT (policy ACCEPT 28 packets, 1624 bytes)
  9. pkts bytes target prot opt in out source destination
  10. Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
  11. pkts bytes target prot opt in out source destination
  12. Chain OUTPUT (policy ACCEPT 15 packets, 1172 bytes)
  13. pkts bytes target prot opt in out source destination
  14. 0 0 DROP icmp -- * * 192.168.21.136 192.168.21.135
  15. # 主机B
  16. # 可以ping通主机A
  17. [root@hostb ~]# ping 192.168.21.135 -c 2
  18. PING 192.168.21.135 (192.168.21.135) 56(84) bytes of data.
  19. 64 bytes from 192.168.21.135: icmp_seq=1 ttl=64 time=0.651 ms
  20. 64 bytes from 192.168.21.135: icmp_seq=2 ttl=64 time=1.47 ms
  21. --- 192.168.21.135 ping statistics ---
  22. 2 packets transmitted, 2 received, 0% packet loss, time 1003ms
  23. rtt min/avg/max/mdev = 0.651/1.062/1.473/0.411 ms
  24. [root@hostb ~]#

在上面的示例中,我们看到如果将iptables规则添加到filter表的OUTPUT链中,不能阻止主机B ping通主机A,原因是发往本地系统的传入数据包(PREROUTING->INPUT)不通过OUTPUT链。

示例2:发往另一台主机的传入数据包:PREROUTING-> FORWARD->POSTROUTING

通过对iptables数据包处理规则进行分析,需要过滤发往另一台主机的传入数据包时,应当在filter表的“FORWARD”链添加规则

示例3:本地生成的数据包:OUTPUT->POSTROUTING

在过滤本地生成的数据包时,我们应当在filter表的“OUTPUT”链添加规则

网络地址转换示例

示例:客户机通过主机B访问主机A的web服务

主机A:192.168.21.135,nginx测试网站(80端口)

主机B:192.168.21.136,配置iptables

数据访问:客户机 --> 主机B:8888 --> 主机A:80

环境确认:

客户机可以访问主机A的测试网站

客户机不能访问http://192.168.21.136:8888/

在主机B上配置iptables

  1. # 主机B
  2. # 启用路由转发功能
  3. [root@hostb ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
  4. # 配置端口转发
  5. [root@hostb ~]# iptables -t nat -A PREROUTING -p tcp --dport 8888 -j DNAT --to-destination 192.168.21.135:80
  6. # 配置源地址转换,否则不能建立tcp连接(当客户机和服务器在不同网段,相互访问时路由都经过主机B的情况下可以不启用源地址转换)
  7. iptables -t nat -A POSTROUTING --dst 192.168.21.135 -p tcp --dport 80 -j SNAT --to-source 192.168.21.136
  8. # 查看iptables nat表的配置
  9. [root@hostb ~]# iptables -t nat -nvL
  10. Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
  11. pkts bytes target prot opt in out source destination
  12. 68 3536 DNAT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8888 to:192.168.21.135:80
  13. Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
  14. pkts bytes target prot opt in out source destination
  15. Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
  16. pkts bytes target prot opt in out source destination
  17. Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
  18. pkts bytes target prot opt in out source destination
  19. 2 104 SNAT tcp -- * * 0.0.0.0/0 192.168.21.135 tcp dpt:80 to:192.168.21.136
  20. [root@hostb ~]#

客户机可以成功访问http://192.168.21.136:8888/

在主机A上抓包查看到,主机A上的请求来自于主机B

  1. root@hosta:~# tcpdump tcp port 80
  2. tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
  3. listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
  4. 23:52:27.222612 IP 192.168.21.136.55914 > 192.168.21.135.http: Flags [S], seq 2649980519, win 64240, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0
  5. 23:52:27.222678 IP 192.168.21.135.http > 192.168.21.136.55914: Flags [S.], seq 2076304222, ack 2649980520, win 64240, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 0
  6. 23:52:27.223275 IP 192.168.21.136.55915 > 192.168.21.135.http: Flags [S], seq 4081047654, win 64240, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0
  7. 23:52:27.223275 IP 192.168.21.136.55914 > 192.168.21.135.http: Flags [.], ack 1, win 4106, length 0
  8. 23:52:27.223303 IP 192.168.21.135.http > 192.168.21.136.55915: Flags [S.], seq 2019065736, ack 4081047655, win 64240, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 0
  9. 23:52:27.223983 IP 192.168.21.136.55914 > 192.168.21.135.http: Flags [P.], seq 1:544, ack 1, win 4106, length 543: HTTP: GET / HTTP/1.1
  10. 23:52:27.223983 IP 192.168.21.136.55915 > 192.168.21.135.http: Flags [.], ack 1, win 4106, length 0
  11. 23:52:27.224051 IP 192.168.21.135.http > 192.168.21.136.55914: Flags [.], ack 544, win 501, length 0
  12. 23:52:27.224714 IP 192.168.21.135.http > 192.168.21.136.55914: Flags [P.], seq 1:190, ack 544, win 501, length 189: HTTP: HTTP/1.1 304 Not Modified
  13. 23:52:27.269742 IP 192.168.21.136.55914 > 192.168.21.135.http: Flags [.], ack 190, win 4105, length 0

nftables常用命令演示

在较新的系统上(centos8或ubuntu22等),nftables作为默认的防火墙软件,nft作为nftables的配置工具

nft命令参考

  1. └─# nft -h
  2. Usage: nft [ options ] [ cmds... ]
  3. Options (general):
  4. -h, --help Show this help
  5. -v, --version Show version information
  6. -V Show extended version information
  7. Options (ruleset input handling):
  8. -f, --file <filename> Read input from <filename>
  9. -D, --define <name=value> Define variable, e.g. --define foo=1.2.3.4
  10. -i, --interactive Read input from interactive CLI
  11. -I, --includepath <directory> Add <directory> to the paths searched for include files. Default is: /etc
  12. -c, --check Check commands validity without actually applying the changes.
  13. -o, --optimize Optimize ruleset
  14. Options (ruleset list formatting):
  15. -a, --handle Output rule handle.
  16. -s, --stateless Omit stateful information of ruleset.
  17. -t, --terse Omit contents of sets.
  18. -S, --service Translate ports to service names as described in /etc/services.
  19. -N, --reversedns Translate IP addresses to names.
  20. -u, --guid Print UID/GID as defined in /etc/passwd and /etc/group.
  21. -n, --numeric Print fully numerical output.
  22. -y, --numeric-priority Print chain priority numerically.
  23. -p, --numeric-protocol Print layer 4 protocols numerically.
  24. -T, --numeric-time Print time values numerically.
  25. Options (command output formatting):
  26. -e, --echo Echo what has been added, inserted or replaced.
  27. -j, --json Format output in JSON
  28. -d, --debug <level [,level...]> Specify debugging level (scanner, parser, eval, netlink, mnl, proto-ctx, segtree, all)

nft命令与iptables命令语法不同,但只要熟悉iptables命令,可以通过iptables-translate将iptables命令转换成nft命令,有时也可以直接通过iptables命令配置nftables防火墙,这是因为执行iptables命令时,实际上是调用了兼容iptables命令的nftables配置工具,下面以kali linux为例演示

  1. ┌──(root㉿kali)-[~]
  2. └─# ip add show dev eth0
  3. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
  4. link/ether 00:0c:29:0d:d7:f9 brd ff:ff:ff:ff:ff:ff
  5. inet 192.168.21.132/24 brd 192.168.21.255 scope global dynamic noprefixroute eth0
  6. valid_lft 1777sec preferred_lft 1777sec
  7. inet6 fe80::29ba:2e02:382a:5a49/64 scope link noprefixroute
  8. valid_lft forever preferred_lft forever
  9. # 将iptables命令转换成nft命令
  10. ┌──(root㉿kali)-[~]
  11. └─# iptables-translate -A INPUT -p icmp -d 192.168.21.132 -j DROP
  12. nft 'add rule ip filter INPUT ip protocol icmp ip daddr 192.168.21.132 counter drop'
  13. ┌──(root㉿kali)-[~]
  14. └─#
  15. # 查看iptables命令调用的工具软件
  16. ┌──(root㉿kali)-[~]
  17. └─# which iptables
  18. /usr/sbin/iptables
  19. ┌──(root㉿kali)-[~]
  20. └─# ll /usr/sbin/iptables
  21. lrwxrwxrwx 1 root root 26 Jan 16 2023 /usr/sbin/iptables -> /etc/alternatives/iptables
  22. ┌──(root㉿kali)-[~]
  23. └─# ll /etc/alternatives/iptables
  24. lrwxrwxrwx 1 root root 22 Aug 21 14:58 /etc/alternatives/iptables -> /usr/sbin/iptables-nft
  25. ┌──(root㉿kali)-[~]
  26. └─# ll /usr/sbin/iptables-nft
  27. lrwxrwxrwx 1 root root 17 Jan 16 2023 /usr/sbin/iptables-nft -> xtables-nft-multi

通过上面的命令演示我们可以看到,执行iptables命令时,调用的是兼容iptables命令语法的nftables配置工具(xtables-nft-multi)

下面我们演示包过滤配置,禁止客户端ping kali linux主机

配置nftables规则前环境确认

  1. # kali linux
  2. # 配置前
  3. # 清空nftables规则
  4. ┌──(root㉿kali)-[~]
  5. └─# ip add show dev eth0
  6. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
  7. link/ether 00:0c:29:0d:d7:f9 brd ff:ff:ff:ff:ff:ff
  8. inet 192.168.21.132/24 brd 192.168.21.255 scope global dynamic noprefixroute eth0
  9. valid_lft 1341sec preferred_lft 1341sec
  10. inet6 fe80::29ba:2e02:382a:5a49/64 scope link noprefixroute
  11. valid_lft forever preferred_lft forever
  12. ┌──(root㉿kali)-[~]
  13. └─# nft flush ruleset
  14. ┌──(root㉿kali)-[~]
  15. └─# nft list ruleset
  16. # 客户端可以ping通kali linux
  17. C:\Users\49285>ping 192.168.21.132
  18. 正在 Ping 192.168.21.132 具有 32 字节的数据:
  19. 来自 192.168.21.132 的回复: 字节=32 时间<1ms TTL=64
  20. 来自 192.168.21.132 的回复: 字节=32 时间<1ms TTL=64
  21. 来自 192.168.21.132 的回复: 字节=32 时间<1ms TTL=64
  22. 来自 192.168.21.132 的回复: 字节=32 时间<1ms TTL=64
  23. 192.168.21.132 的 Ping 统计信息:
  24. 数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
  25. 往返行程的估计时间(以毫秒为单位):
  26. 最短 = 0ms,最长 = 0ms,平均 = 0ms
  27. C:\Users\49285>

配置nftables规则

  1. # kali linux
  2. # 直接使用iptables命令配置nftables防火墙
  3. ┌──(root㉿kali)-[~]
  4. └─# iptables -A INPUT -p icmp -d 192.168.21.132 -j DROP
  5. # 使用nft查看nftables规则
  6. ┌──(root㉿kali)-[~]
  7. └─# nft list ruleset
  8. table ip filter {
  9. chain INPUT {
  10. type filter hook input priority filter; policy accept;
  11. meta l4proto icmp ip daddr 192.168.21.132 counter packets 0 bytes 0 drop
  12. }
  13. }
  14. # 配置完nftables规则后,客户端ping不通kali linux
  15. C:\Users\49285>ping 192.168.21.132
  16. 正在 Ping 192.168.21.132 具有 32 字节的数据:
  17. 请求超时。
  18. 请求超时。
  19. 请求超时。
  20. 请求超时。
  21. 192.168.21.132 的 Ping 统计信息:
  22. 数据包: 已发送 = 4,已接收 = 0,丢失 = 4 (100% 丢失),
  23. C:\Users\49285>

ufw以及firewalld

ufw、firewalld,以及iptables配置工具和nft配置工具,他们的作用都是为了配置iptables防火墙或nftables防火墙,只是命令执行的语法不同而已,一般情况下,linux系统都自带iptables命令工具,centos系统的高级封装软件一般用firewalld,ubuntu则使用ufw,选择自己熟悉的工具即可,尽量不要混合使用,容易导致配置混乱而发生不可预知的网络问题。

总结

iptables防火墙是linux系统非常流行的防火墙软件,不管是ufw,firewalld还是iptables命令配置工具都只是防火墙的配置管理工具,对网络数据包进行动作是依据netfilter框架的钩子函数,钩子函数由iptables进行重写,用于确定如何对数据包进行处理并将结果返回给netfilter框架。

nftables我们可以理解为iptables的替代软件,实现方式不一样,但实现的功能相同,都是通过重写netfilter框架提供的钩子函数实现对网络数据包的处理。

种草

本文为"计算机网络实用工具系列"的内容之一,会持续更新其它相关博文。

我的博文内容主要针对“计算机网络”、“安全”、“运维”和“云计算”方向,感兴趣朋友的请关注我,我将不定期发布新的博文并不断改进已发布博文。

后期依据大家对博文的评论,点赞及关注情况,针对大家感兴趣的内容我也会录制视频并整理出成套的学习资料免费分享给大家,期待能和大家一起交流学习。

 参考资料

https://www.netfilter.org/

A Deep Dive into Iptables and Netfilter Architecture | DigitalOcean

导览:计算机网络基础、进阶、安全实践

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/828677
推荐阅读
相关标签
  

闽ICP备14008679号