Skip to content
Back to Series Network Security from Scratch
Networking

TCP/IP for Security: Ports, Protocols, and Encapsulation

Understand how TCP/IP works from a security perspective: the three-way handshake, ports, public versus private IP addresses, and why encapsulation matters.

A
Alif Afriza Author
· · 4 min read
TCP/IP for Security: Ports, Protocols, and Encapsulation

TCP/IP is the language every device on the internet uses to talk to one another. To understand network security, you first need to understand how TCP/IP works, because nearly every attack exploits the way it operates.

The TCP/IP Model in Four Layers

LayerRoleExample Protocols
ApplicationData that users understandHTTP, HTTPS, DNS, SSH
TransportManages delivery between applicationsTCP, UDP
InternetRoutingIP, ICMP
Network AccessPhysical connection between devicesEthernet, WiFi

When you open a website, data flows from the application layer, is wrapped (encapsulated) by each layer, sent over the wire, and then unwrapped again on the receiving side.

IP Addresses and Ports

Public and Private IPs

  • Public IP: an address visible from the internet, unique worldwide.
  • Private IP: an internal network address (for example: 192.168.x.x, 10.x.x.x, 172.16-31.x.x), which cannot be used directly from the internet without NAT.

Most homes have a single public IP for the entire network, with many devices using private IPs behind it. This in itself is a basic form of protection: internal devices are not directly reachable from the internet.

Ports

A port is a “cabinet” inside an IP address. One device can run many services, and ports distinguish one service from another. Common examples:

PortService
21FTP (insecure)
22SSH
25SMTP (outgoing email)
80HTTP (insecure)
443HTTPS
3389Remote Desktop (Windows)

Every open port is a door. The more doors, the larger the attack surface. The basic rule of security: close all ports, open only the ones you need.

Transport: TCP and UDP

TCP

TCP guarantees that data arrives intact and in order. Every TCP connection begins with a three-way handshake:

Client → Server: SYN
Server → Client: SYN-ACK
Client → Server: ACK

This handshake is the target of several types of attacks (for example SYN flood, which will be covered in the DDoS article). Security scanners exploit it to detect open ports: if the server replies with SYN-ACK, that port is alive.

UDP

UDP is lighter, with no acknowledgement of receipt. It is a good fit for video, games, and DNS. Because there is no handshake, spoofing is harder to detect with UDP, and it is frequently abused for reflection (amplification) attacks.

Encapsulation: Why Does It Matter for Security?

Suppose you send the message “hello” over HTTPS. Layer by layer:

[Ethernet header][IP header][TCP header][TLS header]["hello"]

Each header contains control information: where to, from where, which port, and so on. Attacks often work by manipulating these headers: writing a fake address (spoofing), changing the port, or fragmenting data so it slips past filters.

Encryption happens at the application layer (TLS). Only the message contents are encrypted, not the headers. That is why an eavesdropper can tell that you are visiting site X (from the IP address), but cannot read the contents of the page being sent.

Exercise

  1. Look at the open ports on your machine with the following command in PowerShell:
netstat -ano | findstr LISTENING
  1. Note the ports that appear and find out which service is behind each one.
  2. Open your browser to your public IP address, but do not do anything yet. Watch the router logs for any incoming probing.

Next in the curriculum: after understanding protocols, learn how to read network traffic with Wireshark for beginners.

References

  • RFC 791, Internet Protocol, rfc-editor.org/rfc/rfc791 (IP specification).
  • RFC 793, Transmission Control Protocol, rfc-editor.org/rfc/rfc793 (TCP and the three-way handshake).
  • RFC 768, User Datagram Protocol, rfc-editor.org/rfc/rfc768 (UDP).
  • James Kurose and Keith Ross, Computer Networking: A Top-Down Approach, Pearson (a thorough explanation of the TCP/IP model).
  • RFC 1918, Address Allocation for Private Internets, rfc-editor.org/rfc/rfc1918 (private IPs).
Open for Contributors

Your ideas deserve to be read by thousands of people.

ByteNesia is built for everyone. If you have experience, tutorials, or opinions about technology, write and share them here. Your name will be credited as a contributor.

Write anything: tool reviews, homelab tutorials, security tips, or your tech stories.

Discussion & Comments