使用 nftables 过滤表和规则,可以轻松地限制网络访问,仅允许访问特定的网站,例如只允许访问百度 (baidu.com) 的 HTTP 和 HTTPS 内容,禁止访问其他所有网站。

首先,需要定义一个名为 'filter' 的过滤表,用于在数据包到达网络接口时执行规则:

nft add table filter

接下来,定义两个链,一个用于入站数据包 ('input'),一个用于出站数据包 ('output'):

nft add chain filter input { type filter hook input priority 0 ; }
nft add chain filter output { type filter hook output priority 0 ; }

现在,在 'filter' 表的 'input' 和 'output' 链中添加规则,限制访问。为了禁止访问除了百度 (baidu.com) 以外的所有 HTTP 和 HTTPS 网络,可以使用以下规则:

nft add rule filter input tcp dport { 80, 443 } not saddr 'baidu.com' drop
nft add rule filter output tcp sport { 80, 443 } not daddr 'baidu.com' drop

这些规则将阻止所有源地址不是 'baidu.com' 的入站和出站 TCP 数据包,目标端口为 80 和 443 的访问。

注意: 这些规则假定您已经使用 DNS 解析器将 'baidu.com' 解析为一个或多个 IP 地址。如果您没有正确配置 DNS 解析器,这些规则将无法工作。

如果您想要更细粒度的控制,可以使用更多的规则来允许或禁止特定的 IP 地址或子网访问。例如,以下规则将允许来自 192.168.1.0/24 子网的所有数据包访问网络:

nft add rule filter input tcp dport { 80, 443 } ip saddr 192.168.1.0/24 accept
nft add rule filter output tcp sport { 80, 443 } ip daddr 192.168.1.0/24 accept

请根据您的网络环境和需求进行适当的调整。

nftables 限制网络访问:仅允许访问百度 (baidu.com)

原文地址: https://www.cveoy.top/t/topic/nUjE 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录