Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
Doel
An attacker gets a shell in a pod and reads a secret. You make both steps visible: an audit policy that logs who reads secrets and who uses exec, and Falco that alerts when a shell starts inside a container. Then you investigate like an incident responder and make the pod immutable.
Voordat je begint
- A kubeadm control plane node with root access (the API server is a static pod).
- Helm 3 for Falco. Make a test app:
kubectl create deploy shop --image=nginx:1.27 && kubectl create secret generic db-pass --from-literal=pw=lab.
Taken
Vink een taak aan als hij klaar is. Alleen je browser onthoudt dit.
- Write
/etc/kubernetes/audit/policy.yaml: logMetadatafor secrets,RequestResponseforpods/exec, nothing for the noisygeton endpoints and events,Metadatafor the rest. - Turn on auditing in the API server: policy file, log path
/var/log/kubernetes/audit/audit.log, keep 30 days and 5 files of 100 MB. Mount both folders into the static pod. - Install Falco with Helm (modern eBPF driver) and follow its logs.
- Play the attacker:
kubectl execinto theshoppod with a shell, read/etc/shadow, and usekubectl get secret db-pass -o yaml. - Find the Falco alert and the two audit events (who, when, from which IP, which object).
- Make the deployment immutable: read-only root file system with
emptyDirfor the nginx temp folders, and no shell use possible as root.
Controleer je werk
tail -n 50 /var/log/kubernetes/audit/audit.log | jq -c 'select(.objectRef.resource=="secrets" or .objectRef.subresource=="exec") | {user: .user.username, verb, res: .objectRef.resource, name: .objectRef.name, ip: .sourceIPs[0]}'
kubectl -n falco logs ds/falco | grep -i "shell was spawned"
kubectl get deploy shop -o jsonpath='{.spec.template.spec.containers[0].securityContext}'- The audit log shows your user with
getonsecrets/db-passandcreateonpods/exec. - Falco prints Terminal shell in container with the pod name and the command.
- The deployment shows
readOnlyRootFilesystem: true;kubectl exec ... -- touch /xfails with Read-only file system.
Hints
Toon de hints
- API server flags:
--audit-policy-file,--audit-log-path,--audit-log-maxage,--audit-log-maxbackup,--audit-log-maxsize. - The rules in an audit policy are checked from top to bottom; the first match wins. Put the
Nonerules first. - If the API server does not come back, read
crictl ps -aandcrictl logsof the kube-apiserver container; usually a path or a volume mount is wrong. helm repo add falcosecurity https://falcosecurity.github.io/chartsand--set driver.kind=modern_ebpf.
Oplossing
Toon de oplossing
- The policy./etc/kubernetes/audit/policy.yaml
apiVersion: audit.k8s.io/v1 kind: Policy omitStages: [RequestReceived] rules: - level: None verbs: [get, list, watch] resources: - group: "" resources: [endpoints, events] - level: Metadata resources: - group: "" resources: [secrets, configmaps] - level: RequestResponse resources: - group: "" resources: [pods/exec, pods/attach] - level: Metadata - API server changes in
/etc/kubernetes/manifests/kube-apiserver.yaml.kube-apiserver.yaml (parts)spec: containers: - command: - kube-apiserver - --audit-policy-file=/etc/kubernetes/audit/policy.yaml - --audit-log-path=/var/log/kubernetes/audit/audit.log - --audit-log-maxage=30 - --audit-log-maxbackup=5 - --audit-log-maxsize=100 volumeMounts: - {name: audit-policy, mountPath: /etc/kubernetes/audit, readOnly: true} - {name: audit-log, mountPath: /var/log/kubernetes/audit} volumes: - {name: audit-policy, hostPath: {path: /etc/kubernetes/audit, type: DirectoryOrCreate}} - {name: audit-log, hostPath: {path: /var/log/kubernetes/audit, type: DirectoryOrCreate}} - Falco.
- The attack (in a second terminal).
- Investigate: run the commands of Check your work. Write down user, time, source IP, object and command — that is your incident timeline.
- Make it immutable.
kubectl patch deploy shop --type json -p '[{"op":"add","path":"/spec/template/spec/containers/0/securityContext","value":{"readOnlyRootFilesystem":true,"allowPrivilegeEscalation":false}},{"op":"add","path":"/spec/template/spec/volumes","value":[{"name":"cache","emptyDir":{}},{"name":"run","emptyDir":{}}]},{"op":"add","path":"/spec/template/spec/containers/0/volumeMounts","value":[{"name":"cache","mountPath":"/var/cache/nginx"},{"name":"run","mountPath":"/var/run"}]}]' kubectl exec deploy/shop -- touch /x touch: cannot touch '/x': Read-only file system
Mijn notities
Nog geen notities.