From e4a93e824bf760bc77e1eedc94d0ad5d3e902b59 Mon Sep 17 00:00:00 2001 From: Martin Desrumaux <9059840+gnuletik@users.noreply.github.com> Date: Fri, 11 Sep 2026 14:40:15 +0200 Subject: [PATCH] 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. --- service_scan.cc | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/service_scan.cc b/service_scan.cc index a4e284718..de9568099 100644 --- a/service_scan.cc +++ b/service_scan.cc @@ -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)