看起来很简单 – 我想使用iptables防火墙限制对除了22,80和443之外的所有端口的访问.Linode在这里有一个很好的写:https://library.linode.com/securing-your-server#sph_creating-a-firewall,我按原样使用了他们的防火墙规则.防火墙运行良好.
我还想预先路由一些端口,因为这是一个nodejs应用程序.所以我用过:
sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080 sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 3000
如果我没有防火墙规则,这些规则就有效.事实上,我现在正在使用它们,但我不得不放弃防火墙.
如果我添加防火墙规则,PREROUTING将停止工作.如果我将活动的iptables规则保存到要查看的文件中,则会出现防火墙(过滤规则)和PREROUTING(nat规则),但只有防火墙规则有效.看这里:
# Generated by iptables-save v1.4.18 on Wed Mar 26 02:40:04 2014 *security :INPUT ACCEPT [1606:135329] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [1206:144815] COMMIT # Completed on Wed Mar 26 02:40:04 2014 # Generated by iptables-save v1.4.18 on Wed Mar 26 02:40:04 2014 *raw :PREROUTING ACCEPT [1620:139613] :OUTPUT ACCEPT [1206:144815] COMMIT # Completed on Wed Mar 26 02:40:04 2014 # Generated by iptables-save v1.4.18 on Wed Mar 26 02:40:04 2014 *nat :PREROUTING ACCEPT [4:248] :INPUT ACCEPT [6:376] :OUTPUT ACCEPT [12:728] :POSTROUTING ACCEPT [12:728] -A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080 -A PREROUTING -p tcp -m tcp --dport 443 -j REDIRECT --to-ports 3000 COMMIT # Completed on Wed Mar 26 02:40:04 2014 # Generated by iptables-save v1.4.18 on Wed Mar 26 02:40:04 2014 *mangle :PREROUTING ACCEPT [1620:139613] :INPUT ACCEPT [1606:135329] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [1206:144815] :POSTROUTING ACCEPT [1206:144815] COMMIT # Completed on Wed Mar 26 02:40:04 2014 # Generated by iptables-save v1.4.18 on Wed Mar 26 02:40:04 2014 *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -i lo -j ACCEPT -A INPUT -d 127.0.0.0/8 -j REJECT --reject-with icmp-port-unreachable -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7 -A INPUT -j DROP -A FORWARD -j DROP -A OUTPUT -j ACCEPT COMMIT # Completed on Wed Mar 26 02:40:04 2014
如果我使用iptables -F,它将仅刷新防火墙(过滤器)规则,PREROUTING将再次开始工作.所以这肯定是一场冲突.规则块的顺序似乎并不重要,因为这(上面)是iptables的标准输出,无论我将规则保存到iptables的顺序如何.
对我来说,这似乎是两件事之一:
> PREROUTING也具有ACCEPT规则的端口可能意味着
预选被忽略了
>我正在向一个技术上受阻的端口进行预测(但我认为这是“PRE”进来的地方)
谁知道?
谢谢!
保罗
所以你只需要允许传入流量到iptables中的那些端口:
iptables -A INPUT -p tcp -m tcp –dport 8080 -j ACCEPT
iptables -A INPUT -p tcp -m tcp –dport 3000 -j ACCEPT
由于你将http(s)流量从80/443重定向到8080/3000就像后面的端口是开放的并且无论如何都暴露在互联网上,因此与外部的世界没有区别,并且没有进一步的安全隐患.