Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 38 ยท Scanning and Enumeration

Port Scanning in Depth

Understand how port scanners work: TCP connect and SYN scans, UDP scanning, port states, timing, firewall evasion limits and how to read and log scan results.

Every network service listens on a port: a number from 0 to 65535 that, together with an address and a protocol, identifies where a program accepts connections. A web server usually listens on TCP 80 and 443, SSH on 22, a database on its own number. A port scan asks each port of a host "are you open?", which maps the host's exposed services. It is the heart of the scanning phase, because every open port is a doorway to examine.

Only scan in-scope hosts, and only at rates the rules of engagement allow (Rules of Engagement). The scans below are read-only examples with documentation addresses, plus stand-ins that model the logic on invented data.

Port states

A scanner reports one of several states per port.

State Meaning
open A program is listening and accepted the probe
closed The host is reachable but nothing listens; it replied that the port is closed
filtered No clear answer: a firewall dropped or blocked the probe, so the state is unknown
open or filtered Typical for UDP: silence could mean either
unfiltered Reachable, but the scan could not tell if open or closed (ACK scans)

Closed is not the same as filtered. A closed port tells you the host exists and no firewall is hiding it. A filtered port tells you a firewall is in the way.

How TCP works, briefly

A TCP connection starts with a three-way handshake: the client sends SYN, the server answers SYN/ACK if it is listening, and the client completes with ACK. If nothing listens, the server answers RST (reset). Scanners exploit these replies.

sequenceDiagram
    participant S as Scanner
    participant H as Host
    S->>H: SYN to port 443
    H-->>S: SYN/ACK (port open)
    S->>H: RST (a SYN scan stops here)
    S->>H: SYN to port 81
    H-->>S: RST (port closed)
    S->>H: SYN to port 25
    Note over S,H: no reply: filtered

A SYN scan sends the first packet and reads the answer without completing the connection.

Common scan types

  • TCP connect scan (-sT): the scanner uses the operating system to complete the full handshake. Works without special privileges, but is slower and more likely to be logged by the application.
  • TCP SYN scan (-sS): sends raw SYN packets and does not complete the handshake. Faster and quieter; needs elevated privileges. It is the default for privileged users.
  • UDP scan (-sU): sends UDP datagrams. Open ports often stay silent, so the scan retries and is slow. Important because services such as DNS, SNMP and NTP use UDP.
  • ACK scan (-sA): maps firewall rules rather than open ports.
  • Version and script scans build on discovered ports; see Service and Version Enumeration.

Fancy variants (FIN, NULL, Xmas scans, fragmentation, decoys) mostly matter against old devices. Modern firewalls and intrusion detection often recognize them. They are useful to understand, but not usually needed in a standard test.

What to scan

Start with the top 1000 TCP ports (the default), then scan all 65535 on important hosts, because services sometimes hide on odd ports. Add the most important UDP ports (53, 67, 123, 161, 500 and a few more). Scan twice if the network is flaky, and compare.

Read-only: example output

Terminal
nmap -sS -p- --min-rate 200 192.0.2.10
TEXT
Starting Nmap 7.x ( https://nmap.org ) at 2026-05-12 10:02 UTC
Nmap scan report for 192.0.2.10
Host is up (0.0040s latency).
Not shown: 65530 closed tcp ports (reset)
PORT     STATE    SERVICE
22/tcp   open     ssh
80/tcp   open     http
443/tcp  open     https
3306/tcp filtered mysql
8080/tcp open     http-proxy
Nmap done: 1 IP address (1 host up) scanned in 38.41 seconds

This is an example in the real format, not a recording. Reading it: five ports are interesting. MySQL on 3306 is filtered, which suggests a firewall protects the database from outside. 8080 is unusual and deserves a look.

Timing and politeness

Timing templates run from -T0 (paranoid) to -T5 (insane). -T3 is the default; -T4 is common in labs. For client systems, remember that rate limits in the RoE take priority. Slow scans take longer but are friendlier to old equipment and less likely to trip alarms. An aggressive scan on a production firewall can fill its state table and cause an outage.

Stand-in: model a SYN scan and report states

Real scanning needs raw sockets and a network. The first example models the reply rules of a TCP host so you can see how the three states arise. It uses an invented host with a firewall rule.

Python Model open, closed and filtered ports

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.

The second example parses scan output saved in nmap's "grepable"-style text. Parsing results is exactly how you move from raw output to a table in your report. The text is an invented sample.

Python Parse saved scan results

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.

Interpreting results

For each open port ask: is this service expected? Is it needed from this network? Is it the current version? Is it encrypted? An exposed database port, remote desktop or old management interface is often a finding even before any exploitation. Compare what you found with what the client expected; surprises are the valuable part.

Defender takeaways

  • Close what you do not need; filter what you cannot close.
  • Do not expose databases and management ports to the internet; use VPNs or allow lists.
  • Alert on scan patterns, such as one source touching many ports quickly.
  • Keep a baseline of expected open ports and compare regularly.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A port replies with RST to a SYN probe. What state is it?

    A reset means the host is reachable and nothing listens on that port.
  2. 2. Why is a filtered port different from a closed one?

    Filtered means the scanner could not determine the state because probes or replies were blocked.
  3. 3. Why are UDP scans slow?

    Silence is ambiguous for UDP, so scanners retry and wait for ICMP errors.
  4. 4. What is the advantage of a SYN scan over a connect scan?

    A SYN scan does not complete the connection, so applications rarely see it. It needs raw socket privileges.

Summary

  • A port scan maps exposed services by probing port numbers and reading the replies.
  • States: open, closed, filtered, open or filtered, unfiltered.
  • SYN scans are fast and quiet; connect scans work without privileges; UDP scans are slow but necessary.
  • Scan the top ports first and all ports on important hosts; respect timing limits.
  • Interpret results against expectations; exposed databases and management ports are common findings.

Next: Service and Version Enumeration