Port and service scanning
A port scan finds which services are listening; fingerprinting names their versions. Both are reconnaissance against authorized targets only — and every probe also leaves a line in the defender's log, which is what makes scanning a two-sided story.
On an authorized lab host you point a scanner at a single IP and, eight seconds later, you have a map you didn’t have before: port 22 is open and answers as OpenSSH 7.4, port 5432 is open and speaks the PostgreSQL protocol, port 6379 has Redis listening with no auth banner at all. Nobody handed you that inventory — you inferred all of it from how the host replied to a few thousand carefully shaped packets. That eight-second map is reconnaissance, and the same eight seconds wrote roughly three thousand connection attempts into a log on the other side. Scanning is never a one-way read; every question you ask the network, the network can hear you ask.
By the end of this lesson you’ll know how a port scan actually works at the TCP level, how service fingerprinting turns an open port into a named version, and exactly what a defender sees while you do it — all against systems you’re authorized to test.
Authorization first — scanning is reconnaissance, not a free read
Before any of the mechanism below, the rule that opens this whole track still holds: a port scan is only legitimate against a system you own or are explicitly authorized to test. In MITRE ATT&CK terms, scanning is Active Scanning under the Reconnaissance tactic (TA0043) — and “active” is the operative word. Unlike reading a target’s public DNS records, a scan sends packets to the target and waits for replies, which means it touches infrastructure you don’t control and leaves evidence on it. Pointing a scanner at a host you have no authorization for is unauthorized access in nearly every jurisdiction, regardless of how gentle the scan is. Everything here is framed for a CTF box, a deliberately vulnerable VM you deployed, or an in-scope engagement — never a real target “just to see what’s open.”
We learn it defensively: if you understand exactly what a scan reveals and exactly what it leaves behind, you can both reduce what your own systems expose and recognize the scan in your logs when someone runs it against you.
How a port scan works: the TCP handshake, weaponized as a question
A port scan is just the TCP three-way handshake used as an interrogation. To open a normal connection a client sends SYN, the server replies SYN-ACK if the port is listening, and the client finishes with ACK. A scanner exploits the middle of that exchange: it sends a SYN, and the reply alone tells it the port’s state, before any connection is fully established.
SYN-ACKback → the port is open: something is listening.RST(reset) back → the port is closed: the host is reachable but nothing is listening there.- Nothing back (after retries/timeout) → filtered: a firewall is silently dropping the probe, so you can’t tell open from closed.
The classic default, a SYN scan (often called a “half-open” or “stealth” scan), sends the SYN, reads the reply, and then sends RST to tear down the half-built connection before the handshake completes. Historically this mattered because a connection that never fully opened often wasn’t written to the application’s own logs. The contrast is a connect scan, which completes the full three-way handshake using the OS’s normal connection call — more reliable and the fallback when the scanner lacks the privilege to craft raw packets, but louder, because a completed connection is exactly what application logging records.
From open port to named service: fingerprinting and banners
An open port number is a weak guess. Port 22 usually means SSH and 5432 usually means PostgreSQL — but services run on non-standard ports all the time, and “open” alone tells you nothing about which software or which version is listening. Turning a port number into a confident identification is service fingerprinting, and it has two flavors.
The cheap one is banner grabbing: many services announce themselves the moment you connect. Open a raw TCP connection to an SSH port and it greets you with a version string like SSH-2.0-OpenSSH_7.4; an SMTP server answers with a 220 line naming the mail software; a misconfigured web server returns a Server: header. You read the banner the service volunteers and you know what it is — no cleverness required, because the service told you.
The deeper one is active fingerprinting (nmap’s -sV service/version detection). When a banner is missing, lies, or is ambiguous, the scanner sends a sequence of crafted probes and matches the pattern of responses — timing, exact byte sequences, error behavior, protocol quirks — against a database of known service signatures. It can also infer the OS from subtle differences in how the TCP/IP stack itself responds (TTL values, window sizes, option ordering), because no two stacks behave identically. This is more accurate than trusting a banner, but it is also far louder: instead of one connection it fires dozens of unusual probes per port, which is exactly the anomalous traffic an IDS is tuned to notice.
| Technique | What the scanner does | What it learns | Noise / detectability |
|---|---|---|---|
SYN scan (-sS) | SYN, read reply, RST before handshake completes | Open / closed / filtered per port | Quieter at app layer; visible to firewall/IDS |
Connect scan (-sT) | Completes the full 3-way handshake via the OS | Same states, no raw-packet privilege needed | Loudest: each connection hits app logs |
| Banner grab | Connects, reads the greeting the service sends | Service name + version, if volunteered | Low: looks like one ordinary connection |
Version detection (-sV) | Sends crafted probes, matches response signatures | Confident version even when banner lies/missing | High: many unusual probes per port |
▸Why this works
Why does scan speed trade directly against stealth and accuracy? A scanner has a few knobs — how many ports per second, how many parallel probes, how long to wait for a reply. Crank them up and a full scan finishes in seconds, but the burst of identical packets in a tight window is the single easiest thing for a defender to alert on (e.g. “30 SYNs to 30 ports from one source in one second”). Slow it down — randomize the order, space probes across minutes — and you blend into normal traffic, but the scan now takes hours and a flaky network drops replies, costing you accuracy. There is no setting that is fast, accurate, and invisible; you are always spending one to buy another. The defender’s whole job is to make the fast-and-accurate corner trip an alarm.
What the defender sees — and why scanning is two-sided
This is the half newcomers forget: while you are reading the target, the target is reading you. Every probe is a packet with your source IP, and the defensive stack is built to notice exactly the patterns a scan creates.
- The firewall / connection logs record the flood: many SYNs to many ports from one source in a short window. A run of
RST-terminated half-open connections (the SYN-scan signature) or a fan-out across the whole port range is a textbook scanning fingerprint. - An IDS/IPS (Snort, Suricata, or a cloud equivalent) has rules tuned for precisely this — a threshold of distinct ports touched per source per interval — and can alert, rate-limit, or block the source IP outright.
- Version detection is even more visible: the unusual, non-protocol-conforming probes that
-sVand OS detection send don’t look like any normal client, so they light up signature-based detection far more than a plain port sweep.
The defensive lessons fall straight out of this. Reduce what’s exposed in the first place (close ports, bind services to internal interfaces, put a firewall in front so most probes get filtered, not answered). Strip or genericize banners so a successful connection volunteers nothing — though remember that’s only hiding the label, not the service, so it slows an attacker rather than stopping them. And monitor for the scan signature so you detect reconnaissance early, because in the kill chain recon is the first stage, and catching an attacker there is the cheapest place to break the chain.
On an authorized engagement you need to identify the exact software versions on a host, but the client's SOC is actively monitoring and you want to delay detection as long as possible. Which approach best fits the goal?
In a SYN scan, what does the host's reply tell the scanner, and why was the SYN scan historically called 'half-open'?
Why is nmap's active version detection (`-sV`) far more visible to an IDS than a plain banner grab?
Order the steps a scanner takes to go from an IP address to a named, versioned service:
- 1 Send a SYN probe to a target port
- 2 Read the reply to classify the port (open / closed / filtered)
- 3 On an open port, read any banner the service volunteers
- 4 If the banner is missing or ambiguous, send crafted version-detection probes
- 5 Match the response pattern against a signature database to name the version
- 01Explain how a SYN port scan works at the TCP level, what the three port states mean, and why a SYN scan is quieter than a connect scan.
- 02Compare banner grabbing and active version detection, and describe what a defender sees during scanning and how they reduce exposure.
A port scan is the TCP three-way handshake turned into an interrogation: the scanner sends a SYN and the reply alone classifies the port — SYN-ACK is open, RST is closed, and silence is filtered (a firewall dropping the probe). The default SYN (‘half-open’) scan resets the connection before the handshake completes, historically quieter at the application layer than a connect scan, which completes the full handshake and lands in app logs. Finding an open port is only half the job; turning it into a named, versioned service is fingerprinting — cheaply through banner grabbing, where the service volunteers its identity, and more accurately but far more loudly through active version detection, which fires crafted probes and matches response signatures (and can infer the OS from stack quirks). The unifying senior idea is that scanning is two-sided: every probe carries your source IP into the defender’s firewall logs and IDS, where threshold rules on distinct ports per source are tuned to fire on exactly this pattern — and version detection’s abnormal probes are louder still. So the trade-off is permanent — fast and accurate is loud, quiet is slow or partial — and the defensive moves fall straight out of it: shrink the attack surface so probes are filtered, strip banners so a connection reveals nothing, and alert on the scan signature to break the kill chain at its first, cheapest stage. And always, only against systems you’re authorized to test.
Practice
Start at the top. Tasks go easiest → hardest: recall a fact, apply it to a case, then a senior-level stretch. Open one, attempt it, then reveal.
Something unclear?
Ask a question about this lesson. Questions are anonymous and go straight to the author to make the lesson better.
Apply this
Put this lesson to work on a real build.