the_open_door
coffee
ESEN
← Back to index

Port Scanning (Nmap)

Before attacking anything you need to know what is listening on the machine. An open TCP/UDP port is a service waiting for connections, and every service is a potential attack surface. Port scanning is the first step of the whole process, and how you do it shapes what you find: a poor scan leaves services unseen and closes off paths before you even start.

The standard strategy is two phases: a fast sweep of all 65535 ports to discover which ones are open and then, over those (and only those), a slower targeted scan that identifies the service and its version. Discovery is cheap; fingerprinting is expensive, so you only fingerprint what actually exists.

# Phase 1: full sweep of all 65535 TCP ports, fast
sudo nmap -sS -n -Pn -p- --min-rate 5000 --open 10.10.10.10

# Phase 2: over the open ports, service + version + default scripts
nmap -p22,80,445 -sCV 10.10.10.10 -oN targetedScan

What each flag does (and why).

The UDP scan: why and when. Services that speak UDP show up in no TCP scan, however complete, so a box can look almost empty while the vector sits there listening. To see them you need -sU:

sudo nmap -sU -n -Pn --top-ports 100 --min-rate 5000 10.10.10.10

It is far slower than TCP, and the reason is the protocol itself: TCP has a handshake, so a SYN always draws a reply (SYN/ACK open, RST closed) and one packet settles the state. UDP has none of that: if the port is open, the service usually will not answer an empty packet, and that silence is identical to a port filtered by a firewall. nmap has to wait for timeouts and retry, and it depends on an ICMP port unreachable arriving to mark something "closed" (a message that is typically rate-limited too). Hence all those open|filtered results: not a flaw in the scan, simply that with no reply there is nothing to tell apart.

That is why you do not sweep all 65535 UDP ports: you fire a --top-ports (100 or 1000) alongside the TCP scan, and look harder if TCP yields no vector. What lives there is worth it: SNMP on 161 (with the public community it leaks users, processes and sometimes credentials), DNS on 53, NTP on 123, TFTP on 69 (no authentication), mDNS on 5353, IKE/IPsec on 500, and the 111 of rpcbind giving away NFS. On a domain controller the 53/123/389 trio is practically a signature.

The practical rule: the short UDP sweep always runs, and when TCP offers no vector, UDP stops being optional.

Why it matters. The scan is the foundation of everything that follows: each open port is an attack hypothesis. Skipping -p- or -Pn is the most common cause of a pentest that stalls "with no vector": the vector was on a port that was never looked at. And the version phase (-sV) is not a formality, it is what turns "there is something on 8080" into "there is a Tomcat 9.0.30 vulnerable to X".

How to detect it. A scan leaves a clear network signature: many connection attempts to numerous or sequential ports from a single IP in a few seconds. The SYN scan (-sS) gives itself away with SYNs that never complete the ACK (half-open connections) and a burst of RSTs; the UDP sweep, with a run of ICMP port unreachable. IDS/IPS (Suricata/Snort/Zeek) ship port-scan thresholds for this, and the firewall logs the many denied ports from the same source. A high --min-rate and -p- make the pattern even more obvious.

Defense. Reduce the surface: expose only the ports you need, filter the rest at the firewall and do not rely on the obscurity of a high port. Version detection feeds on banners: minimizing (or faking) them slows the attacker down, though it does not stop them.

Toolsnmapmasscan

Machines where it's used