AWS CodeBuild - Untrusted PR Webhook Bypass (CodeBreach-style)
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 का समर्थन करें
- सदस्यता योजनाओं की जांच करें!
- हमारे 💬 Discord समूह या टेलीग्राम समूह में शामिल हों या हमें Twitter 🐦 @hacktricks_live** पर फॉलो करें।**
- हैकिंग ट्रिक्स साझा करें, PRs को HackTricks और HackTricks Cloud गिटहब रिपोजिटरी में सबमिट करके।
यह हमला तभी होता है जब एक public-facing PR workflow को कमजोर webhook नियंत्रण वाले एक privileged CodeBuild project से जोड़ा गया हो।
यदि बाहरी हमलावर अपने pull request को CodeBuild में execute करवा सके, तो आम तौर पर वे build के अंदर arbitrary code execution पा सकते हैं (build scripts, dependency hooks, test scripts, आदि), और फिर secrets, IAM credentials, या source-provider credentials तक पहुँच सकते हैं।
Why this is dangerous
CodeBuild webhook filters regex patterns के साथ evaluate होते हैं (non-EVENT filters के लिए)। ACTOR_ACCOUNT_ID filter में, एक कमजोर pattern उम्मीद से अधिक users से मिल सकता है।
यदि untrusted PRs किसी project में build होते हैं जिसके पास privileged AWS role permissions या GitHub credentials हैं, तो यह पूरा supply-chain compromise बन सकता है।
Wiz ने एक practical chain दिखाया जहां:
- एक webhook actor allowlist ने unanchored regex इस्तेमाल किया।
- एक attacker ने ऐसा GitHub ID register किया जो trusted ID का superstring था।
- एक malicious PR ने CodeBuild trigger किया।
- Build code execution का उपयोग कर memory dump करके source-provider credentials/tokens recover किये गए।
Misconfigurations that allow external PR code execution
नीचे high-risk गलतियां और attackers किस तरह इन्हें abuse करते हैं दिए गए हैं:
EVENTfilters allow untrusted triggers
- Common risky events:
PULL_REQUEST_CREATED,PULL_REQUEST_UPDATED,PULL_REQUEST_REOPENED. - अन्य events जो privileged builds से जुड़ने पर भी खतरनाक बन सकते हैं:
PUSH,PULL_REQUEST_CLOSED,PULL_REQUEST_MERGED,RELEASED,PRERELEASED,WORKFLOW_JOB_QUEUED. - खराब:
EVENT="PUSH, PULL_REQUEST_CREATED, PULL_REQUEST_UPDATED"किसी privileged project में। - बेहतर: PR comment approval का उपयोग करें और privileged projects के लिए trigger events को minimize करें।
- Abuse: attacker एक PR खोलता/अपडेट करता है या अपनी control वाली branch पर push करता है, और उनका code CodeBuild में execute होता है।
ACTOR_ACCOUNT_IDregex is weak
- खराब: unanchored patterns जैसे
123456|7890123. - बेहतर: exact-match anchoring
^(123456|7890123)$. - Abuse: regex over-match unauthorized GitHub IDs को allowlists से पास कर देता है।
- Other regex filters are weak or missing
HEAD_REF- खराब:
refs/heads/.* - बेहतर:
^refs/heads/main$(या explicit trusted list) BASE_REF- खराब:
.* - बेहतर:
^refs/heads/main$ FILE_PATH- खराब: path restrictions नहीं होना
- बेहतर: risky files को exclude करें जैसे
^buildspec\\.yml$,^\\.github/workflows/.*,(^|/)package(-lock)?\\.json$ COMMIT_MESSAGE- खराब: loose match के साथ trust marker जैसे
trusted - बेहतर: commit message को PR execution के लिए trust boundary के रूप में उपयोग न करें
REPOSITORY_NAME/ORGANIZATION_NAME- खराब: org/global webhooks में
.* - बेहतर: सिर्फ exact repo/org matches
WORKFLOW_NAME- खराब:
.* - बेहतर: केवल exact workflow name matches (या इसे trust control के रूप में उपयोग न करें)
- Abuse: attacker permissive regex को satisfy करने के लिए ref/path/message/repo context craft करता है और builds trigger करता है।
excludeMatchedPatternis misused
- इस flag को गलत तरीके से सेट करने पर intended logic उलट सकती है।
- खराब:
FILE_PATH '^buildspec\\.yml$'के साथexcludeMatchedPattern=falseजब intent buildspec edits को block करना था। - बेहतर: वही pattern
excludeMatchedPattern=trueके साथ ताकिbuildspec.ymlको छूने वाली builds deny हों। - Abuse: defenders सोचते हैं कि वे risky events/paths/actors को रोक रहे हैं, लेकिन दरअसल वे उन्हें allow कर रहे होते हैं।
- Multiple
filterGroupscreate accidental bypasses
- CodeBuild groups को OR के रूप में evaluate करता है (एक पास होने वाला group ही काफी है)।
- खराब: एक strict group + एक permissive fallback group (उदा., केवल
EVENT=PULL_REQUEST_UPDATED)। - बेहतर: ऐसे fallback groups हटा दें जो actor/ref/path constraints लागू नहीं करते।
- Abuse: attacker को केवल weakest group satisfy करना होता है।
- Comment approval gate disabled or too permissive
pullRequestBuildPolicy.requiresCommentApproval=DISABLEDसबसे कम सुरक्षित है।- बहुत broad approver roles नियंत्रण को कम कर देते हैं।
- खराब:
requiresCommentApproval=DISABLED. - बेहतर:
ALL_PULL_REQUESTSयाFORK_PULL_REQUESTSके साथ minimal approver roles। - Abuse: fork/drive-by PRs बिना trusted maintainer approval के auto-run हो जाते हैं।
- No restrictive branch/path strategy for PR builds
HEAD_REF+BASE_REF+FILE_PATHके साथ defense-in-depth का अभाव।- खराब: केवल
EVENT+ACTOR_ACCOUNT_ID, बिना ref/path controls के। - बेहतर: exact
ACTOR_ACCOUNT_ID+BASE_REF+HEAD_REF+FILE_PATHrestrictions को combine करें। - Abuse: attacker build inputs (buildspec/CI/dependencies) modify करता है और arbitrary command execution पाता है।
- Public visibility + status URL exposure
- Public build/check URLs attacker recon और iterative testing को आसान बनाते हैं।
- खराब:
projectVisibility=PUBLIC_READके साथ sensitive logs/config public builds में। - बेहतर: जब तक ज़रूरी न हो projects को private रखें, और logs/artifacts sanitize करें।
- Abuse: attacker project patterns/behavior discover करके payloads और bypass attempts को tune कर सकता है।
Token leakage from memory
Wiz के write-up में बताया गया है कि source-provider credentials build runtime context में मौजूद होते हैं और build compromise के बाद (उदा., memory dumping के जरिए) चुराए जा सकते हैं, जिससे repository takeover संभव हो सकता है अगर scopes broad हों।
AWS ने disclosure के बाद hardening किया, पर मूल सबक वही है: कभी भी privileged build contexts में untrusted PR code execute न करें और मानकर चलें कि attacker-controlled build code credentials theft का प्रयास करेगा।
CodeBuild में additional credential theft techniques के लिए, यह भी देखें:
Finding CodeBuild URLs in GitHub PRs
यदि CodeBuild commit status GitHub पर report करता है, तो CodeBuild build URL आम तौर पर दिखता है:
- PR page -> Checks tab (या Conversation/Commits में status line)।
- Commit page -> status/checks section -> Details link।
- PR commits list -> commit से जुड़ा check context क्लिक करें।
Public projects के लिए, यह link unauthenticated users को build metadata/configuration दिखा सकता है।
Script: detect CodeBuild URLs in a PR and test if they look public
```bash #!/usr/bin/env bash set -euo pipefailUsage:
./check_pr_codebuild_urls.sh <pr_number>
Requirements: gh, jq, curl
OWNER=“${1:?owner}” REPO=“${2:?repo}” PR=“${3:?pr_number}”
for bin in gh jq curl timeout; do command -v “$bin” >/dev/null || { echo “[!] Missing dependency: $bin” >&2; exit 1; } done
tmp_commits=“$(mktemp)” tmp_urls=“$(mktemp)” trap ‘rm -f “$tmp_commits” “$tmp_urls”’ EXIT
gh_api() { timeout 20s gh api “$@” 2>/dev/null || true }
Get all commit SHAs in the PR (bounded call to avoid hangs)
gh_api “repos/${OWNER}/${REPO}/pulls/${PR}/commits” –paginate –jq ‘.[].sha’ > “$tmp_commits” if [ ! -s “$tmp_commits” ]; then echo “[!] No commits found (or API call timed out/failed).” >&2 exit 1 fi
echo “[*] PR commits:” cat “$tmp_commits” echo
echo “[*] Searching commit statuses/check-runs for CodeBuild URLs…”
while IFS= read -r sha; do [ -z “$sha” ] && continue
Classic commit statuses (target_url)
gh_api “repos/${OWNER}/${REPO}/commits/${sha}/status”
–jq ‘.statuses[]? | .target_url // empty’ 2>/dev/null || true
GitHub Checks API (details_url)
gh_api “repos/${OWNER}/${REPO}/commits/${sha}/check-runs”
–jq ‘.check_runs[]? | .details_url // empty’ 2>/dev/null || true
done < “$tmp_commits” | sort -u > “$tmp_urls”
grep -Ei ‘codebuild|codebuild.aws.amazon.com|console.aws.amazon.com/.*/codebuild’ “$tmp_urls” || true
echo echo “[*] Public-access heuristic:” echo “ - If URL redirects to signin.aws.amazon.com -> likely not public“ echo “ - If URL is directly reachable (HTTP 200) without auth redirect -> potentially public“ echo
cb_urls=“$(grep -Ei ‘codebuild|codebuild.aws.amazon.com|console.aws.amazon.com/./codebuild’ “$tmp_urls” || true)“ if [ -z “$cb_urls” ]; then echo “[] No CodeBuild URLs found in PR statuses/check-runs.” exit 0 fi
while IFS= read -r url; do [ -z “$url” ] && continue final_url=“$(timeout 20s curl -4 -sS -L –connect-timeout 5 –max-time 20 -o /dev/null -w ‘%{url_effective}’ “$url” || true)“ code=“$(timeout 20s curl -4 -sS -L –connect-timeout 5 –max-time 20 -o /dev/null -w ‘%{http_code}’ “$url” || true)“
if echo “$final_url” | grep -qi ‘signin.aws.amazon.com’; then verdict=“NOT_PUBLIC_OR_AUTH_REQUIRED” elif [ “$code” = “200” ]; then verdict=“POTENTIALLY_PUBLIC” else verdict=“UNKNOWN_CHECK_MANUALLY” fi
printf ‘%s\t%s\t%s\n’ “$verdict” “$code” “$url” done <<< “$cb_urls”
इनके साथ परीक्षण किया गया:
```bash
bash /tmp/check_pr_codebuild_urls.sh carlospolop codebuild-codebreach-ctf-lab 1
त्वरित ऑडिट चेकलिस्ट
# Enumerate projects
aws codebuild list-projects
# Inspect source/webhook configuration
aws codebuild batch-get-projects --names <project-name>
# Inspect global source credentials configured in account
aws codebuild list-source-credentials
प्रत्येक प्रोजेक्ट की समीक्षा करें:
webhook.filterGroupsमें PR इवेंट शामिल हैं।ACTOR_ACCOUNT_IDपैटर्न जो ^…$ से एंकर किए नहीं गए हैं।pullRequestBuildPolicy.requiresCommentApprovalDISABLEDके बराबर है।- ब्रांच/पाथ प्रतिबंध अनुपस्थित हैं।
- उच्च-विशेषाधिकार
serviceRole। - स्रोत क्रेडेंशियल्स के दायरे और पुन: उपयोग से जोखिम।
हार्डनिंग दिशानिर्देश
- PR बिल्ड्स के लिए टिप्पणी अनुमोदन आवश्यक करें (
ALL_PULL_REQUESTSयाFORK_PULL_REQUESTS)। - यदि actor allowlists का उपयोग कर रहे हैं, तो regexes को एंकर करें और उन्हें सटीक रखें।
- अनट्रस्टेड संपादनों से बचने के लिए
buildspec.ymlऔर CI स्क्रिप्ट्स में बदलाव रोकने हेतुFILE_PATHप्रतिबंध जोड़ें। - भरोसेमंद रिलीज बिल्ड्स को अनट्रस्टेड PR बिल्ड्स से अलग करें और उन्हें अलग projects/roles में रखें।
- सूक्ष्म-स्तरीय, न्यूनतम-विशेषाधिकार source-provider tokens का उपयोग करें (समर्पित कम-विशेषाधिकार identities को प्राथमिकता दें)।
- webhook filters और source credential उपयोग का लगातार ऑडिट करें।
References
- Wiz: CodeBreach - AWS CodeBuild ACTOR_ID regex bypass and token theft
- AWS CodeBuild API - WebhookFilter
- AWS CLI - codebuild create-webhook
- AWS CodeBuild User Guide - Best practices for webhooks
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 का समर्थन करें
- सदस्यता योजनाओं की जांच करें!
- हमारे 💬 Discord समूह या टेलीग्राम समूह में शामिल हों या हमें Twitter 🐦 @hacktricks_live** पर फॉलो करें।**
- हैकिंग ट्रिक्स साझा करें, PRs को HackTricks और HackTricks Cloud गिटहब रिपोजिटरी में सबमिट करके।
HackTricks Cloud

