SkillPilotAI

nmap

nmap (Network Mapper) sends raw packets to find which hosts are up and which ports and services are open, and it also guesses versions, the OS and firewall rules. It is a daily admin tool for inventory, firewall testing and audits: use it only on your own network or with written permission.

74.1 Why is it used?

  • Inventory: which hosts are up and what runs on them.
  • Firewall testing: is a port open, closed or filtered from this side?
  • Version and OS detection (-sV, -O) for patching and audits; NSE Lua scripts for extra checks.
  • Syntax: nmap [Scan Type] [Options] {targets}.
  • Windows: official installer with Npcap. Linux/macOS: apt/brew. Raw scans need admin/sudo.

74.2 How it works: open, closed, filtered

SYN scan in the lab
  1. 1.Scanner 192.168.94.5 sends SYN :22 through the Docker bridge to web 192.168.94.10

  2. 2.SSH runs there: web replies SYN-ACK -> nmap marks 22 open

  3. 3.SYN :443, no service: web replies RST -> 443 closed

  4. 4.SYN :3306 to the DB: the nftables firewall drops it silently

  5. 5.nmap retries once, still nothing -> 3306 filtered

open = SYN-ACK, closed = RST, filtered = no reply

74.3 Target specification and DNS

  • -sL list scan sends no packets, it only lists targets (a range 8-11 or a /30 gives 4 addresses).
  • --exclude keeps hosts out, -iL reads targets from a file, -iR picks random internet targets (avoid it).
  • Other forms: scanme.nmap.org, 10.0.0-255.1-254 (range per octet), -6 fe80::1%eth0, --unique.
  • -n turns reverse DNS off (faster); -R resolves every target; --dns-servers picks your DNS server.
Terminal
nmap -sL 192.168.94.8-11nmap -sn -n 192.168.94.0/24 --exclude 192.168.94.1nmap -sn -n -iL /tmp/targets.txt   # targets.txt: 192.168.94.10, .30
Output
$ nmap -sL 192.168.94.8-11Nmap scan report for 192.168.94.8Nmap scan report for 192.168.94.9Nmap scan report for n074-web.n074-lan (192.168.94.10)Nmap scan report for 192.168.94.11Nmap done: 4 IP addresses (0 hosts up) scanned in 0.15 seconds$ nmap -sn -n 192.168.94.0/24 --exclude 192.168.94.1Nmap scan report for 192.168.94.10Nmap scan report for 192.168.94.20Nmap scan report for 192.168.94.30Nmap scan report for 192.168.94.5Nmap done: 255 IP addresses (4 hosts up) scanned in 2.10 seconds$ nmap -sn -n -iL /tmp/targets.txtNmap scan report for 192.168.94.10Host is up (0.000028s latency).Nmap scan report for 192.168.94.30Host is up (0.00013s latency).Nmap done: 2 IP addresses (2 hosts up) scanned in 0.26 seconds

74.4 Host discovery

  • -sn = ping scan only (no ports); on the local LAN it uses ARP.
  • Default probes off-LAN: -PE -PS443 -PA80 -PP (ICMP echo, SYN 443, ACK 80, ICMP timestamp), so one blocked probe does not hide the host.
  • -PE ICMP echo, -PS22 SYN ping, -PA80 ACK ping, -PU53 UDP ping, -PP timestamp, -PM address mask, -PO IP protocol ping.
  • -Pn skips discovery and treats the host as up (needed when ping is blocked).
Terminal
nmap -sn -n --disable-arp-ping -PE 192.168.94.10,30,40nmap -sn -n --disable-arp-ping -PS22 192.168.94.10,40nmap -n -Pn -p 3306 192.168.94.20
Output
$ nmap -sn -n --disable-arp-ping -PE 192.168.94.10,30,40Nmap scan report for 192.168.94.10Host is up (0.00011s latency).Nmap scan report for 192.168.94.30Host is up (0.0032s latency).Nmap done: 3 IP addresses (2 hosts up) scanned in 0.50 seconds$ nmap -sn -n --disable-arp-ping -PS22 192.168.94.10,40Nmap scan report for 192.168.94.10Host is up (0.00013s latency).Nmap done: 2 IP addresses (1 host up) scanned in 0.42 seconds$ nmap -n -Pn -p 3306 192.168.94.20PORT     STATE    SERVICE3306/tcp filtered mysqlNmap done: 1 IP address (1 host up) scanned in 0.59 seconds

74.5 Scan techniques

OptionScanNotes
-sSTCP SYN (half open)Default with root; fast
-sTTCP connectDefault without root; full handshake, logged
-sUUDPSlow; can be combined with a TCP scan
-sATCP ACKMaps firewall rules: unfiltered vs filtered
-sN / -sF / -sXNULL / FIN / Xmas (FIN,PSH,URG)Open ports stay silent -> open|filtered
-sW / -sMWindow / Maimon (FIN/ACK)ACK-scan variants
-sI zombieIdle scanUses a zombie host's IP ID
-sOIP protocol scanWhich IP protocols are supported
Terminal
nmap -n -sS -p 22,80,443 192.168.94.10nmap -n -sA -p 22,3306 192.168.94.20nmap -n -sX -p 22,443 192.168.94.10
Output
$ nmap -n -sS -p 22,80,443 192.168.94.10PORT    STATE  SERVICE22/tcp  open   ssh80/tcp  open   http443/tcp closed https$ nmap -n -sA -p 22,3306 192.168.94.20PORT     STATE      SERVICE22/tcp   unfiltered ssh3306/tcp filtered   mysql$ nmap -n -sX -p 22,443 192.168.94.10PORT    STATE         SERVICE22/tcp  open|filtered ssh443/tcp closed        https

74.6 Port specification

  • Default: top 1000 ports, in random order (-r scans low to high).
  • -F = top 100, --top-ports 5 = the 5 most common, -p- = all 65535.
  • -p 22,80,443 a list, -p U:53,T:80 TCP and UDP together, -p ftp,http* by name, --exclude-ports 22.
Terminal
nmap -n -F 192.168.94.10nmap -n -p- -T4 192.168.94.10
Output
$ nmap -n -F 192.168.94.10Not shown: 98 closed tcp ports (reset)PORT   STATE SERVICE22/tcp open  ssh80/tcp open  httpNmap done: 1 IP address (1 host up) scanned in 0.47 seconds$ nmap -n -p- -T4 192.168.94.10Not shown: 65533 closed tcp ports (reset)PORT   STATE SERVICE22/tcp open  ssh80/tcp open  httpNmap done: 1 IP address (1 host up) scanned in 6.30 seconds

74.7 Version, OS detection and NSE

  • -sV talks to open ports and identifies the software; --version-intensity 0-9 (light = 2, all = 9).
  • -O guesses the OS from the TCP/IP fingerprint: it said Linux 4.15 - 5.19 (97%) while the real kernel was 6.18, so treat it as a guess.
  • -sC runs the default NSE scripts (ssh-hostkey, http-title); --script takes a name, category, wildcard or a boolean like "default and safe".
  • 14 categories: auth, broadcast, default, discovery, dos, exploit, external, fuzzer, intrusive, malware, safe, version, vuln, all. dos/exploit/intrusive can crash targets.
Terminal
nmap -n -sV -p 22,80 192.168.94.10nmap -n -sC -p 22,80 192.168.94.10nmap --script-help http-title
Output
$ nmap -n -sV -p 22,80 192.168.94.10PORT   STATE SERVICE VERSION22/tcp open  ssh     OpenSSH 9.7 (protocol 2.0)80/tcp open  http    SimpleHTTPServer 0.6 (Python 3.12.3)$ nmap -n -sC -p 22,80 192.168.94.10PORT   STATE SERVICE22/tcp open  ssh| ssh-hostkey:|   256 f7:e3:dd:96:0a:c4:8b:4c:c6:90:3b:8b:41:28:57:ca (ECDSA)|_  256 78:0a:e2:02:8a:0d:4b:04:6a:53:eb:c9:22:0d:9c:69 (ED25519)80/tcp open  http|_http-title: Directory listing for /$ nmap --script-help http-titlehttp-titleCategories: default discovery safehttps://nmap.org/nsedoc/scripts/http-title.html  Shows the title of the default page of a web server.

74.8 Timing and performance

TemplateNameBehaviour
-T0paranoidSerial, 5 min between probes (IDS evasion)
-T1sneakySerial, 15 s gap
-T2polite0.4 s gap, low load
-T3normalDefault
-T4aggressiveBest for a fast, reliable network
-T5insaneFastest, least accurate
Terminal
nmap -n -T2 -p 20-29 192.168.94.10nmap -n -T4 -p 20-29 192.168.94.10nmap -n --max-retries 0 --max-rate 50 -p 20-29 192.168.94.10
Output
$ nmap -n -T2 -p 20-29 192.168.94.1022/tcp open   sshNmap done: 1 IP address (1 host up) scanned in 4.69 seconds$ nmap -n -T4 -p 20-29 192.168.94.1022/tcp open   sshNmap done: 1 IP address (1 host up) scanned in 0.34 seconds$ nmap -n --max-retries 0 --max-rate 50 -p 20-29 192.168.94.1022/tcp open   sshNmap done: 1 IP address (1 host up) scanned in 0.73 seconds
  • Fine control: --host-timeout 30m (drop a slow host), --scan-delay 1s, --min-rate/--max-rate, --max-retries, --min/max-parallelism.
  • In real work -T4 with a --host-timeout is common so one slow host does not hold the whole scan.

74.9 Firewall/IDS evasion (own lab only)

  • -D decoy1,ME,decoy2: the target sees the same SYN from several source IPs.
  • -g 53 sets source port 53, which can slip past weak rules that allow DNS.
  • Others: -f / --mtu 16 fragments, -S spoof source, --data-length 25, --ttl, --spoof-mac 0, --proxies, --badsum (a reply means a middlebox answered).
Terminal
nmap -n -sS -Pn -p 80 -D 192.168.94.66,ME,192.168.94.77 192.168.94.10nmap -n -sS -Pn -g 53 -p 80 192.168.94.10
Output
web$ cat /tmp/decoy.txt   (tcpdump on the target)IP 192.168.94.66.49990 > 192.168.94.10.80: Flags [S], seq 3147687820, win 1024, options [mss 1460], length 0IP 192.168.94.5.49990 > 192.168.94.10.80: Flags [S], seq 3147687820, win 1024, options [mss 1460], length 0IP 192.168.94.77.49990 > 192.168.94.10.80: Flags [S], seq 3147687820, win 1024, options [mss 1460], length 0web$ cat /tmp/sport.txt   (tcpdump on the target)IP 192.168.94.5.53 > 192.168.94.10.80: Flags [S], seq 63837845, win 1024, options [mss 1460], length 0

74.10 Output, verbosity and -A

OptionOutput
-oN / -oX / -oGNormal / XML (for tools) / grepable (one line per host)
-oA nameAll three at once: name.nmap, name.xml, name.gnmap
--resume scan.nmapContinue a stopped scan
--reasonProof for each state (syn-ack, reset, no-response)
--openOnly open ports
--packet-traceEvery packet sent and received
-v / -dMore detail / debugging; keys v, V, d, p, ? during a scan
-A-sV + -O + -sC + traceroute in one go
Terminal
cd /tmp/n074 && nmap -n -p 22,80,443 -oA lab 192.168.94.10 > /dev/nullgrep Ports /tmp/n074/lab.gnmapnmap -n --reason -p 22,80,443 192.168.94.10
Output
$ grep Ports /tmp/n074/lab.gnmapHost: 192.168.94.10 ()	Ports: 22/open/tcp//ssh///, 80/open/tcp//http///, 443/closed/tcp//https///$ nmap -n --reason -p 22,80,443 192.168.94.10Host is up, received arp-response (0.000086s latency).PORT    STATE  SERVICE REASON22/tcp  open   ssh     syn-ack ttl 6480/tcp  open   http    syn-ack ttl 64443/tcp closed https   reset ttl 64

74.11 Common mistakes

  • Scanning a network you do not own: nmap is legal, unauthorized scanning is the problem.
  • Trusting -O as fact: it is a fingerprint guess (97% Linux 4.15-5.19 vs a real 6.18 kernel).
  • Running dos/exploit/intrusive scripts on production: they can crash the target.
  • Forgetting -n: reverse DNS lookups slow every scan.
  • Using -T5 on a lossy network: speed wins over accuracy and ports get missed.

74.12 Interview tips

  • -sS sends RST right after SYN-ACK (half open, fast, needs root); -sT completes the handshake (no root, shows in app logs).
  • Filtered in both -sS and -sA = a firewall silently drops packets (a closed port would RST, and -sA would say unfiltered).
  • Quick full picture: nmap -A -T4; save results with -oA for diffing later.
  • open = SYN-ACK, closed = RST, filtered = no reply.