The idea: every port answers in one of three ways

A port scan sends one small probe to each port of a host and sorts the ports by the answer. The answers are not chosen by the scanner. The TCP and ICMP standards fix them, so a defender can predict exactly what a scan looks like, and what it leaves in each log.

The page uses a toy lab: a scanner at 10.0.9.66 probes a host at 10.0.0.5, a private address. The host runs sshd on TCP 22, nginx on TCP 80 and a DNS server on UDP 53. Nothing listens on TCP 23 and 445 or on UDP 69, 161 and 137. A stateful firewall sits in between, and an IDS watches a copy of the outside link.

Three rows, each a SYN from the scanner. Row 1: to port 22 where sshd listens, the host answers SYN-ACK, so the scanner writes open. Row 2: to port 23 with no socket, the host answers RST, so closed. Row 3: to port 445, the firewall drops the SYN and logs it, nothing comes back, so filtered.
The reply decides the verdict: SYN-ACK means open, RST means closed, silence means filtered.
replyscanner writeswhy
SYN-ACKopena socket is in LISTEN on that port
RSTclosedthe host is up, but no socket is bound to the port
nothingfiltereda firewall dropped the probe or its reply

A full connect and a half-open probe

The connect probe finishes the handshake: SYN, SYN-ACK, ACK. The connection moves to the accept queue, the service calls accept(), and then the scanner closes it at once. Because the service saw a real connection, it can log it: sshd and nginx both record the scanner's address (Demo: connect scan, the services log it).

The SYN probe, also called half-open, stops after the SYN-ACK and sends a RST instead of the last ACK. The SYN-ACK already proves the port is open. The half-open entry in the kernel's SYN queue is removed, accept() never returns, and the service log stays empty (Demo: SYN scan, firewall blocks 445). How the two queues work is on the TCP handshake page. The firewall and the IDS still see every packet, so a quiet service log does not mean nobody looked.

FIN and NULL probes, and the TCP rule behind them

RFC 9293 (which replaced RFC 793) says what a host does with a segment that fits no connection:

  • On a CLOSED port (no socket), any segment without RST gets a RST back (section 3.10.7.1).
  • On a port in LISTEN, a segment with neither SYN nor ACK is dropped without a reply.

A FIN probe (a close with no connection behind it) and a NULL probe (no flags at all) use this. A closed port answers RST, an open port stays silent. But silence also happens when a firewall drops the probe, so the best the scanner can write is open|filtered (Demo: FIN scan, no firewall: 22 and 80 open|filtered, 23 and 445 closed). Some stacks, Windows among them, send RST from open ports too, so these probes are unreliable even on a bare LAN.

A stateful firewall ends the trick. A FIN or a no-flag segment with no flow behind it fits no TCP state, so conntrack gives it ct state INVALID, and the usual ct state invalid drop rule drops it. All four ports come out open|filtered, the drop log gets four lines, and the host never sees a packet (Demo: FIN scan meets a stateful firewall). The stateful firewall page shows the conntrack table itself.

UDP: no handshake, ICMP errors, and a rate limit

UDP has no SYN and no reply of its own. An empty datagram to a closed port makes the host send ICMP type 3 code 3, port unreachable, which quotes the datagram's header (RFC 792). An open service usually ignores an empty datagram: the DNS server sees no valid query and says nothing. So the scanner can only write closed (ICMP came back) or open|filtered (silence).

Hosts also limit how many ICMP errors they send; Linux allows about one per second to the same peer (net.ipv4.icmp_ratelimit, 1000 ms, after a small burst). The page uses a strict toy limit of one per second. Probes come 0.5 s apart, so the ICMP for UDP 161 is held back, and a closed port looks open|filtered (Demo: UDP scan and the ICMP rate limit: 69 and 137 closed, 53 and 161 open|filtered, ICMP sent 2, held back 1). UDP scans are slow and give the defender less to see in the host's own logs.

A grid. Rows: connect probe, SYN probe, FIN or NULL probe through the firewall, UDP probe. Columns: service log, firewall, IDS tap. Connect: service log yes, firewall yes, IDS yes. SYN: service log no, firewall yes, IDS yes. FIN or NULL: service log no, firewall yes as INVALID drops, IDS yes with ODD-FLAGS. UDP: service log no, firewall yes, IDS yes.
Only the full connect reaches an application's log; the firewall and the IDS see every kind of probe.

What the firewall and the IDS see

The firewall judges every packet. With logging on its drop rules, each filtered probe leaves a line with time, port, flags and the ct state. The IDS sits on a tap of the outside link and sees even the packets the firewall drops. Two simple rules catch these scans:

IDS rulematcheson the canvas
PORTSCAN3 or more distinct destination ports from one source within 5 sone alert per scan, at the third probe
ODD-FLAGSa TCP segment with FIN but no ACK, or with no flags at allone alert per FIN or NULL scan, with a count

Real systems such as Snort and Suricata have port-scan detectors and flag rules of the same shape, with thresholds you tune. The intrusion detection page shows signature and anomaly rules, and how a threshold trades false alarms against missed attacks.

Defences

  • Default deny. Allow only what must be reachable: port 80 from anywhere, SSH only from the admin network. Everything else drops and is logged. A scan then finds one open port and seven filtered ones (Demo: default deny, only port 80 shows: drop log 7 lines).
  • Close unused services. A port with no listener cannot be attacked. Port 23 here has nothing on it: it answers RST, which tells only that the host is up.
  • Drop invalid packets. ct state invalid drop silences FIN and NULL probes and many malformed packets.
  • Watch and alert. Keep the firewall's drop log and the IDS alerts, and look at who scans you. A scan is often the first step before an attack.
  • Scan yourself. Teams scan their own networks, with written permission, to find exposed services before others do. Scanning systems you do not own or have no permission to test is against the law in many countries.

What the page leaves out

The page has one host and four ports per protocol, and every probe is sent once. Retries, timing templates, IPv6, OS and version detection, and the ICMP "administratively prohibited" reply of a REJECT rule are not shown. The ICMP rate limit is a strict one per second, with no burst. The service log lines are simplified, and the conntrack table is reduced to a count of open flows. Ways of hiding a scan from an IDS are left out on purpose.

On the mind map

This page on the Networking mind map · 70 pages in 10 branches · open it to see the whole map and test yourself in recall mode.