Skip to main content
TCP, UDP, Ports & ICMP Diagnostics
Lesson 3 of 7

TCP, UDP, Ports & ICMP Diagnostics

Compare TCP and UDP headers, trace the TCP three-way handshake, inspect Linux sockets and port ranges, and mitigate SYN floods and weak TLS.

Beginner 25 min Networking
Authorised environments only — The techniques described here are used only in training labs and on systems you own or have written permission to test.

Learning objectives

  • Trace the TCP three-way handshake and explain the role of SYN, ACK, FIN, RST, PSH, and URG flags
  • Compare TCP and UDP header sizes, reliability guarantees, and ICMP diagnostic feedback
  • Classify well-known, registered, and ephemeral port ranges and inspect listening sockets with ss
  • Identify transport-layer attacks including SYN floods and session hijacking and apply modern TLS 1.3 defenses

Recommended first: IPv4/IPv6 Addressing & CIDR Subnetting

#Why this matters

IP delivers packets between hosts, yet one server often runs a web app, SSH, and a database simultaneously. The Transport Layer (OSI Layer 4) separates concurrent services with port numbers and lets applications choose reliable delivery (TCP) or low-overhead speed (UDP).

In digital forensics and incident response, Layer 4 evidence shows which services accepted connections, whether handshakes completed, and whether a host faced a flood or port scan.

#TCP: reliable, connection-oriented delivery

TCP (Transmission Control Protocol) guarantees ordered, reliable delivery with flow control and congestion control. Its header is 20 to 60 bytes (20 bytes minimum plus options).

TCP opens a connection with the three-way handshake: the client sends SYN (SYN_SENT), the server replies SYN-ACK (SYN_RCVD), and the client confirms with ACK so both sides reach ESTABLISHED.

TCP three-way handshakeTCP three-way handshakeClientServerSYN (seq=100)SYN-ACK (seq=300, ack=101)ACK (ack=301)Both endpoints reach ESTABLISHED and exchange data

Six TCP control flags manage session state:

  • SYN: initiates a connection and synchronizes sequence numbers.
  • ACK: acknowledges received data or control flags.
  • FIN: closes a connection gracefully.
  • RST: terminates a connection immediately on error or a closed port.
  • PSH: flushes buffered data to the application immediately.
  • URG: marks urgent data (rare in modern networks).

#UDP and ICMP diagnostics

UDP (User Datagram Protocol) is connectionless and sends independent datagrams with no handshake or retries. Its header is fixed at 8 bytes (Source Port, Destination Port, Length, and Checksum, 16 bits each), making it ideal for DNS, VoIP, video streaming, gaming, and QUIC (HTTP/3), which layers its own reliability and TLS 1.3 over UDP.

TCP vs UDP
TCP
  • Connection-oriented (3-way handshake)
  • 20-60 byte header
  • Guaranteed, ordered delivery
  • Flow and congestion control
  • HTTP/HTTPS, SSH, SMTP, PostgreSQL
UDP
  • Connectionless (no handshake)
  • Fixed 8-byte header
  • Best-effort, unordered datagrams
  • No flow or congestion control
  • DNS, VoIP, video, QUIC (HTTP/3)

Although ICMP belongs to Layer 3, it supports Layer 4 diagnostics. A closed TCP port typically replies with a TCP RST, whereas a closed UDP port usually returns ICMP Type 3, Code 3 (Port Unreachable) unless a firewall silently drops the probe.

#Port numbers and socket inspection

Ports span 0 to 65535 across three ranges:

  • Well-known (0-1023): System services like FTP (20/21), SSH (22), SMTP (25), DNS (53), HTTP (80), and HTTPS (443). Binding ports below 1024 on Unix typically requires root or CAP_NET_BIND_SERVICE.
  • Registered (1024-49151): Application services including MS SQL (1433), MySQL (3306), RDP (3389), PostgreSQL (5432), alternate HTTP (8080), and alternate HTTPS (8443).
  • Dynamic / Ephemeral (49152-65535): Temporary client source ports assigned automatically for outbound connections.

Use ss -tuln to list local listening sockets on Linux, and nmap to inspect open ports on authorized lab targets.

Inspecting local sockets and running authorized lab scans
$ ss -tuln
Netid State  Recv-Q Send-Q Local Address:Port  Peer Address:Port
udp   UNCONN 0      0      10.10.10.15:53      0.0.0.0:*
tcp   LISTEN 0      128    10.10.10.15:22      0.0.0.0:*
tcp   LISTEN 0      128    10.10.10.15:5432    0.0.0.0:*
$ sudo nmap -sS 192.0.2.20
$ nmap -sV -p 1-1000 192.0.2.20
$ sudo nmap -sU -p 53,123 192.0.2.20

In Nmap, -sS runs a TCP SYN scan (stealth in name, though modern firewalls still log it), -sV -p 1-1000 detects service versions on ports 1-1000, and -sU scans UDP ports.

#Transport-layer attacks and TLS

  • SYN Flood: Forged SYN packets fill the server's SYN_RCVD table without completing the handshake, exhausting memory.
  • Port Scanning: Maps exposed TCP and UDP services to locate vulnerable daemons.
  • TCP Session Hijacking: Predicts or intercepts TCP sequence numbers to inject packets into unencrypted sessions.

Modern services encrypt traffic above TCP with TLS (HTTPS = HTTP + TLS). Legacy SSL, TLS 1.0, and TLS 1.1 are deprecated and insecure (RFC 8996). TLS 1.2 is acceptable, and TLS 1.3 is recommended.

#Defending / Common mistakes

  • Enable SYN cookies and rate limiting: SYN cookies prevent backlog exhaustion during SYN floods, while firewall rate limits curb volumetric probes.
  • Close unused ports and track state: Bind databases to loopback or private subnets and enforce stateful firewall rules.
  • Randomize sequence numbers and enforce TLS 1.3: Protect against session hijacking by pairing OS sequence randomization with TLS 1.3 and disabling TLS 1.0 and 1.1.
  • Do not assume -sS is invisible: Firewalls and IDS log SYN scans, and blocking all ICMP breaks UDP troubleshooting.

#Summary

  • TCP (20-60 byte header) provides reliable delivery via the SYN, SYN-ACK, ACK handshake and flags (FIN graceful close, RST immediate abort).
  • UDP (8-byte header) sends connectionless datagrams; closed UDP ports return ICMP Port Unreachable.
  • Ports split into Well-known (0-1023), Registered (1024-49151), and Ephemeral (49152-65535), audited locally with ss -tuln.
  • Defend Layer 4 with SYN cookies, rate limiting, stateful firewalls, sequence randomization, and TLS 1.3.

Knowledge check

Sign in to take the test, complete lessons and save your progress. Log in

Practice: test your knowledge