Skip to content

Commit 577457e

Browse files
committed
f
1 parent b0aba5f commit 577457e

File tree

2 files changed

+112
-5
lines changed
  • src/pentesting-cloud
    • aws-security/aws-post-exploitation/aws-ecr-post-exploitation
    • kubernetes-security/kubernetes-hardening

2 files changed

+112
-5
lines changed

src/pentesting-cloud/aws-security/aws-post-exploitation/aws-ecr-post-exploitation/README.md

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,6 @@ aws ecr-public batch-delete-image --repository-name your-ecr-repo-name --image-i
9595
```
9696

9797

98-
99-
100-
101-
102-
10398
### Exfiltrate upstream registry credentials from ECR Pull‑Through Cache (PTC)
10499

105100
If ECR Pull‑Through Cache is configured for authenticated upstream registries (Docker Hub, GHCR, ACR, etc.), the upstream credentials are stored in AWS Secrets Manager with a predictable name prefix: `ecr-pullthroughcache/`. Operators sometimes grant ECR admins broad Secrets Manager read access, enabling credential exfiltration and reuse outside AWS.
@@ -218,4 +213,39 @@ aws ecr put-registry-scanning-configuration --region $REGION --scan-type BASIC -
218213
aws ecr put-account-setting --region $REGION --name BASIC_SCAN_TYPE_VERSION --value AWS_NATIVE
219214
```
220215

216+
217+
### Scan ECR images for vulenrabilities
218+
219+
```bash
220+
#!/bin/bash
221+
222+
# This script pulls all images from ECR and runs snyk on them showing vulnerabilities for all images
223+
224+
region=<region>
225+
profile=<aws_profile>
226+
227+
registryId=$(aws ecr describe-registry --region $region --profile $profile --output json | jq -r '.registryId')
228+
229+
# Configure docker creds
230+
aws ecr get-login-password --region $region --profile $profile | docker login --username AWS --password-stdin $registryId.dkr.ecr.$region.amazonaws.com
231+
232+
while read -r repo; do
233+
echo "Working on repository $repo"
234+
digest=$(aws ecr describe-images --repository-name $repo --image-ids imageTag=latest --region $region --profile $profile --output json | jq -r '.imageDetails[] | .imageDigest')
235+
if [ -z "$digest" ]
236+
then
237+
echo "No images! Empty repository"
238+
continue
239+
fi
240+
url=$registryId.dkr.ecr.$region.amazonaws.com/$repo@$digest
241+
echo "Pulling $url"
242+
docker pull $url
243+
echo "Scanning $url"
244+
snyk container test $url --json-file-output=./snyk/$repo.json --severity-threshold=high
245+
# trivy image -f json -o ./trivy/$repo.json --severity HIGH,CRITICAL $url
246+
# echo "Removing image $url"
247+
# docker image rm $url
248+
done < <(aws ecr describe-repositories --region $region --profile $profile --output json | jq -r '.repositories[] | .repositoryName')
249+
```
250+
221251
{{#include ../../../../banners/hacktricks-training.md}}

src/pentesting-cloud/kubernetes-security/kubernetes-hardening/README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,83 @@ helm template chart /path/to/chart \
179179
```
180180

181181

182+
## Scan dependency issues
183+
184+
### Scan images
185+
186+
```bash
187+
#!/bin/bash
188+
export images=$(kubectl get pods --all-namespaces -o jsonpath="{range .items[]}{.spec.containers[].image}{'\n'}{end}" | sort | uniq)
189+
echo "All images found: $images"
190+
echo ""
191+
echo ""
192+
for image in $images; do
193+
# Run trivy scan and save JSON output
194+
trivy image --format json --output /tmp/result.json --severity HIGH,CRITICAL "$image" >/dev/null 2>&1
195+
# Extract binary targets that have vulnerabilities
196+
binaries=$(jq -r '.Results[] | select(.Vulnerabilities != null) | .Target' /tmp/result.json)
197+
if [ -n "$binaries" ]; then
198+
echo "- **Image:** $image"
199+
while IFS= read -r binary; do
200+
echo " - **Binary:** $binary"
201+
jq -r --arg target "$binary" '
202+
.Results[] | select(.Target == $target) | .Vulnerabilities[] |
203+
" - **\(.Title)** (\(.Severity)): Affecting `\(.PkgName)` fixed in version `\(.FixedVersion)` (current version is `\(.InstalledVersion)`)."
204+
' /tmp/result.json
205+
done <<< "$binaries"
206+
echo ""
207+
echo ""
208+
echo ""
209+
fi
210+
done
211+
```
212+
213+
### Scan Helm charts
214+
215+
```bash
216+
#!/bin/bash
217+
# scan-helm-charts.sh
218+
# This script lists all Helm releases, renders their manifests,
219+
# and then scans each manifest with Trivy for configuration issues.
220+
221+
# Check that jq is installed
222+
if ! command -v jq &>/dev/null; then
223+
echo "jq is required but not installed. Please install jq and rerun."
224+
exit 1
225+
fi
226+
227+
# List all helm releases and extract namespace and release name
228+
echo "Listing Helm releases..."
229+
helm list --all-namespaces -o json | jq -r '.[] | "\(.namespace) \(.name)"' > helm_releases.txt
230+
231+
# Check if any releases were found
232+
if [ ! -s helm_releases.txt ]; then
233+
echo "No Helm releases found."
234+
exit 0
235+
fi
236+
237+
# Loop through each Helm release and scan its rendered manifest
238+
while IFS=" " read -r namespace release; do
239+
echo "---------------------------------------------"
240+
echo "Scanning Helm release '$release' in namespace '$namespace'..."
241+
# Render the Helm chart manifest
242+
manifest_file="${release}-manifest.yaml"
243+
helm get manifest "$release" -n "$namespace" > "$manifest_file"
244+
if [ $? -ne 0 ]; then
245+
echo "Failed to get manifest for $release in $namespace. Skipping."
246+
continue
247+
fi
248+
# Scan the manifest with Trivy (configuration scan)
249+
echo "Running Trivy config scan on $manifest_file..."
250+
trivy config --severity MEDIUM,HIGH,CRITICAL "$manifest_file"
251+
echo "Completed scan for $release."
252+
done < helm_releases.txt
253+
254+
echo "---------------------------------------------"
255+
echo "Helm chart scanning complete."
256+
```
257+
258+
182259

183260
## Tips
184261

0 commit comments

Comments
 (0)