大网数据广告活动
服务器运维问答

DNAT to another hidden port on same host

· 52CE 我爱测

I am hoping to use iptables to silently redirect incoming traffic arriving at a privileged port on the public eth interface (public IPv4 address) to a hidden unprivileged port on the same ubuntu box. The ufw firewall is active. T…

问题原文

原帖链接 · user12304836 · CC BY-SA 4.0

I am hoping to use iptables to silently redirect incoming traffic arriving at a privileged port on the public eth interface (public IPv4 address) to a hidden unprivileged port on the same ubuntu box. The ufw firewall is active. There are other options, of course, such as capabilities, or a privileged process copying traffic to the unprivileged port, or a privileged process dropping its privs after bind, etc, but to me at least iptables seems to be the most appealing.

The dummy interface solution in DNAT to 127.0.0.1 with iptables / Destination access control for transparent SOCKS proxy would be perfect for me (once I insert an extra 'dev' keyword into the ip add addr... command). Except for one thing:

  • I must open the firewall up for incoming traffic to the IP of the dummy interface on the hidden port

As far as I can see, this allows others to send incoming traffic directly to that IP:port, should they be able to guess the roughly 40 bits of hidden info (a private IPv4 address + a port).

Is there a solution where no outside traffic can reach that hidden port, unless the original dest IP is the public IP of the box, and the dest port is the privileged port?


[In everyday terms, the dummy interface solution is like having my buddy come to my club, and telling him that here is a VIP pass, take it to the back entrance, and the bouncer will let you in. He will get in, sure, but anyone else may try to fool the bouncer with a fake pass. Can I instead escort my buddy to the back entrance, tell the bouncer he is good, and walk in with him? In other words, can I mark this NATted packet somehow that the firewall rules are bypassed on it?]

已采纳解答

原帖链接 · user12304836 · CC BY-SA 4.0

In the meantime I learned that one CAN mark packets (see, e.g., How to set mark on packet when forwarding it in nat prerouting table?), and also learned more about which iptables operations are applied when (https://www.digitalocean.com/community/tutorials/a-deep-dive-into-iptables-and-netfilter-architecture). With these the solution is straightforward:

  1. in /etc/ufw/before.rules, add a MANGLE rule:
*mangle
-F
-A PREROUTING -p tcp -i eth0 -d 1.2.3.4 --dport 80 -j MARK --set-mark 0x401
COMMIT
  1. in the same file insert a redirect into the NAT table rules (*nat section) with the line
-A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
  1. and finally, insert into the ufw-before-input chain of the FILTER table (*filter section) with the line
-A ufw-before-input -m mark --mark 0x401 -j ACCEPT
  1. restart ufw
services ufw restart

There is no need to open any ports, incoming packets to 1.2.3.4:80 get redirected to 1.2.3.4:8080 and are explicitly greenlighted through the firewall. The time order of iptables operations on the packet is: mark, redirect, let through.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-02-11)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。