Lab 02: TCP/IP Fundamentals

🎯 Objective

Understand how TCP/IP works by observing real connections with netcat. Learn about the TCP three-way handshake, port numbers, and why this matters for security — including how attackers exploit TCP/IP weaknesses.

📚 Background

TCP/IP is the foundation of the internet. TCP (Transmission Control Protocol) ensures reliable, ordered data delivery, while IP (Internet Protocol) handles addressing and routing. Every time you load a webpage, send an email, or use an app, TCP/IP is working behind the scenes.

The TCP Three-Way Handshake establishes a connection: the client sends a SYN (synchronize) packet, the server responds with SYN-ACK, and the client completes with ACK. Only after this handshake do the parties exchange data. This mechanism is what makes TCP reliable — but it's also exploited in SYN flood attacks.

Ports are like apartment numbers in a building (the building is the IP address). Port 80 is HTTP, port 443 is HTTPS, port 22 is SSH, port 25 is SMTP email. Port numbers 0-1023 are "well-known" ports requiring root to bind. Attackers scan ports to discover what services are running on a target.

UDP (User Datagram Protocol) is TCP's sibling — faster but unreliable (no handshake, no guaranteed delivery). DNS uses UDP by default. UDP attacks include amplification attacks where small requests trigger large responses.

⏱️ Estimated Time

40 minutes

📋 Prerequisites

  • Lab 1 (OSI Model) completed

  • Docker with innozverse-cybersec image

🛠️ Tools Used

  • netcat (nc) — TCP/UDP connection tool

  • ss — Socket statistics

  • python3 — Scripting TCP connections

  • nmap — Port scanning to observe port states

🔬 Lab Instructions

Step 1: Start a TCP Server and Connect

Netcat is often called the "Swiss army knife" of networking. Let's create a server-client TCP connection.

📸 Verified Output:

💡 What this means: Two processes communicated over TCP on port 9999 — the server listening, the client connecting. The TCP handshake happened invisibly. Each side sent a message. This is exactly how web servers work: the web server listens on port 80/443, your browser connects, they exchange HTTP data, connection closes.

Step 2: Observe a Listening Port

Before a client connects, the server must be "listening" — waiting for connections on a specific port.

📸 Verified Output:

💡 What this means: LISTEN is the socket state — it's waiting for incoming connections. 0.0.0.0:8080 means it's listening on ALL interfaces on port 8080. The 1 in Send-Q is the connection backlog. If you were an attacker scanning this system, you'd see this open port and know a service is running.

Step 3: Test Port States — Open vs Closed vs Filtered

Ports can be in three states: open (service running), closed (nothing running), or filtered (firewall blocking).

📸 Verified Output:

💡 What this means: "Connection refused" means the OS actively rejected the connection — no service is on that port. An "open" port accepts connections. "Filtered" ports (blocked by firewall) give no response at all (timeout). This is the basis of port scanning: attackers probe ports to map what's running.

Step 4: Build a Simple TCP Chat

Let's see bidirectional TCP communication — the foundation of every protocol:

📸 Verified Output:

💡 What this means: This Python code demonstrates a complete TCP client-server exchange at the socket level — the same level your browser and web server communicate. The server bind()s to an address, listen()s, then accept()s connections. The client connect()s, then both sides send() and recv(). Every network application uses this pattern.

Step 5: Understand TCP Flags — The Language of TCP

TCP uses flags to signal the state of communication. Understanding flags helps you read packet captures and detect attacks.

📸 Verified Output:

💡 What this means: TCP flags are the language TCP speaks. A RST in your logs means connections are being forcefully rejected — possibly a firewall rule or closed port. A flood of SYN packets without ACK responses indicates a SYN flood DoS attack. IDS/IPS systems look for abnormal flag combinations to detect attacks.

Step 6: UDP vs TCP Comparison

UDP is faster but unreliable — no handshake, no acknowledgment. Let's see the difference:

📸 Verified Output:

💡 What this means: Choosing TCP vs UDP is a security design decision. UDP-based protocols need to implement their own security measures since there's no built-in reliability or authentication. DNS over UDP is why DNS amplification attacks work — a small query can trigger a large response sent to a spoofed victim IP.

Step 7: Explore Well-Known Ports

Security professionals need to know which ports correspond to which services:

📸 Verified Output:

💡 What this means: An open port 23 (Telnet) is a critical finding — it transmits credentials in plaintext. Port 3306 (MySQL) exposed to the internet is a catastrophic misconfiguration. Port 445 (SMB) was exploited by the WannaCry ransomware. Knowing these ports is essential for firewall configuration and penetration testing.

Step 8: IP Address Classes and CIDR Notation

Understanding IP addressing is fundamental:

📸 Verified Output:

💡 What this means: Private IP ranges (RFC 1918) can't be routed on the public internet. When you see 10.x.x.x or 192.168.x.x in logs, it's internal traffic. CIDR notation like /24 means 24 bits are the network, 8 bits are for hosts — giving 254 usable addresses. Misrouting these networks is a common misconfiguration attackers exploit.

Step 9: Scan for Open Ports (on Localhost)

Now let's use nmap to scan our own container — the safe way to practice port scanning:

📸 Verified Output:

💡 What this means: Nmap found our two netcat listeners on ports 8888 and 9999 — listed as "open." Ports 22, 80, and 443 are "closed" because nothing is listening. This is exactly what a penetration tester sees when scanning a target: open ports reveal running services that might have vulnerabilities.

Step 10: Understand TCP Sequence Numbers and Session Hijacking

TCP uses sequence numbers for reliability. This also enables session hijacking:

📸 Verified Output:

💡 What this means: Early TCP implementations used sequential ISNs (easy to predict), enabling session hijacking. Modern OSes use cryptographically random ISNs. Combined with TLS encryption, session hijacking over TCP is now extremely difficult. This is why "HTTPS everywhere" is such important advice.

✅ Verification

📸 Verified Output:

🚨 Common Mistakes

  • Confusing TCP and UDP: Remember TCP = reliable (handshake), UDP = fast (no handshake). Many protocols can use either — choose based on security requirements.

  • Thinking port numbers = services: Port 8080 is often HTTP, but anyone can run any service on any port. Always fingerprint the service, don't assume from port number alone.

  • Forgetting localhost is still a network: 127.0.0.1 still uses TCP/IP — tools and concepts that work on internet traffic apply to loopback too.

📝 Summary

  • TCP uses a three-way handshake (SYN, SYN-ACK, ACK) to establish reliable connections; attackers exploit this with SYN floods

  • Port numbers identify services: well-known ports (0-1023) include HTTP (80), HTTPS (443), SSH (22), and many attack targets like RDP (3389) and SMB (445)

  • UDP is faster but connectionless — used by DNS, streaming, and VoIP, but also exploited in amplification attacks

  • Tools like netcat, ss, nmap, and Python sockets let you observe and test TCP/IP directly

🔗 Further Reading

Last updated