AWS - API Gateway Enum
Tip
学习并练习 AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
学习并练习 GCP Hacking:HackTricks Training GCP Red Team Expert (GRTE)
学习并练习 Az Hacking:HackTricks Training Azure Red Team Expert (AzRTE)
支持 HackTricks
- 查看 subscription plans!
- 加入 💬 Discord group 或者 telegram group 或 关注 我们的 Twitter 🐦 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud github 仓库 提交 PRs 来分享 hacking tricks。
API Gateway
基本信息
AWS API Gateway 是亚马逊网络服务(AWS)提供的一项综合服务,旨在帮助开发人员大规模创建、发布和管理 API。它作为应用程序的入口点,允许开发人员建立一套规则和程序框架。该框架管理外部用户对应用程序中特定数据或功能的访问。
API Gateway 使您能够定义如何处理对您的 API 的请求,并可以创建具有特定方法(例如 GET、POST、PUT、DELETE)和资源的自定义 API 端点。它还可以生成客户端 SDK(软件开发工具包),以便开发人员更轻松地从其应用程序调用您的 API。
API 网关类型
- HTTP API:构建低延迟和成本效益高的 REST API,具有内置功能,如 OIDC 和 OAuth2,以及原生 CORS 支持。与以下内容兼容:Lambda、HTTP 后端。
- WebSocket API:使用持久连接构建 WebSocket API,适用于实时用例,如聊天应用程序或仪表板。与以下内容兼容:Lambda、HTTP、AWS 服务。
- REST API:开发 REST API,您可以完全控制请求和响应以及 API 管理功能。与以下内容兼容:Lambda、HTTP、AWS 服务。
- REST API 私有:创建仅可从 VPC 内部访问的 REST API。
API Gateway 主要组件
- 资源:在 API Gateway 中,资源是构成您 API 结构的组件。它们代表您 API 的不同路径或端点,并对应于您的 API 支持的各种操作。资源是每个方法(例如 GET、POST、PUT、DELETE)在每个路径内(/,或 /users,或 /user/{id})。
- 阶段:API Gateway 中的阶段代表您 API 的不同版本或环境,例如开发、预发布或生产。您可以使用阶段来管理和部署多个版本的 API 同时,允许您测试新功能或修复错误而不影响生产环境。阶段还支持阶段变量,这些是可以根据当前阶段配置 API 行为的键值对。例如,您可以使用阶段变量将 API 请求定向到不同的 Lambda 函数或其他后端服务,具体取决于阶段。
- 阶段在 API Gateway 端点的 URL 开头指示。
- 授权者:API Gateway 中的授权者负责控制对您 API 的访问,通过在允许请求继续之前验证调用者的身份。您可以使用AWS Lambda 函数作为自定义授权者,这使您能够实现自己的身份验证和授权逻辑。当请求到达时,API Gateway 将请求的授权令牌传递给 Lambda 授权者,后者处理该令牌并返回一个 IAM 策略,确定调用者被允许执行的操作。API Gateway 还支持内置授权者,如AWS 身份和访问管理(IAM)和亚马逊 Cognito。
- 资源策略:API Gateway 中的资源策略是一个 JSON 文档,定义访问您 API 的权限。它类似于 IAM 策略,但专门针对 API Gateway 量身定制。您可以使用资源策略来控制谁可以访问您的 API,他们可以调用哪些方法,以及他们可以从哪些 IP 地址或 VPC 连接。资源策略可以与授权者结合使用,为您的 API 提供细粒度的访问控制。
- 为了使效果生效,API 需要在修改资源策略后重新部署。
日志记录
默认情况下,CloudWatch 日志是关闭的,访问日志是关闭的,X-Ray 跟踪也是关闭的。
枚举
Tip
请注意,在两个 AWS API 中枚举资源(
apigateway和apigatewayv2)时,您所需的唯一权限和唯一可授予的读取权限是**apigateway:GET,通过该权限您可以枚举所有内容。**
# Generic info
aws apigateway get-account
aws apigateway get-domain-names
aws apigateway get-usage-plans
aws apigateway get-vpc-links
aws apigateway get-client-certificates
# Enumerate APIs
aws apigateway get-rest-apis # This will also show the resource policy (if any)
## Get stages
aws apigateway get-stages --rest-api-id <id>
## Get resources
aws apigateway get-resources --rest-api-id <id>
## Get API resource action per HTTP verb (check authorizers and api key required)
aws apigateway get-method --http-method GET --rest-api-id <api-id> --resource-id <resource-id>
## Call API
https://<api-id>.execute-api.<region>.amazonaws.com/<stage>/<resource>
## API authorizers
aws apigateway get-authorizers --rest-api-id <id>
## Models
aws apigateway get-models --rest-api-id <id>
## More info
aws apigateway get-gateway-responses --rest-api-id <id>
aws apigateway get-request-validators --rest-api-id <id>
aws apigateway get-deployments --rest-api-id <id>
# Get api keys generated
aws apigateway get-api-keys --include-value
aws apigateway get-api-key --api-key <id> --include-value # Get just 1
## Example use API key
curl -X GET -H "x-api-key: AJE&Ygenu4[..]" https://e83uuftdi8.execute-api.us-east-1.amazonaws.com/dev/test
## Usage plans
aws apigateway get-usage-plans #Get limit use info
aws apigateway get-usage-plan-keys --usage-plan-id <plan_id> #Get clear text values of api keys
aws apigateway get-usage-plan-key --usage-plan-id <plan_id> --key-id <key_id>
###Already consumed
aws apigateway get-usage --usage-plan-id <plan_id> --start-date 2023-07-01 --end-date 2023-07-12
访问 API Gateway 端点的不同授权
资源策略
可以使用资源策略来定义谁可以调用 API 端点。
在以下示例中,您可以看到 指示的 IP 无法通过 GET 调用 端点 /resource_policy。
.png)
IAM 授权者
可以设置路径中的方法(资源)需要 IAM 身份验证才能调用。
当设置此项时,如果您尝试在没有任何授权的情况下访问端点,将收到错误 {"message":"Missing Authentication Token"}。
生成应用程序所需的预期令牌的一种简单方法是使用 curl。
$ curl -X <method> https://<api-id>.execute-api.<region>.amazonaws.com/<stage>/<resource> --user <AWS_ACCESS_KEY>:<AWS_SECRET_KEY> --aws-sigv4 "aws:amz:<region>:execute-api"
另一种方法是在 Postman 中使用 Authorization 类型 AWS Signature。
.png)
设置您要使用的帐户的 accessKey 和 SecretKey,您就可以对 API 端点进行身份验证。
这两种方法都会生成一个 Authorization header,例如:
AWS4-HMAC-SHA256 Credential=AKIAYY7XU6ECUDOTWB7W/20220726/us-east-1/execute-api/aws4_request, SignedHeaders=host;x-amz-date, Signature=9f35579fa85c0d089c5a939e3d711362e92641e8c14cc571df8c71b4bc62a5c2
请注意,在其他情况下,Authorizer 可能被 错误编码,只需在 Authorization header 中发送 任何内容 就会 允许查看隐藏内容。
使用 Python 进行请求签名
pip install requests
pip install requests-aws4auth
pip install boto3
import boto3
import requests
from requests_aws4auth import AWS4Auth
region = 'us-east-1' # Region
service = 'execute-api'
access_key = 'YOUR_ACCESS_KEY'
secret_key = 'YOUR_SECRET_KEY'
url = 'https://<apiid>.execute-api.us-east-1.amazonaws.com/<stage>/<resource>'
session = boto3.Session(aws_access_key_id=access_key, aws_secret_access_key=secret_key)
credentials = session.get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)
response = requests.get(url, auth=awsauth)
print(response.text)
自定义 Lambda 授权器
可以使用一个基于给定令牌的 lambda,返回一个 IAM 策略,指示用户是否有权调用 API 端点。
您可以设置将使用授权器的每个资源方法。
Lambda 授权器代码示例
```python import jsondef lambda_handler(event, context): token = event[‘authorizationToken’] method_arn = event[‘methodArn’]
if not token: return { ‘statusCode’: 401, ‘body’: ‘Unauthorized’ }
try:
Replace this with your own token validation logic
if token == “your-secret-token”: return generate_policy(‘user’, ‘Allow’, method_arn) else: return generate_policy(‘user’, ‘Deny’, method_arn) except Exception as e: print(e) return { ‘statusCode’: 500, ‘body’: ‘Internal Server Error’ }
def generate_policy(principal_id, effect, resource): policy = { ‘principalId’: principal_id, ‘policyDocument’: { ‘Version’: ‘2012-10-17’, ‘Statement’: [ { ‘Action’: ‘execute-api:Invoke’, ‘Effect’: effect, ‘Resource’: resource } ] } } return policy
</details>
调用它时可以使用类似以下的命令:
<pre class="language-bash" data-overflow="wrap"><code class="lang-bash"><strong>curl "https://jhhqafgh6f.execute-api.eu-west-1.amazonaws.com/prod/custom_auth" -H 'Authorization: your-secret-token'
</strong></code></pre>
> [!WARNING]
> 根据Lambda代码,这个授权可能存在漏洞
请注意,如果生成并返回了**拒绝策略**,API Gateway返回的错误是:`{"Message":"User is not authorized to access this resource with an explicit deny"}`
这样你可以**识别出这个授权**的存在。
### 所需的API密钥
可以设置需要**有效API密钥**才能联系的API端点。
<figure><img src="../../../images/image (88).png" alt=""><figcaption></figcaption></figure>
可以在API Gateway门户中生成API密钥,甚至可以设置其使用量(每秒请求数和每月请求数)。
要使API密钥生效,需要将其添加到**使用计划**中,该使用计划必须添加到**API阶段**,并且相关的API阶段需要为需要API密钥的**端点**配置**方法限流**:
<figure><img src="../../../images/image (198).png" alt=""><figcaption></figcaption></figure>
## 未经身份验证的访问
<a class="content_ref" href="../aws-unauthenticated-enum-access/aws-api-gateway-unauthenticated-enum/index.html"><span class="content_ref_label">AWS - API Gateway Unauthenticated Enum</span></a>
## 权限提升
<a class="content_ref" href="../aws-privilege-escalation/aws-apigateway-privesc/index.html"><span class="content_ref_label">AWS - Apigateway Privesc</span></a>
## 后期利用
<a class="content_ref" href="../aws-post-exploitation/aws-api-gateway-post-exploitation/index.html"><span class="content_ref_label">AWS - API Gateway Post Exploitation</span></a>
## 持久性
<a class="content_ref" href="../aws-persistence/aws-api-gateway-persistence/index.html"><span class="content_ref_label">AWS - API Gateway Persistence</span></a>
> [!TIP]
> 学习并练习 AWS Hacking:<img src="../../../../../images/arte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://hacktricks-training.com/courses/arte)<img src="../../../../../images/arte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">\
> 学习并练习 GCP Hacking: <img src="../../../../../images/grte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">[**HackTricks Training GCP Red Team Expert (GRTE)**](https://hacktricks-training.com/courses/grte)<img src="../../../../../images/grte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">\
> 学习并练习 Az Hacking: <img src="../../../../../images/azrte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">[**HackTricks Training Azure Red Team Expert (AzRTE)**](https://hacktricks-training.com/courses/azrte)<img src="../../../../../images/azrte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">
>
> <details>
>
> <summary>支持 HackTricks</summary>
>
> - 查看 [**subscription plans**](https://github.com/sponsors/carlospolop)!
> - **加入** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) 或者 [**telegram group**](https://t.me/peass) 或 **关注** 我们的 **Twitter** 🐦 [**@hacktricks_live**](https://twitter.com/hacktricks_live)**.**
> - **通过向** [**HackTricks**](https://github.com/carlospolop/hacktricks) 和 [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github 仓库 提交 PRs 来分享 hacking tricks。
>
> </details>
HackTricks Cloud

