Roles Should Have MFA and External ID Set
More Info:
This rule identifies IAM roles that do not require multi-factor authentication (MFA) or external ID for assumed roles. Roles without MFA or external ID can pose security risks, as they may allow unauthorized access or increase the attack surface for potential breaches. Enforcing MFA and external ID requirements adds an additional layer of security to IAM roles and helps prevent unauthorized access.
Risk Level
High
Address
Security
Compliance Standards
- APRA CPS 234 (Australia)
- BSI C5 (Germany)
- Brazil LGPD
- CCPA / CPRA (California)
- CIS Critical Security Controls v8
- CMMC 2.0
- CSA Cloud Controls Matrix v4
- Cloudanix Best Practice
- 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
- Prevention
- Cause
- Remediation
How to Prevent
Using Console
To prevent the misconfiguration where the root account should have Multi-Factor Authentication (MFA) and an external ID set in AWS Identity and Access Management (IAM) using the AWS Management Console, follow these steps:
-
Enable MFA for the Root Account:
- Sign in to the AWS Management Console using your root account credentials.
- Navigate to the IAM dashboard.
- In the left navigation pane, select Dashboard.
- Under Security Status, find the section labeled Activate MFA on your root account and click on Manage MFA.
- Follow the on-screen instructions to enable MFA for the root account. You can choose between a virtual MFA device, a U2F security key, or other supported MFA devices.
-
Create an External ID for Cross-Account Access:
- Go to the IAM dashboard.
- In the left navigation pane, select Roles.
- Click on Create role.
- Select Another AWS account as the type of trusted entity.
- Enter the Account ID of the external account that will assume this role.
- In the Options section, enter a unique External ID. This ID should be shared with the external account that will assume the role.
- Click Next: Permissions to attach the necessary policies and complete the role creation process.
-
Review and Update IAM Policies:
- In the IAM dashboard, navigate to Policies in the left navigation pane.
- Review existing policies to ensure they do not grant excessive permissions to the root account.
- Update policies as necessary to follow the principle of least privilege.
-
Monitor and Audit IAM Activities:
- Enable AWS CloudTrail to log all API calls made in your AWS account.
- Regularly review CloudTrail logs to monitor activities performed by the root account.
- Set up AWS Config rules to continuously monitor and alert on any changes to the root account's MFA status or IAM roles.
By following these steps, you can ensure that the root account in your AWS environment is secured with MFA and that an external ID is set for cross-account access, thereby reducing the risk of unauthorized access.
Using CLI
To prevent the misconfiguration where the root account should have Multi-Factor Authentication (MFA) and an External ID set in AWS Identity and Access Management (IAM) using AWS CLI, follow these steps:
-
Enable MFA on the Root Account:
- First, list the MFA devices associated with the root account to ensure none are already configured:
aws iam list-mfa-devices --user-name root
- If no MFA devices are listed, you can enable MFA by creating a virtual MFA device and associating it with the root account. First, create the virtual MFA device:
aws iam create-virtual-mfa-device --virtual-mfa-device-name root-account-mfa --outfile /path/to/root-account-mfa.png
- Then, enable the MFA device for the root account. You will need the authentication codes from the virtual MFA device:
aws iam enable-mfa-device --user-name root --serial-number arn:aws:iam::account-id:mfa/root-account-mfa --authentication-code1 123456 --authentication-code2 654321
- First, list the MFA devices associated with the root account to ensure none are already configured:
-
Set an External ID for IAM Roles:
- Identify the IAM role that requires an external ID. List the roles to find the specific role:
aws iam list-roles
- Update the trust policy of the IAM role to include an external ID. First, get the current trust policy:
aws iam get-role --role-name YourRoleName
- Modify the trust policy JSON to include the
sts:ExternalIdcondition. Here is an example of a trust policy with an external ID:{"Version": "2012-10-17","Statement": [{"Effect": "Allow","Principal": {"AWS": "arn:aws:iam::account-id:root"},"Action": "sts:AssumeRole","Condition": {"StringEquals": {"sts:ExternalId": "YourExternalID"}}}]} - Update the role with the modified trust policy:
aws iam update-assume-role-policy --role-name YourRoleName --policy-document file://path/to/modified-trust-policy.json
- Identify the IAM role that requires an external ID. List the roles to find the specific role:
-
Verify MFA and External ID Configuration:
- Verify that the MFA device is enabled for the root account:
aws iam list-mfa-devices --user-name root
- Verify the trust policy of the IAM role to ensure the external ID is set correctly:
aws iam get-role --role-name YourRoleName
- Verify that the MFA device is enabled for the root account:
-
Automate Checks Using AWS CLI Scripts:
- Create a script to periodically check and ensure that MFA is enabled and the external ID is set. Here is a simple example in Bash:
#!/bin/bash# Check MFA for root accountMFA_DEVICES=$(aws iam list-mfa-devices --user-name root)if [ -z "$MFA_DEVICES" ]; thenecho "MFA is not enabled for the root account."elseecho "MFA is enabled for the root account."fi# Check External ID for a specific roleROLE_NAME="YourRoleName"TRUST_POLICY=$(aws iam get-role --role-name $ROLE_NAME)if echo $TRUST_POLICY | grep -q "sts:ExternalId"; thenecho "External ID is set for the role $ROLE_NAME."elseecho "External ID is not set for the role $ROLE_NAME."fi
- Create a script to periodically check and ensure that MFA is enabled and the external ID is set. Here is a simple example in Bash:
By following these steps, you can ensure that the root account has MFA enabled and that IAM roles have an external ID set, thereby preventing the misconfiguration using AWS CLI.
Using Python
To prevent the misconfiguration where the root account should have Multi-Factor Authentication (MFA) and an External ID set in IAM using Python scripts, you can follow these steps:
1. Install Required Libraries
Ensure you have the necessary libraries installed. You will need boto3 for AWS, azure-identity and azure-mgmt-resource for Azure, and google-auth and google-api-python-client for GCP.
pip install boto3 azure-identity azure-mgmt-resource google-auth google-api-python-client
2. AWS: Enforce MFA on Root Account
import boto3
def enforce_mfa_on_root():
iam_client = boto3.client('iam')
# List MFA devices for the root account
mfa_devices = iam_client.list_mfa_devices(UserName='root')
if not mfa_devices['MFADevices']:
print("Root account does not have MFA enabled. Please enable MFA.")
else:
print("Root account has MFA enabled.")
enforce_mfa_on_root()
3. Azure: Enforce MFA on Root Account
Azure does not have a direct equivalent of a "root" account, but you can enforce MFA for all users in the directory.
from azure.identity import DefaultAzureCredential
from azure.mgmt.resource import ResourceManagementClient
def enforce_mfa_on_root():
credential = DefaultAzureCredential()
client = ResourceManagementClient(credential, '<subscription_id>')
# This is a placeholder for enforcing MFA. Azure AD Conditional Access policies should be used.
print("Ensure that Conditional Access policies enforce MFA for all users.")
enforce_mfa_on_root()
4. GCP: Enforce MFA on Root Account
GCP also does not have a direct equivalent of a "root" account, but you can enforce MFA for all users in the organization.
from google.oauth2 import service_account
from googleapiclient.discovery import build
def enforce_mfa_on_root():
credentials = service_account.Credentials.from_service_account_file('path/to/your/service-account-file.json')
service = build('admin', 'directory_v1', credentials=credentials)
# This is a placeholder for enforcing MFA. GCP Identity Platform should be used.
print("Ensure that Identity Platform enforces MFA for all users.")
enforce_mfa_on_root()
Summary
- Install Required Libraries: Ensure you have the necessary Python libraries installed.
- AWS: Use
boto3to check and enforce MFA on the root account. - Azure: Use
azure-identityandazure-mgmt-resourceto ensure Conditional Access policies enforce MFA. - GCP: Use
google-authandgoogle-api-python-clientto ensure Identity Platform enforces MFA.
These scripts provide a basic framework to check and enforce MFA on root accounts or equivalent in AWS, Azure, and GCP. For a complete solution, you would need to integrate these checks into your CI/CD pipeline or monitoring system.
Check Cause
Using Console
- Sign in to the AWS Management Console as a root user or an IAM user.
- In the navigation pane, choose "Users". If the AWS account root user is signed in, the navigation pane is not available. In that case, choose "Dashboard" and then choose "Manage Security Credentials".
- In the "User" section, find the user that you want to check for MFA. In the "Security Credentials" column, look for a checkmark in the MFA column. If there's a checkmark, the user has MFA enabled. If there's no checkmark, the user doesn't have MFA enabled.
- To check for External ID, navigate to the IAM role that you want to check. Under the "Trust relationships" tab, click on "Edit trust relationship". In the policy document, look for "sts:ExternalId". If it's present, then the External ID is set. If it's not present, then the External ID is not set.
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. Once you have AWS CLI installed and configured, you can proceed to the next steps.
-
To check if the root account has MFA enabled, you can use the following command:
aws iam get-account-summaryThis command will return a JSON object that contains information about the account. Look for the "AccountMFAEnabled" field in the output. If the value of this field is 1, then MFA is enabled for the root account. If the value is 0, then MFA is not enabled.
-
To check if the root account has an external ID set, you can use the following command:
aws iam list-rolesThis command will return a list of all roles in the account. For each role, check the "AssumeRolePolicyDocument" field in the output. If the policy document contains a "sts:ExternalId" condition, then the role has an external ID set.
-
If you want to automate these checks, you can write a Python script that uses the Boto3 library to interact with the AWS API. The script would use the
get_account_summaryandlist_rolesmethods of theIAMclient to perform the same checks as the CLI commands above.
Using Python
- AWS SDK for Python (Boto3) can be used to interact with AWS services. To check if the root account has MFA enabled, you can use the IAM client in Boto3. First, you need to import Boto3 and create an IAM client:
import boto3
iam = boto3.client('iam')
- Then, you can use the
get_account_summarymethod to get information about the account. This method returns a dictionary with various account attributes. TheAccountMFAEnabledattribute indicates whether MFA is enabled for the root account:
response = iam.get_account_summary()
if response['SummaryMap']['AccountMFAEnabled'] == 1:
print("MFA is enabled for the root account.")
else:
print("MFA is not enabled for the root account.")
- To check if the root account has an external ID set, you can use the
list_rolesmethod to get a list of all IAM roles in the account. Then, you can iterate over the roles and check theAssumeRolePolicyDocumentattribute for each role. If thests:ExternalIdcondition is present in the policy document, it means that the role requires an external ID for access:
roles = iam.list_roles()['Roles']
for role in roles:
policy_document = role['AssumeRolePolicyDocument']
if 'sts:ExternalId' in str(policy_document):
print(f"Role {role['RoleName']} requires an external ID.")
- Note that the above script only checks if any role in the account requires an external ID. It does not check if the root account specifically has an external ID set. This is because the root account does not have an associated IAM role and therefore does not have an
AssumeRolePolicyDocument. The external ID is typically used in a cross-account role scenario, where one AWS account assumes a role in another account. In this case, the external ID is set in the trusting account (the account that owns the role), not in the trusted account (the account that assumes the role).
Remediation
Using Console
To remediate the misconfiguration in AWS IAM where the root account should have MFA and External ID set, follow these steps using the AWS Management Console:
-
Enable Multi-Factor Authentication (MFA) for the Root Account:
- Log in to the AWS Management Console using the root account credentials.
- Navigate to the IAM service.
- In the navigation pane, click on "Users".
- Click on the root account username.
- In the "Security credentials" tab, locate the "Assigned MFA device" section and click on "Manage".
- Follow the prompts to set up MFA for the root account. You can choose to use a virtual MFA device or a hardware MFA device.
- Once MFA is enabled, make sure to complete the MFA setup process.
-
Set an External ID for the Root Account:
- While still in the IAM Management Console, click on the root account username.
- In the "Permissions" tab, click on the "Add inline policy" button.
- Select the JSON tab to provide a custom policy.
- Enter a policy document similar to the following, replacing
YOUR_EXTERNAL_IDwith your desired external ID:
{"Version": "2012-10-17","Statement": [{"Effect": "Allow","Action": "*","Resource": "*","Condition": {"StringEquals": {"sts:ExternalId": "YOUR_EXTERNAL_ID"}}}]}- Click on Review policy, provide a name for the policy, and click on "Create policy".
-
Test the External ID:
- To test the External ID, you can try to assume a role that requires the External ID. If the External ID is set correctly, the assumption of the role should succeed.
By following these steps, you will have successfully remediated the misconfiguration in AWS IAM where the root account should have MFA and an External ID set.
Using CLI
To remediate the misconfiguration of the root account not having MFA and External ID set in AWS IAM using AWS CLI, follow these steps:
-
Enable MFA for Root Account:
- Run the following AWS CLI command to enable MFA for the root account:
Replaceaws iam enable-mfa-device --user-name <root_account_username> --serial-number arn:aws:iam::<account_id>:mfa/root-account-mfa
<root_account_username>with the root account's username and<account_id>with your AWS account ID.
- Run the following AWS CLI command to enable MFA for the root account:
-
Set External ID for Root Account:
- Generate a random external ID using a tool like
openssl:openssl rand -hex 32 - Copy the generated External ID.
- Generate a random external ID using a tool like
-
Attach the Policy to the Root Account:
- Run the following AWS CLI command to attach the
IAMFullAccesspolicy to the root account with the External ID:Replaceaws iam attach-user-policy --user-name <root_account_username> --policy-arn arn:aws:iam::aws:policy/IAMFullAccess --policy-inputs '{"ExternalId":"<generated_external_id>"}'<root_account_username>with the root account's username and<generated_external_id>with the External ID you generated in step 2.
- Run the following AWS CLI command to attach the
-
Verify Configuration:
- To verify that MFA and External ID are set for the root account, run the following AWS CLI commands:
- Check MFA status:
aws iam list-mfa-devices --user-name <root_account_username>
- Check attached policies with External ID:
aws iam list-attached-user-policies --user-name <root_account_username>
- Check MFA status:
- To verify that MFA and External ID are set for the root account, run the following AWS CLI commands:
By following these steps, you can remediate the misconfiguration of the root account not having MFA and External ID set in AWS IAM using AWS CLI.
Using Python
To remediate the misconfiguration in AWS IAM where the root account should have MFA and External ID set, you can use the AWS SDK for Python (Boto3) to automate the process. Here are the step-by-step instructions to remediate this issue:
-
Install Boto3: If you haven't installed Boto3 yet, you can install it using pip:
pip install boto3 -
Create a Python script: Create a Python script (e.g.,
remediate_root_account_mfa.py) and import the necessary libraries:import boto3 -
Enable MFA for the root account: You can use the following code snippet to enable MFA for the root account:
iam_client = boto3.client('iam')iam_client.enable_mfa_device(UserName='root', SerialNumber='arn:aws:iam::aws:policy/IAMUser') -
Set External ID for the root account: You can use the following code snippet to set an External ID for the root account:
account_id = boto3.client('sts').get_caller_identity().get('Account')external_id = 'your_external_id_here'iam_client.create_account_alias(AccountAlias=external_id) -
Run the Python script: Execute the Python script using the command:
python remediate_root_account_mfa.py
By following these steps, you can remediate the misconfiguration in AWS IAM where the root account should have MFA and External ID set using Python and Boto3.
Using Terraform
resource "aws_iam_role" "ROLE_NAME" {
name = "ROLE_NAME" # replace with your IAM role name
# Trust policy requiring MFA and External ID for sts:AssumeRole
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Principal = {
AWS = "arn:aws:iam::TRUSTED_ACCOUNT_ID:root" # or specific principal ARN
}
Action = "sts:AssumeRole"
Condition = {
Bool = {
"aws:MultiFactorAuthPresent" = "true"
}
StringEquals = {
"sts:ExternalId" = "YOUR_UNIQUE_EXTERNAL_ID" # replace with the third party's external ID
}
}
}
]
})
}
Applying this change will immediately prevent principals without MFA or without the correct external ID from assuming this role, but it does not force replacement of the role (in-place update of the trust policy only).
To verify, terraform plan should show an in-place update to aws_iam_role.ROLE_NAME with only the assume_role_policy JSON changing to include the Bool aws:MultiFactorAuthPresent and StringEquals sts:ExternalId conditions.