Lesson 2 of 7 · 18 min · Intermediate

Nmap mastery

Host discovery, port scanning, service detection and NSE scripts.

In this lesson you will

  • Choose the right host discovery method for the network you're on
  • Run SYN, connect and UDP scans and interpret the results
  • Use service and version detection and safe NSE scripts
  • Tune timing so scans are accurate without harming fragile systems
  • Save and compare results using a repeatable workflow

Nmap is the most important tool in network testing. Almost everyone can run nmap target; professionals know exactly what each option sends on the wire, how long it'll take, and what could go wrong. That knowledge is what lets you scan a hospital network without taking anything down.

Run every command in this lesson against your own lab, or against systems you have written permission to test.

Before the first packet

  • Confirm scope. Put the in-scope ranges in a file and exclude anything out of scope:
bash
nmap -iL scope.txt --excludefile exclusions.txt ...
  • Know where you're scanning from. Note your source IP in your log. If the client's SOC sees your traffic, they'll ask.
  • Root or not? SYN scans, UDP scans and OS detection need raw sockets, so run Nmap with sudo on Linux.
  • Log everything. Every scan you run should be saved (see the workflow section).

Host discovery

Before scanning ports, Nmap works out which hosts are up. The default probes differ depending on whether you're on the same segment.

Option What it does When to use it
-sn Discovery only, no port scan A quick inventory of live hosts
-PR ARP discovery Automatic on the local segment; finds hosts that block ping
-PE ICMP echo (ping) Networks that allow ping
-PS22,80,443,445,3389 TCP SYN to chosen ports Hosts that drop ping but run services
-PA80,443 TCP ACK probe Getting past some stateless filters
-PU53,161 UDP probe Finding UDP-only devices
-Pn Skip discovery, treat every host as up External tests where firewalls block all probes
bash
# Internal: who is alive on my VLAN?
sudo nmap -sn 10.20.0.0/24 -oA discovery/vlan20

# External: combine probes so ping-blocking hosts are still found
sudo nmap -sn -PE -PS22,80,443,3389 -PA80,443 -iL external.txt -oA discovery/external

On external tests, -Pn is common because perimeter firewalls drop everything. It's slower, since Nmap scans every address whether or not it's alive, so budget time for it.

Port scan types

bash
# SYN scan (default when root): fast and reliable
sudo nmap -sS 10.20.0.15

# Connect scan: no root needed, completes the handshake, more likely to be logged
nmap -sT 10.20.0.15

# UDP scan of the most useful ports
sudo nmap -sU --top-ports 50 10.20.0.15

Which ports?

Option Ports scanned
(default) The 1,000 most common TCP ports
--top-ports 100 The 100 most common
-p 22,80,443 Exactly those
-p- All 65,535 TCP ports
-p U:53,161,T:1-1024 Mixed UDP and TCP lists

A reliable pattern is two passes: a fast full-range scan to find open ports, then a deep scan of only those ports.

bash
# Pass 1: every TCP port, just find what's open
sudo nmap -sS -p- --min-rate 1000 10.20.0.15 -oA scans/host15-allports

# Pass 2: detailed service detection on the ports that answered
sudo nmap -sS -sV -sC -p 22,80,139,445,3306 10.20.0.15 -oA scans/host15-detail

Understanding the states

Beyond open, closed and filtered, Nmap reports open|filtered (common with UDP: no reply, so it can't tell) and unfiltered (from ACK scans: reachable, state unknown). Never report a UDP port as open from an open|filtered result alone; confirm it with a protocol-specific probe.

Timing and performance

Timing templates run from -T0 (paranoid) to -T5 (insane). -T3 is the default; -T4 is common on modern, stable networks.

Option Effect
-T4 Shorter timeouts, more parallelism
--min-rate 1000 Send at least 1,000 packets per second
--max-rate 100 Never exceed 100 packets per second
--max-retries 2 Fewer retransmissions; faster but may miss ports
--host-timeout 30m Give up on hosts that take too long

Fragile environments need the opposite of speed. Old printers, industrial controllers, medical devices and some VoIP phones can crash under heavy scanning. On those segments use --max-rate, avoid -T4 and above, skip OS detection and UDP floods, and agree the approach with the client in advance.

Service, version and OS detection

bash
sudo nmap -sV --version-intensity 5 -p 21,22,80,445 10.20.0.15
sudo nmap -O 10.20.0.15
sudo nmap -A 10.20.0.15   # -sV -sC -O plus traceroute
  • -sV sends probes and matches the replies against a database to name the software and version, for example OpenSSH 7.4 or Apache httpd 2.4.29. This is what you'll later map to vulnerabilities.
  • -O guesses the operating system from TCP/IP quirks. Treat it as a hint, not proof.
  • Banners can lie. Admins sometimes change them, and Linux distributions backport security fixes without changing the version string. You'll deal with that in the vulnerability validation lesson.

The Nmap Scripting Engine (NSE)

NSE scripts extend Nmap with protocol-specific checks. They're grouped into categories:

Category Contents Use on client systems?
default (-sC) Useful, generally safe scripts Yes
safe Won't crash services or use many resources Yes
discovery Gather more information Usually
vuln Check for specific known vulnerabilities Carefully; read what each does
auth / brute Credential checks and guessing Only if authorised; risk of lockouts
intrusive May crash or disrupt services Only with explicit approval
dos Denial of service Never on client systems
bash
# Read what a script does before you run it
nmap --script-help smb-security-mode

# Safe, high-value checks
sudo nmap -p 445 --script "smb-security-mode,smb2-security-mode,smb-os-discovery" 10.20.0.0/24
sudo nmap -p 443 --script ssl-enum-ciphers,ssl-cert example-in-scope-host
sudo nmap -sU -p 161 --script snmp-info 10.20.0.0/24

Avoid lazy category runs like --script all or --script vuln against production. Pick scripts by name, read their help, and you'll always know what you sent.

A repeatable workflow

bash
mkdir -p discovery scans
# 1. Discovery
sudo nmap -sn -iL scope.txt --excludefile exclusions.txt -oA discovery/live
grep Up discovery/live.gnmap | cut -d' ' -f2 > live.txt

# 2. Fast full TCP sweep
sudo nmap -sS -p- --min-rate 1000 -iL live.txt -oA scans/tcp-all

# 3. Detailed scan of open ports (example for one host)
sudo nmap -sS -sV -sC -p 22,80,443 10.20.0.15 -oA scans/10.20.0.15

# 4. Top UDP ports
sudo nmap -sU --top-ports 25 -iL live.txt -oA scans/udp-top
  • -oA name writes normal (.nmap), grepable (.gnmap) and XML (.xml) output in one go. XML imports into reporting tools.
  • ndiff old.xml new.xml shows what changed between two scans, which is perfect for retests.
  • Faster sweepers like masscan and RustScan can find open ports on huge ranges quickly, but they're aggressive. Use them only where the client agrees, and confirm their results with Nmap.

Lab

In your isolated lab, against Metasploitable 2:

  1. Find it with an -sn sweep of your host-only network.
  2. Run a full TCP sweep with -p- and note how many ports are open.
  3. Run -sV -sC against the open ports and list each service and version.
  4. Run a UDP scan of the top 25 ports. Which results are open|filtered, and how could you confirm them?
  5. Save all output with -oA and write three lines summarising what you'd investigate first, and why.

Checkpoint

  1. When would you use -Pn, and what does it cost you?
  2. What's the practical difference between -sS and -sT?
  3. Why scan in two passes instead of running -A -p- on everything?
  4. How would you change your approach for a network full of medical devices?
  5. Why should you avoid --script vuln against production systems?

Next: service enumeration, where you'll dig into what each open port actually exposes.