Slide 1

Slide 1 text

Secure your Kubernetes Containers Hossam Barakat Lead Consultant at Telstra Purple @hossambarakat_

Slide 2

Slide 2 text

@hossambarakat_ 3

Slide 3

Slide 3 text

@hossambarakat_ Attack Vectors 4 OS Application

Slide 4

Slide 4 text

@hossambarakat_ Attack Vectors 5 OS Kubernetes Container Image Container Application

Slide 5

Slide 5 text

@hossambarakat_ 6

Slide 6

Slide 6 text

@hossambarakat_ Kubernetes Cluster 7

Slide 7

Slide 7 text

@hossambarakat_ Kubernetes Architecture Master Worker Worker Client Worker Cluster

Slide 8

Slide 8 text

@hossambarakat_ Kubernetes Architecture Master API Server Worker Kubelet Container Runtime UI (Dashboard) CLI (Kubectl) Other Client(s) Pod Pod Cluster Scheduler TLS TLS

Slide 9

Slide 9 text

@hossambarakat_ Role Based Access Control (RBAC) 10 Role Binding Role Resource User Group Service Account Verb Verb Subject

Slide 10

Slide 10 text

@hossambarakat_ Service Account 11 apiVersion: v1 kind: ServiceAccount metadata: name: webapp-service-account namespace: default

Slide 11

Slide 11 text

@hossambarakat_ Role 12 Role Based Access Control (RBAC) Role Binding kind: Role apiVersion: rbac.authorization.k8s.io/v1 metadata: name: my-role namespace: default rules: - apiGroups: [""] resources: ["pods"] verbs: ["get", "list"] kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: my-role-binding namespace: default subjects: - kind: ServiceAccount name: webapp-service-account namespace: default roleRef: kind: Role name: my-role apiGroup: rbac.authorization.k8s.io

Slide 12

Slide 12 text

@hossambarakat_ Pod 13 Role Based Access Control (RBAC) kind: Pod apiVersion: v1 metadata: name: webapp spec: serviceAccountName: webapp-service-account containers: - name: webapp image: hossambarakat/k8s-security-webapp ports: - containerPort: 3000

Slide 13

Slide 13 text

@hossambarakat_ CIS Kubernetes Benchmark » Document that provide guidance for establishing a secure configuration posture for Kubernetes » Specific recommendations with a description, rationale, method of audit and remediation » Can be automated with kube-bench 14

Slide 14

Slide 14 text

@hossambarakat_ Container Images 15

Slide 15

Slide 15 text

@hossambarakat_ Images Security » Never run as root • Set USER in Dockerfile » Minimal base image • Alpine 2 MB • Ubuntu 60 MB » Trusted base image » Private image registry » Do NOT use latest tag » Vulnerability scans 16

Slide 16

Slide 16 text

@hossambarakat_ Image Scanning Tools » aquasecurity/trivy » coreos/clair » optiopay/klar » aquasecurity/microscanner » Aqua Security » Twistlock 17

Slide 17

Slide 17 text

@hossambarakat_ Trivy 18

Slide 18

Slide 18 text

@hossambarakat_ Vulnerability Scanning CI Pipeline Integration 19 Code CI Vulnerability Scanning Image Registry Schedule Container

Slide 19

Slide 19 text

@hossambarakat_ Vulnerability Scanning CI Pipeline Integration 20 Code CI Vulnerability Scanning Image Registry Schedule Container Publish Scanning Results Is Scanned Image? Admission Webhook

Slide 20

Slide 20 text

@hossambarakat_ Containers 21

Slide 21

Slide 21 text

@hossambarakat_ Privilege Escalation 22 Pod Worker Container Modify container file system Modify host file system Crypto Miner Hacker icon by karina from the Noun Project

Slide 22

Slide 22 text

@hossambarakat_ Demo 23 Privilege Escalation

Slide 23

Slide 23 text

@hossambarakat_ » RunAsUser » RunAsGroup 24 Security Context securityContext: runAsUser: 1000 runAsGroup: 3000

Slide 24

Slide 24 text

@hossambarakat_ » AllowPrivilegdeEscalation 25 Security Context securityContext: allowPrivilegeEscalation: false

Slide 25

Slide 25 text

@hossambarakat_ » ReadOnlyRootFilesystem 26 Security Context securityContext: readOnlyRootFilesystem: true

Slide 26

Slide 26 text

@hossambarakat_ » RunAsUser » RunAsGroup » AllowPrivilegdeEscalation » ReadOnlyRootFilesystem 27 Security Context apiVersion: v1 kind: Pod metadata: name: my-app spec: securityContext: runAsUser: 1000 RunAsGroup: 2000 containers: - name: my-app image: my-app securityContext: allowPrivilegeEscalation: false readOnlyRootFilesystem: true

Slide 27

Slide 27 text

@hossambarakat_ Enter Pod Security Policy 28

Slide 28

Slide 28 text

@hossambarakat_ Pod Security Policy » A Pod Security Policy is a cluster-level resource that controls the actions that a pod can perform and what it has the ability to access. » The PodSecurityPolicy objects define a set of conditions that a pod must run with in order to be accepted into the system. 29

Slide 29

Slide 29 text

@hossambarakat_ Pod Security Policy » privileged » volumes » fsGroup » runAsUser, runAsGroup » readOnlyRootFilesystem » allowedHostPaths » hostNetwork » Linux capabilities 30

Slide 30

Slide 30 text

@hossambarakat_ kube-psp-advisor 31

Slide 31

Slide 31 text

@hossambarakat_ » All pods can communicate with each other 32 Network Communication Pod Pod Pod

Slide 32

Slide 32 text

@hossambarakat_ App 1 33 Network Communication Frontend DB Pod apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: my-frontend-policy spec: podSelector: matchLabels: app: db ingress: - from: - podSelector: matchLabels: app: frontend

Slide 33

Slide 33 text

@hossambarakat_ Network Plugin » Calico » Cilium » Kube-Router » Weave Net » … 34

Slide 34

Slide 34 text

@hossambarakat_ 35

Slide 35

Slide 35 text

@hossambarakat_ Service Mesh » Security specific policy enforcement » End-to-end encryption » Rolling certificates 38

Slide 36

Slide 36 text

@hossambarakat_ Summary 39 Kubernetes Cluster Bootstrap TLS Authentication Enable RBAC CIS Benchmark Container Images No root user Small images Do NOT use latest Private Image Registry Containers Pod Security Context Pod Security Policy Network Policy Service Mesh Vulnerability Scans

Slide 37

Slide 37 text

@hossambarakat_ Resources » https://kubernetes-security.info » http://github.com/hossambarakat/secure-k8s-containers 40

Slide 38

Slide 38 text

@hossambarakat_ Questions? #2019addo-devsecops 41

Slide 39

Slide 39 text

@hossambarakat_ 42

Slide 40

Slide 40 text

Thank You Hossam Barakat @hossambarakat_