Your AWS CloudTrail logging bucket should use the Multi-Factor Authentication (MFA) Delete feature in order to prevent the deletion of any versioned log files.
Replace “your-bucket-name” with the actual name of your S3 bucket.
Click on “Save Changes” to update the Bucket Policy.
This will add a DenyDeleteWithoutMFA policy statement to the bucket policy, which will prevent any deletion of objects or modification of bucket policies without MFA authentication.
Create a new IAM policy that grants permission to delete objects from the S3 bucket only if MFA authentication is provided. You can use the following policy as an example:
Create a new IAM policy that grants permission to update the bucket policy to require MFA authentication for object deletions. You can use the following policy as an example:
Create a new IAM role that can assume the policy created in step 3 and attach the policy created in step 2 to it. You can use the following command to create the role:
Copy
Ask AI
aws iam create-role --role-name <role-name> --assume-role-policy-document file://trust-policy.json
Define the name of the S3 bucket that you want to remediate:
Copy
Ask AI
bucket_name = 'your-bucket-name'
Create an S3 client:
Copy
Ask AI
s3 = boto3.client('s3')
Check if MFA delete is enabled for the bucket:
Copy
Ask AI
try: response = s3.get_bucket_versioning(Bucket=bucket_name) if 'MFADelete' in response and response['MFADelete'] == 'Enabled': print('MFA Delete is already enabled for the bucket.') returnexcept ClientError as e: if e.response['Error']['Code'] == 'NoSuchBucketVersioning': print('Bucket versioning is not enabled for the bucket.') else: print(f'Error: {e}') return
If MFA delete is not enabled, enable it:
Copy
Ask AI
try: response = s3.put_bucket_versioning( Bucket=bucket_name, VersioningConfiguration={ 'Status': 'Enabled', 'MFADelete': 'Enabled' } ) print('MFA Delete has been enabled for the bucket.')except ClientError as e: print(f'Error: {e}')
Configure MFA delete for the bucket:
Copy
Ask AI
try: response = s3.put_bucket_mfa( Bucket=bucket_name, MFADelete='Enabled', MFADeleteEnable='Enabled', MFA='your-mfa-serial-number' ) print('MFA Delete has been configured for the bucket.')except ClientError as e: print(f'Error: {e}')
Note: Replace ‘your-bucket-name’ and ‘your-mfa-serial-number’ with the actual values of your S3 bucket name and MFA serial number respectively.