Network Protocols for Penetration Testing: Where Scanning Fits in the Attack Chain
Before delving into nmap flags, why bother with port scanning? In a real attack or CTF, port scanning is the first active step after obtaining the target's IP address. According to the MITRE ATT&CK classification, this technique is Active Scanning (T1595, Reconnaissance Tactic) : the attacker sends packets to target hosts to gather information about available services. Next comes Network Service Discovery (T1046, Discovery Tactic), which identifies specific applications and their versions on open ports.
For CTF, the chain is linear: obtain the box's IP → scan ports → identify services and versions → find a vulnerable version in exploit-db or searchsploit → exploit → claim the flag. If you only scan the default 1,000 ports out of 65,535 in the first step, you risk missing a non-standard service that the entire task relies on.
The motivation of a real attacker is similar. A web server with an outdated version, SSH with default passwords, a forgotten FTP with anonymous access—all are detected during the scanning stage. According to the OWASP Top 10 (A05:2021 — Security Misconfiguration) , misconfigured and unprotected services are among the most common risks for web applications. Nmap is the tool that begins both a penetration test and preparation for a CTF task.
A port is a numeric identifier from 0 to 65535 through which the OS routes network traffic to a specific application. Ports 0–1023 are well-known (SSH — 22, HTTP — 80, HTTPS — 443, DNS — 53), 1024–49151 are registered (MySQL — 3306, PostgreSQL — 5432), and 49152–65535 are dynamic. On CTF boxes, services are often deliberately assigned to non-standard ports—so a full scan of all 65535 ports is critical.
TCP Handshake and UDP: What Happens During an Nmap Port Scan
To read nmap output meaningfully, you need to understand two transport layer protocols—TCP and UDP. The entire logic of nmap port scanning is built on the specifics of their operation.
How TCP Handshakes Work
TCP is a connection-oriented protocol. Before data is transmitted, the client and server perform a three-way handshake:
The client sends a packet with the SYN flag — “I want to establish a connection”
The server responds with a SYN/ACK packet - "accepted, I confirm"
The client sends an ACK — "I confirm, let's go."
SYN scanning is built on this mechanics ( -sS): nmap sends a SYN and receives a SYN/ACK (the port is open, the service is listening) or an RST (the port is closed, no one is listening). A third ACK packet is not sent, and the connection is not completed—hence why this scan is called "half-open." Most services don't log incomplete connections, making a SYN scan less noticeable than a full connection.
Run Wireshark in parallel with sudo nmap -sS— and you'll see a clear picture: SYN → SYN/ACK → RST for an open port, SYN → RST for a closed one. One look at the packets, and the scanning logic becomes clear. I recommend trying it at least once: after that, every nmap flag will cease to be magical.
UDP: a protocol without a handshake
UDP works fundamentally differently—it's connectionless. There's no handshake, no delivery confirmation. The client simply sends a packet to the port. If the port is closed, the host returns an ICMP "port unreachable" message (type 3, code 3). If the port is open, there's usually silence. This makes nmap's UDP scan slower and more difficult to interpret: a lack of response could indicate either an open port or firewall filtering.
UDP services are not uncommon. DNS (port 53), SNMP (161/162), and DHCP (67/68) operate over UDP. As the nmap documentation notes, "ignoring UDP is a mistake because attackers do not ignore the protocol entirely." On CTF boxes, a non-standard UDP service can be key to the entire task. Furthermore, many hosts limit the rate of ICMP "port unreachable" responses (Linux and Solaris are particularly strict here), which makes UDP scanning even slower.
Analysis of open ports: open, closed, filtered states
Nmap classifies each scanned port by state. For CTF practice, it's important to understand each of these states not formally, but in terms of "what to do next."
Open — a service is running on the port and has accepted the connection (responded with a SYN/ACK during a TCP scan or sent a UDP response). Every open port is a potential entry point. The next step is to determine which service and version is running there.
Closed — the host is reachable but isn't listening on the port (responded with RST). This isn't particularly interesting for an attack, but it confirms two things: the machine is alive and the firewall isn't blocking your traffic. If nmap shows hundreds of closed ports, the host is responding normally; there's no filtering.
Filtered — nmap received no response at all (or received an ICMP unreachable error type 3, codes 1, 2, 9, 10, or 13). There's a firewall or packet filter between you and the port that's dropping packets. Filtered is less common in CTF contexts, as organizers typically don't make accessing services difficult. This is a common occurrence in a real pentest.
Nmap also uses combined states for ambiguous cases:
State What does it mean? When does it arise?
open The service is listening and responded to the request. SYN/ACK for TCP, UDP response
closed The host is alive, but the port is not listening. RST for TCP, ICMP port unreachable for UDP
filtered The firewall is dropping packets. No response or ICMP unreachable error
open|filtered Open or filtered - unclear Typical for UDP: no response
closed|filtered Closed or filtered Idle scanning (rare)
unfiltered Port available, status unknown ACK scanning
A rule of thumb for CTFs: open ports are your service enumeration checklist. Everything else is context for understanding the network configuration.
How to Use Nmap: Port Scan Types for the Job
Nmap supports over a dozen scan types, but three are sufficient for CTF and the first steps in pentesting. Each has its own strengths and limitations.
TCP SYN scan (-sS)
The default type when running nmap as root is ( sudo). It sends a SYN packet, receives a SYN/ACK (the port is open) or RST (closed), but doesn't complete the handshake—it sends an RST instead of an ACK. According to nmap documentation, a SYN scan can check thousands of ports per second on a fast connection without restrictive firewalls. It works with any TCP stack and clearly distinguishes between open, closed, and filtered states.
When to use: Almost always. It's the primary tool for CTF tasks and penetration testing.
Prerequisites: root/sudo is required because it handles raw packets via libpcap. Without privileges, nmap will automatically switch to Connect scan. Sudo is available on most CTF platforms.
TCP Connect and UDP scanning
Connect scan (-sT) — used when root access is not available. Instead of raw packets, nmap calls the system scan. connect()— establishes a full TCP connection. Slower, more noticeable (services log connections via syslog), and less flexible. Rarely used in CTFs—sudo is usually present.
UDP scan (-sU) — sends UDP packets to target ports. For popular ports (53, 161), nmap sends a protocol-specific payload to increase the likelihood of a response. For other ports, it sends an empty UDP header. Can be combined with TCP scanning: sudo nmap -sS -sUwill check both protocols in one pass.
Scan type Flag Speed Requires root Noisiness When to use in CTF
SYN scan -sS High Yes Low Default, primary choice
Connect scan -sT Average No High When there is no root access
UDP scan -sU Low Yes Average Search for DNS, SNMP, DHCP
FIN/NULL/Xmas -sF/-sN/-sX Average Yes Low Bypassing simple stateless firewalls
FIN, NULL, and Xmas scans are a separate topic. They exploit a feature of RFC 793 : packets without SYN, RST, or ACK to a closed port trigger an RST response, while those to an open port trigger silence. This allows you to bypass simple stateless firewalls that only block packets with the SYN flag set. However, it doesn't work everywhere: Windows and many Cisco devices send RSTs regardless of the port state. It's rarely used for CTF tasks, but it's useful to know about it—sometimes a box has just such a filter.
Before delving into nmap flags, why bother with port scanning? In a real attack or CTF, port scanning is the first active step after obtaining the target's IP address. According to the MITRE ATT&CK classification, this technique is Active Scanning (T1595, Reconnaissance Tactic) : the attacker sends packets to target hosts to gather information about available services. Next comes Network Service Discovery (T1046, Discovery Tactic), which identifies specific applications and their versions on open ports.
For CTF, the chain is linear: obtain the box's IP → scan ports → identify services and versions → find a vulnerable version in exploit-db or searchsploit → exploit → claim the flag. If you only scan the default 1,000 ports out of 65,535 in the first step, you risk missing a non-standard service that the entire task relies on.
The motivation of a real attacker is similar. A web server with an outdated version, SSH with default passwords, a forgotten FTP with anonymous access—all are detected during the scanning stage. According to the OWASP Top 10 (A05:2021 — Security Misconfiguration) , misconfigured and unprotected services are among the most common risks for web applications. Nmap is the tool that begins both a penetration test and preparation for a CTF task.
A port is a numeric identifier from 0 to 65535 through which the OS routes network traffic to a specific application. Ports 0–1023 are well-known (SSH — 22, HTTP — 80, HTTPS — 443, DNS — 53), 1024–49151 are registered (MySQL — 3306, PostgreSQL — 5432), and 49152–65535 are dynamic. On CTF boxes, services are often deliberately assigned to non-standard ports—so a full scan of all 65535 ports is critical.
TCP Handshake and UDP: What Happens During an Nmap Port Scan
To read nmap output meaningfully, you need to understand two transport layer protocols—TCP and UDP. The entire logic of nmap port scanning is built on the specifics of their operation.
How TCP Handshakes Work
TCP is a connection-oriented protocol. Before data is transmitted, the client and server perform a three-way handshake:
The client sends a packet with the SYN flag — “I want to establish a connection”
The server responds with a SYN/ACK packet - "accepted, I confirm"
The client sends an ACK — "I confirm, let's go."
SYN scanning is built on this mechanics ( -sS): nmap sends a SYN and receives a SYN/ACK (the port is open, the service is listening) or an RST (the port is closed, no one is listening). A third ACK packet is not sent, and the connection is not completed—hence why this scan is called "half-open." Most services don't log incomplete connections, making a SYN scan less noticeable than a full connection.
Run Wireshark in parallel with sudo nmap -sS— and you'll see a clear picture: SYN → SYN/ACK → RST for an open port, SYN → RST for a closed one. One look at the packets, and the scanning logic becomes clear. I recommend trying it at least once: after that, every nmap flag will cease to be magical.
UDP: a protocol without a handshake
UDP works fundamentally differently—it's connectionless. There's no handshake, no delivery confirmation. The client simply sends a packet to the port. If the port is closed, the host returns an ICMP "port unreachable" message (type 3, code 3). If the port is open, there's usually silence. This makes nmap's UDP scan slower and more difficult to interpret: a lack of response could indicate either an open port or firewall filtering.
UDP services are not uncommon. DNS (port 53), SNMP (161/162), and DHCP (67/68) operate over UDP. As the nmap documentation notes, "ignoring UDP is a mistake because attackers do not ignore the protocol entirely." On CTF boxes, a non-standard UDP service can be key to the entire task. Furthermore, many hosts limit the rate of ICMP "port unreachable" responses (Linux and Solaris are particularly strict here), which makes UDP scanning even slower.
Analysis of open ports: open, closed, filtered states
Nmap classifies each scanned port by state. For CTF practice, it's important to understand each of these states not formally, but in terms of "what to do next."
Open — a service is running on the port and has accepted the connection (responded with a SYN/ACK during a TCP scan or sent a UDP response). Every open port is a potential entry point. The next step is to determine which service and version is running there.
Closed — the host is reachable but isn't listening on the port (responded with RST). This isn't particularly interesting for an attack, but it confirms two things: the machine is alive and the firewall isn't blocking your traffic. If nmap shows hundreds of closed ports, the host is responding normally; there's no filtering.
Filtered — nmap received no response at all (or received an ICMP unreachable error type 3, codes 1, 2, 9, 10, or 13). There's a firewall or packet filter between you and the port that's dropping packets. Filtered is less common in CTF contexts, as organizers typically don't make accessing services difficult. This is a common occurrence in a real pentest.
Nmap also uses combined states for ambiguous cases:
State What does it mean? When does it arise?
open The service is listening and responded to the request. SYN/ACK for TCP, UDP response
closed The host is alive, but the port is not listening. RST for TCP, ICMP port unreachable for UDP
filtered The firewall is dropping packets. No response or ICMP unreachable error
open|filtered Open or filtered - unclear Typical for UDP: no response
closed|filtered Closed or filtered Idle scanning (rare)
unfiltered Port available, status unknown ACK scanning
A rule of thumb for CTFs: open ports are your service enumeration checklist. Everything else is context for understanding the network configuration.
How to Use Nmap: Port Scan Types for the Job
Nmap supports over a dozen scan types, but three are sufficient for CTF and the first steps in pentesting. Each has its own strengths and limitations.
TCP SYN scan (-sS)
The default type when running nmap as root is ( sudo). It sends a SYN packet, receives a SYN/ACK (the port is open) or RST (closed), but doesn't complete the handshake—it sends an RST instead of an ACK. According to nmap documentation, a SYN scan can check thousands of ports per second on a fast connection without restrictive firewalls. It works with any TCP stack and clearly distinguishes between open, closed, and filtered states.
When to use: Almost always. It's the primary tool for CTF tasks and penetration testing.
Prerequisites: root/sudo is required because it handles raw packets via libpcap. Without privileges, nmap will automatically switch to Connect scan. Sudo is available on most CTF platforms.
TCP Connect and UDP scanning
Connect scan (-sT) — used when root access is not available. Instead of raw packets, nmap calls the system scan. connect()— establishes a full TCP connection. Slower, more noticeable (services log connections via syslog), and less flexible. Rarely used in CTFs—sudo is usually present.
UDP scan (-sU) — sends UDP packets to target ports. For popular ports (53, 161), nmap sends a protocol-specific payload to increase the likelihood of a response. For other ports, it sends an empty UDP header. Can be combined with TCP scanning: sudo nmap -sS -sUwill check both protocols in one pass.
Scan type Flag Speed Requires root Noisiness When to use in CTF
SYN scan -sS High Yes Low Default, primary choice
Connect scan -sT Average No High When there is no root access
UDP scan -sU Low Yes Average Search for DNS, SNMP, DHCP
FIN/NULL/Xmas -sF/-sN/-sX Average Yes Low Bypassing simple stateless firewalls
FIN, NULL, and Xmas scans are a separate topic. They exploit a feature of RFC 793 : packets without SYN, RST, or ACK to a closed port trigger an RST response, while those to an open port trigger silence. This allows you to bypass simple stateless firewalls that only block packets with the SYN flag set. However, it doesn't work everywhere: Windows and many Cisco devices send RSTs regardless of the port state. It's rarely used for CTF tasks, but it's useful to know about it—sometimes a box has just such a filter.