zone
的介绍具和具体规则的添加服务,端口和协议,ICMP 阻塞,SNAT/DNAT,IP伪装,端口转发等Demo
firewall
离线命令(服务未启动规则预设方式)傍晚时分,你坐在屋檐下,看着天慢慢地黑下去,心里寂寞而凄凉,感到自己的生命被剥夺了。当时我是个年轻人,但我害怕这样生活下去,衰老下去。在我看来,这是比死亡更可怕的事。--------王小波
系统服务 firewalld 主要用于 管理防火墙链和规则,相对于 iptables.services
,它更灵活,表意性更强。对应的管理工具:firewall-cmd、firewall-config。
需要说明的是,如果你在启动 firewalld
服务之前,使用 ipatables
添加了一些防火墙规则,那么,在启动后,添加的规则可能会消失。iptables 是一个 内核功能的管理工具。他不需要任何服务就可以使用 ,有时可能仅仅需要安装它对应的工具,而 iptables.service
, firewalld.service
是用于 管理 iptables 链和规则的工具,它们存在的意义即可以在系统启动时自动加载保存的防火墙规则,在关机时卸载对应的规则。单纯的通过 iptables
命令设置的 防火墙规则是基于内存的,类似 /proc
目录一样,会随着系统重启消失。
所以如果你通过命令 iptables
配置了对应的规则。那么在开启 firewalld
之前一定要导出 之前配置的 iptables
规则。
┌──[root@vms152.liruilongs.github.io]-[~]
└─$iptables-save > ips
先来看下区域这个概念:
What is a zone?
A network zone defines the level of trust for network connections. This is a one to many
relation, which means that a connection can only be part of one zone, but a zone can be used
for many network connections.
The zone defines the firewall features that are enabled in this zone:
Predefined services
A service is a combination of port and/or protocol entries. Optionally netfilter helper
modules can be added and also a IPv4 and IPv6 destination address.
Ports and protocols
Definition of tcp or udp ports, where ports can be a single port or a port range.
ICMP blocks
Blocks selected Internet Control Message Protocol (ICMP) messages. These messages are
either information requests or created as a reply to information requests or in error
conditions.
Masquerading
The addresses of a private network are mapped to and hidden behind a public IP address.
This is a form of address translation.
Forward ports
A forward port is either mapped to the same port on another host or to another port on
the same host or to another port on another host.
Rich language rules
The rich language extends the elements (service, port, icmp-block, masquerade,
forward-port and source-port) with additional source and destination addresses, logging,
actions and limits for logs and actions. It can also be used for host or network white
and black listing (for more information, please have a look at
firewalld.richlanguage(5)).
For more information on the zone file format, please have a look at firewalld.zone(5).
帮助文档查看
┌──[root@vms152.liruilongs.github.io]-[/var/spool]
└─$man firewalld.zones| cat
区域用于定义了在该区启用的防火墙功能:一个网络区域定义了网络连接的信任级别
。这是一种一对多的的关系,这意味着一个连接只能是一个区域的一部分,但一个区可以被用于许多网络连接。可以把区域理解为一堆防火墙规则的别名。这些规则可以是常见的:服务,端口和协议,ICMP 阻塞,NAT,IP伪装,端口转发
等
查看当前防火墙的默认区域
┌──[root@vms153.liruilongs.github.io]-[~]
└─$firewall-cmd --state
running
┌──[root@vms153.liruilongs.github.io]-[~]
└─$firewall-cmd --get-default-zone
trusted
可以设置的区域
┌──[root@vms153.liruilongs.github.io]-[~]
└─$ firewall-cmd --get-zones
block dmz drop external home internal public trusted work
这些是由 firewalld 提供的区域,根据区域的默认信任级别从不受信任到受信任排序:
一般情况下,我们用的比较多是 trusted
和 public
这两个区域。trusted
一般用与添加类似黑名单一样的规则
,public
添加类似白名单规则,。
关于区域的其他的一些命令:
设置查看默认区域
┌──[root@vms152.liruilongs.github.io]-[~]
└─$firewall-cmd --set-default-zone=work
success
┌──[root@vms152.liruilongs.github.io]-[~]
└─$firewall-cmd --get-default-zone
work
指定网卡设置查看区域
┌──[root@vms152.liruilongs.github.io]-[~]
└─$firewall-cmd --get-zone-of-interface=ens32
no zone
┌──[root@vms152.liruilongs.github.io]-[~]
└─$firewall-cmd --zone=public --add-interface=ens32
success
┌──[root@vms152.liruilongs.github.io]-[~]
└─$firewall-cmd --get-zone-of-interface=ens32
public
查看所有网卡的区域
┌──[root@vms152.liruilongs.github.io]-[~]
└─$firewall-cmd --get-active-zones
public
interfaces: ens32
┌──[root@vms152.liruilongs.github.io]-[~]
└─$
设置好区域之后,我们需要添加对应的规则,这里我们以 public
和 trusted
这两个区域为例:
注意:添加完规则一定要重载才可以使配置生效
当前在public
区域,我们期望运行一些端口通信或者服务,添加某些允许的规则
┌──[root@vms152.liruilongs.github.io]-[~]
└─$firewall-cmd --add-service=http
success
┌──[root@vms152.liruilongs.github.io]-[~]
└─$firewall-cmd --add-port=30001/tcp
success
┌──[root@vms152.liruilongs.github.io]-[~]
└─$firewall-cmd --add-port=30005/udp
success
可以通过 firewall-cmd --list-all
查看当前端口的预设区域信息
┌──[root@vms152.liruilongs.github.io]-[~]
└─$firewall-cmd --list-all
trusted
target: ACCEPT
icmp-block-inversion: no
interfaces:
sources:
services: http
ports: 30001/tcp 30005/udp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks: timestamp-request timestamp-reply
rich rules:
┌──[root@vms152.liruilongs.github.io]-[~]
└─$
当前在 trusted
区域,我添加一些 icmp
协议的限制的规则,用于解决,ICMP 时间戳请求响应漏洞
icmp-block
规则┌──[root@vms16.liruilongs.github.io]-[~]
└─$firewall-cmd --add-icmp-block=timestamp-request --permanent
success
┌──[root@vms16.liruilongs.github.io]-[~]
└─$firewall-cmd --add-icmp-block=timestamp-reply --permanent
success
┌──[root@vms16.liruilongs.github.io]-[~]
└─$firewall-cmd --reload
success
┌──[root@vms16.liruilongs.github.io]-[~]
└─$firewall-cmd --list-icmp-blocks
timestamp-request timestamp-reply
查看区域的全部规则信息
┌──[root@vms16.liruilongs.github.io]-[~]
└─$firewall-cmd --list-all
trusted
target: ACCEPT
icmp-block-inversion: no
interfaces:
sources:
services:
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks: timestamp-request timestamp-reply
rich rules:
NAT 的出现,缓解了 IP 不够用的问题,使得我们可以通过 局域网访问到公网,实现私有 IP 到公共 IP 不同网段的访问,可以通过一台 Linux 机器,开启路由功能,通过 iptables 的 NAT 路由转发表实现。使用 firewalld,可以配置以下网络地址转换(NAT)类型:
需要做一些准备工作。开启 ipv4 的地址转发
┌──[root@vms152.liruilongs.github.io]-[~]
└─$echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
┌──[root@vms152.liruilongs.github.io]-[~]
└─$sysctl -p
net.ipv4.ip_forward = 1
┌──[root@vms152.liruilongs.github.io]-[~]
└─$
配置开启 IP 地址伪装
┌──[root@vms152.liruilongs.github.io]-[~]
└─$firewall-cmd --zone=external --add-masquerade
success
┌──[root@vms152.liruilongs.github.io]-[~]
└─$firewall-cmd --zone=external --query-masquerade
yes
┌──[root@vms152.liruilongs.github.io]-[~]
└─$firewall-cmd --zone=external --delete-masquerade --permanent
更改数据包的源 IP 地址
。例如,互联网服务提供商不路由私有 IP 范围,如 10.0.0.0/8。如果您在网络中使用私有 IP 范围,并且用户应该能够访问 Internet 上的服务器,需要将这些范围内的数据包的源 IP 地址映射到公共 IP 地址。
比如提供的 公网 IP 为 39.27.24.141
, 你通过这个公网 IP 请求服务,实际你在内网机器 192.168.26.3
上面发出请求,使用 NAT 做了映射,根据 参考模型,网络层会添加目标端和源端的 IP 地址,通过 路由寻址获取下一跳地址,但是你请求源端地址是 192.168.26.3
这个 私有 IP 地址,所以公网 IP 的服务,没办法响应你的请求,那这个时候你可以更改 192.168.26.3
这个私有 IP 为做 NAT 的对应公网,否则数据出的去,但是回不来。
伪装和 SNAT
相互类似。不同之处是:
动态 IP 地址,则使用伪装
。SNAT 要比伪装更快
。如果传出接口使用了固定 IP 地址,则使用 SNAT
配置 SNAT
内部访问外部,将source ip
为src_net
网段来的数据包伪装成external
区域对应的网卡的地址
rich规则
firewall-cmd --zone=external --permanent \
--add-rich-rule='rule family="ipv4" source address="<src_net/mask>" masquerade’
设置NAT规则也可实现(设置POSTROUTING),将 源IP 192.168.100.0/24
修改为 eth0
对应的IP地址,修改后的地址可以是 区域或者IP或则网卡
firewall-cmd --permanent --direct \
--passthrough ipv4 -t nat POSTROUTING -o eth0 -j MASQUERADE -s 192.168.100.0/24
firewall-cmd --permanent --direct \
--passthrough ipv4 -t nat -A POSTROUTING -s <internal_ip|internal_net/mask> -j SNAT --to-source <external_ip>
firewall-cmd --permanent --direct \
--passthrough ipv4 -t nat POSTROUTING -o ens0 -j MASQUERADE -s <internal_ip|internal_net/mask>
使用此 NAT 类型重写传入数据包的目标地址和端口。例如,如果您的 Web 服务器使用私有 IP 范围内的 IP 地址,那么无法直接从互联网访问它,您可以在路由器上设置 DNAT 规则,以便将传入的流量重定向到此服务器。
firewall-cmd --permanent --direct \
--passthrough ipv4 -t nat -A PREROUTING -d <external_ip> -j DNAT --to-destination <internal_ip>
这个类型是 IDT 的特殊示例,它根据链 hook 将数据包重定向到本地机器。例如,如果服务运行在与其标准端口不同的端口上,您可以将传入的流量从标准端口重定向到此特定端口。
将访问本机’external_port’端口流量转发到’internal_ip’
firewall-cmd --zone=external --permanent \
--add-forward-port=port=<external_port>:proto=tcp:toaddr=<internal_ip>
将访问本机’external_port’端口流量转发到’internal_ip’的’internal_port’
firewall-cmd --zone=external --permanent \
--add-forward-port=port=<external_port>:proto=tcp:toport=<internal_port>:toaddr=<internal_ip>
target是就是对该区域内流经的数据包作最终的处理动作,
firewall-cmd --zone=public --set-target=DROP
要允许来自特定IP地址(或范围)的所有传入流量,使用–zone选项指定区域,并使用–add-source选项指定源IP
firewall-cmd --zone=public --add-source=192.168.100.10
如果我们希望在防火墙启动之前设置相关区域,比如 K8s
集群中,在集群部署中我们关闭的 firewalld
,但是在之后的运维中我们需要开启防火墙,比如处理漏洞,那么这个时候我们可用通过 firewall-offline-cmd
命令来 在 firewalld
为启动之前设置区域或者规则。
注意的是:需要成为 root
用户才能使用 firewall-offline-cmd
。
帮助文档查看
┌──[root@vms152.liruilongs.github.io]-[~]
└─$man firewall-offline-cmd
看一个 Demo ,在运行的 k8s
集群中我们遇到了一个漏洞,解决这个漏洞需要开启防火墙。但是防火墙默认的区域是 public
,它会限制 k8s 的一些节点通信端口,并且会影响集群上的 SDN
,所以我们需要在启动之前设置为 trusend
┌──[root@vms152.liruilongs.github.io]-[~]
└─$firewall-cmd --get-default-zone
FirewallD is not running
┌──[root@vms152.liruilongs.github.io]-[~]
└─$firewall-cmd --state
not running
┌──[root@vms152.liruilongs.github.io]-[/var/spool]
└─$firewall-offline-cmd --version
0.6.3
当前防火墙未启动,通过命令修改默认区域
┌──[root@vms152.liruilongs.github.io]-[/var/spool]
└─$firewall-offline-cmd --get-default-zone
public
┌──[root@vms152.liruilongs.github.io]-[/var/spool]
└─$firewall-offline-cmd --set-default-zone=trusted
success
┌──[root@vms152.liruilongs.github.io]-[/var/spool]
└─$firewall-offline-cmd --get-default-zone
trusted
然后启动防火墙,查看设置的区域
┌──[root@vms152.liruilongs.github.io]-[/var/spool]
└─$systemctl start firewalld.service
┌──[root@vms152.liruilongs.github.io]-[/var/spool]
└─$firewall-cmd --get-default-zone
trusted
https://firewalld.org/documentation/zone/predefined-zones.html
https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/8/html/securing_networks/assembly_configuring-nat-using-firewalld_using-and-configuring-firewalld
https://blog.51cto.com/huanghai/2656485
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有