防火墙 防火墙作为您防御网络入侵的第一道防线。防火墙是一种网络安全系统,根据某些预定义的规则监控和控制进出的流量,充当受信任和不受信任网络之间的屏障。 几种类型的防火墙: 代理防火墙 状态防火墙 统一威胁管理(UTM)防火墙 下一代防火墙 威胁聚焦防火墙 虚拟防火墙 防火墙可以在硬件和软件层面实现。为了这个例子,我们将只关注软件部分。 Windows 防火墙 Windows 防火墙(在 Windows 10 中称为 Windows Defender)是微软 Windows 提供的一个防火墙组件。 官方链接到 Microsoft Defender 手册可以找到。 Linux 防火墙 Linux 中的防火墙可以通过不同的方式实现。这些只是其中的一部分。
防火墙作为您防御网络入侵的第一道防线。防火墙是一种网络安全系统,根据某些预定义的规则监控和控制进出的流量,充当受信任和不受信任网络之间的屏障。
几种类型的防火墙:
防火墙可以在硬件和软件层面实现。为了这个例子,我们将只关注软件部分。
Windows 防火墙(在 Windows 10 中称为 Windows Defender)是微软 Windows 提供的一个防火墙组件。
官方链接到 Microsoft Defender 手册可以找到。
Linux 中的防火墙可以通过不同的方式实现。这些只是其中的一部分。
IPTables 是一种基于规则的防火墙,并且大多数 Linux 发行版都预装了它。默认情况下,它没有任何规则并且自 Linux 内核版本 2.4 以来一直是其一部分。
然而,如果您想自行安装或更新它
对于基于 apt 的发行版$ sudo apt install iptables
对于基于 dnf 的发行版$ sudo dnf install iptables
对于基于 pacman 的发行版$ sudo pacman -Syu iptables
IPTables 服务和协议
IPTables 主要配置文件
/etc/init.d/iptables - 启动|停止|重启和保存规则集的初始化脚本。/sbin/iptables - IPTables 二进制文件IPTables 链
$ iptables -L -v 可以用来检查这一点。例如,在路由器上数据总是发送到它,但很少是为路由器自身准备的;数据只是被转发到目标。特定连接的响应
允许或阻止特定连接
单个 IP 源的连接
$ iptables -A INPUT -s 10.10.10.10 -j DROP 这会阻止来自 IP 10.10.10.10 的所有连接
IP 源范围的连接
$ iptables -A INPUT -s 10.10.10.10/24 -j DROP 这会阻止来自 IP 10.10.10.10/24 网络范围的所有连接
掩码或标准斜杠也可以使用
$ iptables -A INPUT -s 10.10.10.10/255.255.255.0 -j DROP
保存更改
$ sudo /sbin/iptables save or $ sudo iptables save or $ sudo /sbin/service iptables save or /etc/init.d/iptables save
随着 Red Hat Enterprise Linux 7.0 引入 Firewalld,IPTables 被取代。Firewalld 是一种基于区域的防火墙。一个区域与至少一个网络接口相关联(例如 eth0)。Firewalld 为不同的连接区域提供不同程度的安全性。
$ firewall-cmd --get-zones can be used to get the list of preconfigured zones.
$ firewall-cmd --list-all can be used to display services associated to different zones. If the zone is not defined by the user, the public zone is taken as default. The --zone=zonename can be used to specify a zone.
For example $ firewall-cmd --zone=external --list-all can be used to see the external zone.
Allow and Deny by service
# firewall-cmd --zone=external --add-service=ftp can be use to allow ftp service in the external zone.
To make rule persistent # firewall-cmd --permanent --zone=external --add-service=ftp
Removing a service can be as simple as # firewall-cmd --permanent --zone=external --remove-service=ftp
Allow and Deny by port
# firewall-cmd --permanent --zone=external --add-port=60001/udp can be used to add a dedicated UDP port to your external zone, where 60001 is your port number.
# firewall-cmd --zone=external --list-ports can be used to check the allowed ports in the given zone. For some other zone --zone should be changed.
Similar to removing services, ports can be removed using --remove-port option. For example # firewall-cmd --permanent --zone=external --remove-port=60001/udp
As the name suggests ufw provides us with a much user friendly approach to managing firewall on Linux. ufw also has an GUI interface named gufw.
Installation
$ sudo apt install ufw
You will need to start the service.
$ sudo systemctl enable ufw and then $ sudo systemctl start ufw
A fundamental ufw command may look like ufw [--dry-run] [option] [rule syntax]
Few useful commands:
Change the port number (here 22, 8 and 25), I.P. ( here 192.168.2.100 or 192.168.2.101 ), protocol and services (here TCP, UDP and SSH) according to your requirement.
$ sudo ufw allow 22 can be used to allow traffic on port 22.$ sudo ufw deny 22 can be used to deny traffic on port 22.$ sudo ufw deny 22/tcp can be used to deny traffic and TCP protocol on port 22, same goes for 允许.$ sudo ufw allow ssh can be used to allow ssh traffic. This is done by defining a rule using a service and ufw will run against /etc/services. Same applies for 拒绝$ sudo ufw allow ssh/tcp can be used to add protocols on top of services.$ sudo ufw deny from 192.168.2.100/8 to 192.168.2.101 port 25, this will deny 192.168.2.100 from gaining access (through any port) to 192.168.2.101's port 25.如果 iptables 启用,ufw 将不起作用。
声明:
本文件灏天文库团队进行了翻译。尽管我们力求准确,但请注意,翻译可能包含错误或不准确之处。原文档以其原始语言为准。我们不对因使用此翻译而产生的任何误解或误译负责。