#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.
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.
- Connection-oriented (3-way handshake)
- 20-60 byte header
- Guaranteed, ordered delivery
- Flow and congestion control
- HTTP/HTTPS, SSH, SMTP, PostgreSQL
- 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 orCAP_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.
$ 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.20In 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
SYNpackets fill the server'sSYN_RCVDtable 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
-sSis 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 (
FINgraceful close,RSTimmediate 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 withss -tuln. - Defend Layer 4 with SYN cookies, rate limiting, stateful firewalls, sequence randomization, and TLS 1.3.