Networking Basics
To defend a network you need to know how data travels through it. Most attacks abuse a normal networking feature in an unexpected way, so the basics in this lesson come back again and again.
Hosts, packets and addresses
A host is any device on a network: a laptop, a phone, a server, a printer. Hosts don't send whole files in one go. They split data into small packets, each with a header that says where it comes from and where it is going, much like an envelope with a sender and a recipient address.
Every host has two kinds of address:
| Address | Layer | Looks like | Scope |
|---|---|---|---|
| MAC address | Data link (2) | 3c:22:fb:10:9a:01 |
Only the local network segment |
| IP address | Network (3) | 192.168.1.20 or 2001:db8::20 |
Routed across networks |
Switches deliver frames inside one local network using MAC addresses. Routers move packets between networks using IP addresses.
The layered model
Networking is organised in layers. Each layer does one job and relies on the layer below it. The OSI model has seven layers; the simpler TCP/IP model has four. Security people mostly use OSI numbers in conversation ("a layer 7 attack").
| OSI layer | Name | Job | Examples | Typical attacks |
|---|---|---|---|---|
| 7 | Application | What the user's program speaks | HTTP, DNS, SMTP | SQL injection, phishing |
| 6 | Presentation | Encoding, encryption | TLS, JSON | Downgrade attacks |
| 5 | Session | Conversations | sessions, RPC | Session hijacking |
| 4 | Transport | Ports, reliable delivery | TCP, UDP | Port scans, SYN floods |
| 3 | Network | Addressing and routing | IP, ICMP | IP spoofing |
| 2 | Data link | Local delivery | Ethernet, WiFi, ARP | ARP spoofing, MAC flooding |
| 1 | Physical | Bits on wire or radio | cables, radio | Wiretapping, jamming |
When you send data, each layer wraps the payload in its own header. This is called encapsulation. The receiver unwraps the layers in reverse order.
def encapsulate(payload):
http = f"[HTTP GET /index.html]{payload}"
tcp = f"[TCP 51000->80]{http}"
ip = f"[IP 192.168.1.20->203.0.113.80]{tcp}"
frame = f"[ETH 3c:22:fb:10:9a:01->00:1a:2b:3c:4d:5e]{ip}"
return frame
def decapsulate(frame):
layers = []
while frame.startswith("["):
header, frame = frame[1:].split("]", 1)
layers.append(header)
return layers, frame
frame = encapsulate("")
print(frame)
layers, rest = decapsulate(frame)
for depth, header in enumerate(layers):
print(" " * depth + header)[ETH 3c:22:fb:10:9a:01->00:1a:2b:3c:4d:5e][IP 192.168.1.20->203.0.113.80][TCP 51000->80][HTTP GET /index.html]
ETH 3c:22:fb:10:9a:01->00:1a:2b:3c:4d:5e
IP 192.168.1.20->203.0.113.80
TCP 51000->80
HTTP GET /index.html
Notice that the outer Ethernet header changes at every router hop (new MAC addresses), while the IP header stays the same from start to end.
Ports
An IP address identifies a host; a port (0–65535) identifies a program on that host. A web server listens on port 443, an SSH server on 22. The pair (IP, port) is called a socket address, and a connection is identified by both ends.
| Port | Service | Note |
|---|---|---|
| 22 | SSH | Remote shell; brute-force target |
| 25 | SMTP | Mail between servers |
| 53 | DNS | Name lookups |
| 80 / 443 | HTTP / HTTPS | Web; 443 is encrypted |
| 3306 | MySQL | Should never face the internet |
| 3389 | RDP | Windows remote desktop; frequent ransomware entry point |
Ports below 1024 are "well known"; client programs use random high ports (like 51000 above) for their side of a connection.
Building a packet header by hand
Network headers are just bytes in a fixed layout. Python's struct module packs and unpacks such layouts. Here we build a simplified UDP header (source port, destination port, length, checksum: four 16-bit numbers in network byte order, which is big-endian):
import struct
payload = b"hello"
header = struct.pack("!HHHH", 51000, 53, 8 + len(payload), 0)
packet = header + payload
print(len(header), "byte header")
print(packet.hex(" "))
src, dst, length, checksum = struct.unpack("!HHHH", packet[:8])
print(f"src port={src} dst port={dst} length={length}")
print("payload:", packet[8:].decode())8 byte header
c7 38 00 35 00 0d 00 00 68 65 6c 6c 6f
src port=51000 dst port=53 length=13
payload: hello
The ! in the format string means "network byte order". Getting byte order wrong is a classic source of parsing bugs, and parsing bugs in network code are a classic source of vulnerabilities.
LAN, WAN and the internet
- A LAN (local area network) is a home or office network. Hosts usually get private addresses such as
192.168.x.x. - A WAN connects LANs across distance. The internet is the largest WAN.
- NAT (network address translation) on your router lets many private hosts share one public address. As a side effect, outsiders cannot start connections to your hosts unless a port is forwarded.
Tip: NAT is not a firewall, but it hides internal hosts. Exposing a service with port forwarding removes that protection, so only forward what you must.
Go further: Networking for Security reads the same layers from real packet bytes and captures, with each attack placed on the layer it abuses.
Test yourself
Which address does a switch use to deliver traffic inside a local network?
- IP address
- MAC address
- Port number
- Domain name
At which OSI layer do TCP and UDP operate?
- Layer 2
- Layer 3
- Layer 4
- Layer 7
What does ! mean in the struct format string "!HHHH"?
- Ignore errors
- Network (big-endian) byte order
- Signed numbers
- Pad to 8 bytes