Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 37 ยท Threats and Models

Reconnaissance and Port Scanning for Defenders

Learn how port scans work, what open, closed and filtered mean, how to read scanner output and detect scans, with a safe loopback-only scanner you run yourself.

Before attacking, an intruder usually maps the target. The most common map-making technique is the port scan: try to connect to many ports and see which ones answer. Every defender should understand scanning from both sides. You scan your own networks to find what you accidentally expose, and you watch your logs to notice when someone else is scanning you.

This lesson explains how scanning works, how to read the output of a typical scanner, and how to detect scans. The scanner you run here is deliberately limited: it connects only to services started by the same program on the loopback address (127.0.0.1), so it can never touch another machine. Scanning systems you do not own or have permission to test is illegal in many places; see the ground rules.

What a port is, and what a scan learns

A TCP port is a numbered door on a host (0 to 65535). A service "listens" on a port. When a client sends a SYN to a port, three things can happen:

Reply Port state Meaning
SYN+ACK open A service is listening and accepts the connection
RST (reset) closed The host is reachable, but nothing listens on that port
No reply (or ICMP "unreachable") filtered A firewall is dropping the packets, so the scanner cannot tell

The mapping is exactly why firewalls that drop (silently discard) are slower to scan than those that reject (send a reset): drops make the scanner wait for timeouts. Dropping also reveals less. UDP scanning is harder because there is no handshake; an open UDP service often stays silent too.

Scan types, conceptually

  • Connect scan: completes the full TCP handshake with the operating system's normal connect call. Reliable, needs no special privileges, and is the type you can write in a few lines. It is also the easiest to log, because the target sees complete connections.
  • SYN ("half-open") scan: sends a SYN and, if the reply is SYN+ACK, resets instead of completing. Faster and leaves fewer application logs, though firewalls and IDS see it.
  • Host discovery: finding which addresses are alive, using ping or ARP, before scanning ports.
  • Service and version detection: connecting to open ports and reading the banner to learn the software and version. This is how attackers match services to known vulnerabilities.
  • OS fingerprinting: guessing the operating system from subtle packet-level behaviour.

Defenders use the same techniques on their own ranges, with the same rules: scope, authorisation, a quiet time window, and a result list that someone fixes.

A safe scanner on loopback

The program below starts three small servers on 127.0.0.1 with random free ports, closes one of them so that port is "closed", and then scans a list of ports with a connect scan. Because it opens its own listeners and only connects to 127.0.0.1, it is a self-contained demonstration. One service sends a banner that reveals its software version, which shows why banners matter.

Node.js A loopback-only connect scan

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Three results appear: service A is open and its banner reveals product and version, service B is open but silent, and the closed port refuses the connection. Run against your own systems, a scanner produces the same kinds of facts, only for more ports. Note how the banner leaks information: hide or genericise version strings where you can, but never rely on that alone, because attackers can also fingerprint behaviour.

Reading scanner output

Real tools print results in tables. The next TryIt parses a fabricated scanner report (the format is typical of such tools, but the text is invented) and turns it into the list a defender actually wants: which exposed services are not on the approved list?

Python Compare a scan report to the approved list

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Finding "MySQL on a public interface" is exactly the kind of result that justifies a firewall change. Compare scans over time and you also learn what changed.

Detecting scans against you

A scan has a pattern: many connection attempts from one source to many ports, many closed-port resets, short intervals. Logs from firewalls, servers and intrusion detection systems reveal it. The earlier detection example counted distinct ports per source. Real systems add:

  • Time windows: ten ports in one second is a scan; ten ports in an hour may be normal.
  • Low-and-slow detection: patient scanners spread probes over days, so long retention helps.
  • Allow-lists: your own vulnerability scanner and monitoring tools should not set off alarms (but should be logged).
  • Tarpits and honeypots: decoy services that no legitimate user would touch; any connection is suspicious by definition.

Reducing what scanning reveals

  1. Default-deny firewall rules so only intended ports respond at all.
  2. Drop rather than reject on the internet edge, to reveal less and slow scans.
  3. Hide or generalise version banners, remove default pages, and patch regardless.
  4. Rate-limit and auto-block repeated probes from one address, with care that this cannot be abused to block legitimate users.
  5. Scan yourself first, regularly, and fix what the scanner finds.

Shodan-style search engines scan the whole internet continuously and publish what they find. Assume that anything exposed has already been catalogued, and treat "nobody knows about this port" as false.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A scanner gets no reply at all from a port. Which state does it report?

    Silence usually means a firewall dropped the probe, so the scanner cannot tell whether anything listens. Tools call that filtered.
  2. 2. What does a TCP RST reply to a SYN mean?

    A reset in response to a SYN means the host is alive and the port is closed.
  3. 3. Why does this lesson's scanner only connect to 127.0.0.1?

    Scanning systems you do not own is illegal in many places. Loopback keeps the demonstration entirely on your own machine.
  4. 4. Why is a banner showing 'MySQL 5.7.30' useful to an attacker?

    Version information lets an attacker look up known flaws for that release, which is one reason to patch and to avoid advertising versions needlessly.
  5. 5. Which is a good way to detect a slow, patient scan?

    Low-and-slow scans evade short time windows. Long retention and correlation reveal them.

Summary

  • A port scan maps what a host exposes: open (SYN+ACK), closed (RST) or filtered (silence).
  • Connect scans use the normal handshake; SYN scans are half-open; banners and fingerprinting reveal software.
  • Defenders scan their own ranges with authorisation and compare results to an approved list.
  • Detect scans by counting probes per source over time windows, with allow-lists and decoys.
  • Reduce exposure with default deny, dropping, patching and regular self-scans.

Next: Packet Analysis for Defenders