Skip to content
Hands-on IT IT Drills

Day 8: Flat Network and Lateral Movement

Goal: You stop the lateral movement from the attacker Pod to the database with default deny plus targeted allow rules. · Duration: approx. 70 min · Prerequisite: Day 1

Briefing

In Kubernetes, by default every Pod may reach every other – across all namespaces. A compromised Pod in attacker talks to the database in shop as if they belonged together. That is lateral movement: the jump from the first Pod taken over to the actual target. The defense is a NetworkPolicy – first close everything (Default Deny), then open only the paths the application really needs.

   attacker/shell  --X-->  shop/db        (should be blocked)
   shop/frontend   ----->  shop/api  ---->  shop/db   (should be allowed)

Drill 1: The Misconfiguration – No Default Deny

The starting state from Day 1, armed once more: the attacker reaches the DB.

kubectl -n attacker exec shell -- nc -zv -w3 db.shop.svc 5432

Expected:

Connection to db.shop.svc (10.43.161.163) 5432 port [tcp/postgresql] succeeded!

Save your progress? Sign up for free

Drill 2: First Step – Close Everything

A NetworkPolicy with an empty podSelector hits all Pods of the namespace; without an ingress rule, all incoming traffic is thereby forbidden:

kubectl -n shop apply -f - <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: {name: default-deny-ingress, namespace: shop}
spec:
  podSelector: {}
  policyTypes: ["Ingress"]
EOF

The attacker is locked out:

kubectl -n attacker exec shell -- nc -zv -w4 db.shop.svc 5432

Expected:

command terminated with exit code 1

But watch out – now the application itself is blocked too:

FE=$(kubectl -n shop get pod -l app=frontend -o jsonpath='{.items[0].metadata.name}')
kubectl -n shop exec $FE -- curl -s -o /dev/null -w "%{http_code}\n" --max-time 4 \
  http://api.shop.svc:80

Expected:

command terminated with exit code 28

28 is the curl timeout: default deny really does hit everyone. That is why the second step follows immediately.

Save your progress? Sign up for free

Drill 3: Fix – Open Only the Necessary Paths

Allow exactly frontend → api and api → db, each on the right port:

kubectl -n shop apply -f - <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: {name: allow-frontend-to-api, namespace: shop}
spec:
  podSelector: {matchLabels: {app: api}}
  policyTypes: ["Ingress"]
  ingress:
  - from: [{podSelector: {matchLabels: {app: frontend}}}]
    ports: [{protocol: TCP, port: 80}]
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: {name: allow-api-to-db, namespace: shop}
spec:
  podSelector: {matchLabels: {app: db}}
  policyTypes: ["Ingress"]
  ingress:
  - from: [{podSelector: {matchLabels: {app: api}}}]
    ports: [{protocol: TCP, port: 5432}]
EOF

Why podSelector instead of namespaceSelector? NetworkPolicies select sources via labels, not via IPs. app: frontend is the contract – whoever carries this label may; all others may not.

Save your progress? Sign up for free

Drill 4: Proof – Legitimate Works, Attacker Stays Out

FE=$(kubectl -n shop get pod -l app=frontend -o jsonpath='{.items[0].metadata.name}')
echo -n "frontend->api: "; kubectl -n shop exec $FE -- \
  curl -s -o /dev/null -w "%{http_code}\n" --max-time 5 http://api.shop.svc:80
echo -n "attacker->db:  "; kubectl -n attacker exec shell -- \
  nc -zv -w4 db.shop.svc 5432 2>&1 | tail -1
echo -n "attacker->api: "; kubectl -n attacker exec shell -- \
  curl -s -o /dev/null -w "code=%{http_code}\n" --max-time 4 http://api.shop.svc:80

Expected:

frontend->api: 200
attacker->db:  command terminated with exit code 1
attacker->api: command terminated with exit code 28

The application works, the lateral jump is dead. These policies stay – from now on they are the hardened state of shop.

Save your progress? Sign up for free

Checkpoint

The attacker no longer reaches the DB:

kubectl -n attacker exec shell -- nc -zv -w4 db.shop.svc 5432; echo "rc=$?"

Expected: rc=1

The legitimate path stands:

FE=$(kubectl -n shop get pod -l app=frontend -o jsonpath='{.items[0].metadata.name}')
kubectl -n shop exec $FE -- curl -s -o /dev/null -w "%{http_code}\n" --max-time 5 \
  http://api.shop.svc:80

Expected: 200

Challenge

Default deny for Ingress keeps attackers away from shop. But a compromised shop Pod could still phone out (exfiltration, C2). Add a default-deny Egress rule that allows only DNS, and verify that arbitrary outbound traffic then fails.

Hint: DNS runs over UDP/TCP 53 to CoreDNS in kube-system (k8s-app: kube-dns). Without this exception, not even name resolution works.

Solution

kubectl -n shop apply -f - <<'EOF'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: {name: default-deny-egress, namespace: shop}
spec:
  podSelector: {}
  policyTypes: ["Egress"]
  egress:
  - to:
    - namespaceSelector: {matchLabels: {kubernetes.io/metadata.name: kube-system}}
      podSelector: {matchLabels: {k8s-app: kube-dns}}
    ports: [{protocol: UDP, port: 53}, {protocol: TCP, port: 53}]
EOF

FE=$(kubectl -n shop get pod -l app=frontend -o jsonpath='{.items[0].metadata.name}')
kubectl -n shop exec $FE -- curl -s -o /dev/null -w "%{http_code}\n" --max-time 4 \
  https://example.com

Expected:

command terminated with exit code 28

A curl timeout – arbitrary outbound traffic is shut, only DNS would remain allowed. (For the following days, clean this egress rule up again: kubectl -n shop delete networkpolicy default-deny-egress.)

Save your progress? Sign up for free

Enterprise Note

  • Default deny (Ingress and Egress) per namespace is the baseline; Kyverno can generate it automatically for each new namespace (Day 12).
  • Policies need a CNI that enforces them – RKE2 ships Canal (Calico). Flannel alone silently ignores NetworkPolicies.
  • For L7 rules (only GET /health), FQDN egress and visibility (which traffic was dropped?) see Cilium/Hubble – the topic of the course “Kyverno & NetworkPolicies”.
  • Label namespaces consistently; namespaceSelector is only as reliable as the labels it points to.