Foundations

TCP/IP, Ports and the Three-Way Handshake

how data finds the right program on the right machine

2 min readupdated by Syed Khaleel Ahmed

An IP address gets data to the right machine. A port gets it to the right program on that machine. Together with the protocol that carries the data, they are the basis of almost all network security rules.

IP, TCP and UDP

  • IP moves packets between networks. It does not promise delivery or order.
  • TCP sits on top and adds reliability: it sets up a connection, numbers the data, resends what is lost and puts it back in order. Web, email and file transfer mostly use it.[1]
  • UDP is lighter: no connection and no guarantees, but low delay. DNS lookups, video calls and games often use it.

Ports

A port is a number from 0 to 65535. A service "listens" on a port and clients connect to it. Some well-known ones:

Port Service
22 SSH (secure remote login)
25 SMTP (email between servers)
53 DNS
80 HTTP
443 HTTPS
3389 Remote Desktop (RDP)

The registry of assigned numbers is kept by IANA.[2] Ports are only conventions: a service can run on any port, and an open port does not by itself mean something is wrong. But every open port is something that can be attacked, which is why unneeded ones are closed.

The three-way handshake

Before TCP sends data, the two sides agree to talk:

  1. The client sends SYN ("I want to connect").
  2. The server replies SYN-ACK ("OK, I am ready").
  3. The client sends ACK ("great, starting now").

Where attacks fit

  • Port scanning sends probes to see which ports answer. It is the first step of many attacks and of many legitimate security tests.
  • SYN flood sends huge numbers of SYN packets and never completes the handshake, tying up the server's resources. It is a denial-of-service technique.
  • Firewalls allow or block traffic using addresses, ports and protocols, the very fields above.

Knowing which ports a system should be using is a quiet superpower for defenders: an unexpected connection on an odd port stands out.

Sources

  1. IETF RFC 9293: Transmission Control Protocol (TCP)
  2. IANA Service Name and Transport Protocol Port Number Registry

Test yourself

My notes

Private — saved only on this device, never sent anywhere.