Kubernetes हार्डनिंग

Tip

AWS हैकिंग सीखें और अभ्यास करें:HackTricks Training AWS Red Team Expert (ARTE)
GCP हैकिंग सीखें और अभ्यास करें: HackTricks Training GCP Red Team Expert (GRTE) Azure हैकिंग सीखें और अभ्यास करें: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks का समर्थन करें

क्लस्टर का विश्लेषण करने के लिए उपकरण

Steampipe - Kubernetes Compliance

यह Kubernetes क्लस्टर पर कई अनुपालन जाँचें प्रदान करता है। इसमें CIS, National Security Agency (NSA) और Cybersecurity and Infrastructure Security Agency (CISA) के Kubernetes hardening के लिए Cybersecurity technical report का समर्थन शामिल है।

# Install Steampipe
brew install turbot/tap/powerpipe
brew install turbot/tap/steampipe
steampipe plugin install kubernetes

# Start the service
steampipe service start

# Install the module
mkdir dashboards
cd dashboards
powerpipe mod init
powerpipe mod install github.com/turbot/steampipe-mod-kubernetes-compliance

# Run the module
powerpipe server

Kubescape

Kubescape एक K8s ओपन-सोर्स टूल है जो मल्टी-क्लाउड K8s के लिए एक single pane of glass प्रदान करता है, जिसमें risk analysis, security compliance, RBAC visualizer और image vulnerabilities scanning शामिल हैं। Kubescape K8s clusters, YAML files, और HELM charts को स्कैन करता है, कई frameworks (such as the NSA-CISA , MITRE ATT&CK®) के अनुसार misconfigurations, software vulnerabilities और RBAC (role-based-access-control) उल्लंघनों का पता लगाता है, CI/CD pipeline के शुरुआती चरणों में जोखिम स्कोर तुरंत गणना करता है और समय के साथ risk trends दिखाता है।

curl -s https://raw.githubusercontent.com/kubescape/kubescape/master/install.sh | /bin/bash
kubescape scan --verbose

Popeye

Popeye एक उपयोगिता है जो लाइव Kubernetes cluster को स्कैन करती है और डिप्लॉय किए गए resources और configurations में संभावित समस्याओं की रिपोर्ट करती है। यह आपके cluster को उस आधार पर सैनिटाइज़ करती है जो डिप्लॉय किया गया है और न कि जो डिस्क पर पड़ा है। अपने cluster को स्कैन करके यह गलत कॉन्फ़िगरेशन का पता लगाती है और यह सुनिश्चित करने में मदद करती है कि best practices लागू हैं, जिससे भविष्य की परेशानियाँ रोकी जा सकें। यह वाइल्ड में Kubernetes cluster ऑपरेट करते समय होने वाले cognitive _over_load को कम करने का लक्ष्य रखता है। इसके अतिरिक्त, यदि आपका cluster metric-server उपयोग करता है, तो यह संभावित resources के over/under allocations की रिपोर्ट करती है और यदि आपकी cluster की capacity खत्म होने वाली हो तो चेतावनी देने का प्रयास करती है।

Kube-bench

The tool kube-bench is a tool that checks whether Kubernetes is deployed securely by running the checks documented in the CIS Kubernetes Benchmark.
आप चुन सकते हैं:

  • container के अंदर से kube-bench चलाएँ (host के साथ PID namespace साझा करते हुए)
  • एक container चलाएँ जो host पर kube-bench इंस्टॉल करता है, और फिर host पर सीधे kube-bench चलाएँ
  • Releases page से नवीनतम binaries इंस्टॉल करें,
  • इसे source से compile करें।

Kubeaudit

[DEPRECATED] टूल kubeaudit एक command line tool और एक Go package है जो विभिन्न सुरक्षा चिंताओं के लिए Kubernetes clusters का audit करता है।

Kubeaudit पहचान सकता है कि वह किसी container में cluster के भीतर चल रहा है या नहीं। अगर हाँ, तो यह उस cluster के सभी Kubernetes resources का audit करने की कोशिश करेगा:

kubeaudit all

यह टूल autofix आर्गुमेंट भी प्रदान करता है ताकि स्वचालित रूप से पाए गए मुद्दों को ठीक किए जा सकें।

Kube-hunter

[अप्रचलित] टूल kube-hunter Kubernetes clusters में सुरक्षा कमजोरियों का पता लगाता है। यह टूल Kubernetes environments में सुरक्षा मुद्दों के प्रति जागरूकता और दृश्यता बढ़ाने के लिए विकसित किया गया था।

kube-hunter --remote some.node.com

Trivy

Trivy में ऐसे scanners हैं जो सुरक्षा समस्याओं की तलाश करते हैं, और उन लक्ष्यों को जहाँ ये समस्याएँ मिल सकती हैं:

  • कंटेनर इमेज (Container Image)
  • फ़ाइल सिस्टम (Filesystem)
  • Git रिपॉज़िटरी (remote)
  • वर्चुअल मशीन इमेज (Virtual Machine Image)
  • Kubernetes

Kubei

[ऐसा लगता है कि अब मेंटेन नहीं किया जा रहा है]

Kubei एक vulnerabilities scanning और CIS Docker benchmark tool है जो उपयोगकर्ताओं को उनके Kubernetes clusters का सटीक और तात्कालिक risk assessment प्राप्त करने की अनुमति देता है। Kubei Kubernetes क्लस्टर में उपयोग हो रही सभी इमेजेस को स्कैन करता है, जिनमें application pods और system pods की इमेजेज़ भी शामिल हैं।

KubiScan

KubiScan एक टूल है जो Kubernetes क्लस्टर में Kubernetes के Role-based access control (RBAC) authorization मॉडल में risky permissions की स्कैनिंग करता है।

Managed Kubernetes Auditing Toolkit

Mkat एक ऐसा टूल है जो अन्य tools की तुलना में अन्य प्रकार के उच्च-जोखिम चेक्स को टेस्ट करने के लिए बनाया गया है। इसमें मुख्यतः 3 अलग-अलग मोड हैं:

  • find-role-relationships: यह पता लगाएगा कि कौन से AWS roles किस pods में चल रहे हैं
  • find-secrets: यह Pods, ConfigMaps, और Secrets जैसे K8s resources में secrets की पहचान करने की कोशिश करता है
  • test-imds-access: यह pods चलाने की कोशिश करेगा और metadata v1 और v2 तक पहुँचने की कोशिश करेगा। WARNING: यह क्लस्टर में एक pod चलाएगा, बहुत सावधान रहें क्योंकि शायद आप यह करना नहीं चाहेंगे!

Audit IaC Code

KICS

KICS निम्नलिखित Infrastructure as Code solutions में सुरक्षा कमजोरियाँ (security vulnerabilities), अनुपालन समस्याएँ (compliance issues), और इन्फ्रास्ट्रक्चर misconfigurations ढूँढता है: Terraform, Kubernetes, Docker, AWS CloudFormation, Ansible, Helm, Microsoft ARM, और OpenAPI 3.0 specifications

Checkov

Checkov एक static code analysis tool है infrastructure-as-code के लिए।

यह उस cloud infrastructure को स्कैन करता है जो Terraform, Terraform plan, Cloudformation, AWS SAM, Kubernetes, Dockerfile, Serverless या ARM Templates का उपयोग करके provision किया गया है, और graph-based scanning के माध्यम से security और compliance misconfigurations का पता लगाता है।

Kube-score

kube-score एक टूल है जो आपके Kubernetes object definitions का static code analysis करता है।

To install:

डिस्ट्रिब्यूशनकमांड / लिंक
macOS, Linux, और Windows के लिए pre-built binariesGitHub releases
Dockerdocker pull zegl/kube-score (Docker Hub)
Homebrew (macOS and Linux)brew install kube-score
Krew (macOS and Linux)kubectl krew install score

Tools to analyze YAML files & Helm Charts

Kube-linter

# Install Kube-linter
brew install kube-linter

# Run Kube-linter
## lint ./path/to/yaml/or/chart

Checkov

# Install Checkov
pip install checkov

# Run Checkov
checkov -d ./path/to/yaml/or/chart

kube‑score

# Install kube-score
brew install kube-score

# Run kube-score
kube-score score ./path/to/yaml
# or
helm template chart /path/to/chart | kube-score score -
# or if the chart needs some values
helm template chart /path/to/chart \
--set 'config.urls[0]=https://dummy.backend.internal' \
| kube-score score -

Kubesec

# Install Kubesec
## Download from https://github.com/controlplaneio/kubesec/releases

# Run Kubesec in a yaml
kubesec scan ./path/to/yaml
# or
helm template chart /path/to/chart | kubesec scan -
# or if the chart needs some values
helm template chart /path/to/chart \
--set 'config.urls[0]=https://dummy.backend.internal' \
| kubesec scan -

Scan निर्भरता समस्याएँ

Scan इमेजेस

#!/bin/bash
export images=$(kubectl get pods --all-namespaces -o jsonpath="{range .items[]}{.spec.containers[].image}{'\n'}{end}" | sort | uniq)
echo "All images found: $images"
echo ""
echo ""
for image in $images; do
# Run trivy scan and save JSON output
trivy image --format json --output /tmp/result.json --severity HIGH,CRITICAL "$image" >/dev/null 2>&1
# Extract binary targets that have vulnerabilities
binaries=$(jq -r '.Results[] | select(.Vulnerabilities != null) | .Target' /tmp/result.json)
if [ -n "$binaries" ]; then
echo "- **Image:** $image"
while IFS= read -r binary; do
echo "  - **Binary:** $binary"
jq -r --arg target "$binary" '
.Results[] | select(.Target == $target) | .Vulnerabilities[] |
"    - **\(.Title)** (\(.Severity)): Affecting `\(.PkgName)` fixed in version `\(.FixedVersion)` (current version is `\(.InstalledVersion)`)."
' /tmp/result.json
done <<< "$binaries"
echo ""
echo ""
echo ""
fi
done

Helm चार्ट्स स्कैन करें

#!/bin/bash
# scan-helm-charts.sh
# This script lists all Helm releases, renders their manifests,
# and then scans each manifest with Trivy for configuration issues.

# Check that jq is installed
if ! command -v jq &>/dev/null; then
echo "jq is required but not installed. Please install jq and rerun."
exit 1
fi

# List all helm releases and extract namespace and release name
echo "Listing Helm releases..."
helm list --all-namespaces -o json | jq -r '.[] | "\(.namespace) \(.name)"' > helm_releases.txt

# Check if any releases were found
if [ ! -s helm_releases.txt ]; then
echo "No Helm releases found."
exit 0
fi

# Loop through each Helm release and scan its rendered manifest
while IFS=" " read -r namespace release; do
echo "---------------------------------------------"
echo "Scanning Helm release '$release' in namespace '$namespace'..."
# Render the Helm chart manifest
manifest_file="${release}-manifest.yaml"
helm get manifest "$release" -n "$namespace" > "$manifest_file"
if [ $? -ne 0 ]; then
echo "Failed to get manifest for $release in $namespace. Skipping."
continue
fi
# Scan the manifest with Trivy (configuration scan)
echo "Running Trivy config scan on $manifest_file..."
trivy config --severity MEDIUM,HIGH,CRITICAL "$manifest_file"
echo "Completed scan for $release."
done < helm_releases.txt

echo "---------------------------------------------"
echo "Helm chart scanning complete."

टिप्स

Kubernetes PodSecurityContext and SecurityContext

You can configure the security context of the Pods (with PodSecurityContext) and of the containers that are going to be run (with SecurityContext). For more information read:

Kubernetes SecurityContext(s)

Kubernetes API Hardening

यह बहुत महत्वपूर्ण है कि Kubernetes Api Server तक पहुँच की सुरक्षा की जाए क्योंकि पर्याप्त privileges वाला कोई malicious actor इसका दुरुपयोग कर सकता है और वातावरण को कई तरीकों से नुकसान पहुँच सकता है.
यह आवश्यक है कि दोनों को सुरक्षित किया जाए: access (whitelist origins to access the API Server and deny any other connection) और the authentication (सिद्धांत least privilege का पालन करते हुए). और निश्चित रूप से never allow anonymous requests.

Common Request process:
User or K8s ServiceAccount –> Authentication –> Authorization –> Admission Control

टिप्स:

  • पोर्ट बंद करें।
  • Anonymous access से बचें।
  • NodeRestriction; specific nodes को API तक पहुँच न दें।
  • https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#noderestriction
  • आम तौर पर यह kubelets को node-restriction.kubernetes.io/ prefix वाले labels जोड़ने/हटाने/अपडेट करने से रोकता है। यह label prefix administrators के लिए सुरक्षित है ताकि वे अपने Node objects को workload isolation के उद्देश्यों के लिए label कर सकें, और kubelets को उस prefix वाले labels को संशोधित करने की अनुमति नहीं होगी।
  • और साथ ही, यह kubelets को इन labels और label prefixes को जोड़ने/हटाने/अपडेट करने की अनुमति भी देता है।
  • labels के माध्यम से सुरक्षित workload isolation सुनिश्चित करें।
  • कुछ specific pods को API access से रोकें।
  • ApiServer को इंटरनेट पर एक्सपोज़ होने से बचाएँ।
  • अनधिकृत पहुँच से बचें — RBAC लागू करें।
  • ApiServer पोर्ट पर firewall और IP whitelisting लागू करें।

SecurityContext Hardening

डिफ़ॉल्ट रूप से, यदि कोई अन्य user निर्दिष्ट नहीं किया गया है, तो Pod शुरू होने पर root user का उपयोग किया जाएगा। आप अपने application को निम्नलिखित जैसी template का उपयोग करके एक अधिक सुरक्षित context में चला सकते हैं:

apiVersion: v1
kind: Pod
metadata:
name: security-context-demo
spec:
securityContext:
runAsUser: 1000
runAsGroup: 3000
fsGroup: 2000
volumes:
- name: sec-ctx-vol
emptyDir: {}
containers:
- name: sec-ctx-demo
image: busybox
command: [ "sh", "-c", "sleep 1h" ]
securityContext:
runAsNonRoot: true
volumeMounts:
- name: sec-ctx-vol
mountPath: /data/demo
securityContext:
allowPrivilegeEscalation: true

सामान्य हार्डनिंग

आपको अपने Kubernetes वातावरण को आवश्यकतानुसार बार-बार अपडेट करना चाहिए ताकि:

  • Dependencies अप-टू-डेट रहें।
  • बग और सुरक्षा पैच लागू हों।

Release cycles: हर 3 महीने में एक नया minor release आता है – 1.20.3 = 1(Major).20(Minor).3(patch)

Kubernetes Cluster अपडेट करने का सबसे अच्छा तरीका (सूचना के लिए here):

  • Master Node कंपोनेंट्स को निम्न क्रम में अपग्रेड करें:
  • etcd (सभी instances)।
  • kube-apiserver (सभी control plane hosts)।
  • kube-controller-manager।
  • kube-scheduler।
  • cloud controller manager, यदि आप इसे उपयोग करते हैं।
  • Worker Node कंपोनेंट्स जैसे kube-proxy, kubelet को अपग्रेड करें।

Kubernetes मॉनिटरिंग और सुरक्षा:

  • Kyverno Policy Engine
  • Cilium Tetragon - eBPF-आधारित सुरक्षा अवलोकन और Runtime Enforcement
  • Network Security Policies
  • Falco - Runtime सुरक्षा मॉनिटरिंग और डिटेक्शन

Tip

AWS हैकिंग सीखें और अभ्यास करें:HackTricks Training AWS Red Team Expert (ARTE)
GCP हैकिंग सीखें और अभ्यास करें: HackTricks Training GCP Red Team Expert (GRTE) Azure हैकिंग सीखें और अभ्यास करें: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks का समर्थन करें