IAM Groups Policy Blacklisted Check Remediation
Triage and Remediation
- Prevention
- Cause
- Remediation
How to Prevent
Using Console
To prevent blocked KMS actions in inline policies in IAM using the AWS Management Console, follow these steps:
-
Navigate to IAM Policies:
- Open the AWS Management Console.
- In the navigation pane, choose "Policies" under the "Access management" section.
-
Create or Edit a Policy:
- To create a new policy, click on the "Create policy" button.
- To edit an existing policy, find the policy you want to modify and click on its name, then click the "Edit policy" button.
-
Specify KMS Actions:
- In the policy editor, switch to the "JSON" tab.
- Ensure that the policy explicitly specifies the allowed KMS actions. For example:
{"Version": "2012-10-17","Statement": [{"Effect": "Allow","Action": ["kms:Encrypt","kms:Decrypt","kms:GenerateDataKey"],"Resource": "*"}]}
-
Review and Save:
- After specifying the allowed KMS actions, click on the "Review policy" button.
- Provide a name and description for the policy if creating a new one.
- Click on the "Create policy" or "Save changes" button to apply the policy.
By following these steps, you ensure that the inline policies in IAM explicitly allow the necessary KMS actions, preventing any misconfigurations related to blocked KMS actions.
Using CLI
To prevent blocked KMS actions in inline policies in IAM using AWS CLI, you can follow these steps:
-
Create a JSON Policy Document:
- First, create a JSON file that defines the inline policy with the necessary permissions and explicitly denies the blocked KMS actions.
- Example JSON policy (
policy.json):{"Version": "2012-10-17","Statement": [{"Effect": "Allow","Action": ["kms:Encrypt","kms:Decrypt","kms:GenerateDataKey"],"Resource": "*"},{"Effect": "Deny","Action": ["kms:DisableKey","kms:ScheduleKeyDeletion"],"Resource": "*"}]}
-
Attach the Inline Policy to an IAM User:
- Use the
put-user-policycommand to attach the inline policy to a specific IAM user. - Command:
aws iam put-user-policy --user-name <username> --policy-name <policy-name> --policy-document file://policy.json
- Use the
-
Attach the Inline Policy to an IAM Group:
- Use the
put-group-policycommand to attach the inline policy to a specific IAM group. - Command:
aws iam put-group-policy --group-name <groupname> --policy-name <policy-name> --policy-document file://policy.json
- Use the
-
Attach the Inline Policy to an IAM Role:
- Use the
put-role-policycommand to attach the inline policy to a specific IAM role. - Command:
aws iam put-role-policy --role-name <rolename> --policy-name <policy-name> --policy-document file://policy.json
- Use the
By following these steps, you can ensure that the necessary KMS actions are allowed while explicitly denying the blocked KMS actions in inline policies using AWS CLI.
Using Python
To prevent blocked KMS actions in inline policies in IAM using Python scripts, you can use the AWS SDK for Python (Boto3). Here are the steps to achieve this:
Step 1: Install Boto3
Ensure you have Boto3 installed in your Python environment. You can install it using pip if you haven't already:
pip install boto3
Step 2: Initialize Boto3 Client
Initialize the Boto3 client for IAM:
import boto3
iam_client = boto3.client('iam')
Step 3: Define the Inline Policy
Create a JSON structure for the inline policy that blocks specific KMS actions. For example, you can block the kms:Decrypt action:
inline_policy = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Deny",
"Action": [
"kms:Decrypt"
],
"Resource": "*"
}
]
}
Step 4: Attach the Inline Policy to an IAM User or Role
Attach the inline policy to a specific IAM user or role. Here’s an example of attaching it to a user:
user_name = 'your-iam-user-name'
policy_name = 'BlockKMSActionsPolicy'
response = iam_client.put_user_policy(
UserName=user_name,
PolicyName=policy_name,
PolicyDocument=json.dumps(inline_policy)
)
print(f"Policy {policy_name} attached to user {user_name}")
Full Script Example
Here is the complete script combining all the steps:
import boto3
import json
# Initialize Boto3 client
iam_client = boto3.client('iam')
# Define the inline policy
inline_policy = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Deny",
"Action": [
"kms:Decrypt"
],
"Resource": "*"
}
]
}
# Attach the inline policy to an IAM user
user_name = 'your-iam-user-name'
policy_name = 'BlockKMSActionsPolicy'
response = iam_client.put_user_policy(
UserName=user_name,
PolicyName=policy_name,
PolicyDocument=json.dumps(inline_policy)
)
print(f"Policy {policy_name} attached to user {user_name}")
Summary
- Install Boto3: Ensure Boto3 is installed in your Python environment.
- Initialize Boto3 Client: Set up the IAM client using Boto3.
- Define the Inline Policy: Create a JSON structure for the inline policy to block specific KMS actions.
- Attach the Inline Policy: Use the
put_user_policymethod to attach the policy to an IAM user.
By following these steps, you can prevent blocked KMS actions in inline policies using Python scripts.
Check Cause
Using Console
-
Sign in to the AWS Management Console and open the IAM console at https://console.aws.amazon.com/iam/.
-
In the navigation pane, choose "Policies". This will open a list of all the IAM policies that are currently configured in your AWS environment.
-
Select the policy you want to check for blocked KMS actions. This will open the policy details page.
-
In the policy details page, check the policy document for any "Deny" statements that are applied to KMS actions. If there are any "Deny" statements applied to KMS actions, then the policy is blocking those actions.
Using CLI
-
First, you need to install and configure AWS CLI on your local machine. You can do this by following the instructions provided by AWS. Make sure you have the necessary permissions to execute IAM related commands.
-
Once the AWS CLI is set up, you can list all the IAM policies using the following command:
aws iam list-policies --scope LocalThis command will return a list of all the IAM policies that are created within your AWS account.
-
For each policy, you can get the policy details including the policy document by using the following command:
aws iam get-policy-version --policy-arn <Policy_ARN> --version-id <Policy_Version_ID>Replace
<Policy_ARN>with the ARN of the policy and<Policy_Version_ID>with the version ID of the policy. This command will return the policy document which includes the permissions set by the policy. -
Now, you need to check the policy document for any blocked KMS actions. You can do this by looking for "kms:Deny" statements in the policy document. If you find any such statements, it means that some KMS actions are blocked in the inline policy. You can use a JSON parser or a script to automate this process. For example, in Python, you can use the
jsonmodule to parse the policy document and check for blocked KMS actions.
Using Python
-
Install and configure AWS SDK for Python (Boto3): You need to install and configure Boto3 to interact with AWS services. You can install it using pip:
pip install boto3Then, configure your AWS credentials either by setting the following environment variables:
AWS_ACCESS_KEY_ID = 'your_access_key'AWS_SECRET_ACCESS_KEY = 'your_secret_key'Or, you can create the credential file yourself at ~/.aws/credentials. At a minimum, it should look like this:
[default]aws_access_key_id = YOUR_ACCESS_KEYaws_secret_access_key = YOUR_SECRET_KEY -
Use Boto3 to list all IAM policies: You can use the
list_policiesmethod to retrieve all IAM policies. Here is a sample script:import boto3# Create IAM clientiam = boto3.client('iam')# List policiesresponse = iam.list_policies(Scope='All')for policy in response['Policies']:print(policy['PolicyName']) -
Get the policy details: For each policy, you can use the
get_policymethod to retrieve the policy details, including the policy document. Here is a sample script:import boto3# Create IAM clientiam = boto3.client('iam')# Get policyresponse = iam.get_policy(PolicyArn='arn:aws:iam::aws:policy/AdministratorAccess')policy_document = response['Policy']['PolicyDocument']print(policy_document) -
Check for blocked KMS actions: You can parse the policy document to check if it contains any blocked KMS actions. Here is a sample script:
import boto3import json# Create IAM clientiam = boto3.client('iam')# Get policyresponse = iam.get_policy(PolicyArn='arn:aws:iam::aws:policy/AdministratorAccess')policy_document = response['Policy']['PolicyDocument']# Parse policy documentpolicy_document = json.loads(policy_document)# Check for blocked KMS actionsfor statement in policy_document['Statement']:if 'kms:Decrypt' in statement['Action'] and statement['Effect'] == 'Deny':print('Blocked KMS action found: kms:Decrypt')This script checks if the 'kms:Decrypt' action is blocked. You can modify it to check for other KMS actions.
Remediation
Using Console
To remediate the "Blocked KMS Actions In Inline Policies Should Be Set" misconfiguration in AWS IAM using the AWS Management Console, follow these steps:
-
Login to AWS Console: Go to the AWS Management Console (https://aws.amazon.com/console/) and login to your AWS account.
-
Navigate to IAM Service: Click on the "Services" dropdown in the top left corner and select "IAM" under the Security, Identity, & Compliance section.
-
Select the User/Role/Group: In the left-hand navigation pane, choose the appropriate option (User, Role, or Group) where the inline policy with blocked KMS actions exists.
-
Locate the Inline Policy: Scroll down to the "Permissions" tab and find the inline policy that contains blocked KMS actions. Click on the policy to edit it.
-
Edit the Inline Policy: In the policy editor, locate the section where KMS actions are blocked. This may look like:
{"Effect": "Deny","Action": "kms:Encrypt","Resource": "*"}Remove the block or update it to allow the necessary KMS actions as needed.
-
Save Changes: After making the necessary changes to the inline policy, click on the "Review policy" button to review the changes.
-
Review and Apply Changes: Review the policy changes to ensure that the necessary KMS actions are now allowed. If everything looks correct, click on the "Save changes" button to apply the updated policy.
-
Verify Changes: Once the policy is saved, verify that the blocked KMS actions have been remediated by attempting to perform the previously blocked actions. You can also use the IAM Policy Simulator to validate the changes.
By following these steps, you should be able to remediate the "Blocked KMS Actions In Inline Policies Should Be Set" misconfiguration in AWS IAM using the AWS Management Console.
Using CLI
To remediate the misconfiguration of blocked KMS actions in inline policies in AWS IAM using AWS CLI, follow these steps:
-
Identify the IAM user, group, or role that has inline policies with blocked KMS actions.
-
Use the AWS CLI to list all the inline policies attached to the IAM entity. You can use the following command:
aws iam list-user-policies --user-name <IAM-USER-NAME>aws iam list-group-policies --group-name <IAM-GROUP-NAME>aws iam list-role-policies --role-name <IAM-ROLE-NAME> -
For each inline policy identified in step 2, use the AWS CLI to get the policy document:
aws iam get-user-policy --user-name <IAM-USER-NAME> --policy-name <POLICY-NAME>aws iam get-group-policy --group-name <IAM-GROUP-NAME> --policy-name <POLICY-NAME>aws iam get-role-policy --role-name <IAM-ROLE-NAME> --policy-name <POLICY-NAME> -
Review the policy document to identify any blocked KMS actions. Look for statements that deny access to KMS actions.
-
Modify the policy document to allow the required KMS actions. Remove any explicit denies for KMS actions.
-
Update the inline policy with the modified policy document. Use the following command:
aws iam put-user-policy --user-name <IAM-USER-NAME> --policy-name <POLICY-NAME> --policy-document file://<UPDATED-POLICY-DOCUMENT>aws iam put-group-policy --group-name <IAM-GROUP-NAME> --policy-name <POLICY-NAME> --policy-document file://<UPDATED-POLICY-DOCUMENT>aws iam put-role-policy --role-name <IAM-ROLE-NAME> --policy-name <POLICY-NAME> --policy-document file://<UPDATED-POLICY-DOCUMENT> -
Verify that the inline policies no longer have blocked KMS actions by listing the policies and reviewing the policy documents.
By following these steps, you can remediate the misconfiguration of blocked KMS actions in inline policies in AWS IAM using AWS CLI.
Using Python
To remediate the issue of blocked KMS actions in inline policies in AWS IAM using Python, follow these steps:
- Identify the IAM user or role with the inline policy that contains blocked KMS actions.
- Use the AWS SDK for Python (Boto3) to update the inline policy to allow the necessary KMS actions.
- Modify the inline policy to include the required KMS actions.
Here is a sample Python script to remediate this issue:
import boto3
# Initialize the IAM client
iam_client = boto3.client('iam')
# Specify the IAM user or role name
user_name = 'YOUR_USER_NAME'
policy_name = 'YOUR_POLICY_NAME'
# Specify the required KMS actions
kms_actions = [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:DescribeKey"
]
# Get the current policy document
policy_document = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": kms_actions,
"Resource": "*"
}
]
}
# Update the inline policy
response = iam_client.put_user_policy(
UserName=user_name,
PolicyName=policy_name,
PolicyDocument=json.dumps(policy_document)
)
print("Policy updated successfully.")
Replace 'YOUR_USER_NAME' and 'YOUR_POLICY_NAME' with the actual IAM user or role name and policy name that need to be remediated. Update the kms_actions list with the required KMS actions that should be allowed in the inline policy.
Run this Python script to update the inline policy and allow the necessary KMS actions.
Using Terraform
# IAM group definition (already present in your config)
resource "aws_iam_group" "EXAMPLE_GROUP" {
name = "EXAMPLE_GROUP_NAME" # replace with your IAM group name
}
# REMOVE or UPDATE any attachment that references the blacklisted policy ARN.
# This resource is the Terraform equivalent of:
# aws iam detach-group-policy --group-name ... --policy-arn ...
# Example of an attachment that MUST be deleted or changed:
# (this is the misconfiguration)
resource "aws_iam_group_policy_attachment" "EXAMPLE_GROUP_BLACKLISTED_ATTACHMENT" {
group = aws_iam_group.EXAMPLE_GROUP.name
policy_arn = "BLACKLISTED_POLICY_ARN" # replace with the exact violating policy ARN, then remove/change it
}
# To remediate:
# 1. Either delete the above aws_iam_group_policy_attachment resource from your Terraform code
# OR change policy_arn to a NON‑blacklisted policy ARN.
# 2. Keep any remaining attachments only for allowed policies, for example:
resource "aws_iam_group_policy_attachment" "EXAMPLE_GROUP_ALLOWED_ATTACHMENT" {
group = aws_iam_group.EXAMPLE_GROUP.name
policy_arn = "ALLOWED_POLICY_ARN" # replace with an approved IAM policy ARN
}
This change does not replace the IAM group itself, but it does remove permissions from all users in the group when the attachment is destroyed or changed; review the policy’s permissions before applying.
Verification: terraform plan should show the aws_iam_group_policy_attachment that used the BLACKLISTED_POLICY_ARN being destroyed (or its policy_arn argument changing to a non‑blacklisted ARN).