AWS - API Gateway Enum
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Learn & practice GCP Hacking:
HackTricks Training GCP Red Team Expert (GRTE)
API Gateway
Basic Information
AWS API Gateway is a comprehensive service offered by Amazon Web Services (AWS) designed for developers to create, publish, and oversee APIs on a large scale. It functions as an entry point to an application, permitting developers to establish a framework of rules and procedures. This framework governs the access external users have to certain data or functionalities within the application.
API Gateway enables you to define how requests to your APIs should be handled, and it can create custom API endpoints with specific methods (e.g., GET, POST, PUT, DELETE) and resources. It can also generate client SDKs (Software Development Kits) to make it easier for developers to call your APIs from their applications.
API Gateways Types
HTTP API: Build low-latency and cost-effective REST APIs with built-in features such as OIDC and OAuth2, and native CORS support. Works with the following: Lambda, HTTP backends.
WebSocket API: Build a WebSocket API using persistent connections for real-time use cases such as chat applications or dashboards. Works with the following: Lambda, HTTP, AWS Services.
REST API: Develop a REST API where you gain complete control over the request and response along with API management capabilities. Works with the following: Lambda, HTTP, AWS Services.
REST API Private: Create a REST API that is only accessible from within a VPC.
API Gateway Main Components
Resources: In API Gateway, resources are the components that make up the structure of your API. They represent the different paths or endpoints of your API and correspond to the various actions that your API supports. A resource is each method (e.g., GET, POST, PUT, DELETE) inside each path (/, or /users, or /user/{id}.
Stages: Stages in API Gateway represent different versions or environments of your API, such as development, staging, or production. You can use stages to manage and deploy multiple versions of your API simultaneously, allowing you to test new features or bug fixes without affecting the production environment. Stages also support stage variables, which are key-value pairs that can be used to configure the behavior of your API based on the current stage. For example, you could use stage variables to direct API requests to different Lambda functions or other backend services depending on the stage.
The stage is indicated at the beggining of the URL of the API Gateway endpoint.
Authorizers: Authorizers in API Gateway are responsible for controlling access to your API by verifying the identity of the caller before allowing the request to proceed. You can use AWS Lambda functions as custom authorizers, which allows you to implement your own authentication and authorization logic. When a request comes in, API Gateway passes the request's authorization token to the Lambda authorizer, which processes the token and returns an IAM policy that determines what actions the caller is allowed to perform. API Gateway also supports built-in authorizers, such as AWS Identity and Access Management (IAM) and Amazon Cognito.
Resource Policy: A resource policy in API Gateway is a JSON document that defines the permissions for accessing your API. It is similar to an IAM policy but specifically tailored for API Gateway. You can use a resource policy to control who can access your API, which methods they can call, and from which IP addresses or VPCs they can connect. Resource policies can be used in combination with authorizers to provide fine-grained access control for your API.
In order to make effect the API needs to be deployed again after the resource policy is modified.
Logging
By default, CloudWatch Logs are off, Access Logging is off, and X-Ray tracing is also off.
Enumeration
Note that in both AWS apis to enumerate resources (apigateway
and apigatewayv2
) the only permission you need and the only read permission grantable is apigateway:GET
, with that you can enumerate everything.
Different Authorizations to access API Gateway endpoints
Resource Policy
It's possible to use resource policies to define who could call the API endpoints.
In the following example you can see that the indicated IP cannot call the endpoint /resource_policy
via GET.
![](https://cloud.hacktricks.xyz/~gitbook/image?url=https%3A%2F%2F430239558-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252F5uvPQhxNCPYYTqpRwsuS%252Fuploads%252FF8c17dEaH2foUfuUhMkM%252Fimage.png%3Falt%3Dmedia%26token%3D6f5413fc-6d11-458d-91a6-6fbca367ac40&width=768&dpr=4&quality=100&sign=2778ff72&sv=1)
IAM Authorizer
It's possible to set that a methods inside a path (a resource) requires IAM authentication to call it.
When this is set you will receive the error {"message":"Missing Authentication Token"}
when you try to reach the endpoint without any authorization.
One easy way to generate the expected token by the application is to use the Authorization
type AWS Signature
inside Postman.
![](https://cloud.hacktricks.xyz/~gitbook/image?url=https%3A%2F%2F430239558-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252F5uvPQhxNCPYYTqpRwsuS%252Fuploads%252F4x9vHmcBANjnO4jPiy8R%252Fimage.png%3Falt%3Dmedia%26token%3Dcfbee711-fb5e-4438-b3c1-35cf7bd00cc7&width=768&dpr=4&quality=100&sign=7ad4edfc&sv=1)
Set the accessKey and the SecretKey of the account you want to use and you can know authenticate against the API endpoint.
It will generate an Authorization header such as:
Note that in other cases the Authorizer might have been bad coded and just sending anything inside the Authorization header will allow to see the hidden content.
Request Signing Using Python
Custom Lambda Authorizer
It's possible to use a lambda that based in a given token will return an IAM policy indicating if the user is authorized to call the API endpoint. You can set each resource method that will be using the authoriser.
Call it with something like:
Depending on the Lambda code, this authorization might be vulnerable
Note that if a deny policy is generated and returned the error returned by API Gateway is: {"Message":"User is not authorized to access this resource with an explicit deny"}
This way you could identify this authorization being in place.
Required API Key
It's possible to set API endpoints that require a valid API key to contact it.
![](https://cloud.hacktricks.xyz/~gitbook/image?url=https%3A%2F%2F430239558-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252F5uvPQhxNCPYYTqpRwsuS%252Fuploads%252FGh79gmxwyHtGkuq3RUAy%252Fimage.png%3Falt%3Dmedia%26token%3D42b77ccf-136e-4eaa-acb2-d2ba198f322a&width=768&dpr=4&quality=100&sign=174832b8&sv=1)
It's possible to generate API keys in the API Gateway portal and even set how much it can be used (in terms of requests per second and in terms of requests per month).
To make an API key work, you need to add it to a Usage Plan, this usage plan mus be added to the API Stage and the associated API stage needs to have a configured a method throttling to the endpoint requiring the API key:
![](https://cloud.hacktricks.xyz/~gitbook/image?url=https%3A%2F%2F430239558-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252F5uvPQhxNCPYYTqpRwsuS%252Fuploads%252Fu1qqOBuEG2u1C0XbrkdR%252Fimage.png%3Falt%3Dmedia%26token%3Db3eea343-6749-49cf-8070-3153607f87c0&width=768&dpr=4&quality=100&sign=75aa2bde&sv=1)
Unauthenticated Access
AWS - API Gateway Unauthenticated EnumPrivesc
AWS - Apigateway PrivescPost Exploitation
AWS - API Gateway Post ExploitationPersistence
AWS - API Gateway PersistenceLearn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Learn & practice GCP Hacking:
HackTricks Training GCP Red Team Expert (GRTE)
Last updated