Do not reuse the raw scan source port for service scan connections

The service scan binds the source address returned by SourceSockAddr(),
which carries the source port used by the raw packet scans. Every
connect() for a given target therefore uses the same source port, so a
second probe to the same host and port reuses the 4-tuple of the first
while it is still in TIME_WAIT. connect() fails with EADDRNOTAVAIL and
service detection for that port is abandoned:

  Callback: CONNECT ERROR [Cannot assign requested address (99)]
  Got nsock CONNECT response with status ERROR - aborting this service

This affects any service whose first probe only softmatches, since that
is what makes nmap open a second connection to try for a hard match. It
is reproducible whenever nmap runs privileged; it does not occur with
-sT or --unprivileged, where no source address is set.

Bind the source address only and let the kernel choose the source port.
This commit is contained in:
Martin Desrumaux 2026-09-11 14:40:15 +02:00
parent 08312c289b
commit e4a93e824b

View file

@ -2137,6 +2137,16 @@ static void startNextProbe(nsock_pool nsp, nsock_iod nsi, ServiceGroup *SG,
fatal("Failed to allocate Nsock I/O descriptor in %s()", __func__);
}
if (0 == svc->target->SourceSockAddr(&ss, &ss_len)) {
/* The port in the source address belongs to the raw packet scans.
Reusing it for every connect() socket means a second probe to the
same target reuses the same 4-tuple as the first, which is still in
TIME_WAIT: connect() then fails with EADDRNOTAVAIL and service
detection for the port is aborted. Bind the source address only and
let the kernel pick the source port. */
if (ss.ss_family == AF_INET)
((struct sockaddr_in *) &ss)->sin_port = 0;
else if (ss.ss_family == AF_INET6)
((struct sockaddr_in6 *) &ss)->sin6_port = 0;
nsock_iod_set_localaddr(svc->niod, &ss, ss_len);
}
if (o.ipoptionslen)