1. What Is a Port?
A port is a logical communication endpoint used by the Transport Layer (TCP/UDP) to identify a specific service running on a device.
The IP address ensures the data reaches the correct computer. The Port number ensures the data reaches the correct application (like SSH, Web Browser, or Email Client) inside that computer.
2. Why Ports Exist
Without ports, computers would be limited to running a single network service at a time. You couldn't browse the web while listening to Spotify.
Multiplexing
Ports allow multiplexing: multiple applications transmitting data simultaneously over the same physical network connection. The OS uses the port number to sort incoming packets to the right process.
3. Port Number Ranges
Port numbers range from 0 to 65535. The IANA (Internet Assigned Numbers Authority) divides them into three categories.
3.1 Well-Known Ports (0–1023)
Reserved for core system services. Requires root/admin privileges to bind.
| Port | Service | Description |
|---|---|---|
| 20/21 | FTP | File Transfer (Unsecure) |
| 22 | SSH | Secure Shell (Remote Access) |
| 25 | SMTP | Email Sending |
| 53 | DNS | Domain Name Resolution |
| 80 | HTTP | Web Traffic (Unencrypted) |
| 443 | HTTPS | Web Traffic (Encrypted) |
| 445 | SMB | Windows File Sharing |
3.2 Registered Ports (1024–49151)
Used by user applications and games. (e.g., MySQL uses 3306, RDP uses 3389).
3.3 Ephemeral Ports (49152–65535)
Temporary ports assigned automatically by the OS for client-side connections. Ephemeral ports are chosen dynamically from a range defined by the operating system.
cat /proc/sys/net/ipv4/ip_local_port_range
⚠️ Crucial Distinction: Port ≠ Service
A port number does not guarantee which service is running. An attacker can run a web server on port 9999 or SSH on port 80. Always verify using service detection:
4. How Services Bind (Sockets)
A Socket is the unique combination of: IP Address + Port + Protocol.
LISTEN State
The service is awake and waiting for new connections.
Example: Web Server on Port 80.
ESTABLISHED State
An active data connection exists between two sockets.
Example: You downloading a file.
The Full Connection (4-Tuple)
A TCP connection is uniquely identified by four values:
- Source IP
- Source Port
- Destination IP
- Destination Port
This "4-Tuple" is why multiple users can connect to the same web server (Port 443) simultaneously without their data getting mixed up.
5. TCP vs UDP Ports
Ports behave differently depending on the protocol used.
TCP Ports
Connection-Oriented. Uses a handshake (SYN, SYN-ACK, ACK). Reliable, but slower. Used for HTTP, SSH, FTP, SMTP.
UDP Ports
Connectionless. "Fire and forget." Unreliable but fast.
Examples: DNS (53), DHCP (67/68), NTP (123), SNMP (161).
6. Port States (The Scanner's View)
When you scan a target with Nmap, ports will return one of three primary states:
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
999/tcp closed garcon
- Open: Application is listening. Target acquired.
- Closed: No application listening. Host replied with "Reset".
- Filtered: Firewall blocked the probe. Often means the firewall is silently dropping packets (no reply), which is common in perimeter defense.
7. Security Perspective
The Attack Surface
Every open port is a potential door for hackers. Security hardening implies the "Principle of Least Privilege": Close everything that is not strictly needed.
High-Risk Ports
- Port 23 (Telnet): Sends passwords in cleartext.
- Port 21 (FTP): Vulnerable to anonymous login.
- Port 445 (SMB): Famous for EternalBlue/WannaCry exploits.
- Port 3389 (RDP): Target for brute-force attacks.
8. Firewalls & Ports
Firewalls control traffic based on port rules. A secure firewall uses the Default Deny policy.
ALLOW TCP Port 80 (Web)
ALLOW TCP Port 22 (SSH) from 10.0.0.5 (Admin)
DENY ALL (Block everything else)
9. Practical Commands
Use these commands to inspect ports on your own machine.
root@kali:~# ss -tulnp
# Windows: Show active connections
C:\> netstat -ano
# Scan local machine
root@kali:~# nmap localhost
🧠 Knowledge Check
Test your understanding of ports and protocols.
📌 Module Recap
- Ports act as logical doors for specific services on an IP address.
- Socket = IP Address + Port Number + Protocol.
- Well-Known Ports (0-1023) are reserved for core services like SSH (22) and HTTP (80).
- TCP is reliable and connection-oriented; UDP is fast and connectionless.
- Open Ports increase attack surface; firewalls should block everything by default.