← 返回列表

Exfiltrating S3 Data with Bucket Replication Policies

单站 Hacking The Cloud 发布 Thu, 26 Jan 2023 01:02:06 +0000 入库 2026-11-18T05:01:01

Article by Ben Leembruggen

Exfiltrating S3 Data with Bucket Replication Policies

Introduction

S3 data replication provides the ability to copy objects to another bucket, which can be useful from an enterprise logging, integration or security perspective. This can be configure between buckets in the same account, or an unrelated account. Where this feature could be abused is where a malicious actor could input a replication policy to copy objects to an attacker controlled bucket. Objects will continue to be replicated for as long as the policy in place, applying to all future objects placed into the bucket. Using S3 batch operations, attackers can also replicate objects already in the bucket, making it a convenient method for extracting all current and future objects uploaded to the impacted bucket.

Required Configurations and Permissions

Pre-requisites

For bucket replication to be enabled, the following pre-requisites need to be in place:

IAM Role

Minimum Required IAM Permissions - Source Account

Like most things in AWS, the replication service requires a user supplied role to carry out the replication on your behalf. To replicate all data (including existing objects), an example trust policy and permission set would look something like:

Trust Policy

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": [
                    "s3.amazonaws.com",
                    "batchoperations.s3.amazonaws.com"
                ]
            },
            "Action": "sts:AssumeRole"
        }
    ]
}

IAM Permissions

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetReplicationConfiguration",
                "s3:ListBucket",
                "s3:PutInventoryConfiguration",
                "s3:InitiateReplication"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObjectVersionForReplication",
                "s3:GetObjectVersionAcl",
                "s3:GetObjectVersionTagging"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:ReplicateObject",
                "s3:ReplicateDelete",
                "s3:ReplicateTags"
            ],
            "Resource": "*"
        },
        {
         "Sid": "OnlyRequiredIfReplicatingEncryptedObjects",
         "Effect":"Allow",
         "Action":[
            "kms:Decrypt"
         ],
         "Condition":{
            "StringLike":{
               "kms:EncryptionContext:aws:s3:arn":[
                  "arn:aws:s3:::SOURCEBUCKET/*"
               ]
            }
         },
         "Resource":[
            "KEY ID IN SOURCE ACCOUNT CURRENTLY ENCRYPTING OBJECTS" 
         ]
      },
      {
        "Sid": "OnlyRequiredIfReplicatingEncryptedObjectsToo",
        "Effect":"Allow",
         "Action":[
            "kms:Encrypt"
         ],
         "Condition":{
            "StringLike":{
               "kms:EncryptionContext:aws:s3:arn":[
                  "arn:aws:s3:::DESTINATIONBUCKET/*"
               ]
            }
         },
         "Resource":[
            "KEY ID IN DESTINATION ACCOUNT TO BE SPECIFIED IN REPLICATION POLICY" 
         ]
      }
    ]
}

KMS - If objects are encrypted

Minimum Required IAM Permissions - Source Account

Where a KMS policy is configured to only allow a subset of principals to access an encrypted S3 object, the key policy will need to be updated to allow the above replication role access to decrypt the S3 objects.

Attacker Account Configuration

Minimum Required IAM Permissions (Destination Account)

In order for a bucket to receive logs from another account, it requires a bucket policy explicitly allowing the replication of objects across. An example of this policy is below.

Destination Bucket Policy

{
   "Version":"2012-10-17",
   "Id":"",
   "Statement":[
      {
         "Sid":"Set permissions for objects",
         "Effect":"Allow",
         "Principal":{
            "AWS":"arn:aws:iam::SOURCE_ACCOUNT_ID:role/S3_REPLICATION_ROLE"
         },
         "Action":[
            "s3:ReplicateObject", 
            "s3:ReplicateDelete"],
         "Resource":"arn:aws:s3:::DESTINATION_BUCKET/*"
      },
      {
         "Sid":"Set permissions on bucket",
         "Effect":"Allow",
         "Principal":{
            "AWS":"arn:aws:iam::SOURCE_ACCOUNT_ID:role/S3_REPLICATION_ROLE"
         },
         "Action":[
            "s3:List*", 
            "s3:GetBucketVersioning", 
            "s3:PutBucketVersioning"],
         "Resource":"arn:aws:s3:::DESTINATION_BUCKET"
      }
   ]
}

If the S3 objects in the source account are encrypted, a key must be created in the destination account to encrypt the objects on replication. Additionally, a pre-requisite of bucket replication is that Bucket Versioning is enabled on the destination bucket.

Configuring the replication

Minimum Required IAM Permissions - Source Account

The final step is to configure the replication between the source and destination buckets. Depending on whether you use the CLI or console, the steps can change slightly. The full process for both options is documented by AWS here.

In line with the steps above, ensure that:

What defenders can look for

S3 Batch Operation Job Created

原文链接:https://hackingthe.cloud/aws/exploitation/s3-bucket-replication-exfiltration/