Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 53 ยท Reconnaissance and Scanning

Port Scanning and Service Discovery Concepts

Understand how port scanners find open services: TCP connect and SYN scans, UDP, banners, how to read Nmap-style output on authorized targets, and how defenders detect scans.

Every service on a network is reachable through a port, and every open port is a door. Port scanning is the practice of asking a host, port by port, whether anything is listening, and then asking each open port what it is. For defenders the result is an inventory of what a system exposes. For attackers it is a shopping list. The technique is the same, so the difference is permission.

Scan only systems you own or have written permission to test. Even a simple scan of someone else's network can be illegal, can trigger incident response at the other end, and can disrupt fragile devices. In this course, scanning happens only inside your own isolated lab.

This lesson explains how scanners decide that a port is open, closed or filtered, how to read the output of a tool such as Nmap, and how defenders notice scans. The real scanner needs raw network access and a real target, so its commands are shown read-only. The runnable parts are a stand-in: a simulated scanner that probes a fake host that lives entirely in memory, which lets you see the logic without any network traffic. Read Networking Essentials for Security first if the handshake is not familiar.

What a scanner learns

Three answers per port

Open A program accepted the connection.
Closed Host replied, nothing listens.
Filtered No reply. A firewall may drop it.

Open ports are what you must justify.

1 / 2

TCP connect and SYN scans

The simplest method is the TCP connect scan: the scanner asks the operating system to open a normal connection to each port. If the full three-way handshake succeeds, the port is open; if the host answers with a reset, it is closed; if nothing comes back before a timeout, it is probably filtered. It needs no special privileges, but because the connection completes, the application on the other side often logs it.

A SYN scan (sometimes called a half-open scan) sends only the first packet of the handshake and looks at the reply. A SYN-ACK means open, and the scanner then resets instead of finishing. It is faster, and older applications never see a connection. It needs raw sockets and therefore administrator rights. Modern firewalls and intrusion detection systems still notice it, so it is not stealthy against a competent defender.

UDP scans are slower and less certain. UDP has no handshake, so an open UDP port often says nothing. A closed one usually triggers an ICMP "port unreachable" message, and the scanner infers "open or filtered" from silence. Important services run on UDP (DNS on 53, SNMP on 161, NTP on 123), so skipping UDP leaves a blind spot.

Other decisions shape a scan: how many ports (the 1,000 most common, or all 65,535), how fast (aggressive speed floods small devices and trips alarms), and in what order (random order is harder to spot than a sequential sweep).

Banners and version detection

Once a port is open, many protocols greet the client with a text line, called a banner. An SSH server sends a line that begins with SSH-2.0- followed by the software name and version; many mail and FTP servers announce themselves similarly. For web servers, the Server header and error pages often disclose the product. A scanner can also send a harmless protocol probe and match the answer against a database of known response patterns to guess the product and version.

Banners are information, not proof: administrators can change or hide them, and a banner can lie. The defensive lesson is the reverse of the attacker's: do not advertise exact versions, but also do not treat hidden banners as a security control. A patched server with a revealing banner is safer than an unpatched one with a bland banner.

Reading Nmap-style output

Nmap is the standard open-source network scanner. The following is a sample of the kind of output it prints for a host in a lab; it is shown for reading practice and was not produced on this site. The addresses are in a documentation range.

TEXT
$ nmap -sV 192.0.2.50          # run only against a host you are authorized to scan
Starting Nmap scan report for 192.0.2.50
Host is up (0.0010s latency).
Not shown: 997 closed tcp ports (reset)
PORT     STATE    SERVICE  VERSION
22/tcp   open     ssh      OpenSSH 8.9 (protocol 2.0)
80/tcp   open     http     nginx 1.18.0
3306/tcp filtered mysql

How to read it: Host is up tells you the host answered something. "Not shown: 997 closed" says the other ports sent resets. The table lists notable ports with their state, the service name from a port table (a guess based on the number), and the version from probing. Questions to ask: Should ssh be reachable from here? Is that nginx version still supported? Why is the database port filtered rather than closed: a firewall is hiding it from this vantage point, which is good, but it may be open from inside. A scan only tells you the view from where you stood.

Common options you will see in documentation: -sT (connect scan), -sS (SYN scan, privileged), -sU (UDP), -p to pick ports, -sV for versions, -oN/-oX to save results. Use them only on authorized targets and only as slowly as the engagement allows.

A scanner against a fake host

The first stand-in builds a fake host in memory with some services, then implements a connect scan, a SYN scan and banner grabbing against it. Nothing touches a network: probe is a plain function that mimics how the host's stack answers.

Node.js Simulate a scanner against an in-memory host

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.

What is real here: the three outcomes (open, closed, filtered) and the idea that a banner is text the service volunteers. What is simplified: real scanners deal with timeouts, retries, rate limiting, packet loss and many protocols. The host log at the end is the doorway to the defender's view.

How defenders notice scans

A scan has a recognizable shape: one source address touching many different ports (or many hosts) in a short time, most of them failing. Typical detection points:

  • Firewall and flow logs that record denied or reset connections.
  • Intrusion detection systems with rules for sequences of SYN packets to many ports.
  • Host logs that record many connection attempts or failed protocol handshakes.
  • Honeypots: a decoy service nobody has a legitimate reason to touch, so any contact is suspicious.

The simplest rule counts distinct destination ports per source within a time window. The second stand-in applies that rule to sample connection records.

Node.js Detect a scan in connection logs

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.

Real detection engines add allow-lists (your own vulnerability scanner is expected to look like this), slower thresholds for patient "low and slow" scans, and correlation across sources. The principle is the same: counting distinct targets per source over time. You will build more of this in Logging, Monitoring and SIEM Basics.

Defensive takeaways

  • Expose only the ports you need, and close or firewall the rest. Run your own scans against your own systems regularly to see what an outsider sees.
  • Keep services patched; a hidden banner is not a fix.
  • Log denied and reset connections and alert on one source touching many ports.
  • Scan from more than one position (outside, inside, between segments), because "filtered" depends on where you stand.
  • Feed scan results into vulnerability scanning and triage.

Nmap scan types, the six port states and output formats

Nmap reports one of six port states, and knowing all six prevents misreading a scan.

State Meaning
open A program is accepting connections or packets on the port.
closed The port answers, but nothing is listening (usually a TCP reset).
filtered Nmap cannot tell, because packets are dropped or blocked before reaching the port.
unfiltered The port answers, but Nmap cannot tell whether it is open or closed (seen with ACK scans).
`open filtered`
`closed filtered`

The main scan types differ in what they send and what they need:

  • Ping (host discovery) scans such as -sn ask which hosts are alive without scanning ports. They use ARP on a local network and ICMP or TCP probes elsewhere. A host that blocks them can still be up.
  • TCP connect scan (-sT) completes the handshake through the operating system; no special rights needed, most visible in application logs.
  • SYN scan (-sS) sends the first packet and reads the reply; needs raw-socket rights and is the default for privileged users.
  • UDP scan (-sU) is slow and ambiguous (see above) but covers DNS, SNMP and NTP.
  • ACK scan (-sA) does not find open ports; it maps firewall rules by showing which ports are filtered versus unfiltered.
  • Version detection (-sV) and OS detection (-O) probe open ports and fingerprint the stack.
  • The Nmap Scripting Engine (--script) runs small scripts for discovery, version checks and vulnerability detection. Some scripts are intrusive, so choose categories deliberately (safe and default first) and only on targets in scope.

Output can be saved as normal text (-oN), grepable text (-oG) or XML (-oX), and -oA name writes all three. The XML or grepable form is what you feed into other tools and reports. Timing templates (-T0 to -T5) trade speed for stealth and load; slower is usually safer on fragile networks.

The stand-in parses a sample of grepable-style output into a summary by state, which is the first thing you do with a large scan: count what is open and list only the ports that need a decision. The text is an invented lab sample.

Node.js Summarize Nmap-style output by port state

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.

Treat open|filtered honestly: it is not a finding, it is a question. Repeat the scan from another position, add version detection for UDP, or check the firewall rules directly.

XMAS, FIN and NULL scans: unusual flag combinations

Besides the connect and SYN scans, scanners can send TCP segments with unusual flag combinations. A FIN scan sets only FIN, a NULL scan sets no flags, and an XMAS scan sets FIN, PSH and URG, so the packet is "lit up like a Christmas tree". The idea comes from the TCP specification: a closed port should answer with RST, while an open port silently drops the odd packet. So a reset means closed, silence means "open or filtered", and the scanner cannot be sure which. These scans can slip past simple stateless packet filters, but they do not work on every system: Windows stacks answer RST regardless, so results are misleading there.

Countermeasures: use a stateful firewall that drops packets that do not belong to a known connection, drop illegal flag combinations at the edge, enable IDS rules for scan signatures, and monitor for one source hitting many ports. Port scans are loud in logs. See firewalls and segmentation.

UDP scanning

UDP has no handshake, so scanning it is slower and less certain. The scanner sends a datagram to a port. If the port is closed, the target usually returns an ICMP "port unreachable" message. If a service answers, the port is open. If nothing comes back, the port is "open or filtered". Operating systems rate-limit ICMP errors (often one per second), so a full UDP scan of 65,535 ports can take hours; scanners therefore try common ports such as DNS (53), DHCP (67), SNMP (161) and NTP (123), and send protocol-specific probes. In Nmap this is the -sU option, which can be combined with a TCP scan in one run.

UDP services are often forgotten and badly configured (an SNMP agent with default community strings, an exposed DNS resolver used for amplification). Defenders should scan their own UDP ports too and close or filter what is not needed.

Syntax of Nmap commands

A command has the shape: nmap, then optional scan type, then options, then the target.

TEXT
nmap [scan type] [options] {target specification}

Targets can be a name, an address, a range (192.0.2.1-20) or a CIDR block. Common scan types are -sS (SYN), -sT (connect), -sU (UDP), -sX/-sF/-sN (flag-based) and -sn (host discovery only). Options include -p for ports, -sV for version detection, -O for operating-system guessing and -oN, -oX, -oG for output formats. Only run these against systems you own or have written permission to test.

Other port-scanning tools and their limits

Nmap is not the only scanner. A vanilla (plain TCP connect) scan simply tries to complete the handshake with each port and works without special privileges, but it is the noisiest. Angry IP Scanner is a fast graphical tool for sweeping ranges. Netcat can test one port at a time and read banners. Zenmap is the official Nmap graphical front end and draws network maps.

Every scanner has limits. A firewall that drops packets looks like "filtered", rate limiting slows the scan, intrusion prevention may block the scanner after a few probes, results are only valid for the moment of the scan, and scanning a large network can disturb weak devices. Verify surprising results with a second method.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A SYN is sent to a port and the host replies with RST. How does a scanner label the port?

    A reset means the host is reachable but no program listens on that port. Silence would suggest filtering and SYN-ACK would mean open.
  2. 2. Why can UDP scanning be unreliable?

    Without a handshake, silence can mean open or filtered. Closed ports usually trigger an ICMP port-unreachable reply, which is the only clear signal.
  3. 3. Which pattern in logs most strongly suggests a port scan?

    Scans are broad: many distinct ports (or hosts) from one source in a short window, with many resets or drops.
  4. 4. What is the correct way to treat a hidden or changed service banner?

    Hiding a version removes a convenience for attackers but does nothing about the flaws in the software. Patching and exposure control are the real defenses.

Summary

  • A port scan classifies ports as open, closed or filtered; banners and probes then identify the service and version.
  • Connect scans complete the handshake and need no privileges; SYN scans stop after the reply and need raw sockets; UDP scans are slower and less certain.
  • Nmap output is read as host status, port, state, service and version. It shows the view from where you scanned.
  • Scan only authorized targets, preferably inside your own lab.
  • Defenders spot scans by counting distinct ports or hosts per source over time in firewall, IDS and host logs.
  • Reduce exposure, patch services and treat banner hiding as a minor extra.

Next: Vulnerability Scanning and Triage