服务器裸奔风险拉满!Firewalld 防火墙加固实战教程

发布时间:2026/8/4 3:01:23
服务器裸奔风险拉满!Firewalld 防火墙加固实战教程 Linux 网络安全加固防火墙介绍防火墙 FireWall 工作在网络或主机边缘对进出网络或主机的数据包基于一定的规则检查并在匹配某规则时由规则定义的行为进行处理的一组功能的组件。基本上的实现都是默认情况下关闭所有的通过型访问只开放允许访问的策略将希望外网访问的主机放在 DMZdemilitarized zone网络中。防火墙类型按保护范围划分主机防火墙服务范围为当前一台主机。网络防火墙服务范围为防火墙一侧的局域网。按实现方式划分:硬件防火墙在专用硬件级别实现防火墙功能例如华为、华三、天融信等产品。软件防火墙运行于通用硬件平台之上的防火墙的应用软件例如iptablesfirewalld。按网络协议划分网络层防火墙工作在OSI 模型下四层又称为包过滤防火墙。网络层对数据包进行选择选择的依据是系统内设置的过滤逻辑被称为访问控制列表ACL通过检查数据流中每个数据的源地址目的地址所用端口号和协议状态等因素或他们的组合来确定是否允许该数据包通过。优点对用户来说透明处理速度快且易于维护。缺点无法检查应用层数据如病毒等。应用层防火墙/代理服务器工作在OSI 模型七层或者承担proxy 代理网关。异常检测协议Web应用防火墙会对HTTP的请求进行异常检测拒绝不符合HTTP标准的请求。它也可以只允许HTTP协议的部分选项通过从而减少攻击的影响范围。增强的输入验证可以有效防止网页篡改、信息泄露、木冯植入等恶意网络入侵行为。从而减小Web服务器被攻击的可能性。及时补丁修补Web安全漏洞是Web应用开发者最头痛的问题没人会知道下一秒有什么样的漏洞出现会为Web应用带来什么样的危害。现在WAFWeb Application Firewall可以为我们做这项工作了只要有全面的漏洞信息WAF能在不到一个小时的时间内屏蔽掉这个漏洞。当然这种屏蔽掉漏洞的方式不是非常完美的没有安装对应的补丁本身就是一 种安全威胁但我们在没有选择的情况下任何保护措施都比没有保护措施更好。基于规则的保护和基于异常的保护基于规则的保护可以提供各种Web应用的安全规则WAFWeb Application Firewall 生产商会维护这个规则库并时时为其更新。用户可以按照这些规测对应用进行全方面检测。**状态管理。**WAF能够判断用户是否是第一次访问并且将请求重定向到默认登录页面并且记录事件。通过检测用户的整个操作行为我们可以更容易识别攻击。状态管理模式还能检测出异常事件(比如登陆失败)并組在达到极限值时进行处理。这对暴力攻击的识别和响应是十分有利的。**其他防护技术。**WAF还有一安全增强的功能可以用来解决WEB程序员过分信任输入数据带来的问题。比如:隐藏表单域保护、抗入侵规避技术、 响应监视和信息泄露保护。优点提供应用层保护。缺点处理速度慢等Netfilter 子系统Netfilter子系统是Linux内核中一个强大的网络过滤子系统对进入系统的每个数据包在到达用户空间组件或应用之前进行检查通过编程方式来修改、丟弃或路由数据包。Netfilter子系统工作在内核态用户使用以下命令配置防火墙规则告诉Netfilter子系统如何处理数据包iptables用于过滤 IPv4 协议的数据包。如果 Linux 系统连接到LAN或因特网 则iptables可用于在 Linux 系统上更好地控制 IP 信息包过滤和防火墙配置。ip6tables用于过滤 IPv6 协议的数据包效果等同iptables工具。arptables用于过滤 arp 协议的数据包。arptables既能防止别的机器对自己进行arp欺骗又能防止本机病毒或错误程序向其他机器发起arp攻击。如果善于运用的话不失为一个优秀的arp防火墙。ebtables用于过滤数据链路层数据包。ebtables 过滤数据包比 iptables 更靠前获得的数据更“原始”ebtables 多用于桥模式比如控制 VLAN ID 等。[rootserver ~13:31:33]# ls -l /sbin/*tables-rwxr-xr-x.1root root70164月112018/sbin/ebtables lrwxrwxrwx.1root root137月1410:31 /sbin/ip6tables -xtables-multi lrwxrwxrwx.1root root137月1410:31 /sbin/iptables -xtables-multiNftables 子系统Nftables子系统是netfilter的增强版其仍保留了netfilter的架构。nftables优点更快的数据包处理更快的规则集更新使用单个 nft 用户空间实用程序通过一个接口来管理所有协议消除了以往不同前端和多个netfilter接口引起的争用问题。静态防火墙和动态防火墙静态防火墙用户管理防火墙规则时为了让规则永久保存防火墙服务会重新加载所有防火墙规则哪怕只修改一条规则也要重新读取所有规则这种载入模式称为静态模式。静态模式加载规则时会导致网络连接丢失。我们称使用静态模式载入规则的防火墙为静态防火墙。例如iptables用户执行service iptables reload命令将变更的规则保存到配置文件里并重新加载所有防火墙规则。动态防火墙用户管理防火墙规则时为了让规则永久保存防火墙只需要将变更部分保存并更新到运行中的 iptables 即可而不需要对整个防火墙规则列表进行重新加载这种载入模式称为动态模式。我们称使用动态模式载入规则的防火墙为动态防火墙例如 firewalld。Firewalld 防火墙Firewalld 介绍Firewalld 是 Red Hat 公司开发的防火墙软件默认后端为 nftables。在 CentOS 7 之前的发行版中默认使用 iptables 命令来管理防火墙。在 CentOS 7 之后的发行版中 firewalld 取代 iptables 成为了默认防火墙软件。用户可以同时使用 iptables 和 firewalld 两个防火墙软件但容易搞混规则。建议将一个防火墙关闭或默认允许所有流量只使用另一个防火墙。Firewalld 防火墙的另外一个特点就是动态加载防火墙规则。Firewalld 和 iptables 之间的关系firewalld 提供了一个 daemon 和 service还有命令行和图形界面配置工具它仅仅是替代了 iptables service 部分其底层还是使用 iptables 作为防火墙规则管理入口。firewalld 和 iptables 一样他们的作用都是用于维护规则自身并不具备防火墙的功能而真正使用规则干活的是内核的 netfilter只不过 firewalld 和 iptables 的结构以及使用方法不一样罢了。Firewalld 区域firewalld 防火墙为了简化管理将所有网络流量分为多个区域(zone)每个 zone 就是一套过滤规则集。Firewalld 提供的区域firewalld 防火墙提供了如下zonetrusted(信任区域)允许所有的传入流量。public(公共区域)允许与ssh或dhcpv6-client预定义服务匹配的传入流量其余均拒绝。是新添加网络接口的默认区域。external(外部区域)允许与ssh预定义服务匹配的传入流量其余均拒绝。默认将通过此区域转发的IPv4传出流量将进行地址伪装可用于为路由器启用了伪装功能的外部网络。home(家庭区域)允许与ssh、mdn3、samba-client或dhcpv6-client预定义服务匹配的传入流量其余均拒绝。internal(内部区域)默认值时与homel区域相同。work(工作区域)允许与 ssh、dhcpv6-client预定义服务匹配的传入流量其余均拒绝。dmz(隔离区域也称为非军事区域)允许与ssh 预定义服务匹配的传入流量其余均拒绝。block(限制区域)拒绝所有传入流量。drop(丢弃区域)丢弃所有传入流量并且不产生包含ICMP的错误响应。数据包与区域匹配规则数据包与zone匹配规则如下该源地址被分配给特定区域 则应用该区域的规则。如果数据包未匹配源地址则分配给与传入网络接口关联的区域并应用该区域的规则。如果以上都未匹配firewalld将数据包分配给默认区域。注意事项firewalld默认指定public区域为默认区域并将lo 回环接口映射至 trusted 区域。如果流量不与允许的端口或协议或服务匹配则通常会被拒绝。Firewalld 规则对于所有区域区域内规则的优先级是相同的。区域内规则匹配优先级如下首先匹配为该区域设置的任何端口转发和伪装原则。其次匹配该区域设置的任何允许规则。最后匹配该区域设置的任何拒绝规则。还可以同时为以上3个规则设置记录和审计规则可以同时生效。富规则中的规则优先级高于区域中其他规则。如果区域中所有规则都不匹配那么通常会拒绝该包trusted 例外。Firewalld 配置Firewalld 配置方法直接编辑 /etc/firewalld/ 中的配置文件firewall-cmd 命令行工具firewall-config 图形工具Firewalld 配置文件Firewalld 配置配置文件存放在/usr/lib/firewalld/目录Firewalld软件包自带配置位置。/etc/firewalld/目录目录结构与 /usr/lib/firewalld/ 一致管理员自定义配置保存在该位置。提示我们不建议大家通过修改 /etc/firewalld/ 目录配置防火墙。查看zone配置[rootserver ~14:22:49]# cd /usr/lib/firewalld/[rootserver firewalld14:33:33]# lshelpers icmptypes ipsets services zones# 查看zone配置[rootserver firewalld14:33:44]# ls zones/block.xml drop.xml home.xml public.xml work.xml dmz.xml external.xml internal.xml trusted.xml[rootserver firewalld14:34:07]# cat zones/trusted.xml?xmlversion1.0encodingutf-8?zonetargetACCEPTshortTrusted/shortdescriptionAll network connections are accepted./description/zone[rootserver firewalld14:34:22]# cat zones/block.xml?xmlversion1.0encodingutf-8?zonetarget%%REJECT%%shortBlock/shortdescriptionUnsolicited incoming network packets are rejected. Incoming packets that are related to outgoing network connections are accepted. Outgoing network connections are allowed./description/zone[rootserver firewalld14:50:04]# cat zones/drop.xml?xmlversion1.0encodingutf-8?zonetargetDROPshortDrop/shortdescriptionUnsolicited incoming network packets are dropped. Incoming packets that are related to outgoing network connections are accepted. Outgoing network connections are allowed./description/zone查看服务配置[rootserver firewalld14:50:24]# ls -1 servicesamanda-client.xml amanda-k5-client.xml...[rootserver firewalld14:51:03]# cat services/http.xml?xmlversion1.0encodingutf-8?serviceshortWWW(HTTP)/shortdescriptionHTTP is the protocol used to serve Web pages. If you plan tomakeyour Web server publicly available,enablethis option. This option is not requiredforviewing pages locally or developing Web pages./descriptionportprotocoltcpport80//service[rootserver firewalld14:51:33]# cat services/https.xml?xmlversion1.0encodingutf-8?serviceshortSecure WWW(HTTPS)/shortdescriptionHTTPS is a modified HTTP used to serve Web pages when security is important. Examples are sites that require logins like stores or web mail. This option is not requiredforviewing pages locally or developing Web pages. You need the httpd package installedforthis option to be useful./descriptionportprotocoltcpport443//servicefirewall-cmd 命令行工具两个基本概念配置防火墙必须知道两个基本概念防火墙的配置有两个状态runtime运行时命令行执行是的默认状态该状态下的配置立刻生效。permanent永久通过选项--permanent指定该状态下的配置不会立刻生效而是写入配置文件需要通过选项--reload重新reload才会生效重新reload将导致运行时状态下未保存的配置丢失但会保留当前连接状态。如果使用选项--complete-reload重新reload永久配置将导致当前连接状态也丢失。建议配置防火墙一次性写两条规则第一条使用选项--permanent永久生效第二条不使用选项--permanent立刻生效。配置防火墙的规则必须一个区域内直接规则除外如果不使用选项--zone明确指明区域则使用默认区域。默认区域是public。zone 管理# 查看zone清单[rootserver firewalld14:52:01]# firewall-cmd --get-zonesblock dmz drop external home internal public trusted work# 查看激活的zone清单也就是分配了interface和source的zone[rootserver firewalld14:52:42]# firewall-cmd --get-active-zonespublic interfaces: ens33# 查看默认zone[rootserver firewalld14:53:29]# firewall-cmd --get-default-zonepublic# 设置默认zone例如设置为trusted该操作同时设置永久态默认zone[rootserver firewalld14:53:44]# firewall-cmd --set-default-zonetrustedsuccess[rootserver firewalld14:54:22]# firewall-cmd --get-default-zonetrusted# 再次设置回来[rootserver firewalld14:54:31]# firewall-cmd --set-default-zonepublicsuccess# 查看所有zone中的规则[rootserver ~14:55:27]# firewall-cmd --list-all-zonesblock target: %%REJECT%% icmp-block-inversion: no interfaces: sources: services: ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: dmz target: default icmp-block-inversion: no...# 查看默认zone中的规则[rootserver ~14:56:07]# firewall-cmd --list-allpublic(active)target: default icmp-block-inversion: no interfaces: ens33 sources: services: dhcpv6-client httpsshports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:# 查看特定zone中的规则[rootserver ~14:56:39]# firewall-cmd --list-all --zonehomehome target: default icmp-block-inversion: no interfaces: sources: services: dhcpv6-client mdns samba-clientsshports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:source 管理# 来源于特定source的数据包交给特定zone处理[rootserver ~14:57:06]# firewall-cmd --add-source10.1.1.0/24 --zonehomesuccess# 查看source清单[rootserver ~14:58:35]# firewall-cmd --list-sources --zonehome10.1.1.0/24# 查看source属于哪个zone[rootserver ~14:59:10]# firewall-cmd --get-zone-of-source10.1.1.0/24home# 查看source是否添加[rootserver ~15:00:11]# firewall-cmd --query-source10.1.1.0/24 --zonehomeyes# 变更source到其他zone[rootserver ~15:00:55]# firewall-cmd --change-source10.1.1.0/24 --zonepublicsuccess[rootserver ~15:01:43]# firewall-cmd --query-source10.1.1.0/24 --zonepublicyes# 删除zone中source[rootserver ~15:02:00]# firewall-cmd --remove-source10.1.1.0/24 --zonepublicsuccessinterface 管理# 查看zone中interface[rootserver ~15:02:41]# firewall-cmd --list-interfacesens33# 查看interface属于哪个zone[rootserver ~15:03:11]# firewall-cmd --get-zone-of-interfaceens33public# 查看interface是否添加[rootserver ~15:03:49]# firewall-cmd --query-interfaceens33yes# 将interface变更到其他zone[rootserver ~15:04:16]# firewall-cmd --change-interfaceens33 --zonehomesuccess# 删除zone中interface[rootserver ~15:04:53]# firewall-cmd --remove-interfaceens33 --zonehomesuccess# 如果interface不属于任何zone使用以下命令将interface绑定到特定zone[rootserver ~15:05:41]# firewall-cmd --add-interfaceens33 --zonepublicsuccessservice 管理准备nginx服务[rootserver ~15:06:22]# yum install -y ngnix[rootserver ~15:07:06]# systemctl start nginx# 查看系统中预定义了哪些服务[rootserver ~15:32:17]# firewall-cmd --get-services# 添加放行服务[rootserver ~15:40:05]# firewall-cmd --add-servicehttpsuccess# 查看放行服务列表[rootserver ~15:40:38]# firewall-cmd --list-servicesdhcpv6-client httpssh# 查看服务是否放行[rootserver ~15:41:14]# firewall-cmd --query-servicehttpyes# 删除服务[rootserver ~15:41:47]# firewall-cmd --remove-servicehttpsuccessfirewall-cmd 命令行还可以定义新的服务。常用选项如下--permanent--new-serviceservice Add a new permanent and empty service.--permanent--delete-serviceservice Delete an existing permanent service.--permanent--serviceservice --set-descriptiondescription Set new description toservice--permanent--serviceservice --get-description Print descriptionforservice--permanent--serviceservice --set-shortdescription Set short description toservice--permanent--serviceservice --get-short Print short descriptionforservice--permanent--serviceservice --add-portportid[-portid]/protocol Add a new port to the permanent service.--permanent--serviceservice --remove-portportid[-portid]/protocol Remove a port from the permanent service.--permanent--serviceservice --get-ports List ports added to the permanent service.--permanent--serviceservice --add-protocolprotocol Add a new protocol to the permanent service.--permanent--serviceservice --remove-protocolprotocol Remove a protocol from the permanent service.--permanent--serviceservice --get-protocols List protocols added to the permanent service.--permanent--serviceservice --add-source-portportid[-portid]/protocol Add a newsourceport to the permanent service.--permanent--serviceservice --remove-source-portportid[-portid]/protocol Remove asourceport from the permanent service.--permanent--serviceservice --get-source-ports Listsourceports added to the permanentserviceport 管理# 添加放行端口[rootserver ~15:09:11]# firewall-cmd --add-port5900/tcp# 查看端口放行列表[rootserver ~15:09:23]# firewall-cmd --list-ports5900/tcp# 查看端口是否放行[rootserver ~15:09:46]#irewall-cmd --query-port5900/tcpyes# 删除端口[rootserver ~15:10:08]# firewall-cmd --remove-port5900/tcp实践#服务端#防火墙放行[rootserver ~15:12:00]# firewall-cmd --add-port65000/tcpsuccess#客户端[rootclient ~15:12:47]# ncat 10.1.8.10 65000#输入内容hello world#服务端验证[rootserver ~15:13:16]# nc -l 65000#同步出现客户端输入内容hello world#服务端[rootserver ~15:15:08]# nc --exec /bin/bash -l 65000#客户端[rootclient ~15:15:11]# ncat 10.1.8.10 65000#输入命令会以服务端身份运行hostnameserver.cywip-bra lo UNKNOWN127.0.0.1/8 ::1/128 ens33 UP10.1.8.10/24 fe80::499d:f1dc:59d:7399/64 virbr0 DOWN192.168.122.1/24 virbr0-nic DOWNmasquerade 管理网络拓扑server: 10.1.1.10、10.1.8.10可以访问公网client10.1.1.11 网关 10.1.1.10需求确保client能访问公网。环境准备server 增加一块关联vmnet1模式的网卡[rootserver ~16:08:34]# nmcli connection delete 有线连接\ 1成功删除连接有线连接 1(842ad19b-57f9-3ea3-b44f-dc2933676ea4)。[rootserver ~16:10:39]# nmcli deviceDEVICE TYPE STATE CONNECTION ens33 ethernet 已连接 ens33 virbr0 bridge 已连接 virbr0 ens36 ethernet 已断开 -- lo loopback 未托管 -- virbr0-nic tun 未托管 --[rootserver ~16:11:05]# nmcli connection add type ethernet ifname ens36 ipv4.method manual ipv4.addresses 10.1.1.10/24连接ethernet-ens36(abb2dc10-8e18-49e3-950c-aa7799add2ae)已成功添加。[rootserver ~16:12:55]# nmcli connection up ens36错误未知的连接ens36。[rootserver ~16:13:11]# nmcli deviceDEVICE TYPE STATE CONNECTION ens33 ethernet 已连接 ens33 ens36 ethernet 已连接 ethernet-ens36 virbr0 bridge 已连接 virbr0 lo loopback 未托管 -- virbr0-nic tun 未托管 --[rootserver ~16:13:36]# nmcli connection delete ethernet-ens36成功删除连接ethernet-ens36(abb2dc10-8e18-49e3-950c-aa7799add2ae)。[rootserver ~16:14:30]# nmcli connection add type ethernet ifname ens36 con-name ens36 ipv4.method manual ipv4.addresses 10.1.1.10/24连接ens36(f4fcbea5-62d6-4701-a897-ffd38c8381ac)已成功添加。[rootserver ~16:15:06]# nmcli connection up ens36连接已成功激活D-Bus 活动路径/org/freedesktop/NetworkManager/ActiveConnection/7[rootserver ~16:15:13]# nmcli deviceDEVICE TYPE STATE CONNECTION ens33 ethernet 已连接 ens33 ens36 ethernet 已连接 ens36 virbr0 bridge 已连接 virbr0 lo loopback 未托管 -- virbr0-nic tun 未托管 --client 网卡模式更改为vmnet1仅主机#在虚拟机终端操作[cywclient ~16:09:50]$ nmcli connection NAME UUID TYPE DEVICE ens33 5a143031-505e-4ac6-a14c-d53d8d489e0a ethernet ens33 virbr0 8cb6157f-800c-4158-8c48-5e18816b5706 bridge virbr0[cywclient ~16:22:54]$ nmcli connection modify ens33 ipv4.addresses10.1.1.11/24 ipv4.gateway10.1.1.10[cywclient ~16:23:51]$ nmcli connection up ens33配置伪装SNAT# 查看masquerade是否启动[rootserver ~16:32:41]# firewall-cmd --query-masqueradeno# 启动 masquerade[rootserver ~16:33:51]# firewall-cmd --add-masqueradesuccess[rootserver ~16:34:49]# firewall-cmd --query-masqueradeyes[rootserver ~16:35:04]# firewall-cmd --remove-masqueradesuccessclient 验证[rootclient ~16:40:02]# mtr 11.1.1.1补充普通主机和路由器区别。普通主机目的地不是我的数据包丢弃。路由器目的地不是我的数据包根据路由表进行转发。forward-port 管理使用 port forward前确保对应zone启用masquerade功能。# 启动 masquerade[rootserver ~16:46:22]# firewall-cmd --add-masquerade# 添加端口转发语法--add-forward-portportportid[-portid]:protoprotocol[:toportportid[-portid]][:toaddraddress[/mask]]# 访问本机端口8000转发到本机80[rootserver ~16:46:53]# firewall-cmd --add-forward-portport8000:prototcp:toport80success# 查看是否具有特定端口转发规则[rootserver ~16:47:11]#firewall-cmd --query-forward-portport8000:prototcp:toport80yes# 访问本机端口1022转发到10.1.1.11:22[rootserver ~16:47:34]# firewall-cmd --add-forward-portport1022:prototcp:toport22:toaddr10.1.1.11# 查询端口转发清单[rootserver ~16:47:52]# firewall-cmd --list-forward-portsport8000:prototcp:toport80:toaddrport1022:prototcp:toport22:toaddr10.1.1.11# 删除端口转发[rootserver ~16:48:44]# firewall-cmd --remove-forward-portport8000:prototcp:toport80:toaddr[rootserver ~16:48:57]# firewall-cmd --remove-forward-portport1022:prototcp:toport22:toaddr10.1.8.80