Nmap Port Scanning: CTF Basics part 2

Depov

Moderator
Staff member
MODERATOR
ULTIMATE
SUPREME
PREMIUM
MEMBER
Joined
Feb 18, 2025
Messages
790
Reaction score
1,510
Nmap version detection and banner grabbing

Knowing that port 80 is open isn't enough. You need to determine what's running on it: Apache, Nginx, Python SimpleHTTPServer, or a custom application. The service version determines whether there's a public exploit for it.

Flag -sVEnables version detection. Nmap sends a set of probes to an open port and analyzes the responses, matching them against a signature database. nmap-service-probes. As a result, instead of the line 80/tcp open http you see 80/tcp open http Apache httpd 2.4.51— and this is already a specific request in searchsploit.

Banner grabbing is a simpler approach, but sometimes more informative. Many daemons immediately return a version string upon connection. Connecting to port 22 via nc -nv <ip> 22can show SSH-2.0-OpenSSH_8.9p1 Ubuntu-3ubuntu0.1It's just one line, but it tells you: SSH protocol, specific OpenSSH version 8.9p1, Ubuntu distribution. Sometimes, a banner via netcat shows more detail than nmap's automatic detection—especially for non-standard or custom services.

Combination -sV -sCor aggressive mode -A(includes -sV, -sC, -Oand traceroute) extracts maximum information in a single pass. It's safe on a local CTF network. On remote targets or during a penetration test -Amay be excessively noisy - each NSE script from the default set generates additional traffic.

A critical point for CTF: once you receive a version of the service, immediately look for public exploits. searchsploit <service_name> <version>Or searching exploit-db.com is the standard next step. Without a specific version, you're just stumbling around in the dark.
Nmap NSE scripts for service enumeration

The Nmap Scripting Engine (NSE) transforms nmap from a port scanner into a full-fledged enumeration tool. Scripts are written in Lua and are located in /usr/share/nmap/scripts/and cover dozens of tasks—from determining supported HTTP methods to checking for specific vulnerabilities.

Flag -sCRuns a set of scripts from the "default" category—considered safe and non-invasive. These include checks for: which HTTP methods are allowed, whether anonymous FTP access is available, which SMB shares are accessible without authentication, and the HTTP title of the web page.

The category is useful for CTF vuln: flag --script=vulnChecks ports for known vulnerabilities. A crude but quick way to get a hint. On Easy boxes, this script sometimes immediately reveals the attack vector.

Here are a few scripts that will come in handy for CTF tasks:

http-enum— iterates through popular directories and files on a web server (similar to dirb/gobuster, but built-in)
smb-enum-shares— shows available SMB shares on Windows/Samba
ftp-anon— checks whether anonymous FTP login is allowed
ssh-auth-methods— shows available SSH authentication methods

Running a specific script: --script=http-enum. Launch category: --script=vuln. Launching multiple: --script="http-enum,ftp-anon"Categories intrusive And exploitThey launch active checks that may impact the service's operation—this isn't usually a problem during CTFs, but during a real-world pentest, approval is required.

A detail that many people miss: by default, NSE scripts use User-Agent Mozilla/5.0 (compatible; Nmap Scripting Engine; https://nmap.org/book/nse.html)It doesn't interfere with CTFs, but in a real pentest, such a User-Agent would be instantly detected. Edit: --script-args http.useragent="Custom Agent".
Preparing for a CTF Task: Workflow from Scan to Exploit

All of the above fits into a specific workflow. A two-pass approach saves time and prevents non-standard ports from being missed.

Step 1: Quick scan of all ports. Default nmap without -p-Checks only the top 1000 ports. The CTF task can be built around a service on port 31337 or 49152. The first pass is all 65535 ports, without version detection, as fast as possible:

# Шаг 1: быстрый скан всех портов
sudo nmap -p- --open -sS --min-rate 5000 -Pn -n <target_ip>
# Шаг 2: целевой скан найденных портов с энумерацией
sudo nmap -sV -sC -p 22,80,8080 <target_ip>

First Pass Flags: -p-(all ports), --open(show only open ones), -sS(SYN scanning), --min-rate 5000(minimum 5000 packets/s is more than enough for a CTF network), -Pn(don't waste time on ping, consider the host active), -n(Don't resolve DNS, saves time). On a CTF network, a full scan with these parameters takes 10-30 seconds.

Step 2: Targeted scan with enumeration. Once you have a list of open ports, run a targeted scan with -sV -sCNmap will only spend time on actual open ports, but will show the versions and results of the underlying NSE scripts.

Step 3: Analyze the results. For each service, three questions: what version and whether there are public CVEs (check via searchsploit); are there default credentials (admin:admin, anonymous FTP); what did the NSE scripts show (directories, shares, authentication methods).

Step 4: Deep Enumeration. Web server - start directory enumeration ( gobuster, feroxbuster). SMB — list users and shares. SSH — check for weak passwords if the version doesn't block brute-force.

Step 5: Exploitation. Based on the information gathered, choose an attack vector. On Easy boxes, a single outdated version of the service or default credentials is usually sufficient. This is the transition point from reconnaissance to initial access—the Exploit Public-Facing Application (T1190, Initial Access tactic) according to MITRE ATT&CK.

Scan only hosts for which you have explicit permission. On CTF platforms (HTB, THM), permission is granted when connecting to a VPN. Scanning arbitrary IP addresses on the internet is a violation that is recorded (under the "Port Scan" category on AbuseIPDB) and may result in legal consequences.
Nmap Port Scanning Errors for Beginners

While solving CTF tasks and helping other participants, I've compiled a list of pitfalls that almost everyone falls into:

Scan only the default 1000 ports. Command nmap <ip> without -p-checks only the top 1000 from the file nmap-servicesA CTF task can be running on port 65000, and you won't detect it. Rule: the first scan is always with -p-.

Skipping UDP. TCP scanning has become a reflex, and UDP is forgotten. If TCP ports don't provide an obvious vector, run sudo nmap -sU --top-ports 50 <ip>Not all 65535—at least the top 50 UDP ports, so you don't have to wait hours. Hidden SNMP or TFTP on a CTF box is not uncommon.

Scan without -Pn. Many CTF machines do not respond to ICMP ping. Without -Pnnmap will assume the host is dead and skip it entirely. On CTF platforms, add -Pn by default.

Aggressive timing via VPN. -T5 or --min-rate 10000On an unstable VPN connection, this results in dropped packets and missed ports. Nmap didn't receive a response, marking the port as filtered, but the service is actually working. On a stable local network -T4 or --min-rate 5000They work great, remotely - -T4without min-rate it's safer.

Ignoring the full output. nmap -sCSometimes it shows directly in the text: "Anonymous FTP login allowed," "http-title: Apache Tomcat Manager"—while a beginner only looks at the PORT/STATE column. Read the entire output, line by line. NSE output often contains direct clues.

Running without sudo. Without root, nmap uses Connect scan instead of SYN—it's slower, more noticeable, and less informative. On CTF machines, it's always sudo nmap.
Error Consequence Solution
Only 1000 ports Service was denied on a non-standard port. -p-on the first pass
Forgot UDP DNS/SNMP/TFTP not found -sU --top-ports 50second pass
No -Pn The host is marked as dead -Pnon CTF platforms always
-T5 via VPN Lost packets, false positives -T4 or --min-rate 5000
They don't read the NSE output Obvious information was omitted Read the entire output
No sudo Connect scan instead of SYN sudo nmap by default

Nmap port scanning isn't rocket science. It's a disciplined process: scan everything, read everything, find inconsistencies and vulnerable versions. The vast majority of Easy-level CTF challenges are solved within the "find version, find exploit, run" framework.

I've noticed a pattern: newbies spend hours memorizing flag cheat sheets - -sS, -sV, -sC, -A— but they can't explain what each does at the packet level. They've memorized the command, but they haven't learned what a SYN packet is or why the absence of an ACK makes the connection "half-open." The result is predictable: scanning remains a black box, and every non-standard output leaves one baffled.

One hour is enough to change this. Launch Wireshark, do sudo nmap -sSOn the local host, examine SYN → SYN/ACK → RST with your own eyes. After this, each nmap flag becomes a logical consequence: -sSbreaks the handshake after SYN/ACK, -sUIt works without a handshake; FIN-scan sends a packet with an unexpected flag and observes the reaction. It's not magic—it's protocol manipulation.
 
Top Bottom