1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-06 12:41:29 +00:00

Increment base port modulo a prime to avoid repeating.

Base port would repeat after 4035 increments (32280 / gcd(256, 32280))
Likely wouldn't cause problems, but this is cleaner and more correct
with the original intent.
This commit is contained in:
dmiller
2021-08-06 02:47:55 +00:00
parent a34e52b931
commit 1ebd91fc0c

View File

@@ -217,26 +217,17 @@ u32 UltraProbe::sctpvtag() const {
3. Nmap sends a TCP SYN probe to port 80 for port scanning. 3. Nmap sends a TCP SYN probe to port 80 for port scanning.
4. Nmap finally receives a delayed TCP RST in response to its earlier ACK 4. Nmap finally receives a delayed TCP RST in response to its earlier ACK
probe, and wrongly marks port 80 as closed. */ probe, and wrongly marks port 80 as closed. */
static u16 base_port;
/* Clamp n to the range [min, max) in a modular fashion. */ /* Base port must be chosen so that there is room to add an 8-bit value (tryno)
static int mod_offset(int n, int min, int max) { * without exceeding 16 bits. We increment modulo the largest prime number N
assert(min < max); * such that 33000 + N + 256 < 65536, which ensures no overlapping cycles. */
n = (n - min) % (max - min); // Nearest prime not exceeding 65536 - 256 - 33000:
if (n < 0) #define PRIME_32K 32261
n += max - min; static u16 base_port = 33000 + get_random_uint() % PRIME_32K;
return n + min;
}
/* Change base_port to a new number in a safe port range that is unlikely to /* Change base_port to a new number in a safe port range that is unlikely to
conflict with nearby past or future invocations of ultra_scan. */ conflict with nearby past or future invocations of ultra_scan. */
void increment_base_port() { void increment_base_port() {
static bool initialized = false; base_port = 33000 + (base_port - 33000 + 256) % PRIME_32K;
if (!initialized) {
base_port = mod_offset(get_random_uint(), 33000, 65536 - 256);
initialized = true;
} else {
base_port = mod_offset(base_port + 256, 33000, 65536 - 256);
}
} }
/* The try number or ping sequence number can be encoded into a TCP SEQ or ACK /* The try number or ping sequence number can be encoded into a TCP SEQ or ACK