iptables:如何允许来自重定向端口的流量

Den*_*niz 5 iptables nat

我有一个在 debian 7 上运行的 Web 服务并监听端口 8080。我想将 80 重定向到 8080 以进行入站连接并只允许端口 80。这是我的iptables配置:

root@localhost:~# iptables -v -L --line-numbers
Chain INPUT (policy DROP 76 packets, 6266 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1       90  8898 ACCEPT     all  --  lo     any     anywhere             anywhere            
2        0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere             tcp dpt:http
3     4515 3113K ACCEPT     all  --  any    any     anywhere             anywhere             state RELATED,ESTABLISHED

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 4858 packets, 587K bytes)
num   pkts bytes target     prot opt in     out     source               destination     
Run Code Online (Sandbox Code Playgroud)

和 nat 表:

root@localhost:~# iptables -L -n -v -t nat
Chain PREROUTING (policy ACCEPT 14 packets, 2288 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REDIRECT   tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80 redir ports 8080
    0     0 REDIRECT   udp  --  *      *       0.0.0.0/0            0.0.0.0/0            udp dpt:80 redir ports 8080

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 841 packets, 53415 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 841 packets, 53415 bytes)
 pkts bytes target     prot opt in     out     source               destination 
Run Code Online (Sandbox Code Playgroud)

我无法在端口 80 上从外部建立连接。可能存在哪些不足?

Rah*_*til 3

当用户点击端口 80 时,iptables首先检查NAT PREROUTING表,然后检查FILTER表,因此根据您的情况,您需要在过滤器输入链中允许端口 8080。

参见下面的示例:

在过滤器表中:

iptables -A INPUT -i eth0 -p tcp -m tcp --dport 8080 -m state --state NEW,ESTABLISHED -j ACCEPT
Run Code Online (Sandbox Code Playgroud)

在 Nat 表中:

iptables -t nat -A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080
Run Code Online (Sandbox Code Playgroud)

上述规则已使用 Filter INPUT Policy Drop 进行了测试,并且有效。

表的顺序如下:

  1. Mangle 预路由
  2. 自然预路由
  3. 管理输入
  4. 过滤输入

欲了解更多详情,请查看页面。

  • 1. 将端口 8080 上的传入流量重定向到 81 `iptables -t nat -I PREROUTING -p tcp --dport 8080 -j REDIRECT --to-ports 81` 2. 由于 INPUT 链被 DROP,因此端口 81 将被阻止 (2认同)