Back home
中文
H7 / SECURITY RESEARCH NOTES

Using Kubectl to Create a Scheduled Task in a Kubernetes Cluster Container

Environment Description

IPHOSTNAMENOTE
10.10.10.91CentOS7Host

Environment Setup

Install kubectl

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x ./kubectl
mv kubectl /usr/bin/kubectl

Create a Scheduled Task

  • An attacker can use a Kubernetes CronJob to schedule the execution of malicious code, which will run as a container in the cluster.

  • Create a scheduled task:

kubectl create -f cronjob.yaml -n default # 在默认namespace下创建定时任务
  • The contents of cronjob.yaml are as follows:
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: art
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox:stable
            imagePullPolicy: IfNotPresent
            command:
            - /bin/sh
            - -c
            - date; echo Hello Test Cronjob
          restartPolicy: OnFailure
  • View the scheduled task just created in the default namespace:
kubectl get cronjob -n default