Notes
Ports
Every machine has sixty five thousand TCP ports and as many UDP ports. A service listens on one: SSH on twenty two, web servers on eighty and 443, databases like MySQL and Postgres on their own well-known numbers. Each listening port is a door into the machine and therefore attack surface. The question a scan answers is simple: which doors does this machine have, and what is behind each one?
- 65,535 TCP ports and the same number of UDP ports per host
- 22 SSH, 80 HTTP, 443 HTTPS, 3306 MySQL, 5432 Postgres, 6379 Redis, 3389 RDP
- a service "listens" on a port
- every listening port is attack surface
Three answers
Nmap sends a probe to a port and reads the reply. If a SYN-ACK comes back, the port is open: something is listening. If a reset comes back, the port is closed: the host is there but nothing listens. If nothing comes back at all, the port is filtered: a firewall silently dropped the probe. From the outside, a well-configured server shows a few open ports that are meant to be public, and everything else filtered.
- open: a SYN-ACK comes back; a service is listening
- closed: a RST comes back; the host is there, nothing listens
- filtered: no reply; a firewall dropped the probe
- filtered is what you want outsiders to see
Two scan types
There are two main scan types. The SYN scan sends a SYN, reads the answer, and never completes the handshake, which makes it fast and a little quieter, because many services only log completed connections. It needs root. The connect scan completes the full handshake through the operating system and needs no special rights, but it is slower and shows up in the service's own logs. Both find the same open ports.
# SYN scan (needs root) sudo nmap -sS 10.10.10.5 sends SYN, reads the reply, never completes the handshake # connect scan (no root) nmap -sT 10.10.10.5 full handshake; the service logs a connection
What is behind it
Knowing a port is open is only the start. With version detection, Nmap talks to each open port and identifies the software and its version. That is the real finding, because a version can be matched against known vulnerabilities. In this example from a lab machine, a database port is open next to the web server. If this were the public face of a real server, that open database port would be the first thing to fix.
$ nmap -sV -p 22,80,5432 10.10.10.5 PORT STATE SERVICE VERSION 22/tcp open ssh OpenSSH 9.6 80/tcp open http nginx 1.24 5432/tcp open postgres PostgreSQL (example output from a lab VM)
Useful options
A handful of options covers most real work. Minus s n finds which hosts are up without scanning ports. Minus p minus scans all ports, not just the common thousand. Minus s V detects versions. Minus s C runs the default set of safe scripts. Minus O guesses the operating system. Minus s U scans UDP, which is slow, so limit it to the top ports. And always save the output, because a scan you cannot show is a scan you did not do.
nmap -sn 10.10.10.0/24 who is up nmap -p- host all 65535 nmap -sV host versions nmap -sC host safe scripts nmap -O host guess the OS nmap -sU --top-ports 20 host UDP nmap -oA scan host save output nmap -A host -sV -sC -O + trace
The scripting engine
Nmap also has a scripting engine with hundreds of scripts, grouped by category. The safe ones report useful facts: which TLS versions and ciphers a server accepts, the titles and security headers of web pages, the algorithms an SSH server offers. Others test for specific vulnerabilities or try passwords. Stay with the safe and default categories unless your scope explicitly allows more. Intrusive scripts can crash fragile systems, and that is your responsibility.
- hundreds of scripts, grouped: default, safe, discovery, vuln, auth, brute, intrusive
- safe examples: TLS versions and ciphers, HTTP titles and headers, SSH algorithms
- stay in "safe" and "default" unless the scope says more intrusive ones can crash fragile systems
What the target sees
Now the defender's view. A scan is one source touching many ports in seconds. Firewall logs show a burst of dropped connection attempts, an intrusion detection system raises a port scan alert, and connect scans leave entries in each service's own log. Attackers slow their scans down to hide, but the pattern of one source walking through ports remains. Internet-facing servers are scanned constantly by strangers; what matters is what those scans find.
- one source, many ports, in seconds
- firewall logs: a burst of dropped SYNs
- IDS: a "port scan" alert
- connect scans: the service logs each connection
- slower scans hide better, but the pattern remains
The defender's scan
The best use of Nmap is on yourself. Scan your own address ranges from the outside, as the internet sees you, and from the inside, as an intruder would. Do it on a schedule and compare each scan with the last; a tool called ndiff shows exactly what changed. Every open port should have an owner and a reason. Close anything that has neither. A new open port that nobody can explain is either a mistake or an incident.
- scan your own ranges from outside and from inside
- compare with the last scan: what is new?
- ndiff shows the difference
- every open port needs an owner and a reason
- close what has neither
- this is attack surface management
Rules
The rules, because this is the first tool in the track that touches other machines. Scan only your own lab, systems you own, or targets inside a written scope. In the cloud, check the provider's testing policy. At work, tell the network and security operations teams before you scan, or you will become their incident. Go gently on old or fragile devices such as printers and industrial equipment. And keep the scope document where you can find it.
- scan only: your lab, your own systems, or a written scope
- cloud: check the provider's testing policy
- at work: tell the network and SOC teams first
- be gentle on old or fragile devices
- keep the scope document
Common mistakes
Mistakes. Scanning without permission. Checking only the default thousand ports, when services hide on high ones. Forgetting UDP entirely. Trusting a single scan from a single location, when a firewall may show a different face to different sources. Running intrusive scripts against production. Saving no output, so you cannot compare next month. And the most common: producing a neat list of open ports, and closing none of them.
- scanning without permission
- only the top 1000 ports
- forgetting UDP
- trusting one scan from one place
- intrusive scripts on production
- no saved output, no diff
- finding ports, fixing none
Say it like this
In the interview: a port scan sends probes and reads the replies. A SYN-ACK means open, a reset means closed, and silence means filtered by a firewall. A SYN scan never completes the handshake; a connect scan does. Defensively, I scan my own ranges from outside and inside on a schedule with version detection, compare each result with the previous one, and make sure every open port has an owner and a reason, closing the rest.
- "Nmap probes ports and reads
- the reply: SYN-ACK open, RST
- closed, silence filtered. A SYN
- scan never completes the handshake. I scan my own
- ranges on a schedule with version detection, diff the results, and make sure every open port has an owner."