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

简介

名称解释

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系统进行确认

# 在ubuntu22.04上确认iptables的真实软件
ubuntu@22.04:~$ which iptables
/usr/sbin/iptables

ubuntu@22.04:~$ ll /usr/sbin/iptables
lrwxrwxrwx 1 root root 26 Aug 28 07:13 /usr/sbin/iptables -> /etc/alternatives/iptables*

ubuntu@22.04:~$ ll /etc/alternatives/iptables
lrwxrwxrwx 1 root root 22 Aug 28 07:13 /etc/alternatives/iptables -> /usr/sbin/iptables-nft*

ubuntu@22.04:~$ ll /usr/sbin/iptables-nft
lrwxrwxrwx 1 root root 17 Apr 26 22:40 /usr/sbin/iptables-nft -> xtables-nft-multi*
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常用命令演示

命令参考

root@ubuntu:~# iptables -h
iptables v1.8.4

Usage: iptables -[ACD] chain rule-specification [options]
       iptables -I chain [rulenum] rule-specification [options]
       iptables -R chain rulenum rule-specification [options]
       iptables -D chain rulenum [options]
       iptables -[LS] [chain [rulenum]] [options]
       iptables -[FZ] [chain] [options]
       iptables -[NX] chain
       iptables -E old-chain-name new-chain-name
       iptables -P chain target [options]
       iptables -h (print this help information)

Commands:
Either long or short options are allowed.
  --append  -A chain		Append to chain
  --check   -C chain		Check for the existence of a rule
  --delete  -D chain		Delete matching rule from chain
  --delete  -D chain rulenum
				Delete rule rulenum (1 = first) from chain
  --insert  -I chain [rulenum]
				Insert in chain as rulenum (default 1=first)
  --replace -R chain rulenum
				Replace rule rulenum (1 = first) in chain
  --list    -L [chain [rulenum]]
				List the rules in a chain or all chains
  --list-rules -S [chain [rulenum]]
				Print the rules in a chain or all chains
  --flush   -F [chain]		Delete all rules in  chain or all chains
  --zero    -Z [chain [rulenum]]
				Zero counters in chain or all chains
  --new     -N chain		Create a new user-defined chain
  --delete-chain
            -X [chain]		Delete a user-defined chain
  --policy  -P chain target
				Change policy on chain to target
  --rename-chain
            -E old-chain new-chain
				Change chain name, (moving any references)
Options:
    --ipv4	-4		Nothing (line is ignored by ip6tables-restore)
    --ipv6	-6		Error (line is ignored by iptables-restore)
[!] --protocol	-p proto	protocol: by number or name, eg. `tcp'
[!] --source	-s address[/mask][...]
				source specification
[!] --destination -d address[/mask][...]
				destination specification
[!] --in-interface -i input name[+]
				network interface name ([+] for wildcard)
 --jump	-j target
				target for rule (may load target extension)
  --goto      -g chain
                              jump to chain with no return
  --match	-m match
				extended match (may load extension)
  --numeric	-n		numeric output of addresses and ports
[!] --out-interface -o output name[+]
				network interface name ([+] for wildcard)
  --table	-t table	table to manipulate (default: `filter')
  --verbose	-v		verbose mode
  --wait	-w [seconds]	maximum wait to acquire xtables lock before give up
  --wait-interval -W [usecs]	wait time to try to acquire xtables lock
				default is 1 second
  --line-numbers		print line numbers when listing
  --exact	-x		expand numbers (display exact values)
[!] --fragment	-f		match second or further fragments only
  --modprobe=<command>		try to insert modules using this command
  --set-counters PKTS BYTES	set the counter during insert/append
[!] --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可以通

# 主机hosta
# 不配置iptables规则,默认为filter表
root@hosta:~# iptables -nvL
Chain INPUT (policy ACCEPT 995 packets, 65404 bytes)
 pkts bytes target     prot opt in     out     source               destination         

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

Chain OUTPUT (policy ACCEPT 735 packets, 91416 bytes)
 pkts bytes target     prot opt in     out     source               destination         
root@hosta:~# 

# 主机hostb
# 可以ping通主机A
[root@hostb ~]# ping 192.168.21.135 -c 2
PING 192.168.21.135 (192.168.21.135) 56(84) bytes of data.
64 bytes from 192.168.21.135: icmp_seq=1 ttl=64 time=0.614 ms
64 bytes from 192.168.21.135: icmp_seq=2 ttl=64 time=0.945 ms

--- 192.168.21.135 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.614/0.779/0.945/0.167 ms
[root@hostb ~]# 

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

# 主机A
# -A 在未尾追加规则
# -p 协议
# -s 源地址
# -d 目标地址
# -j 动作
# 以下命令禁止192.168.21.136的主机ping 192.168.21.135的主机
root@hosta:~# iptables -A INPUT -p icmp -s 192.168.21.136 -d 192.168.21.135 -j DROP

# 查看iptables规则
root@hosta:~# iptables -nvL
Chain INPUT (policy ACCEPT 28 packets, 1624 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       icmp --  *      *       192.168.21.136       192.168.21.135      

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

Chain OUTPUT (policy ACCEPT 15 packets, 1172 bytes)
 pkts bytes target     prot opt in     out     source               destination         
root@hosta:~# 


# 主机B
# 不能ping通主机A
[root@hostb ~]# ping 192.168.21.135 -c 2
PING 192.168.21.135 (192.168.21.135) 56(84) bytes of data.

--- 192.168.21.135 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1001ms

[root@hostb ~]# 

# 其它主机
# 可以ping通主机A
C:\Users\49285>ping 192.168.21.135

正在 Ping 192.168.21.135 具有 32 字节的数据:
来自 192.168.21.135 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.21.135 的回复: 字节=32 时间=1ms TTL=64
来自 192.168.21.135 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.21.135 的回复: 字节=32 时间<1ms TTL=64

192.168.21.135 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 1ms,平均 = 0ms

C:\Users\49285>

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

# 主机A
# 清除之前的规则
root@hosta:~# iptables -D INPUT -p icmp -s 192.168.21.136 -d 192.168.21.135 -j DROP

# 在filter表中的OUTPUT链中添加规则
root@hosta:~# iptables -A OUTPUT -p icmp -s 192.168.21.136 -d 192.168.21.135 -j DROP

# 查看iptables规则
root@hosta:~# iptables -nvL
Chain INPUT (policy ACCEPT 28 packets, 1624 bytes)
 pkts bytes target     prot opt in     out     source               destination         

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

Chain OUTPUT (policy ACCEPT 15 packets, 1172 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       icmp --  *      *       192.168.21.136       192.168.21.135

# 主机B
# 可以ping通主机A
[root@hostb ~]# ping 192.168.21.135 -c 2
PING 192.168.21.135 (192.168.21.135) 56(84) bytes of data.
64 bytes from 192.168.21.135: icmp_seq=1 ttl=64 time=0.651 ms
64 bytes from 192.168.21.135: icmp_seq=2 ttl=64 time=1.47 ms

--- 192.168.21.135 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1003ms
rtt min/avg/max/mdev = 0.651/1.062/1.473/0.411 ms
[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

# 主机B
# 启用路由转发功能
[root@hostb ~]# echo 1 > /proc/sys/net/ipv4/ip_forward

# 配置端口转发
[root@hostb ~]# iptables -t nat -A PREROUTING -p tcp --dport 8888 -j DNAT --to-destination 192.168.21.135:80

# 配置源地址转换,否则不能建立tcp连接(当客户机和服务器在不同网段,相互访问时路由都经过主机B的情况下可以不启用源地址转换)
iptables -t nat -A POSTROUTING --dst 192.168.21.135 -p tcp --dport 80 -j SNAT --to-source 192.168.21.136

# 查看iptables nat表的配置
[root@hostb ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   68  3536 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:8888 to:192.168.21.135:80

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

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

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    2   104 SNAT       tcp  --  *      *       0.0.0.0/0            192.168.21.135       tcp dpt:80 to:192.168.21.136
[root@hostb ~]# 

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

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

root@hosta:~# tcpdump tcp port 80
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
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
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
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
23:52:27.223275 IP 192.168.21.136.55914 > 192.168.21.135.http: Flags [.], ack 1, win 4106, length 0
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
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
23:52:27.223983 IP 192.168.21.136.55915 > 192.168.21.135.http: Flags [.], ack 1, win 4106, length 0
23:52:27.224051 IP 192.168.21.135.http > 192.168.21.136.55914: Flags [.], ack 544, win 501, length 0
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
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命令参考

└─# nft -h          
Usage: nft [ options ] [ cmds... ]

Options (general):
  -h, --help                      Show this help
  -v, --version                   Show version information
  -V                              Show extended version information

Options (ruleset input handling):
  -f, --file <filename>           Read input from <filename>
  -D, --define <name=value>       Define variable, e.g. --define foo=1.2.3.4
  -i, --interactive               Read input from interactive CLI
  -I, --includepath <directory>   Add <directory> to the paths searched for include files. Default is: /etc
  -c, --check                     Check commands validity without actually applying the changes.
  -o, --optimize                  Optimize ruleset

Options (ruleset list formatting):
  -a, --handle                    Output rule handle.
  -s, --stateless                 Omit stateful information of ruleset.
  -t, --terse                     Omit contents of sets.
  -S, --service                   Translate ports to service names as described in /etc/services.
  -N, --reversedns                Translate IP addresses to names.
  -u, --guid                      Print UID/GID as defined in /etc/passwd and /etc/group.
  -n, --numeric                   Print fully numerical output.
  -y, --numeric-priority          Print chain priority numerically.
  -p, --numeric-protocol          Print layer 4 protocols numerically.
  -T, --numeric-time              Print time values numerically.

Options (command output formatting):
  -e, --echo                      Echo what has been added, inserted or replaced.
  -j, --json                      Format output in JSON
  -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为例演示

┌──(root㉿kali)-[~]
└─# ip add show dev eth0 
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:0d:d7:f9 brd ff:ff:ff:ff:ff:ff
    inet 192.168.21.132/24 brd 192.168.21.255 scope global dynamic noprefixroute eth0
       valid_lft 1777sec preferred_lft 1777sec
    inet6 fe80::29ba:2e02:382a:5a49/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
                                                                                                                               
# 将iptables命令转换成nft命令
┌──(root㉿kali)-[~]
└─# iptables-translate -A INPUT -p icmp -d 192.168.21.132 -j DROP
nft 'add rule ip filter INPUT ip protocol icmp ip daddr 192.168.21.132 counter drop'
                                                                                                                               
┌──(root㉿kali)-[~]
└─# 

# 查看iptables命令调用的工具软件
┌──(root㉿kali)-[~]
└─# which iptables
/usr/sbin/iptables
                                                                                                                               
┌──(root㉿kali)-[~]
└─# ll /usr/sbin/iptables
lrwxrwxrwx 1 root root 26 Jan 16  2023 /usr/sbin/iptables -> /etc/alternatives/iptables
                                                                                                                               
┌──(root㉿kali)-[~]
└─# ll /etc/alternatives/iptables
lrwxrwxrwx 1 root root 22 Aug 21 14:58 /etc/alternatives/iptables -> /usr/sbin/iptables-nft
                                                                                                                               
┌──(root㉿kali)-[~]
└─# ll /usr/sbin/iptables-nft    
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规则前环境确认

# kali linux
# 配置前
# 清空nftables规则
┌──(root㉿kali)-[~]
└─# ip add show dev eth0                                         
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:0d:d7:f9 brd ff:ff:ff:ff:ff:ff
    inet 192.168.21.132/24 brd 192.168.21.255 scope global dynamic noprefixroute eth0
       valid_lft 1341sec preferred_lft 1341sec
    inet6 fe80::29ba:2e02:382a:5a49/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

┌──(root㉿kali)-[~]
└─# nft flush ruleset
                                                                                                                               
┌──(root㉿kali)-[~]
└─# nft list ruleset
                                                                                                                               

# 客户端可以ping通kali linux
C:\Users\49285>ping 192.168.21.132

正在 Ping 192.168.21.132 具有 32 字节的数据:
来自 192.168.21.132 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.21.132 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.21.132 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.21.132 的回复: 字节=32 时间<1ms TTL=64

192.168.21.132 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 0ms,平均 = 0ms

C:\Users\49285>

配置nftables规则

# kali linux
# 直接使用iptables命令配置nftables防火墙
┌──(root㉿kali)-[~]
└─# iptables -A INPUT -p icmp -d 192.168.21.132 -j DROP          
                                                                                                                               
# 使用nft查看nftables规则
┌──(root㉿kali)-[~]
└─# nft list ruleset
table ip filter {
        chain INPUT {
                type filter hook input priority filter; policy accept;
                meta l4proto icmp ip daddr 192.168.21.132 counter packets 0 bytes 0 drop
        }
}
                                                                                                                               
# 配置完nftables规则后,客户端ping不通kali linux
C:\Users\49285>ping 192.168.21.132

正在 Ping 192.168.21.132 具有 32 字节的数据:
请求超时。
请求超时。
请求超时。
请求超时。

192.168.21.132 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 0,丢失 = 4 (100% 丢失),

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

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

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hougang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值