如何在本地使用 iptables 将出站流量重定向到端口 80?

pjf*_*pjf 20 iptables port-forwarding

我正在尝试使用iptables. 类似于透明代理。我想捕获任何试图将我的系统留在端口 80 上的内容并将其重定向到远程主机和端口。

我可以使用 NAT 和预路由功能来实现这一点iptables吗?

slm*_*slm 32

试试这个iptables规则:

$ sudo iptables -t nat -A OUTPUT -p tcp --dport 80 -j DNAT --to-destination IP:80
Run Code Online (Sandbox Code Playgroud)

上面说的是:

  • 将以下规则添加到 NAT 表 ( -t nat)。
  • 此规则将附加 ( -A) 到出站流量 ( OUTPUT)。
  • 我们只对 TCP 流量 ( -p tcp)感兴趣。
  • 我们只对目标端口为 80 ( --dport 80) 的流量感兴趣。
  • 当我们匹配时,跳转到 DNAT ( -j DNAT)。
  • 将此流量路由到其他服务器的 IP @ 端口 80 ( --to-destination IP:80)。

什么是 DNAT?

DNAT
    This target is only valid in the nat table, in the PREROUTING and OUTPUT 
    chains, and user-defined chains which are only called from those chains.
    It specifies that the destination address of the packet should be modified 
    (and all future packets in  this  connection will also be mangled), and
     rules should cease being examined.
Run Code Online (Sandbox Code Playgroud)

参考


小智 6

这可以更具体地仅对特定目标主机的流量进行操作。例如,当 postfix 出错并且队列中的邮件想要发送到旧 IP 地址时。

iptables -t nat -A OUTPUT -p tcp -d {ONLY_TO_THIS_DESTINATION} --dport 25 -j DNAT --to-destination {NEW_IP}:25
Run Code Online (Sandbox Code Playgroud)