The Port That Does Not Exist
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
The Port That Does Not Exist
Port 0 is the port you are not supposed to use, so nobody looks there. So we put an IRC network on it. Then I got curious whether anyone else had, and scanned all of IPv4 for port 0. Turns out we weren’t alone, and some of our neighbours really shouldn’t be there. Ask any program to listen on port 0 and it will hand you a socket on port 47193…or 58037…or whatever the kernel felt like that day.
端口 0 是你不应该使用的端口,所以没人会去查看它。因此,我们在上面架设了一个 IRC 网络。后来我很好奇是否还有其他人这样做过,于是扫描了整个 IPv4 地址空间的端口 0。结果发现我们并不孤单,而且我们的一些邻居确实不应该出现在那里。如果你要求任何程序监听端口 0,它都会给你分配一个诸如 47193 或 58037 之类的端口,具体取决于内核当天的“心情”。
Port 0 is not a port you can have. It is the number you pass when you want the operating system to pick one for you. But a TCP header is just bytes. The destination port field is 16 bits wide, and 0x0000 fits in 16 bits as well as anything else does. Nothing on the wire stops a packet from being addressed to port 0. Your tools will refuse it, your browser will block it, and your scanner’s default port list skips it, because everyone agrees it isn’t a real port. Which makes it a pretty good place to hide things, whether anyone meant to or not.
端口 0 不是你可以拥有的端口。当你希望操作系统为你选择一个端口时,你会传入这个数字。但 TCP 报头仅仅是字节而已。目标端口字段有 16 位宽,0x0000 和其他任何数字一样都能填入这 16 位中。在网络传输中,没有任何机制能阻止数据包被寻址到端口 0。你的工具会拒绝它,浏览器会拦截它,扫描器的默认端口列表也会跳过它,因为大家都认为它不是一个真正的端口。这使得它成为了一个隐藏事物的绝佳场所,无论这是否是有意为之。
So we did some TCP cowboy shit & port scanned the entire fucking internet for open ports on port 0, which uncovered some interesting unexpected findings.
于是我们搞了一些 TCP 的“牛仔操作”,对整个互联网进行了端口 0 的扫描,结果发现了一些有趣且出乎意料的情况。
What Port 0 Actually Is
端口 0 究竟是什么
The standards are thin on this. RFC 9293, the 2022 consolidation of the TCP spec, defines the source and destination port as “16 bits” and leaves it there. It has no rule against zero. IANA lists 0/tcp and 0/udp as Reserved, pointing at RFC 6335. That RFC splits the port space into System (0–1023), User (1024–49151) and Dynamic (49152–65535) ranges, and explains what “Reserved” means: Reserved port numbers include values at the edges of each range, e.g., 0, 1023, 1024, etc., which may be used to extend these ranges or the overall port number space in the future. So port 0 is not forbidden. It is set aside.
关于这一点,标准文件语焉不详。2022 年整合 TCP 规范的 RFC 9293 将源端口和目标端口定义为“16 位”,仅此而已。它没有任何禁止使用零的规则。IANA 将 0/tcp 和 0/udp 列为“保留”,并指向 RFC 6335。该 RFC 将端口空间划分为系统端口(0–1023)、用户端口(1024–49151)和动态端口(49152–65535)范围,并解释了“保留”的含义:保留端口号包括每个范围边缘的值,例如 0、1023、1024 等,这些值可能在未来用于扩展这些范围或整个端口号空间。所以端口 0 并非被禁止,它只是被预留了。
The only place zero has a defined protocol meaning is UDP: RFC 768 makes the source port optional, and “if not used, a value of zero is inserted.” TCP has nothing like that.
零在协议中唯一有明确定义的场景是 UDP:RFC 768 将源端口设为可选,并规定“如果不使用,则填入零值”。TCP 则没有类似的规定。
Why You Can’t Listen On It
为什么你无法监听它
Port 0’s reputation comes from the sockets API, not the wire. When you call bind() with sin_port = 0, you are not asking for port 0. You are asking for any port. The ip(7) man page lists every case where Linux hands out an ephemeral port: bind() with port 0, listen() on an unbound socket, connect() on an unbound socket, and sendto() on an unbound datagram socket. The range comes from net.ipv4.ip_local_port_range, which defaults to 32768 60999 on Linux, not the 49152–65535 that IANA calls Dynamic. And asking for port 0 needs no privileges. __inet_bind() only checks CAP_NET_BIND_SERVICE when the requested port is non-zero.
端口 0 的名声源于套接字 API,而非网络传输本身。当你调用 bind() 并设置 sin_port = 0 时,你并不是在请求端口 0,而是在请求“任意端口”。ip(7) 手册页列出了 Linux 分配临时端口的所有情况:使用端口 0 调用 bind()、在未绑定的套接字上调用 listen()、在未绑定的套接字上调用 connect(),以及在未绑定的数据报套接字上调用 sendto()。该范围来自 net.ipv4.ip_local_port_range,在 Linux 上默认为 32768 到 60999,而不是 IANA 所称的 49152–65535 动态范围。此外,请求端口 0 不需要任何特权。__inet_bind() 仅在请求的端口非零时才会检查 CAP_NET_BIND_SERVICE。
How the kernel picks is more interesting than I expected. In net/ipv4/inet_connection_sock.c (v6.8), inet_csk_find_open_port() starts at a random offset and then does this: offset = get_random_u32_below(remaining); /* __inet_hash_connect() favors ports having @low parity * We do the opposite to not pollute connect() users. */ offset |= 1U; It walks the range in steps of two. The function above it says it outright: “We try to allocate an odd port (and leave even ports for connect()).” Outgoing connections go through __inet_hash_connect() in inet_hashtables.c, which implements RFC 6056 Algorithm 4 (double-hash port selection) and takes the even ports first. The kernel splits the range by parity so servers asking for “any port” and clients making outbound connections don’t fight over the same numbers. If SO_REUSEADDR is set, bind() also tries the lower half of the range before the upper half.
内核的选择方式比我预期的更有趣。在 net/ipv4/inet_connection_sock.c (v6.8) 中,inet_csk_find_open_port() 从一个随机偏移量开始,然后执行以下操作:offset = get_random_u32_below(remaining); /* __inet_hash_connect() 倾向于使用低奇偶校验端口 * 我们采取相反的做法,以免干扰 connect() 用户。 */ offset |= 1U; 它以步长为 2 的方式遍历范围。其上方的函数直言不讳地写道:“我们尝试分配奇数端口(并将偶数端口留给 connect())。” 外出连接通过 inet_hashtables.c 中的 __inet_hash_connect() 处理,该函数实现了 RFC 6056 算法 4(双哈希端口选择),并优先使用偶数端口。内核按奇偶性划分范围,这样请求“任意端口”的服务器和发起外出连接的客户端就不会争抢相同的端口号。如果设置了 SO_REUSEADDR,bind() 还会先尝试范围的下半部分,然后再尝试上半部分。
So We Did It Anyways
所以我们还是做到了
You can bypass all of this with raw sockets, but if you just want to cheat, you can simply forward the port to another with iptables/nftables. As a fun proof of concept, I set up my IRC network at irc.supernets.org to accept plaintext connections on port 0 and SSL connections on port 1. JUST TO FLEX. 😎
你可以通过原始套接字绕过所有这些限制,但如果你只是想投机取巧,完全可以用 iptables/nftables 将该端口转发到另一个端口。作为一个有趣的验证概念,我在 irc.supernets.org 上架设了我的 IRC 网络,使其在端口 0 接受明文连接,在端口 1 接受 SSL 连接。纯粹是为了炫技。😎
-A PREROUTING -p tcp -m tcp —dport 0 -j REDIRECT —to-ports 6667 -A PREROUTING -p tcp -m tcp —dport 1 -j REDIRECT —to-ports 6697
A SYN arrives for port 0, netfilter rewrites the destination to 6667 where the ircd is actually listening, and the reply is rewritten back on the way out. The ircd never knows. Port 1 does the same for TLS on 6697.
一个发往端口 0 的 SYN 包到达,netfilter 将目标重写为 ircd 实际监听的 6667 端口,回复包在出去时会被重写回来。ircd 对此一无所知。端口 1 对 6697 上的 TLS 连接也执行同样的操作。
Talking To Port 0 Is Its Own Problem
与端口 0 通信本身就是个问题
Getting a server onto port 0 was the easy part. Getting a client there is where tools give up. I pointed everything I had at our own server on port 0:
让服务器运行在端口 0 上是简单部分,让客户端连接到那里才是工具们放弃的地方。我用我所有的工具测试了我们运行在端口 0 上的服务器:
ClientResult OpenBSD ncnc: port number too small: 0 telnet (inetutils)Port number 0 is out of range. Chromiumnet::ERR_UNSAFE_PORT (port 1 too) socatconnects, works Python asyncioconnects, works Python ssl on a blocking socketBlockingIOError
That last one is strange, because the TCP connection underneath it is fine. The problem is that the kernel says it isn’t, and it does that on purpose. Here is inet_getname() in net/ipv4/af_inet.c, which backs getpeername(): if (peer) { if (!inet->inet_dport || (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) && peer == 1)) { release_sock(sk); return -ENOTCONN; } Linux uses “destination port is 0” as its internal marker for “this socket isn’t connected to anything.” A socket that really is connected to port 0 trips that check, so getpeername() returns ENOTCONN on a working connection. Python’s ssl.wrap_socket() calls getpeername() to decide whether the socket is connected, gets ENOTCONN, decides it isn’t, switches to non-blocking and tries a recv(1), gets EAGAIN, and raises BlockingIOError. The TCP connection is fine. It just reports that it doesn’t
最后一种情况很奇怪,因为底层的 TCP 连接是正常的。问题在于内核声称它不正常,而且这是故意的。这是 net/ipv4/af_inet.c 中支持 getpeername() 的 inet_getname() 函数:if (peer) { if (!inet->inet_dport || (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) && peer == 1)) { release_sock(sk); return -ENOTCONN; } Linux 使用“目标端口为 0”作为其内部标记,表示“此套接字未连接到任何东西”。一个真正连接到端口 0 的套接字会触发该检查,因此 getpeername() 在一个正常工作的连接上会返回 ENOTCONN。Python 的 ssl.wrap_socket() 调用 getpeername() 来判断套接字是否已连接,结果得到 ENOTCONN,判定其未连接,于是切换到非阻塞模式并尝试 recv(1),得到 EAGAIN,进而抛出 BlockingIOError。TCP 连接本身没问题,它只是报告说它没有……