체크포인트
Create a simple GKE cluster
/ 20
Deploy a pod that mounts the host filesystem
/ 20
Deploy a second node pool
/ 20
Deploy PodSecurityPolicy objects
/ 20
Deploy a blocked pod that mounts the host filesystem
/ 20
Hardening Default GKE Cluster Configurations
- GSP496
- Overview
- Setup and requirements
- Task 1. Create a simple GKE cluster
- Task 2. Run a Google Cloud-SDK pod
- Task 3. Deploy a pod that mounts the host filesystem
- Task 4. Explore and compromise the underlying host
- Task 5. Deploy a second node pool
- Task 6. Run a Google Cloud-SDK pod
- Task 7. Enforce Pod Security Standards
- Task 8. Deploy a blocked pod that mounts the host filesystem
- Congratulations!
GSP496
Overview
This lab demonstrates some of the security concerns of a default GKE cluster configuration and the corresponding hardening measures to prevent multiple paths of pod escape and cluster privilege escalation. These attack paths are relevant in the following scenarios:
- An application flaw in an external facing pod that allows for Server-Side Request Forgery (SSRF) attacks.
- A fully compromised container inside a pod allowing for Remote Command Execution (RCE).
- A malicious internal user or an attacker with a set of compromised internal user credentials with the ability to create/update a pod in a given namespace.
This lab was created by GKE Helmsman engineers to help you grasp a better understanding of hardening default GKE cluster configurations.
The example code for this lab is provided as-is without warranty or guarantee
Objectives
Upon completion of this lab you will understand the need for protecting the GKE Instance Metadata and defining appropriate PodSecurityPolicy policies for your environment.
You will:
- Create a small GKE cluster using the default settings.
- Validate the most common paths of pod escape and cluster privilege escalation from the perspective of a malicious internal user.
- Harden the GKE cluster for these issues.
- Validate the cluster so that those actions are no longer allowed.
Setup and requirements
Before you click the Start Lab button
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.
This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
- Access to a standard internet browser (Chrome browser recommended).
- Time to complete the lab---remember, once you start, you cannot pause a lab.
How to start your lab and sign in to the Google Cloud console
-
Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:
- The Open Google Cloud console button
- Time remaining
- The temporary credentials that you must use for this lab
- Other information, if needed, to step through this lab
-
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
Note: If you see the Choose an account dialog, click Use Another Account. -
If necessary, copy the Username below and paste it into the Sign in dialog.
{{{user_0.username | "Username"}}} You can also find the Username in the Lab Details panel.
-
Click Next.
-
Copy the Password below and paste it into the Welcome dialog.
{{{user_0.password | "Password"}}} You can also find the Password in the Lab Details panel.
-
Click Next.
Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges. -
Click through the subsequent pages:
- Accept the terms and conditions.
- Do not add recovery options or two-factor authentication (because this is a temporary account).
- Do not sign up for free trials.
After a few moments, the Google Cloud console opens in this tab.
Activate Cloud Shell
Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.
- Click Activate Cloud Shell
at the top of the Google Cloud console.
When you are connected, you are already authenticated, and the project is set to your Project_ID,
gcloud
is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
- (Optional) You can list the active account name with this command:
- Click Authorize.
Output:
- (Optional) You can list the project ID with this command:
Output:
gcloud
, in Google Cloud, refer to the gcloud CLI overview guide.
Task 1. Create a simple GKE cluster
- Set a zone into an environment variable called MY_ZONE. This lab is using "
", you can select a zone if you prefer:
- Run this to start a Kubernetes cluster managed by Kubernetes Engine named
simplecluster
and configure it to run 2 nodes:
It takes several minutes to create a cluster as Kubernetes Engine provisions virtual machines for you. The warnings about features available in new versions can be safely ignored for this lab.
- After the cluster is created, check your installed version of Kubernetes using the
kubectl version
command:
The gcloud container clusters create
command automatically authenticated kubectl
for you.
- View your running nodes in the Cloud Console. On the Navigation menu, click Compute Engine > VM Instances.
Your Kubernetes cluster is now ready for use.
Click Check my progress to verify the objective.
Task 2. Run a Google Cloud-SDK pod
- From your Cloud Shell prompt, launch a single instance of the Google Cloud-SDK container:
This will take a few minutes to complete.
- You should now have a bash shell inside the pod's container:
It may take a few seconds for the container to be started and the command prompt to be displayed. If you don't see a command prompt, try pressing Enter.
Explore the Compute Metadata endpoint
- Run the following command to access the
v1
Compute Metadata endpoint:
Output looks like:
/computeMetadata/v1/instance/name
from this server. Missing Metadata-Flavor:Google header.
...snip...
Notice how it returns an error stating that it requires the custom HTTP header to be present.
- Add the custom header on the next run and retrieve the Compute Engine instance name that is running this pod:
Output looks like:
Keep this shell inside the pod available for the next step.
- If you accidentally exit from the pod, simply re-run:
Explore the GKE node bootstrapping credentials
- From inside the same pod shell, run the following command to list the attributes associated with the underlying Compute Engine instances. Be sure to include the trailing slash:
Perhaps the most sensitive data in this listing is kube-env
. It contains several variables which the kubelet
uses as initial credentials when attaching the node to the GKE cluster. The variables CA_CERT
, KUBELET_CERT
, and KUBELET_KEY
contain this information and are therefore considered sensitive to non-cluster administrators.
- To see the potentially sensitive variables and data, run the following command:
Therefore, in any of the following situations:
- A flaw that allows for SSRF in a pod application
- An application or library flaw that allow for RCE in a pod
- An internal user with the ability to create or exec into a pod
There exists a high likelihood for compromise and exfiltration of sensitive kubelet
bootstrapping credentials via the Compute Metadata endpoint. With the kubelet
credentials, it is possible to leverage them in certain circumstances to escalate privileges to that of cluster-admin
and therefore have full control of the GKE Cluster including all data, applications, and access to the underlying nodes.
Leverage the Permissions assigned to this Node Pool's service account
By default, Google Cloud projects with the Compute API enabled have a default service account in the format of NNNNNNNNNN-compute@developer.gserviceaccount.com
in the project and the Editor
role attached to it. Also by default, GKE clusters created without specifying a service account will utilize the default Compute service account and attach it to all worker nodes.
- Run the following
curl
command to list the OAuth scopes associated with the service account attached to the underlying Compute Engine instance:
(output)
The combination of authentication scopes and the permissions of the service account dictates what applications on this node can access. The above list is the minimum scopes needed for most GKE clusters, but some use cases require increased scopes.
Warning: If, during cluster creation, you configured the authentication scope to include `https://www.googleapis.com/auth/cloud-platform`, any Google Cloud API would be in scope and only IAM permissions assigned to the service account would determine access.
Further, if the default service account with the default IAM Role ofEditor
is in use, any pod on this node pool has Editor
permissions to the Google Cloud project where the GKE cluster is deployed. As the Editor
IAM Role has a wide range of read/write permissions to interact with project resources such as Compute instances, Cloud Storage buckets, GCR registries, and more, this is a significant security risk. - Exit out of this pod by typing:
Task 3. Deploy a pod that mounts the host filesystem
One of the simplest paths for "escaping" to the underlying host is by mounting the host's filesystem into the pod's filesystem using standard Kubernetes volumes
and volumeMounts
in a Pod
specification.
- To demonstrate this, run the following to create a Pod that mounts the underlying host filesystem
/
at the folder named/rootfs
inside the container:
- Run
kubectl get pod
and re-run until it's in the "Running" state:
(Output)
Click Check my progress to verify the objective.
Task 4. Explore and compromise the underlying host
- Run the following to obtain a shell inside the pod you just created:
- Switch to the pod shell's root filesystem point to that of the underlying host:
With those simple commands, the pod is now effectively a root
shell on the node. You are now able to do the following:
run the standard docker command with full permissions |
|
list docker images |
|
|
|
examine the Kubernetes secrets mounted |
|
|
|
Nearly every operation that the root
user can perform is available to this pod shell. This includes persistence mechanisms like adding SSH users/keys, running privileged docker containers on the host outside the view of Kubernetes, and much more.
- To exit the pod shell, run
exit
twice - once to leave thechroot
and another to leave the pod's shell:
- Now you can delete the
hostpath
pod:
Understand the available controls
The next steps of this demo will cover:
-
Disabling the Legacy Compute Engine Metadata API Endpoint - By specifying a custom metadata key and value, the
v1beta1
metadata endpoint will no longer be available from the instance. - Enable Metadata Concealment - Passing an additional configuration during cluster and/or node pool creation, a lightweight proxy will be installed on each node that proxies all requests to the Metadata API and prevents access to sensitive endpoints.
- Enable and Utilize Pod Security Admission - Enable the Pod Security Admission (PSA) controller in your GKE cluster. This provides the ability to enforce pod security standards that enhance your cluster's security posture.
Task 5. Deploy a second node pool
To enable you to experiment with and without the Metadata endpoint protections in place, you'll create a second node pool that includes two additional settings. Pods that are scheduled to the generic node pool will not have the protections, and Pods scheduled to the second node pool will have them enabled.
- Create the second node pool:
Click Check my progress to verify the objective.
Task 6. Run a Google Cloud-SDK pod
- In Cloud Shell, launch a single instance of the Google Cloud-SDK container that will be run only on the second node pool with the protections enabled and not run as the root user:
- You should now have a bash shell inside the pod's container running on the node pool named
second-pool
. You should see the following:
It may take a few seconds for the container to start and the command prompt to open.
If you don't see a command prompt, press Enter.
Explore various blocked endpoints
- With the configuration of the second node pool set to
--workload-metadata-from-node=SECURE
, the following command to retrieve the sensitive file,kube-env
, will now fail:
(Output)
- But other commands to non-sensitive endpoints will still succeed if the proper HTTP header is passed:
(Example Output)
- Exit out of the pod:
You should now be back in Cloud Shell.
Task 7. Enforce Pod Security Standards
- In order to have the necessary permissions to proceed, grant explicit permissions to your own user account to become
cluster-admin:
(Output)
- Now you will enforce a pod security standard. Choose the most appropriate security standard for your 'default' namespace. The 'restricted' profile offers stronger security:
- Next you will create a ClusterRole. If you want to control who can modify Pod Security Admission levels on namespaces, create a ClusterRole called
pod-security-manager
:
- Next, you will create a RoleBinding. To restrict who can change namespace labels related to Pod Security Admission, create a RoleBinding in the 'default' namespace:
Click Check my progress to verify the objective.
Task 8. Deploy a blocked pod that mounts the host filesystem
Because the account used to deploy the GKE cluster was granted cluster-admin permissions in a previous step, it's necessary to create another separate "user" account to interact with the cluster and validate the PodSecurityPolicy enforcement.
- To do this, run:
(Output)
- Next, run these commands to grant these permissions to the service account - the ability to interact with the cluster and attempt to create pods:
- Obtain the service account credentials file by running:
- Configure
kubectl
to authenticate as this service account:
- To configure
kubectl
to use these credentials when communicating with the cluster, run:
- Now, try to create another pod that mounts the underlying host filesystem
/
at the folder named/rootfs
inside the container:
- This output validatates that it's blocked by the pod security standard:
- Deploy another pod that meets the criteria of the
restrictive-psp
:
(Output)
Click Check my progress to verify the objective.
Congratulations!
In this lab you configured a default Kubernetes cluster in Google Kubernetes Engine. You then probed and exploited the access available to your pod, hardened the cluster, and validated those malicious actions were no longer possible.
Next steps / Learn more
- IMPORTANT: While this lab covers several security issues in detail, there are other areas that should be considered in your environment. Refer to Harden your cluster's security Guide for additional information.
- Apply predefined Pod-level security policies using PodSecurity and Harden your cluster's security Guide
- Node Service Accounts: Permissions Guide
- Protecting Node Metadata: Protect node metadata Guide
Google Cloud training and certification
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual Last Updated February 14, 2024
Lab Last Tested February 14, 2024
Copyright 2024 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.