Microsegmentation and Flow Policies
Zones stop a phished workstation from reaching the database directly. They don't stop an attacker who is already inside a zone: in Inadequate Segmentation, the compromised file server reached the database's SSH because servers in the same zone could talk freely. Microsegmentation takes the policy down to individual workloads: each server, VM, container or service gets its own allow list, enforced as close to it as possible (host firewall, hypervisor switch, cloud security group, Kubernetes network policy). The unit of policy becomes "the billing API may talk to the billing database on 5432", not "the server VLAN may talk to the server VLAN".
Policy from observed flows
Nobody can write workload-level rules from memory for hundreds of services. The practical method is to observe the flows for a few weeks (flow logs, Zeek conn.log, cloud VPC flow logs), group endpoints by role labels, propose a policy from what legitimately happens, review it with the owners, and then enforce it. The program does those steps on a small synthetic flow log of the lab server zone.
import pandas as pd
labels = {"10.10.2.10": "web", "10.10.2.11": "web", "10.10.2.30": "db", "10.10.2.60": "files",
"10.10.2.25": "mail", "10.10.2.53": "dns", "10.10.5.21": "admin", "10.10.2.40": "backup"}
flows = pd.DataFrame([
# src, dst, dport, connections over 14 days
("10.10.2.10", "10.10.2.30", 5432, 81234), ("10.10.2.11", "10.10.2.30", 5432, 79012),
("10.10.2.10", "10.10.2.53", 53, 40211), ("10.10.2.30", "10.10.2.53", 53, 2201),
("10.10.2.60", "10.10.2.53", 53, 1890), ("10.10.2.25", "10.10.2.53", 53, 5120),
("10.10.2.40", "10.10.2.30", 5432, 14), ("10.10.2.40", "10.10.2.60", 445, 14),
("10.10.5.21", "10.10.2.30", 22, 9), ("10.10.5.21", "10.10.2.10", 22, 12),
("10.10.2.60", "10.10.2.30", 22, 3), # one-off: files -> db SSH
], columns=["src", "dst", "dport", "conns"])
flows["src_role"] = flows.src.map(labels)
flows["dst_role"] = flows.dst.map(labels)
policy = (flows.groupby(["src_role", "dst_role", "dport"], as_index=False)["conns"].sum()
.sort_values(["dst_role", "dport", "src_role"]))
print("proposed allow list (role -> role : port, connections in 14 days):")
for r in policy.itertuples():
note = " <- review: rare, unexpected" if r.conns < 5 else ""
print(f" {r.src_role:7} -> {r.dst_role:6} : {r.dport:<5} {r.conns:>7}{note}")
roles = sorted(set(labels.values()))
possible = len(roles) * (len(roles) - 1)
print(f"{len(policy)} role pairs with traffic out of {possible} possible ordered pairs; the rest is denied")proposed allow list (role -> role : port, connections in 14 days):
admin -> db : 22 9
files -> db : 22 3 <- review: rare, unexpected
backup -> db : 5432 14
web -> db : 5432 160246
db -> dns : 53 2201
files -> dns : 53 1890
mail -> dns : 53 5120
web -> dns : 53 40211
backup -> files : 445 14
admin -> web : 22 12
10 role pairs with traffic out of 42 possible ordered pairs; the rest is denied
The proposal is small: 10 role-to-role flows (of 42 possible ordered pairs of seven roles, before even counting ports). One line stands out: files → db on 22, three connections in two weeks. No legitimate role needs it; the owners confirm it was a test, and it is left out of the policy. If those connections came from an attacker, the observation period would have legitimised them, which is the method's main risk: review every proposed rule, especially rare ones, and prefer flows that match a documented need.
The policy as labels
Written with labels instead of addresses, the policy survives redeployments, autoscaling and IP changes. In Kubernetes the same idea is a NetworkPolicy (enforced by the cluster's network plugin; without one that supports policies, the object does nothing):
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: db-allow-web
namespace: shop
spec:
podSelector:
matchLabels: { role: db }
policyTypes: [Ingress]
ingress:
- from:
- podSelector:
matchLabels: { role: web }
ports:
- protocol: TCP
port: 5432With this policy selecting the db pods, all ingress to them is denied except TCP 5432 from pods labelled role: web in the same namespace. Kubernetes network security in depth is in the planned Cloud & Container Security course; cloud security groups work similarly with group references (iptables, ufw, firewalld and Cloud Security Groups).
Checking flows against the policy
Once enforced (or while still in "monitor only" mode, which most products offer), every new flow is checked against the policy. Violations are both blocked and reported.
ALLOW = {("web", "db", 5432), ("web", "dns", 53), ("db", "dns", 53), ("files", "dns", 53),
("mail", "dns", 53), ("backup", "db", 5432), ("backup", "files", 445),
("admin", "db", 22), ("admin", "web", 22)}
labels = {"10.10.2.10": "web", "10.10.2.30": "db", "10.10.2.60": "files", "10.10.2.53": "dns",
"10.10.5.21": "admin", "10.10.2.40": "backup"}
new_flows = [("10.10.2.10", "10.10.2.30", 5432), ("10.10.2.60", "10.10.2.30", 22),
("10.10.2.30", "10.10.2.10", 8080), ("10.10.2.10", "10.10.2.60", 445),
("10.10.5.21", "10.10.2.30", 22), ("10.10.2.30", "203.0.113.50", 443)]
for src, dst, port in new_flows:
key = (labels.get(src, "unlabelled"), labels.get(dst, "external"), port)
verdict = "allow" if key in ALLOW else "DENY + alert"
print(f"{src:>11} -> {dst:<13}:{port:<5} {key[0]:>6} -> {key[1]:<8} {verdict}") 10.10.2.10 -> 10.10.2.30 :5432 web -> db allow
10.10.2.60 -> 10.10.2.30 :22 files -> db DENY + alert
10.10.2.30 -> 10.10.2.10 :8080 db -> web DENY + alert
10.10.2.10 -> 10.10.2.60 :445 web -> files DENY + alert
10.10.5.21 -> 10.10.2.30 :22 admin -> db allow
10.10.2.30 -> 203.0.113.50 :443 db -> external DENY + alert
The database starting a connection to the web server's 8080, the web server trying SMB on the file server, and the database talking to an Internet address are all denied and alerted. Each is a classic sign of lateral movement or exfiltration, and each would have been allowed inside a plain server zone.
Rolling it out without breaking things
Most products offer a monitor (or "audit") mode: the policy is evaluated on every flow and violations are logged, but nothing is blocked. Run it for a full business cycle, fix the policy where legitimate flows show up as violations, then switch one application at a time to enforcement, starting with the most sensitive data stores. Keep a fast rollback (a single switch back to monitor mode) for the first days, and tell the application owners when their service changes mode.
Where it is enforced
| Enforcement point | Pros | Cons |
|---|---|---|
| Host firewall (nftables, Windows Firewall) managed centrally | works everywhere, sees process context | a root-level attacker on the host can disable it |
| Hypervisor / virtual switch (distributed firewall) | outside the guest, survives guest compromise | tied to the virtualisation platform |
| Cloud security groups | native, reference other groups by ID | per-cloud, coarse logging |
| Kubernetes network policies, service mesh (mTLS + authorisation) | label- and identity-based | needs a policy-capable network plugin; mesh adds complexity |
Pitfalls
- Learning mode forever: observing without ever enforcing.
- Learning from compromised traffic: rare flows need human review.
- Forgetting dependencies that appear monthly or yearly (backups, reports, certificate renewal): observe for a full cycle or ask the owners.
- Policy explosion: per-IP rules instead of labels quickly become unmaintainable.
Practice
1. Why is backup -> db : 5432 with 14 connections not flagged, while files -> db : 22 with 3 is?
The threshold in the program is 5 connections; 14 daily backup runs pass it. More importantly, a backup role reading the database is a documented need, while a file server opening SSH to the database is not. The count is a hint; the review decides.
2. Add the flow ("10.10.2.40", "10.10.2.30", 22) to new_flows. What happens, and is that right?
backup -> db : 22 is not in ALLOW, so it is denied and alerted. Correct, unless the backup system really uses SSH, in which case the owner adds the rule with a reason.
3. A NetworkPolicy exists but pods still reach the database from anywhere. Name a likely cause.
The cluster's network plugin doesn't enforce NetworkPolicies, or the policy's podSelector labels don't match the database pods (a typo in role: db), or the policy is in a different namespace.
Test yourself
What does microsegmentation add to zone segmentation?
- Faster routing
- Per-workload allow lists, so hosts inside one zone can't talk freely
- More VLANs
- Encryption
What is the main risk of building policy from observed flows?
- It is too slow
- Malicious or one-off flows seen during observation get legitimised
- It needs IPv6
- It can't use labels
Why write policies with labels or roles instead of IP addresses?
- Labels are encrypted
- They survive redeployments, scaling and IP changes and stay readable
- IP addresses can't be filtered
- Labels are faster