TCP, UDP and Ports
The IP gets you to the machine; the port gets you to the program. The whole enumeration phase (what’s listening, on which port, with which version) rests on understanding how TCP and UDP respond. If you know what a closed port answers compared with a filtered one, you can read a scan instead of copying it.
Concepts
Section titled “Concepts”TCP vs UDP
Section titled “TCP vs UDP”| TCP | UDP | |
|---|---|---|
| Prior connection | Yes, three-way handshake | No, sends straight away |
| Delivery and order | Guaranteed: numbers, acknowledges and retransmits | No guarantees |
| Cost | Bigger header and more round trips | Light and fast |
| Used for | Web, SSH, SMB, mail, databases | DNS, DHCP, SNMP, TFTP, NTP, voice and video |
UDP isn’t “worse”: it’s better when a late answer is useless (a call) or when the exchange is one question and one answer (DNS).
The handshake and the teardown
Section titled “The handshake and the teardown”sequenceDiagram
participant C as Client
participant S as Server
C->>S: SYN
S-->>C: SYN-ACK
C->>S: ACK
Note over C,S: Connection established: data both ways
C->>S: FIN (graceful close)
S-->>C: ACK + FIN
C->>S: ACK
The flags in the TCP header say what each packet is:
| Flag | Meaning |
|---|---|
| SYN | I want to open a connection. |
| ACK | I acknowledge what I received. |
| FIN | I’m done sending (graceful close). |
| RST | Hard reset: “nothing here” or “this connection doesn’t exist”. |
| PSH · URG | Deliver immediately · urgent data (rare in practice). |
Ports and sockets
Section titled “Ports and sockets”A port is a 16-bit number (0–65535) that identifies which program the traffic is for within a machine. A socket is the IP:port pair, and a TCP connection is identified by four values: source IP and port, destination IP and port.
| Range | Name | What lives there |
|---|---|---|
| 0–1023 | Well-known | Standard services. On Linux you need root to listen on them. |
| 1024–49151 | Registered | Applications (3306 MySQL, 5432 PostgreSQL, 8080 alternative web…). |
| 49152–65535 | Dynamic | Ephemeral range per IANA (and Windows). Linux defaults to 32768–60999. |
The ephemeral port is the source port the system picks at random when you open a connection: when your browser goes to a server’s 443, it leaves from, say, 51000.
A port is just a number: a port is not a service. SSH can listen on 2222 and a website on 9001. That’s why you confirm it with version detection instead of assuming.
Where a service listens
Section titled “Where a service listens”The address a service listens on decides who can reach it:
| Listening on | Who can reach it |
|---|---|
0.0.0.0:22 (or [::]:22) |
Anyone who can reach the machine on any interface. |
192.168.1.50:8080 |
Only connections addressed to that IP. It restricts the address, not the interface: on Linux a packet for that IP can arrive on another interface if routing allows it; isolating interfaces is the firewall’s job. |
127.0.0.1:3306 |
Only the machine itself. Invisible to an external scan. |
Services on 127.0.0.1 don’t show up in your nmap, but they do once you have a shell inside: they’re classic privilege-escalation targets (a database or an internal panel) reached with port forwarding. See pivoting.
How nmap reads the replies
Section titled “How nmap reads the replies”TCP (SYN scan): nmap sends a SYN and looks at what comes back.
| Reply | State | Means |
|---|---|---|
| SYN-ACK | open | A program is listening. |
| RST | closed | The machine exists, but nothing listens on that port. |
| Nothing, or ICMP unreachable | filtered | A firewall drops or rejects the packet. |
UDP: with no handshake, a missing reply is ambiguous.
| Reply | State |
|---|---|
| UDP reply from the service | open |
| ICMP port unreachable | closed |
| Nothing | open|filtered |
| Other ICMP unreachable | filtered |
That’s why UDP scanning is slow: many systems rate-limit the ICMP messages they send, and nmap has to wait and retry.
| Scan type | What it does | Privileges |
|---|---|---|
-sS (SYN) |
Sends a SYN and, on SYN-ACK, tears down with RST without completing the connection. | root |
-sT (connect) |
Completes the handshake through the OS API; the service logs the connection. | none |
-sU (UDP) |
Sends UDP packets (empty or protocol-specific). | root |
In practice
Section titled “In practice”# What's listening on MY machine, and which process?sudo ss -tulnp# tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=812,...))# tcp LISTEN 0 80 127.0.0.1:3306 0.0.0.0:* users:(("mysqld",pid=930,...))# ^ only reachable from inside: a target once you have a shell# Is a specific port open on another machine?nc -zv 10.10.10.5 22 # TCPnc -zuv 10.10.10.5 161 # UDP (unreliable: see the UDP table)
# Full TCP scan, then detail on what's opensudo nmap -p- --min-rate 2000 -oN all.txt 10.10.10.5sudo nmap -sV -sC -p 22,80,445 -oN detail.txt 10.10.10.5
# UDP: only the most common ports, or it takes hourssudo nmap -sU --top-ports 50 10.10.10.5# See the real handshakesudo tcpdump -i tun0 -nn 'host 10.10.10.5 and tcp port 80'# Flags [S] → SYN# Flags [S.] → SYN-ACK (the dot is ACK)# Flags [.] → ACK# Flags [R.] → RST: closed portNext offensive step
Section titled “Next offensive step”- In-depth scanning and its options: nmap.
- What sits behind each well-known port: common ports and protocols.
- Reach services that only listen on
127.0.0.1: pivoting.
Cheat sheet
Section titled “Cheat sheet”| Concept | What to remember |
|---|---|
| TCP · UDP | Reliable and ordered · fast with no guarantees. |
| Handshake | SYN → SYN-ACK → ACK. |
| RST | “Nothing here”: closed port. |
| open · closed · filtered | SYN-ACK · RST · nothing or ICMP. |
| UDP with no reply | open|filtered. Slow and ambiguous. |
| Ports < 1024 | Need root to listen on Linux. |
| Ephemeral port | The source port. Linux: 32768–60999. |
127.0.0.1:port |
Invisible from outside, a target from inside. |
| A port is not a service | Confirm it with -sV. |
ss -tulnp |
What listens on your machine and which process. |
Review questions
Section titled “Review questions”-
A port returns nothing to a SYN. How does nmap mark it and what does it mean?
Answer
filtered: something (usually a firewall) silently drops the packet. You don’t know whether there’s a service behind it. -
And if it returns an RST?
Answer
closed: the machine is alive and reachable on that port, but no program is listening there. -
Why is UDP scanning slow and ambiguous?
Answer
Because UDP has no handshake: an open service may not answer an empty packet, and no reply looks the same as a firewall. On top of that, systems rate-limit the ICMP port unreachable they send, so nmap has to wait and retry.
-
What’s the practical difference between
-sSand-sT?Answer
-sScrafts packets and tears down without completing the connection (needs root, faster, less trace in the service’s logs).-sTuses the OS’s full connection (no privileges needed, the service logs it). -
On a compromised host,
ss -tlnpshows127.0.0.1:8000. Why didn’t you see it in your nmap, and how would you reach it from Kali?Answer
Because it only listens on loopback: it doesn’t accept connections from other machines. To reach it from Kali you need to forward the port through the compromised host (for example with an SSH tunnel or a pivoting tool).
-
Why does DNS use UDP for normal queries?
Answer
Because it’s one small question and one small answer: opening a TCP connection would cost more than the query itself. It switches to TCP when the answer is large or for zone transfers.