我在我的Ubuntu服务器14.04上有以下规则:
#!/bin/sh # Flushing all rules iptables -F iptables -X # Setting default filter policy iptables -P INPUT DROP iptables -P OUTPUT DROP iptables -P FORWARD DROP # Allow unlimited traffic on loopback iptables -A INPUT -i lo -j ACCEPT iptables -A OUTPUT -o lo -j ACCEPT # Allow incoming SSH iptables -A INPUT -p tcp --sport 513:65535 --dport 22 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT iptables -A OUTPUT -p tcp --sport 22 --dport 513:65535 -m state --state ESTABLISHED,RELATED -j ACCEPT # Forward 80 to 8080 iptables -I INPUT -m mark --mark 1 -j DROP iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-destination SERVER_IP:8080 iptables -t mangle -A PREROUTING -p tcp --dport 8080 -j MARK --set-mark 1 iptables -A INPUT -p tcp --dport 8080 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT iptables -A OUTPUT -p tcp --sport 8080 -m state --state ESTABLISHED,RELATED -j ACCEPT # Allow DNS lookup iptables -A OUTPUT -p tcp --dport 53 -j ACCEPT iptables -A OUTPUT -p udp --dport 53 -j ACCEPT iptables -A INPUT -p tcp --sport 53 -m state --state ESTABLISHED,RELATED -j ACCEPT iptables -A INPUT -p udp --sport 53 -m state --state ESTABLISHED,RELATED -j ACCEPT
而我无法做一个简单的(连接拒绝):
wget http://www.example.com
或(拒绝连接):
wget http://localhost/
甚至(保持空闲):
wget http://localhost:8080
如何修复我的configuration,以便能够做到这一点?
谢谢!
其中一些是不必要的复杂(特别是,如果你有一个公共的web服务器,显然是在80端口,试图阻止它通过端口8080访问,如果它实际上监听)是没有意义的; 此外,您只是在开始时冲洗filter表。 (另外请注意, -m state已被弃用;较新的iptables首选-m conntrack --ctstate 。)
尝试这个:
#!/bin/sh # Flush all rules for table in nat filter mangle; do iptables -F -t $table iptables -X -t $table done # Set default filter policy iptables -P INPUT DROP iptables -P OUTPUT DROP iptables -P FORWARD DROP # Allow unlimited traffic on loopback iptables -A INPUT -i lo -j ACCEPT iptables -A OUTPUT -o lo -j ACCEPT # Allow reply packets iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # Allow incoming SSH iptables -A INPUT -p tcp --dport 22 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT # DNAT 80 to 8080 iptables -t nat -A PREROUTING -i eth0 -p tcp -d SERVER_IP --dport 80 -j DNAT --to-destination SERVER_IP:8080 iptables -t nat -A OUTPUT -p tcp -d SERVER_IP --dport 80 -j DNAT --to-destination SERVER_IP:8080 iptables -t nat -A OUTPUT -p tcp -d localhost --dport 80 -j DNAT --to-destination SERVER_IP:8080 iptables -A INPUT -p tcp --dport 8080 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT # Allow DNS lookup iptables -A OUTPUT -p tcp --dport 53 -j ACCEPT iptables -A OUTPUT -p udp --dport 53 -j ACCEPT # ICMP is needed for pmtu discovery (could be more selective here, perhaps apply ratelimit): iptables -A INPUT -p icmp -j ACCEPT iptables -A OUTPUT -p icmp -j ACCEPT # Log dropped packets for debugging, with a ratelimit iptables -A INPUT -m limit --limit 5/minute -j LOG --log-prefix "FW: INPUT DROP: " iptables -A OUTPUT -m limit --limit 5/minute -j LOG --log-prefix "FW: OUTPUT DROP: " # flush conntrack table to make sure new rules are applied to existing connections (at the cost of interrupting them): conntrack -F
如果没有其他的事情,最后的日志logging应该帮助确定你不应该丢弃的数据包。
检查您的Web服务器是否正在侦听回送端口: lsof -Pni:8080应至less显示一个LISTENING套接字,该套接字应绑定到IPv4 *:8080,IPv6 *:8080或一些特定接口(如127.0)。 0.1:8080和SERVER_IP:8080
拒绝连接通常意味着该端口上没有任何监听,因此请检查Web服务器是否正在运行并在该接口上进行监听。
请记住,您的规则集仅针对来自eth1的stream量执行DNAT,因此请确保从机器外部进行testing。
从本地主机,你应该可以做一个curl -I http://127.0.0.1:8080/或类似的。 请注意, localhost并不总是在所有系统上为您提供127.0.0.1 ,但通常会首先给您::1 ,所以在testing时请使用127.0.0.1 。