使用firewalld和firewall-cmd如何将规则添加到主INPUT链而不是INPUT_direct

使用firewalld和firewall-cmd如何将规则添加到主INPUT链而不是INPUT_direct

因此,在阅读了firewalld手册页和fedora文档后,我了解到,要使用特定参数向防火墙添加自定义规则,我需要使用结构

 firewall-cmd [--permanent] --direct --add-rule { ipv4 | ipv6 | eb } <table> <chain> <priority> <args>

我具体想做的是创建一个自定义规则,使用 geoip 匹配来屏蔽所有非美国来源的国家。在执行此操作之前,我需要先添加一条匹配规则,允许从我的本地网络进行访问,因为我通过本地专用网络上的 ssh 控制服务器,所以我添加了一条规则,如下所示

 firewall-cmd --direct --add-rule ipv4 filter INPUT 0 -s 192.168.0.0/24 -j ACCEPT

然后我添加第二条规则,如下所示

 firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -m geoip ! --src-cc US -j DROP

这些添加到输入链,但添加到名为 INPUT_direct 的子链下,此子链在通用不变 INPUT 规则列表中列为第 3 个,并且是快速

 iptables -L INPUT

显示 INPUT 链如下

 Chain INPUT (policy ACCEPT)
 target     prot opt source               destination
 ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
 ACCEPT     all  --  anywhere             anywhere
 INPUT_direct  all  --  anywhere             anywhere
 INPUT_ZONES_SOURCE  all  --  anywhere             anywhere
 INPUT_ZONES  all  --  anywhere             anywhere
 ACCEPT     icmp --  anywhere             anywhere
 DROP       all  --  anywhere             anywhere             ctstate INVALID
 REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

并且 INPUT_direct 为

 Chain INPUT_direct (1 references)
 target     prot opt source               destination
 ACCEPT     all  --  192.168.0.0/24         anywhere
 DROP       all  --  anywhere             anywhere             -m geoip ! --source-country US

这可能对某些人有用,但如果我运行

 ping france.fr

我得到的结果

 PING france.fr (46.18.192.148) 56(84) bytes of data.
 64 bytes from ns1-sgg.produhost.net (46.18.192.148): icmp_seq=1 ttl=52 time=136 ms
 64 bytes from ns1-sgg.produhost.net (46.18.192.148): icmp_seq=2 ttl=52 time=135 ms
 64 bytes from ns1-sgg.produhost.net (46.18.192.148): icmp_seq=3 ttl=52 time=136 ms

这很可能是由于 INPUT 规则 #1

 iptables  -L INPUT 1

 ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED

我意识到我可以简单地将相同的自定义规则集应用于 OUTPUT 链并阻止对 france.fr 或美国以外的任何网站的 ping 请求,但我如何才能将规则集添加到基本 INPUT 链中,以便

 iptables -L INPUT

显示的是这个

 Chain INPUT (policy ACCEPT)
 target     prot opt source               destination
 ACCEPT     all  --  192.168.0.0/24         anywhere
 DROP       all  --  anywhere             anywhere             -m geoip ! --source-country US
 ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
 ACCEPT     all  --  anywhere             anywhere
 INPUT_direct  all  --  anywhere             anywhere
 INPUT_ZONES_SOURCE  all  --  anywhere             anywhere
 INPUT_ZONES  all  --  anywhere             anywhere
 ACCEPT     icmp --  anywhere             anywhere
 DROP       all  --  anywhere             anywhere             ctstate INVALID
 REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

我之所以问这个问题,是因为我觉得我想要的而不是防火墙命令的结果更安全一些,我错了吗?我想让防火墙继续由防火墙控制,而不是放弃防火墙并恢复到 iptables,以便将来更好地集成和解决可能的弃用问题,那么使用防火墙是否可行,或者我是否被迫在启动时运行包含以下内容的自定义脚本

 iptables -I INPUT 1 -s 192.168.0.0/24 -j ACCEPT
 iptables -I INPUT 2 -m geoip ! --src-cc US -j DROP

如果可以选择的话,我应该把这个脚本放在哪里?

答案1

目前,实现此目的的最佳方法就是完全按照我所建议的方式进行,即不仅添加传入丢弃规则,还添加传出丢弃规则,以便命令

 firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -s 192.168.0.0/24 -j ACCEPT
 firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 1 -m geoip ! --src-cc US -j DROP
 firewall-cmd --permanent --direct --add-rule ipv4 filter OUTPUT 0 -d 192.168.0.0/24 -j ACCEPT
 firewall-cmd --permanent --direct --add-rule ipv4 filter OUTPUT 1 -m geoip ! --dst-cc US -j DROP

目前没有其他方法可以通过firewall-cmd将规则直接添加到INPUT或OUTPUT链

我打算这样做只是因为我觉得如果某种蠕虫或恶意软件进入我的服务器,它与任何国家/地区的传出连接都将被视为 RELATED、ASSURED 或 ESTABLISHED,但这种方法只需添加到 delegate_output 链中似乎就可以阻止所有传出连接,所以我很满意

我非常确定有人可以通过解释如何将命令放入某些 init 脚本或 systemd 脚本中来更好地回答这个问题,但我认为如果 fedora 能找到一个将其直接添加到主链的选项,我会更高兴,但也许这是不好的做法

相关内容