Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 36 ยท Getting Started

Networking Basics for Nmap

Learn the networking ideas behind every scan: IP addresses, ports, TCP flags and the three-way handshake, UDP and ICMP, with runnable Python decoders.

Nmap is easier to understand once you know what it sends. A scan is not magic: it is a stream of ordinary network packets, chosen so that the replies reveal something. This lesson covers the small set of ideas you need: addresses, ports, the three protocols (TCP, UDP, ICMP), TCP flags and the three-way handshake. If you already know these well, skim the code and move on to Your First Scan.

Addresses and ports

An IP address identifies a device on a network, such as 192.0.2.10 (IPv4, four numbers 0-255) or 2001:db8::10 (IPv6). A port is a number from 0 to 65535 that identifies a particular service on that device. If the IP address is the street address of a building, the port is the door number.

Well-known services use conventional ports: 22 for SSH, 25 for email delivery (SMTP), 53 for DNS, 80 for HTTP, 443 for HTTPS, 3306 for MySQL. These are conventions, not rules: a web server can listen on 8080, or SSH on 2222. That is why Nmap later inspects what really answers (Service and Version Detection).

A connection is identified by four values: source IP, source port, destination IP, destination port. When your browser connects to a web server, your system picks a random ephemeral source port (often 49152-65535) and the server uses 443. Nmap picks source ports too, and in later chapters you will see why that matters for firewalls.

The three protocols you will meet

TCP, UDP and ICMP

TCP Connection first, then reliable data.
UDP Fire and forget datagrams.
ICMP Error and status messages.

Nmap speaks all three.

1 / 2
  • TCP (Transmission Control Protocol) is connection-oriented and reliable. Web, email, SSH and databases use it. It has a setup phase (the handshake), which is excellent for scanning because the reply tells you a lot.
  • UDP (User Datagram Protocol) has no connection setup. A message is sent and may or may not get an answer. DNS, DHCP, SNMP and many games use it. UDP is harder to scan (see UDP Scanning).
  • ICMP (Internet Control Message Protocol) carries control messages: echo request/reply (the classic ping), "destination unreachable", "time exceeded". It has no ports, but messages such as "port unreachable" are how UDP closed ports announce themselves.

TCP flags

Each TCP segment header carries single-bit flags. The ones Nmap cares about:

Flag Name Meaning
SYN Synchronize "I want to start a connection"
ACK Acknowledge "I received your data up to here"
RST Reset "Abort. There is no connection (or no service)"
FIN Finish "I am done sending; closing politely"
PSH Push "Deliver this data now"
URG Urgent "Urgent data present" (rarely used)

The flags live in one byte, so you can decode them with bit masks. The values are SYN = 0x02, ACK = 0x10, RST = 0x04, FIN = 0x01, PSH = 0x08, URG = 0x20.

Python Decode TCP flag bytes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

0x12 is SYN+ACK, the reply of an open port. 0x14 is RST+ACK, the reply of a closed one. 0x29 (FIN, PSH, URG) lights up like a Christmas tree, which gave the Xmas scan its name; you will use it in Null, FIN and Xmas Scans.

The three-way handshake

Before two hosts exchange TCP data, they agree on a connection:

sequenceDiagram
  participant C as Client
  participant S as Server (port open)
  C->>S: SYN
  S->>C: SYN, ACK
  C->>S: ACK
  Note over C,S: Connection established

Figure: the TCP three-way handshake.

If the destination port is closed, the operating system answers the SYN with RST (and ACK). If a firewall drops the packet, nothing comes back, and the sender eventually gives up after retries. Those three outcomes (SYN-ACK, RST, silence) are the raw material of port scanning.

A stand-in: simulate the handshake against a made-up host

This model decides how a host reacts to a SYN. It uses a dictionary instead of a network; the "host" has three ports, one open, one closed, one behind a firewall.

Python How a host answers a SYN probe

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Port 9999 is not in the dictionary, so it is closed: an operating system answers for ports with no listener. Silence is the unusual case and means something sits in the way.

IPv4, subnets and CIDR

Targets are often written in CIDR notation: 192.0.2.0/24 means "the first 24 bits are the network part", leaving 8 bits for hosts, so 256 addresses (192.0.2.0 to 192.0.2.255). A /16 has 65,536 addresses, a /30 has 4. Each bit you subtract from the prefix doubles the size. You will use CIDR in Specifying Targets.

Python How big is a CIDR block?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

(Usable hosts exclude the network and broadcast addresses for ordinary subnets.)

Local network versus remote network

Devices on the same local network (same Ethernet segment or Wi-Fi) talk using MAC addresses and ARP (Address Resolution Protocol: "who has 192.0.2.10? tell me your MAC"). Devices beyond a router are reached through it. This difference matters for scanning: on a local network Nmap can use ARP for very reliable host discovery (ARP and Local Discovery), but across routers it must rely on ICMP and TCP probes, which firewalls may block.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A closed TCP port normally answers a SYN with which reply?

    The operating system sends a reset to say no service listens there. Silence suggests a firewall.
  2. 2. Which flag combination marks an open port's reply in the handshake?

    An open port accepts the connection with SYN+ACK (0x12).
  3. 3. How many addresses are in a /24 IPv4 network?

    A /24 leaves 8 host bits: 2^8 = 256 addresses.
  4. 4. Why is UDP harder to scan than TCP?

    Without a connection setup, an open UDP port often stays silent, so it is hard to tell from a filtered one.

Summary

  • An IP address identifies a device; a port (0-65535) identifies a service on it.
  • TCP has flags and a three-way handshake; UDP has none; ICMP carries control messages.
  • Scanners classify ports from the reply: SYN-ACK (open), RST (closed), silence (filtered).
  • CIDR notation such as /24 describes address blocks; each prefix bit halves or doubles the size.
  • On a local network ARP gives reliable discovery; across routers Nmap relies on ICMP and TCP probes.

Next: Your First Scan