欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 锐评 > Linux学习——8_Linux下的火墙管理及优化

Linux学习——8_Linux下的火墙管理及优化

2025/5/1 22:00:14 来源:https://blog.csdn.net/weixin_73921499/article/details/144406899  浏览:    关键词:Linux学习——8_Linux下的火墙管理及优化

Linux下的火墙管理及优化

什么是防火墙

从功能角度来讲:防火墙是位于内部网和外部网之间的屏障,它按照系统管理员预先定义好的规则来控制数据包的进出
从功能实现角度来讲:火墙是系统内核上的一个模块netfilter(数据包过滤机制);通过netfiler来管理kernelspace中的策略

netfilter简介

Netfilter是Linux 2.4.x引入的一个子系统

它作为一个通用的、抽象的框架,提供一整套的hook函数的管理机制,使得诸如数据包过滤、网络地址转换(NAT)和基于协议类型的连接跟踪等等

netfilter分析OSI七层协议的2、3、4层

netfiler可以直接分析数据包头部数据,包括硬件地址,软件地址、TCP、UDP、ICMP等数据包的信息都可以进行过滤分析

Inux的netfilter机制可以进行的工作有:
拒绝让Internet的数据包进入主机的某些端口
拒绝让某些来源ip的数据包进入
拒绝让带有某些特殊标志(flag)的数据包进入,最常拒绝的就是带有SYN的主动连接的标志
分析硬件地址(MAC)来决定连接与否
地址转换

防火墙并不能有效阻挡病毒或木马程序,并且防火墙对于内部LAN的攻击无能为力

netfilter策略管理工具

netfilter这个内核网络栈过滤框架的使用需要通过iptables或nftables来进行

与netfilter进行交互工具常用种类
iptables服务使用iptables交互RHEL6之前系统默认使用此服务,管理手段丰富,配置比较复杂
firewalld服务使用nftables交互IRHEL6及之后的版本中默认使用此服务,配置类似windows火墙,功能模块度高,使用简单

netfilter的五类hook函数及iptables的默认表和链

五类HOOK函数

NF_IP_PRE ROUTING: 位于路由之前,报文一致性检査之后(报文一致性检查包括:报文版本、报文长度和checksum
NF_IP_LOCAL_IN:位于报文经过路由之后,并且目的是本机
NF_IP FORWARD:位于在报文路由之后,目的地非本机
NF_IP_LOCAL_OUT:由本机发出去的报文,并且在路由之前
NF IP POST ROUTING:所有即将离开本机的报文

什么是内核空间的iptables

Iptables 是基于Netfilter框架实现的报文选择系统

iptables用于报文的过滤、网络地址转换和报文修改等功能.

Iptables 本质上是包含了5个规则表,而规则表则包含了一些列的报文的匹配规则以及操作目标

1、raw表:
第一优先级的表,设置raw表规则后,不会对数据包进行链接跟踪和NAT转换,使用于PREROUTING和OUTPUT链,对应的动作为NOTRACK。

2、mangle表:
第二优先级的表,根据规则,修改数据包的TOS(Type OfService,服务类型)、TTL(Time ToLive,生存周期)以及设置Mark标记,以实现Qos以及策略路由等:

3、nat表:
第三优先级的表,网络地址转换表,用于修改源和目的的地址,分SNAT(源目的地址转换)和DNAT(目的地址转换)。

4、filter表:
第四优先级的表,用于控制到达链(forward链、input链、output链)上的数据包,是放行(accepte)、丢弃(drop)或者拒绝(reject)

5、security表:
最不常用的表(通常,我们说iptables只有4张表,security表是新加入的特性),用于在数据包上应用SELinux。

iptables服务器

iptables介绍

iptables服务是用户管理内核空间的iptables的管理工具,通过iptables书写内核空间的iptables策略iptables的规则是至上而下的读取方式,遇到与数据包信息匹配的规则后直接采用iptables的规则默认保存在内存中,如果需要永久保存需要把策略以字符的形式保存到/etc/sysconfig/iptables中

刷新iptables(临时)

[root@localhost ~]# iptables -F
[root@localhost ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination   
[root@localhost ~]# cat /etc/sysconfig/iptables
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

重启iptables 

[root@localhost ~]# systemctl restart iptables.service 
[root@localhost ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     0    --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
ACCEPT     1    --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     0    --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     6    --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
REJECT     0    --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibitedChain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     0    --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibitedChain OUTPUT (policy ACCEPT)
target     prot opt source               destination   

 永久保存iptables

[root@localhost ~]# iptables -F
[root@localhost ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables: [  OK  ]
[root@localhost ~]# cat  /etc/sysconfig/iptables
# Generated by iptables-save v1.8.10 (nf_tables) on Mon Dec 16 20:32:49 2024
*filter
:INPUT ACCEPT [51:3612]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [74:8360]
COMMIT
# Completed on Mon Dec 16 20:32:49 2024

iptables命令的语法格式及常用参数

查看当前iptables策略并清空表中指定的策略

[root@localhost ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@localhost ~]# iptables -t nat -nL
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         Chain INPUT (policy ACCEPT)
target     prot opt source               destination         Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination    

允许本机访问回环接口

[root@localhost ~]# iptables -A INPUT -i lo -j ACCEPT 

允许本机sshd的22端口被访问

[root@localhost ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT 

仅允许172.25.254.100访问22端口

[root@localhost ~]# iptables -A INPUT -p tcp --dport 22 ! -s 172.25.254.100 -j REJECT 

删除filter表中INPUT链的第三条策略

[root@localhost ~]# iptables -D INPUT 3

dnat地址转换

[root@localhost ~]# iptables -t nat -A PREROUTING -i eth0 -j DNAT --to-dest 192.168.0.200

firewalld

firewalld服务管理方式与iptables的管理方式区别

iptables是基于Linux内核的Netfilter子系统构建的,直接操作Netfilter;而firewalld则通过libnftables库与Netfilter交互,提供了一个更高的抽象层
iptables使用基于表的规则集,包括filter、nat、mangle、raw及securty五个表;firewalld采用基于区域的规则集,包括default、public、internal、external和dmz五个区域
iptables的配置较为复杂,需要用户掌握特定的命令行语法;firewalld提供了更直观和灵活的配置方式,支持命令行和图形界面
由于firewalld通过libnftables库与Netfilter交豆,其性能相对于直接操作Netfilter的iptables来说较低。
由于firewalld通过libnftables库与Netfilter交互,其性能相对于直接操作Netfilter的iptables来说较低。

 从firewalld管理方式切换到iptables管理方式

下载打开:

[root@localhost ~]# dnf install iptables-nft-services.noarch -y
[root@localhost ~]# systemctl start iptables.service
[root@localhost ~]# systemctl status iptables.service 
● iptables.service - IPv4 firewall with iptablesLoaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; preset: disabled)Active: active (exited) since Mon 2024-12-16 19:20:46 CST; 25s agoProcess: 2283 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)Main PID: 2283 (code=exited, status=0/SUCCESS)CPU: 13msDec 16 19:20:46 localhost.localdomain systemd[1]: Starting IPv4 firewall with iptables...
Dec 16 19:20:46 localhost.localdomain iptables.init[2283]: iptables: Applying firewall rules: [  OK  ]
Dec 16 19:20:46 localhost.localdomain systemd[1]: Finished IPv4 firewall with iptables. 

查看状态:

[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibitedChain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibitedChain OUTPUT (policy ACCEPT)
target     prot opt source               destination     

从iptables管理方式切换到firewalld管理方式

[root@localhost ~]# systemctl disable --now iptables.service
[root@localhost ~]# systemctl status iptables.service 
○ iptables.service - IPv4 firewall with iptablesLoaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; preset: disabled)Active: inactive (dead)Dec 16 19:20:46 localhost.localdomain systemd[1]: Starting IPv4 firewall with iptables...
Dec 16 19:20:46 localhost.localdomain iptables.init[2283]: iptables: Applying firewall rules: [  OK  ]
Dec 16 19:20:46 localhost.localdomain systemd[1]: Finished IPv4 firewall with iptables.
Dec 16 19:24:51 localhost.localdomain systemd[1]: Stopping IPv4 firewall with iptables...
Dec 16 19:24:51 localhost.localdomain iptables.init[2409]: iptables: Setting chains to policy ACCEPT: raw mangle filter nat [  OK  ]
Dec 16 19:24:51 localhost.localdomain iptables.init[2409]: iptables: Flushing firewall rules: [  OK  ]
Dec 16 19:24:51 localhost.localdomain systemd[1]: iptables.service: Deactivated successfully.
Dec 16 19:24:51 localhost.localdomain systemd[1]: Stopped IPv4 firewall with iptables.
[root@localhost ~]# systemctl start firewalld.service 
[root@localhost ~]# systemctl status firewalld.service 
● firewalld.service - firewalld - dynamic firewall daemonLoaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; preset: enabled)Active: active (running) since Mon 2024-12-16 19:30:45 CST; 8s agoDocs: man:firewalld(1)Main PID: 3067 (firewalld)Tasks: 2 (limit: 24456)Memory: 25.4MCPU: 284msCGroup: /system.slice/firewalld.service└─3067 /usr/bin/python3 -s /usr/sbin/firewalld --nofork --nopidDec 16 19:30:44 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
Dec 16 19:30:45 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.

firewalld域介绍

区域默认规则策略
阻塞区域(block)拒绝流入的流量,除非与流出的流量相关
工作区域(work)拒绝流入的流量,除非与流出的流量相关
家庭区域(home)拒绝流入的流量,除非与流出的流量相关
公共区域(public)不相信网络上任何计算机,只有选择接收传入的网络连接
隔离区域(DMZ)也称非军事区域,内外网之间增加的一层网络起到缓冲作用,对于隔离区域,只有选择接收传入的网络连接
信任区域(trusted)允许所有的数据包
丢弃区域(drop)拒绝流入的流量除非与流出的流量相关
内部区域(internal)等同于home区域
外部区域(external)拒绝流入的流量,除非与流出的流量有关;而如果流量与ssh服务相关,则允许流量

firewalld中默认使用的域是pubic
firewalld默认提供的九个zones的调用文件都保存在"/usr/lib/firewalld/zones/"目录下

firewall-cmd命令详解

防火墙信息查看

[root@localhost ~]# systemctl start firewalld.service 
[root@localhost ~]# firewall-cmd --state 
running
[root@localhost ~]# firewall-cmd --list-all
public (active)target: defaulticmp-block-inversion: nointerfaces: ens160 ens224sources: services: cockpit dhcpv6-client sshports: protocols: forward: yesmasquerade: noforward-ports: source-ports: icmp-blocks: rich rules: 

管理域

[root@localhost ~]# firewall-cmd --get-default-zone 
public
[root@localhost ~]# firewall-cmd --set-default-zone=trusted
success
[root@localhost ~]# firewall-cmd --get-default-zone 
trusted
[root@localhost ~]# firewall-cmd --get-active-zones 
publicinterfaces: ens224
trustedinterfaces: ens160

管理服务

[root@localhost ~]# firewall-cmd --get-services 
[root@localhost ~]# firewall-cmd  --list-services --zone=public 
cockpit dhcpv6-client ssh
[root@localhost ~]# firewall-cmd --permanent --remove-service=http

管理IP

[root@localhost ~]# firewall-cmd --add-port=80
[root@localhost ~]# firewall-cmd --add-port=tcp
[root@localhost ~]# firewall-cmd --remove-source=172.25.254.1 --zone=trusted 

管理port接口

[root@localhost ~]# firewall-cmd --add-port=80
[root@localhost ~]# firewall-cmd --add-port=tcp
[root@localhost ~]# firewall-cmd --remove-port=80
[root@localhost ~]# firewall-cmd --remove-port=tcp

firewalld的高级规则

Direct Rules

通过 firewall-cmd 工具,可以使用 --direct 选项在运行时间里增加或者移除链。如果不熟悉iptables,使用直接接口非常危险,因为您可能无意间导致防火墙被入侵
直接端口模式适用于服务或者程序,以便在运行时间内增加特定的防火墙规则。直接端口模式添加的规则优先应用。

例:允许172.25.254.1访问

[root@localhost ~]# firewall-cmd --direct --get-all-rules 
[root@localhost ~]# firewall-cmd --direct --add-rule ipv4 filter IN_public_allow 0 -s 172.25.254.1 -p tcp --dport 80 -j ACCEPT
[root@localhost ~]# firewall-cmd --direct --remove--rule ipv4 filter IN_public_allow 0 -s 172.25.254.1 -p tcp --dport 80 -j ACCEPT
地址伪装与端口转发

地址伪装

[root@localhost ~]# firewall-cmd --add-masquerade

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com