Networking Protocols
A protocol is an agreed set of rules for how two programs talk. Attackers love protocols because every rule creates expectations, and every expectation can be violated. This lesson covers the protocols you will meet most in security work.
TCP and UDP
The two main transport protocols make very different promises:
| TCP | UDP | |
|---|---|---|
| Connection | Yes, set up with a handshake | No, just send datagrams |
| Delivery | Reliable, ordered, retransmits lost data | Best effort, may lose or reorder |
| Speed | More overhead | Very low overhead |
| Used by | HTTP(S), SSH, SMTP, databases | DNS, VoIP, games, video streaming, QUIC |
| Security note | Handshake state can be exhausted (SYN flood) | Source address is easy to spoof (amplification attacks) |
The TCP three-way handshake
Before data flows, TCP hosts agree on starting sequence numbers with three packets: SYN, SYN-ACK, ACK. Here is a simulation of a server's state machine:
def server(packet, state):
flag = packet["flag"]
if state == "LISTEN" and flag == "SYN":
return "SYN_RECEIVED", {"flag": "SYN-ACK", "seq": 7000, "ack": packet["seq"] + 1}
if state == "SYN_RECEIVED" and flag == "ACK" and packet["ack"] == 7001:
return "ESTABLISHED", None
if flag == "RST":
return "LISTEN", None
return state, {"flag": "RST"}
state = "LISTEN"
client_packets = [
{"flag": "SYN", "seq": 1000},
{"flag": "ACK", "seq": 1001, "ack": 7001},
]
for pkt in client_packets:
state, reply = server(pkt, state)
print(f"client -> {pkt['flag']:<4} | server state: {state:<13} | reply: {reply}")client -> SYN | server state: SYN_RECEIVED | reply: {'flag': 'SYN-ACK', 'seq': 7000, 'ack': 1001}
client -> ACK | server state: ESTABLISHED | reply: None
A SYN flood sends huge numbers of SYN packets and never sends the final ACK. Each one leaves the server holding a half-open connection in SYN_RECEIVED until its table is full. The defence, SYN cookies, encodes the connection state into the server's sequence number so nothing needs to be stored until the client answers.
Common application protocols
| Protocol | Port | Purpose | Plain-text danger | Secure version |
|---|---|---|---|---|
| HTTP | 80 | Web pages and APIs | Anyone on the path can read and modify traffic | HTTPS (443) |
| FTP | 21 | File transfer | Passwords sent in clear | SFTP (over SSH) / FTPS |
| Telnet | 23 | Remote shell | Everything, including passwords, in clear | SSH (22) |
| SMTP | 25/587 | Sending mail | Sender address easily forged | STARTTLS + SPF/DKIM/DMARC |
| DNS | 53 | Name lookup | Responses can be spoofed | DNSSEC, DNS over HTTPS/TLS |
| DHCP | 67/68 | Hands out IP addresses | Rogue DHCP server can redirect traffic | DHCP snooping on switches |
| ARP | (layer 2) | IP to MAC lookup on a LAN | No authentication at all | Dynamic ARP inspection |
The rule of thumb: if a protocol sends credentials in plain text, replace it. Anyone on the same WiFi, or any router along the path, can read plain-text traffic.
Reading an HTTP request
HTTP is text, which makes it easy to read and easy to manipulate. A request is a request line, headers, a blank line and an optional body:
raw = (
"POST /login HTTP/1.1\r\n"
"Host: shop.example\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"Content-Length: 28\r\n"
"\r\n"
"user=alice&password=hunter22"
)
head, body = raw.split("\r\n\r\n", 1)
request_line, *header_lines = head.split("\r\n")
method, path, version = request_line.split(" ")
headers = dict(line.split(": ", 1) for line in header_lines)
print(method, path, version)
for name, value in headers.items():
print(f" {name}: {value}")
print("body:", body)POST /login HTTP/1.1
Host: shop.example
Content-Type: application/x-www-form-urlencoded
Content-Length: 28
body: user=alice&password=hunter22
Over plain HTTP, that body (with the password!) crosses the network exactly as printed. HTTPS wraps the same text in TLS encryption, which you will study in the TLS lesson.
Note: Every value in a request — path, headers, cookies, body — is chosen by the client. A server must never trust any of it. The
Hostheader, for example, can be set to anything.
ICMP
ICMP carries control messages such as "destination unreachable" and the echo request/reply used by ping. Attackers use it for host discovery (ping sweeps) and, historically, for floods. Many firewalls rate-limit ICMP rather than blocking it completely, because blocking it breaks useful features like path MTU discovery.
Checksums are not security
Protocols such as IP, TCP and UDP include a checksum to detect accidental corruption. Here is the 16-bit ones' complement checksum they use:
def internet_checksum(data: bytes) -> int:
if len(data) % 2:
data += b"\x00"
total = 0
for i in range(0, len(data), 2):
total += (data[i] << 8) | data[i + 1]
total = (total & 0xFFFF) + (total >> 16) # fold the carry back in
return ~total & 0xFFFF
original = b"PAY 10 EUR TO BOB"
tampered = b"PAY 99 EUR TO EVE"
print(hex(internet_checksum(original)))
print(hex(internet_checksum(tampered)))0xbc12
0xae02
The checksum changes, but an attacker who modifies a packet simply recomputes it. Checksums protect against noise, not against people. Protecting against deliberate changes needs a MAC or a signature, covered in the cryptography section.
Go further: The TCP Handshake and State Machine animates the handshake with real sequence numbers, and SYN Floods and SYN Cookies shows how it is abused.
Test yourself
What are the three packets of the TCP handshake, in order?
- ACK, SYN, SYN-ACK
- SYN, SYN-ACK, ACK
- SYN, ACK, FIN
- HELLO, KEY, DONE
Which protocol should replace Telnet for remote administration?
- FTP
- SSH
- SMTP
- DHCP
Why doesn't a TCP checksum stop an attacker from modifying a packet?
- It is too short to compute
- The attacker can recompute it after the change
- Routers remove it
- It only covers the header