متقدم
البحث فيالمعامل
طريقة البحث
  • ابحث عن مهمة أو أداة (lvm، selinux، ‎@podman)؛ وصفِّ حسب المستوى والكتاب أو الدورة.
  • مع مربع بحث فارغ، الفلتر وحده يعرض المعامل — مثلًا كل المعامل الصعبة لدورة واحدة.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

معمل 9.1CKS, الفصل 9

Turn on audit logging and catch a shell in a container with Falco

المستوى
صعب
60 دقيقة
الأجهزة
a kubeadm cluster where you can edit the API server manifest, Helm 3
الحالة
لم يبدأ

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

الهدف

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.

قبل أن تبدأ

المهام

  1. Write /etc/kubernetes/audit/policy.yaml: log Metadata for secrets, RequestResponse for pods/exec, nothing for the noisy get on endpoints and events, Metadata for the rest.
  2. 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.
  3. Install Falco with Helm (modern eBPF driver) and follow its logs.
  4. Play the attacker: kubectl exec into the shop pod with a shell, read /etc/shadow, and use kubectl get secret db-pass -o yaml.
  5. Find the Falco alert and the two audit events (who, when, from which IP, which object).
  6. Make the deployment immutable: read-only root file system with emptyDir for the nginx temp folders, and no shell use possible as root.

تحقّق من عملك

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 get on secrets/db-pass and create on pods/exec.
  • Falco prints Terminal shell in container with the pod name and the command.
  • The deployment shows readOnlyRootFilesystem: true; kubectl exec ... -- touch /x fails with Read-only file system.

تلميحات

اعرض التلميحات
  • 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 None rules first.
  • If the API server does not come back, read crictl ps -a and crictl logs of the kube-apiserver container; usually a path or a volume mount is wrong.
  • helm repo add falcosecurity https://falcosecurity.github.io/charts and --set driver.kind=modern_ebpf.

الحل

اعرض الحل
  1. 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
  2. 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}}
    watch crictl ps
    kubectl get nodes
  3. Falco.
    helm repo add falcosecurity https://falcosecurity.github.io/charts && helm repo update
    helm install falco falcosecurity/falco -n falco --create-namespace --set driver.kind=modern_ebpf --set tty=true
    kubectl -n falco logs ds/falco -f
  4. The attack (in a second terminal).
    kubectl exec -it deploy/shop -- bash -c "cat /etc/shadow | head -2"
    kubectl get secret db-pass -o yaml
  5. Investigate: run the commands of Check your work. Write down user, time, source IP, object and command — that is your incident timeline.
  6. 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

ملاحظاتي

لا توجد ملاحظات بعد.

دروس هذا المعمل

الأوامر في هذا المعمل