Kali Linux for Beginners

Level 2: Reconnaissance~4 hours

03: Network recon & Nmap

On this page
  1. 1. How scanning works (TCP/IP architecture)
  2. 2. Know your own network first (host & interface enumeration)
  3. 3. Host discovery: who is alive?
  4. 4. Port scan types: the core of Nmap
  5. TCP Connect vs SYN Stealth
  6. UDP scanning
  7. Service & version detection, OS fingerprinting
  8. 5. Targeting, timing, and the scripting engine
  9. Port specification
  10. Timing templates -T0..-T5
  11. Nmap Scripting Engine (NSE)
  12. 6. Output formats (and feeding the parser)
  13. Exporting results
  14. 7. Defensive visibility: how targets see (or don't see) you
  15. Firewall: DROP vs REJECT
  16. What trips an IDS/IPS
  17. Common troubleshooting

Reconnaissance answers three questions before any exploitation: what hosts are alive, what services do they run, and what versions are those services? Nmap is the standard tool for all three. This module explains the TCP/IP mechanics that make scanning work, then goes through each scan type with the flags you'll actually use, always against the isolated Docker lab from the repo root.

Authorized targets only. Everything here should be run against 10.77.0.0/24 (the bundled lab) or infrastructure you own / have written permission to test. Port scanning third-party hosts can be illegal.

Where to run these commands. Start the lab with ./scripts/setup-lab-env.sh, then open a shell on the bundled Kali attacker box with ./scripts/setup-lab-env.sh shell. It sits on the lab network at 10.77.0.5, so every command below works the same on Linux, Docker Desktop (Windows/macOS), and WSL2. You're root there, so sudo is optional, and the repo's scripts (like parse-nmap-xml.py) are on your PATH. If your own Kali runs Docker natively, you can scan from it directly.


1. How scanning works (TCP/IP architecture)#

Nmap infers a port's state from how the target's TCP/IP stack responds to carefully crafted packets. The TCP three-way handshakeSYN, SYN-ACK, ACK: how a TCP connection opens. Port scans work by watching how hosts answer the first step. underpins most of it:

text
Client                         Server
  | ---- SYN --------------->  |   "can we talk?"
  | <--- SYN/ACK ------------  |   "yes" (port OPEN)
  | ---- ACK --------------->  |   connection established

From the response Nmap classifies each port:

Probe result Port state Meaning
SYN/ACK open Something is listening
RST (reset) closed Host is up, nothing listening on that port
No response / ICMP unreachable filtered A firewall dropped the probe

That third case is why firewalls matter (section 6): drop vs reject changes what Nmap can even see.


2. Know your own network first (host & interface enumeration)#

Before scanning others, map your own position on the network.

bash
# [Level 0: Fundamental] Interfaces and IP addresses
ip a                       # all interfaces, IPs, MACs, up/down state
ip -brief a                # compact one-line-per-interface summary
ip route                   # routing table, where does traffic go? (default gw)
ip neigh                   # ARP/neighbor cache, hosts you've recently talked to

# [Level 0: Fundamental] What is MY machine listening on? (local footprint)
ss -tuln                   # TCP+UDP listening sockets, numeric ports
#   -t tcp  -u udp  -l listening  -n numeric (don't resolve names)
ss -tulpn                  # add -p to show the owning process (needs root)

# [Level 1: Intermediate] DNS resolution
dig example.com            # full DNS query/answer detail
dig +short example.com     # just the answer
dig -x 10.77.0.10         # reverse lookup (PTR)
nslookup example.com       # simpler, interactive-capable resolver query
host example.com           # one-line answer

Expected ip -brief a:

expected output
lo               UNKNOWN   127.0.0.1/8 ::1/128
eth0             UP        10.77.0.5/24

3. Host discovery: who is alive? Level 1: Intermediate#

Before port-scanning, find live hosts so you don't waste time on empty IPs.

bash
# Ping scan (-sn): no port scan, just "is it up?"
nmap -sn 10.77.0.0/24               # sweep the lab /24
nmap -sn 10.77.0.10 10.77.0.20     # specific hosts

# Skip discovery entirely (-Pn): treat every host as up. Use when ICMP/probes
# are firewalled and -sn wrongly reports hosts as down.
nmap -Pn 10.77.0.10

# List targets without probing them (-sL): sanity-check your target spec.
# Add -n: otherwise Nmap still sends a reverse-DNS query for every address.
nmap -sL -n 10.77.0.0/28

Expected -sn output:

expected output
Nmap scan report for 10.77.0.1
Host is up (0.000053s latency).
Nmap scan report for web-target.pentest-lab-net (10.77.0.10)
Host is up (0.000013s latency).
Nmap scan report for ftp-service.pentest-lab-net (10.77.0.20)
Host is up (0.000061s latency).
Nmap scan report for attacker (10.77.0.5)
Host is up.
Nmap done: 256 IP addresses (4 hosts up) scanned in 2.49 seconds

Four hosts answer, but only two are targets. 10.77.0.1 is the Docker bridge gateway: your own machine (or the Docker Desktop VM / Codespace). 10.77.0.5 is the attacker container you are typing in. Sweeping the /24 with -sn to see what is alive is fine, but point port, version, OS, and script scans at the targets only (10.77.0.10 10.77.0.20), or add --exclude 10.77.0.1,10.77.0.5 when you scan a range.


4. Port scan types: the core of Nmap#

TCP Connect vs SYN Stealth#

bash
# [Level 1: Intermediate] TCP Connect (-sT): completes the full handshake.
# No root needed. Reliable, but the full connection is logged by the target app.
nmap -sT 10.77.0.10

# [Level 2+: Advanced] SYN "stealth" (-sS): sends SYN, reads the
# reply, then sends RST instead of completing the handshake. Faster and less
# likely to be logged by the application layer. REQUIRES ROOT (raw sockets).
sudo nmap -sS 10.77.0.10

Why "stealth" is only half-true: -sS avoids the application's connection log (the app never sees a completed connect), but a modern IDS/firewall logging at the packet layer still sees the SYN. It's stealthier, not invisible.

UDP scanning#

bash
# [Level 2+: Advanced] UDP (-sU): slow and tricky. UDP has no
# handshake, so "no reply" is ambiguous (open OR filtered). Nmap relies on ICMP
# port-unreachable for 'closed'. Scope it tightly to stay sane.
sudo nmap -sU --top-ports 20 10.77.0.10
sudo nmap -sU -p 53,67,123,161 10.77.0.10   # DNS, DHCP, NTP, SNMP

Service & version detection, OS fingerprinting#

bash
# [Level 1: Intermediate] Service/version detection (-sV): grabs banners and
# matches them against Nmap's signature DB to name product + version.
nmap -sV 10.77.0.10

# [Level 2+: Advanced] OS fingerprinting (-O): infers the OS from
# TCP/IP stack quirks. Needs root and at least one open + one closed port.
sudo nmap -O 10.77.0.10

# Combine common options: version + OS + default scripts + traceroute
sudo nmap -A 10.77.0.10

Expected -sV against the lab web target:

expected output
PORT   STATE SERVICE VERSION
80/tcp open  http    nginx 1.31.6

5. Targeting, timing, and the scripting engine#

Port specification#

bash
nmap 10.77.0.10                       # default: top 1000 TCP ports
nmap -p 80,443,8080 10.77.0.10        # specific ports
nmap -p 1-1024 10.77.0.10             # a range
nmap -p- 10.77.0.10                   # ALL 65535 TCP ports (thorough, slower)
nmap --top-ports 100 10.77.0.10       # the 100 statistically most common
nmap -p U:53,T:80,443 10.77.0.10      # mix UDP and TCP in one spec

Timing templates -T0..-T5#

Timing trades speed for stealth and reliability. Higher = faster/louder.

Template Name Use when
-T0 paranoid IDS evasion; one probe every ~5 min (glacial)
-T1 sneaky slow, evasive
-T2 polite reduce bandwidth/target load
-T3 normal (default) balanced
-T4 aggressive fast networks / lab / authorized speed
-T5 insane very fast, may miss results / overwhelm targets
bash
sudo nmap -sS -T4 10.77.0.10          # a good default for a cooperative lab
sudo nmap -sS -T1 -f 10.77.0.10       # slow + fragment packets to dodge IDS

Nmap Scripting Engine (NSE)#

bash
# [Level 2+: Advanced] Run script categories
nmap --script=default 10.77.0.10          # the '-sC' default set
nmap -sC -sV 10.77.0.10                    # shorthand: default scripts + version
nmap --script=safe 10.77.0.10             # non-intrusive checks only
nmap --script=vuln 10.77.0.10             # known-vulnerability probes (louder)
nmap --script=http-title,http-headers 10.77.0.10   # pick specific scripts
nmap --script "ftp-*" 10.77.0.20          # all FTP scripts (glob) on the FTP box

--script=vuln and many intrusive scripts actively poke services and can crash fragile ones. Never run them outside authorized scope; get sign-off before pointing them at production.


6. Output formats (and feeding the parser) Level 1: Intermediate#

Always save your scans, for reporting, diffing, and tooling.

bash
nmap -sV -oN scan.nmap 10.77.0.10      # -oN: human-readable ("normal")
nmap -sV -oG scan.gnmap 10.77.0.10     # -oG: greppable (one host per line)
nmap -sV -oX scan.xml 10.77.0.10       # -oX: XML (machine-parseable)
nmap -sV -oA scan 10.77.0.10           # -oA: ALL three at once (scan.*)

# Feed the XML into the repo's parser for a clean port/service/version table:
nmap -sV -oX lab.xml 10.77.0.10 10.77.0.20
parse-nmap-xml.py lab.xml
# ...or stream straight through without a temp file:
nmap -sV -oX - 10.77.0.10 | parse-nmap-xml.py -

The parser keeps NSENmap's built-in scripts that go beyond port state, such as ftp-anon or http-title. -sC runs the default set. script results too, printed under their port in Nmap's own | / |_ style, so a -sC finding does not vanish from your table:

bash
nmap -sC -sV -oX - 10.77.0.20 | parse-nmap-xml.py -

Expected output (from the lab):

expected output
Nmap results: 1 host(s), 1 up

================================================================
Host: 10.77.0.20 (ftp-service.pentest-lab-net)   Status: up
================================================================
  PORT  PROTO  STATE  SERVICE  PRODUCT / VERSION
  ----  -----  -----  -------  ---------------------
  21    tcp    open   ftp      vsftpd 2.0.8 or later
  | ftp-syst: STAT:
  | FTP server status:
  |      Connected to 10.77.0.5
  |      Logged in as ftp
  |      TYPE: ASCII
  |      No session bandwidth limit
  |      Session timeout in seconds is 300
  |      Control connection is plain text
  |      Data connections will be plain text
  |      At session startup, client count was 3
  |      vsFTPd 3.0.5 - secure, fast, stable
  |_End of status
  | ftp-anon: Anonymous FTP login allowed (FTP code 230)
  |_drwxr-xr-x    2 0        0            4096 Oct 10 02:48 pub

ftp-anon is the finding: anyone can log in without a password. Defensive note: on a real server, set anonymous_enable=NO in vsftpd.conf unless public downloads are the point, and even then keep the anonymous area read-only.

Exporting results#

The table is for reading in the terminal. --format turns the same scan into something you can hand on: markdown pastes straight into a findings report, csv opens in a spreadsheet for sorting and diffing scans, and json feeds your own scripts.

bash
nmap -sV -oX scan.xml 10.77.0.10 10.77.0.20
parse-nmap-xml.py --format markdown scan.xml >> findings.md
parse-nmap-xml.py --format csv scan.xml > scan.csv

Expected --format markdown output (from the lab):

expected output
### 10.77.0.10 (web-target.pentest-lab-net) (up)

| Port | Proto | State | Service | Product / Version |
|------|-------|-------|---------|-------------------|
| 80 | tcp | open | http | nginx 1.31.6 |

### 10.77.0.20 (ftp-service.pentest-lab-net) (up)

| Port | Proto | State | Service | Product / Version |
|------|-------|-------|---------|-------------------|
| 21 | tcp | open | ftp | vsftpd 2.0.8 or later |

Expected --format csv output:

expected output
address,hostname,status,port,protocol,state,service,product,version,scripts
10.77.0.10,web-target.pentest-lab-net,up,80,tcp,open,http,nginx,1.31.6,
10.77.0.20,ftp-service.pentest-lab-net,up,21,tcp,open,ftp,vsftpd,2.0.8 or later,

The scripts column is empty here because this scan used -sV without -sC. Defensive note: a saved scan is a map of your weak points. Store reports and CSVs like any other sensitive client data, and share them only with the people authorized to see them.


7. Defensive visibility: how targets see (or don't see) you#

Understanding the defender's view makes you both a better attacker and a better blue-teamer.

Firewall: DROP vs REJECT#

  • DROP: the firewall silently discards the probe. Nmap gets no response and marks the port filtered. Scans are slower (Nmap waits for timeouts) and you learn less. This is the stealthier defense.
  • REJECT: the firewall replies with a TCP RST or ICMP unreachable. Nmap learns the port is closed/filtered immediately, faster for the attacker, but the reply confirms a live host/firewall.
text
# iptables examples a defender might use:
iptables -A INPUT -p tcp --dport 22 -j DROP     # invisible; scans hang
iptables -A INPUT -p tcp --dport 22 -j REJECT   # fast RST; confirms presence

What trips an IDS/IPS#

  • SYN scans without follow-through (many half-open connections): a signature Snort/Suricata rules flag readily.
  • Fast timing (-T4/-T5) and full-range (-p-) sweeps generate volume that rate-based rules catch.
  • -sU, -O, and --script=vuln produce unusual probe patterns.
  • Sequential port touches from one source in a short window = textbook port-scan alert.

Defender takeaways: log at the packet layer (not just the app), rate-limit new connections, alert on horizontal (many hosts, one port) and vertical (one host, many ports) sweeps, and prefer DROP to slow attackers down.


Common troubleshooting#

Symptom Cause Fix
-sS/-sU/-O fails: "requires root" Raw sockets need privilege Prefix with sudo
All hosts show "down" ICMP/probes filtered Add -Pn to skip host discoveryFinding which addresses in a range have a live machine, before scanning ports, for example with nmap -sn.
Scan is painfully slow DROP firewall + timeouts, or -p- Raise -T, scope ports, add --min-rate
-sV says "tcpwrapped" Service closes the connection after banner Try again; may be a proxy/firewall
No lab hosts found Lab not up, or scanning from a host that can't reach the Docker bridge (Docker Desktop) ./scripts/setup-lab-env.sh, then scan from ./scripts/setup-lab-env.sh shell
OS detection "too many fingerprints" No open+closed port pair found Scan more ports; -Pn if needed

Prove it on the lab with the self-grading Lab exercises: Network recon & Nmap. Ready-to-run, scenario-organized commands live in Nmap scan profiles.