KMS Keys Should Not Allow Unknown Cross Account Access
More Info:
All your AWS Key Management Service keys should be configured to be accessed only by trusted AWS accounts in order to protect against unauthorized cross account access. This will help prevent data breaches and loss.
Risk Level
High
Address
Security
Compliance Standards
- APRA CPS 234 (Australia)
- AWS Well Architected Framework
- BSI C5 (Germany)
- Brazil LGPD
- CCPA / CPRA (California)
- CIS Critical Security Controls v8
- CMMC 2.0
- CSA Cloud Controls Matrix v4
- DPDPA
- Digital Operational Resilience Act (EU)
- Essential 8
- ISO/IEC 27017
- ISO/IEC 27018
- ISO/IEC 27701
- KSA PDPL
- MAS Technology Risk Management (Singapore)
- MITRE ATT&CK (Cloud)
- NIS2 Directive
- NIST SP 800-171
- NYDFS 23 NYCRR 500
- SWIFT Customer Security Controls Framework
- Sarbanes-Oxley IT General Controls
- UK NCSC Cyber Assessment Framework
Triage and Remediation
- Remediation
Remediation
Using Console
To remediate the KMS Keys Should Not Allow Unknown Cross Account Access misconfiguration for AWS using the AWS console, follow the below steps:
-
Log in to the AWS Management Console.
-
Go to the AWS Key Management Service (KMS) console.
-
Click on the "Customer managed keys" link located in the left-hand menu.
-
Select the KMS key that is not configured properly.
-
Click on the "Key policy" tab.
-
Click on the "Edit" button.
-
In the "Principal" section, remove any unknown or unauthorized cross-account access.
-
Add the appropriate AWS account IDs or IAM roles that should have access to the KMS key.
-
Click on the "Review policy" button.
-
Review the changes and ensure that they are correct.
-
Click on the "Save changes" button.
-
Verify that the KMS key is now properly configured by testing it with authorized access.
By following these steps, you will have successfully remediated the KMS Keys Should Not Allow Unknown Cross Account Access misconfiguration for AWS using the AWS console.
Using CLI
To remediate the misconfiguration "KMS Keys Should Not Allow Unknown Cross Account Access" in AWS using AWS CLI, follow the below steps:
-
Identify the KMS keys that allow unknown cross-account access:
aws kms list-keys --query "Keys[?KeyManager != 'AWS' && KeyState == 'Enabled']" --output text | awk '{print $1}' | while read key_id; do aws kms get-key-policy --key-id $key_id --policy-name default --query Policy --output text | grep -q "Effect\": \"Allow" && echo $key_id; done -
Remove the unknown cross-account access from the KMS key policy:
aws kms list-keys --query "Keys[?KeyManager != 'AWS' && KeyState == 'Enabled']" --output text | awk '{print $1}' | while read key_id; do aws kms get-key-policy --key-id $key_id --policy-name default --query Policy --output text | sed '/Effect\": \"Allow\"/,/\"Principal\": {/d' > policy.json && aws kms put-key-policy --key-id $key_id --policy-name default --policy file://policy.json && rm policy.json; done -
Verify that the unknown cross-account access has been removed:
aws kms list-keys --query "Keys[?KeyManager != 'AWS' && KeyState == 'Enabled']" --output text | awk '{print $1}' | while read key_id; do aws kms get-key-policy --key-id $key_id --policy-name default --query Policy --output text | grep -q "Effect\": \"Allow" && echo $key_id; doneIf there is no output, then the remediation is successful.
Using Python
To remediate the misconfiguration "KMS Keys Should Not Allow Unknown Cross Account Access" for AWS using Python, you can follow these steps:
-
Identify the KMS keys that are allowing unknown cross-account access. You can do this by using the AWS CLI command
aws kms list-keysto get a list of all the KMS keys and then using theaws kms describe-keycommand to get the key policy for each key. -
Check the key policy to see if it allows unknown cross-account access. You can do this by looking for the
"AWS"field in thePrincipalelement of the policy. If the"AWS"field is set to"*"or does not include a specific account ID, then the key allows unknown cross-account access. -
Modify the key policy to remove the unknown cross-account access. You can do this by using the
aws kms put-key-policycommand to update the key policy. You will need to specify the key ID, the policy name, and the new policy document that removes the"AWS"field or replaces it with a specific account ID. -
Test the modified key policy to ensure that it no longer allows unknown cross-account access. You can do this by using the
aws kms encryptcommand to encrypt a test message using the modified key. If the encryption is successful, then the key policy has been remediated.
Here is an example Python script that can be used to remediate the misconfiguration:
import boto3
import json
# Initialize the KMS client
kms = boto3.client('kms')
# Get a list of all KMS keys
response = kms.list_keys()
# Loop through each key
for key in response['Keys']:
# Get the key policy
key_policy = kms.get_key_policy(KeyId=key['KeyId'], PolicyName='default')['Policy']
# Parse the policy document
policy_doc = json.loads(key_policy)
# Check if the policy allows unknown cross-account access
if 'AWS' in policy_doc['Statement'][0]['Principal'] and \
('*' in policy_doc['Statement'][0]['Principal']['AWS'] or \
len(policy_doc['Statement'][0]['Principal']['AWS']) == 1):
# Modify the policy to remove the unknown cross-account access
policy_doc['Statement'][0]['Principal']['AWS'] = ['arn:aws:iam::123456789012:root']
# Update the key policy
response = kms.put_key_policy(KeyId=key['KeyId'], PolicyName='default', Policy=json.dumps(policy_doc))
# Print the result
print('Key policy updated for key ' + key['KeyId'])
Note: Replace the account ID 123456789012 with your own account ID.
Using Terraform
data "aws_caller_identity" "current" {}
data "aws_iam_policy_document" "kms_key_policy" {
# KMS key policy that DOES NOT include unknown cross-account principals.
# Add ONLY explicitly trusted principals here; remove any untrusted
# "arn:aws:iam::ACCOUNT-ID-WITHOUT-HYPHENS:root"
# entries that previously granted cross-account access.
statement {
sid = "EnableRootPermissions"
effect = "Allow"
principals {
type = "AWS"
identifiers = [
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:root",
# OPTIONAL: add other trusted principals explicitly, e.g. a specific role:
# "arn:aws:iam::TRUSTED_ACCOUNT_ID:role/TRUSTED_ROLE_NAME",
]
}
actions = ["kms:*"]
resources = ["*"]
}
# Add any additional statements required for services or roles in
# YOUR account, but do not reintroduce unknown cross-account principals.
}
resource "aws_kms_key" "this" {
description = "KMS key with restricted key policy"
deletion_window_in_days = 30
enable_key_rotation = true
policy = data.aws_iam_policy_document.kms_key_policy.json
}
This Terraform replaces the existing KMS key policy with one that omits untrusted cross-account principals and allows only your account root (and any explicitly added trusted principals). Removing or changing the policy on an existing aws_kms_key does not force key replacement, but an incorrect policy can lock you out of the key, so mirror all required in-account/service permissions when editing the policy document.
After updating, terraform plan should show a single ~ update in-place on aws_kms_key.this with a change only to the policy JSON.