Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 3 of 11
TCP/UDP & The Three-Way Handshake
TCP and UDP are the two core transport-layer protocols. Understanding how they work - especially the TCP three-way handshake - is essential for understanding SYN floods, port scanning, firewall rules, and stateful inspection.
TCP vs UDP: Security Perspective
- TCP (Transmission Control Protocol): Connection-oriented, reliable, ordered delivery with acknowledgments. Used by HTTP/S, SSH, SMTP, FTP. The handshake creates a stateful connection that firewalls track
- UDP (User Datagram Protocol): Connectionless, no handshake, no delivery guarantee. Used by DNS, DHCP, NTP, video streaming. No connection state means stateful firewalls cannot track sessions - easier to spoof
- Security implication: UDP services are commonly abused for amplification DDoS attacks. DNS (port 53 UDP), NTP (port 123), memcached (port 11211) - small request yields massive response, amplifying attack traffic
TCP Three-Way Handshake & SYN Flood Attack
# Normal TCP three-way handshake
Client → SYN (seq=1001) → Server # "I want to connect"
Client ← SYN-ACK (seq=2001) ← Server # "OK, acknowledged"
Client → ACK (seq=2002) → Server # "Connection established"
# Both sides are now in ESTABLISHED state
# SYN Flood Attack - Exhausting Server Resources
Attacker → SYN (spoofed src IP) → Server # Fake connection request
Attacker ← SYN-ACK ← Server # Server waits for ACK...
# Attacker never sends ACK - connection stuck in SYN_RECEIVED state
# Repeat 100,000 times → server's connection table full → legitimate users denied
# Defense: SYN Cookies
# Server encodes connection info in the ISN (Initial Sequence Number)
# No memory allocated until full handshake completes
# Enable on Linux:
# echo 1 > /proc/sys/net/ipv4/tcp_syncookies
# OR in /etc/sysctl.conf:
# net.ipv4.tcp_syncookies = 1TCP Flags and Their Security Relevance
- SYN (Synchronize): Initiates a connection - used in SYN scans to discover open ports
- ACK (Acknowledge): Part of every established connection - ACK scans bypass some firewalls
- FIN (Finish): Gracefully closes a connection - FIN scans used for stealthy port scanning
- RST (Reset): Abruptly terminates a connection - RST injection used in TCP session hijacking
- PSH (Push): Tells receiver to process data immediately - used to identify real TCP sessions vs scans
- URG (Urgent): Marks data as urgent - rarely used legitimately; Xmas scans set FIN+PSH+URG simultaneously
Port States in Security Scanning
# What Nmap tells you about port states
State | Meaning | Security Implication
------------|----------------------------------|----------------------------
Open | Service actively accepting conns | Attack surface - investigate
Closed | Port reachable, no service | Low risk, but host is live
Filtered | Firewall blocks response | Firewall working correctly
Unfiltered | Reachable, state unknown | Investigate further
Open|Filtered| Cannot determine state | Firewall or no responseCommon Mistakes
- Thinking closed ports are the same as filtered - closed ports still reveal the host is alive and responsive; filtered ports give less information to attackers
- Not enabling SYN cookies on internet-facing servers - SYN flood protection is a basic hardening step that takes one command
- Allowing all UDP outbound traffic - UDP amplification attacks originate from victims' networks that allow unrestricted UDP outbound
Tip
Tip
Practice TCPUDP The ThreeWay Handshake in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
The foundational security model: Confidentiality (privacy), Integrity (trust), and Availability (uptime).
Practice Task
Note
Practice Task - (1) Write a working example of TCPUDP The ThreeWay Handshake from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with TCPUDP The ThreeWay Handshake is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cybersecurity code.
Key Takeaways
- TCP and UDP are the two core transport-layer protocols.
- TCP (Transmission Control Protocol): Connection-oriented, reliable, ordered delivery with acknowledgments. Used by HTTP/S, SSH, SMTP, FTP. The handshake creates a stateful connection that firewalls track
- UDP (User Datagram Protocol): Connectionless, no handshake, no delivery guarantee. Used by DNS, DHCP, NTP, video streaming. No connection state means stateful firewalls cannot track sessions - easier to spoof
- Security implication: UDP services are commonly abused for amplification DDoS attacks. DNS (port 53 UDP), NTP (port 123), memcached (port 11211) - small request yields massive response, amplifying attack traffic
Finished this lesson?