Nmap for Beginners: From Scan to CTF Flag part 1

Depov

Moderator
Staff member
MODERATOR
ULTIMATE
SUPREME
PREMIUM
MEMBER
Joined
Feb 18, 2025
Messages
790
Reaction score
1,484
Why does a CTF player need Nmap port scanning?

Each CTF box is a machine with a set of services. One or more of them contain a vulnerability that leads to a flag. The goal is to find these services, determine their versions, and figure out where to dig. This is exactly what Nmap—Network Mapper, a free network reconnaissance tool that has become a standard—does. For more details, see our article on creating CTF tasks.

In MITRE ATT&CK terminology, the actions when running Nmap fall into several techniques:

Active Scanning (T1595, Reconnaissance) - sending packets to identify live hosts and open ports
Network Service Discovery (T1046, Discovery) - identifying services on open ports
System Information Discovery (T1082, Discovery) - determining the OS and target configuration through response analysis

This classification isn't just an academic formality. It helps us understand that scanning is a structured process of information gathering, not just a matter of "running a command and waiting." In CTF, the quality of your reconnaissance determines whether you'll find an attack vector in five minutes or spend an hour searching.

Nmap addresses three CTF challenges:

Detecting open ports - what "doors" into the system even exist
Identifying Services and Versions—What's Behind Each Door: Apache 2.4.41, OpenSSH 7.6, vsftpd 3.0.3
Primary vulnerability reconnaissance – NSE scripts allow you to immediately check for common vulnerabilities without switching to other tools.

Nmap's documentation clearly states that the tool uses raw IP packets to determine accessible hosts, their services and versions, operating systems, and active filtering rules. In practice, a single command provides a complete picture of the attack surface.
First Run: How to Use Nmap

Nmap comes pre-installed on Kali Linux. For other distributions, it's a single command: sudo apt install nmap for Debian/Ubuntu, sudo dnf install nmapfor Fedora, brew install nmapfor macOS. Check installation: nmap --versionOn Windows, there is an installer from nmap.org.

A point that is often missed at the start: most scan types require root privileges because Nmap works with raw sockets. Without sudothe tool silently switches to TCP Connect scan ( -sT) instead of a quick SYN scan ( -sS). The result is similar, but slower and noisier. The habit of launching sudo nmap- basic hygiene.
What happens during a basic Nmap scan?

The first command for any CTF box: sudo nmap -sV -sC <target-ip>. Flag -sVincludes definition of service versions, -sCRuns a set of default NSE scripts. Together, they provide maximum information with minimal effort.

Typical output on a CTF machine:

PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 8.2p1 Ubuntu 4ubuntu0.5
80/tcp open http Apache httpd 2.4.41 ((Ubuntu))
|_http-title: Welcome to My App
|_http-server-header: Apache/2.4.41 (Ubuntu)
3306/tcp filtered mysql
8080/tcp closed http-proxy

Let's analyze each line from a CTF player's perspective. OpenSSH 8.2p1 is a specific version; you can check for known CVEs. Apache 2.4.41 on Ubuntu is a web server, a prime candidate for browser-based investigation and directory enumeration. MySQL filtered means the database is hidden behind a firewall. If you find an SSRF or RCE vulnerability on the web server, try accessing it from within. 8080 closed is currently empty, but remember: sometimes services are brought back up after certain actions on the box.

No flags -sV -sCYou'd see only port numbers and states—no versions, no HTTP headers, no configuration information. The difference between "port 80 is open" and "Apache 2.4.41 on Ubuntu with the Welcome to My App header" is the difference between a guess and a specific vector.
Nmap Port Scan Types for CTF

Nmap supports over a dozen scanning techniques. The documentation on Port Scanning Techniques lists SYN, Connect, UDP, FIN, Xmas, NULL, ACK, Idle, and others. In CTF, three are actually needed: SYN, TCP Connect, and UDP. The others are for firewall bypass in production penetration tests; in a lab setting, they are redundant.
SYN scan is the main network reconnaissance tool

SYN scan ( -sS) is the default method when running as root. This is the fastest and most common type of Nmap port scan. The mechanics are simple: Nmap sends a packet with the SYN flag, analyzes the response, and doesn't complete the TCP handshake.

Three possible answers: - A SYN-ACK arrived—the port is open. Nmap immediately sends a RST, terminating the connection. - RST received - port closed, service not listening - No response after several attempts or ICMP unreachable (type 3, codes 1, 2, 3, 9, 10, 13) - the port is filtered by a firewall

Why is a SYN scan called "half-open"? A TCP connection is never fully established. If you simultaneously monitor the traffic in Wireshark, you'll see the sequence SYN → SYN-ACK → RST instead of the normal three-way handshake of SYN → SYN-ACK → ACK. This is useful for CTF forensic tasks, where you need to determine the type of activity from a traffic dump.

Launch: sudo nmap -sS -p- -T4 <target>. Flag -p-critically important - without it, Nmap will only check the 1000 most popular ports from the database nmap-servicesIn CTF, key services are deliberately placed on non-standard ports (8443, 9090, 50000), and without a full scan, you simply won't see them.
TCP Connect - When There's No Root

TCP Connect-scan ( -sT) performs a full three-way handshake via a system call connect()Root access isn't required, but it's slower and noisier—every connection is logged on the target's end. In CTF, its only use is when scanning through a proxy (proxychains) or from an unprivileged user after gaining initial access to the machine. Command: nmap -sT <target> - without sudo.
UDP Scan - Hidden Services

UDP scanning ( -sU) is what distinguishes a competent enumeration from a superficial one. DNS (53), SNMP (161), TFTP (69), NTP (123) – these services operate over UDP and regularly become attack vectors. If SNMP is open with a community string public, one team snmpwalkmay provide credentials or information about processes.

Problem: UDP scanning is slow. The protocol doesn't acknowledge packet receipt. Open and filtered ports rarely respond, and many systems limit the ICMP response rate for closed ports. A full UDP scan can take hours. The solution is to limit the scope: sudo nmap -sU --top-ports 20 -T4 <target>Checks the 20 most popular UDP ports. This typically takes a few minutes, although the time depends on the ICMP rate limiting on the target—from 1 to 10+ minutes.

Combination of TCP and UDP in one command ( sudo nmap -sS -sU --top-ports 200 <target>) allows Nmap to run both scans in parallel, but the UDP portion will still remain a bottleneck due to rate-limiting ICMP responses.
How to Read Nmap Output: Scan Examples with Parsing
Six port states in the context of CTF

Nmap distinguishes between six port states, and understanding the difference between them is a skill that makes Nmap port scanning a pointless exercise.
State What does it mean CTF Action
open The service is accepting connections. The main goal is to determine the version and look for vulnerabilities.
closed The port is available, but the service is not listening. Remember, services sometimes appear after actions on the box
filtered The firewall is blocking packets Try to reach from the inside after receiving the shell
unfiltered The port is available, but open/closed cannot be determined. ACK scan result - switch to SYN for clarification
open|filtered No response - unclear whether open or filtered Typical for UDP. Use -sVfor clarification
closed|filtered Unable to determine state A rare condition in an idle scan

For CTF the key pattern is: filteredOn the database port (3306, 5432, 1433) is a hint that after receiving an RCE or SSRF on the web server, you should try connecting to the database via localhost. According to the OWASP classification, SSRF (A10:2021) is one of the top ten most critical web application vulnerabilities, and it is often used in CTFs to access filtered internal services.
The full scouting command and what each flag means

Universal command for CTF: sudo nmap -A -T4 -p- <target> -oA scan. Flag -A- an aggressive regime that unites -sV(versions), -sC(default scripts), -O(OS definition) and traceroute. -T4 — a timing pattern that speeds up the scan on a good channel. -p-- all ports. -oA scan— saving results in all formats: .nmap (text), .xml (for parsing) and .gnmap (grepable).

The Grepable format is indispensable for quick searching: grep "80/open" scan.gnmapwill show all hosts with open HTTP. The habit of adding -oAEach scan saves time when you need to recall a specific version of the service after an hour of work.
 
Top Bottom