Network Threats & Defences
Networks are attacked because they connect everything. This lesson explains the most common network-level threats in plain terms and then the three classic defences: firewalls, segmentation and intrusion detection. You will build small models of each.
Common network threats
| Threat | What happens | Why it works | Main defences |
|---|---|---|---|
| Eavesdropping (sniffing) | Someone on the path reads traffic | Plain-text protocols, shared WiFi | Encrypt everything (TLS, SSH, VPN) |
| Man-in-the-middle | Traffic is silently relayed and possibly changed | Unauthenticated protocols such as ARP and plain DNS | TLS with certificate checks, Dynamic ARP Inspection |
| Spoofing | Packets or frames carry a fake sender address | IP and Ethernet don't authenticate senders | Ingress filtering, authenticated protocols |
| Denial of service (DoS/DDoS) | A service is flooded until it fails | Limited bandwidth, CPU or connection tables | Rate limiting, SYN cookies, CDN/DDoS scrubbing |
| Exposed services | Admin interfaces or databases reachable from the internet | Missing firewall rules, port forwarding | Default-deny firewall, VPN, MFA |
| Lateral movement | One compromised machine is used to reach others | Flat networks where everything can talk to everything | Segmentation, internal firewalls, zero trust |
Notice how many defences are "encrypt and authenticate" or "don't let it be reachable". Those two ideas carry most of network security.
Firewalls
A firewall decides, for each connection, whether to allow or drop it. Packet-filtering rules match on fields like source address, destination port and protocol. Most firewalls use first match wins: rules are checked from top to bottom and the first matching rule decides. If nothing matches, a default policy applies, and the safe default is deny.
Here is a small, faithful model of a first-match firewall, using ipaddress so that networks are compared correctly:
import ipaddress
RULES = [
# (action, source network, destination port, protocol)
("allow", "0.0.0.0/0", 443, "tcp"), # public HTTPS
("allow", "10.0.5.0/24", 22, "tcp"), # SSH only from the admin subnet
("deny", "198.51.100.0/24", None, "any"), # known bad range
("allow", "10.0.0.0/8", 53, "udp"), # internal DNS
]
DEFAULT = "deny"
def decide(src, port, proto):
ip = ipaddress.ip_address(src)
for number, (action, net, rport, rproto) in enumerate(RULES, start=1):
if ip not in ipaddress.ip_network(net):
continue
if rport is not None and rport != port:
continue
if rproto != "any" and rproto != proto:
continue
return action, f"rule {number}"
return DEFAULT, "default"
tests = [
("203.0.113.9", 443, "tcp"),
("203.0.113.9", 22, "tcp"),
("10.0.5.17", 22, "tcp"),
("10.0.9.4", 22, "tcp"),
("198.51.100.7", 443, "tcp"),
("10.2.3.4", 53, "udp"),
]
for src, port, proto in tests:
action, why = decide(src, port, proto)
print(f"{src:<14} {proto}/{port:<4} -> {action:<5} ({why})")203.0.113.9 tcp/443 -> allow (rule 1)
203.0.113.9 tcp/22 -> deny (default)
10.0.5.17 tcp/22 -> allow (rule 2)
10.0.9.4 tcp/22 -> deny (default)
198.51.100.7 tcp/443 -> allow (rule 1)
10.2.3.4 udp/53 -> allow (rule 4)
The admin SSH connection from 10.0.5.17 matches rule 2, while SSH from any other address falls through to the default deny. But look at 198.51.100.7: it belongs to the "known bad range", yet it is allowed to reach HTTPS, because rule 1 matched before the deny rule was ever checked. Rule order matters. A block list must come before the broad allow rules it is meant to override. Such a shadowed rule is one of the most common firewall mistakes, and audit tools look for it.
Tip: Write firewall rules as "allow exactly what is needed, deny everything else". A rule list that ends in "allow any" is not a firewall.
Stateful firewalls
Modern firewalls are stateful: they remember connections that were allowed, so the replies flow back without a separate rule. That means you only write rules for who may start a connection. A home router lets your laptop open connections out but drops unsolicited inbound ones for exactly this reason.
Segmentation
Segmentation splits a network into zones (servers, staff, guests, IoT, payment systems) and controls traffic between them. If an attacker compromises a smart TV in the guest zone, segmentation stops them reaching the finance database. A zone policy is simply a list of allowed zone-to-zone flows:
ALLOWED = {
("staff", "servers"): [443, 22],
("servers", "database"): [5432],
("guest", "internet"): [80, 443],
("staff", "internet"): [80, 443],
}
def flow_allowed(src_zone, dst_zone, port):
return port in ALLOWED.get((src_zone, dst_zone), [])
checks = [
("staff", "servers", 443),
("guest", "servers", 443),
("servers", "database", 5432),
("staff", "database", 5432),
("guest", "internet", 443),
]
for src, dst, port in checks:
verdict = "allow" if flow_allowed(src, dst, port) else "deny"
print(f"{src:>7} -> {dst:<9} :{port:<5} {verdict}") staff -> servers :443 allow
guest -> servers :443 deny
servers -> database :5432 allow
staff -> database :5432 deny
guest -> internet :443 allow
Staff can't reach the database directly: only the application servers can. Taken further, this becomes zero trust: no device is trusted because of where it sits on the network; every request is authenticated and authorised.
Intrusion detection and prevention
An IDS (intrusion detection system) watches traffic or logs and raises alerts; an IPS sits inline and can block. They use two approaches:
- Signature-based: match known-bad patterns (a specific exploit string). Precise, but blind to new attacks.
- Anomaly-based: learn what normal looks like and flag deviations. Catches new behaviour, but produces more false positives.
A simple anomaly rule flags any source whose connection count in a time window is far above the usual baseline:
connections_per_minute = {
"10.0.1.15": 12,
"10.0.1.22": 9,
"10.0.1.31": 14,
"10.0.1.40": 480,
"10.0.1.52": 11,
}
baseline = sorted(connections_per_minute.values())[len(connections_per_minute) // 2]
threshold = baseline * 10
print("median baseline:", baseline, "threshold:", threshold)
for host, count in connections_per_minute.items():
if count > threshold:
print(f"ALERT {host}: {count} connections/min")median baseline: 12 threshold: 120
ALERT 10.0.1.40: 480 connections/min
An internal laptop suddenly opening hundreds of connections a minute may be infected malware probing the network, or a misbehaving backup job. The alert doesn't decide which: a human (or a playbook) investigates. You'll do more of this in the logging and SOC lessons.
Go further: Network Security goes deeper into firewalls, segmentation, zero trust and IDS, starting from Stateless versus Stateful Filtering.
Test yourself
In a first-match firewall, a deny rule for a bad network is placed below an 'allow any to port 443' rule. What happens to HTTPS traffic from the bad network?
- It is denied
- It is allowed, because the allow rule matches first
- The firewall reports an error
- It depends on the default policy
What is the safe default policy for a firewall?
- Allow all
- Deny all that isn't explicitly allowed
- Allow internal, deny external
- Log only
Which detection approach can catch a brand-new attack it has never seen?
- Signature-based
- Anomaly-based
- Neither
- Only an IPS