Sub topic 11
Protecting the trail and its logs
A trail is only useful as evidence if an attacker cannot switch it off, edit its logs, or read them without permission. This chapter covers four controls: the S3 bucket policy, encryption with AWS KMS, IAM permissions on CloudTrail itself, and detection of tampering.
This chapter assumes you can read IAM and S3 bucket policies, can run the AWS CLI, and have already created a trail that delivers to S3 (see the earlier chapters on trails and S3 delivery). Examples reuse the trail org-audit-trail, the bucket my-cloudtrail-logs-bucket, and the account 123456789012.
What you are protecting against
Three kinds of failure matter for audit logs.
- Stopping the recorder. Someone calls
StopLogging,DeleteTrail, orUpdateTrail, or narrows the event selectors so that sensitive events are no longer captured. - Altering or deleting delivered logs. Someone with S3 write access removes or rewrites log objects to hide their actions.
- Unintended reading. Logs contain user names, ARNs, IP addresses, and request parameters. That information helps an attacker map your account.
No single control covers all three, so you layer them.
The bucket policy
When you create a trail in the console, CloudTrail can add the required bucket policy for you. With the CLI, you write it yourself. The policy gives the CloudTrail service principal two permissions: read the bucket ACL, and write objects under the AWSLogs/ prefix with the bucket-owner-full-control ACL.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AWSCloudTrailAclCheck",
"Effect": "Allow",
"Principal": { "Service": "cloudtrail.amazonaws.com" },
"Action": "s3:GetBucketAcl",
"Resource": "arn:aws:s3:::my-cloudtrail-logs-bucket",
"Condition": {
"StringEquals": {
"aws:SourceArn": "arn:aws:cloudtrail:us-east-1:123456789012:trail/org-audit-trail"
}
}
},
{
"Sid": "AWSCloudTrailWrite",
"Effect": "Allow",
"Principal": { "Service": "cloudtrail.amazonaws.com" },
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::my-cloudtrail-logs-bucket/AWSLogs/123456789012/*",
"Condition": {
"StringEquals": {
"s3:x-amz-acl": "bucket-owner-full-control",
"aws:SourceArn": "arn:aws:cloudtrail:us-east-1:123456789012:trail/org-audit-trail"
}
}
}
]
}Apply it with:
aws s3api put-bucket-policy \
--bucket my-cloudtrail-logs-bucket \
--policy file://cloudtrail-bucket-policy.jsonWhy the aws:SourceArn condition matters
Without a condition, the policy trusts the CloudTrail service for every account. Another AWS customer could then configure their own trail to write into your bucket if they knew its name. This is the confused deputy problem. The aws:SourceArn condition limits writes to your specific trail. You can use aws:SourceAccount instead if you want to allow any trail in your account.
Keep everyone else out
The bucket policy is also where you restrict human and application access. Good practice for a log bucket:
- Turn on S3 Block Public Access for the bucket and the account.
- Deny
s3:*over non-TLS connections with aDenystatement onaws:SecureTransportset tofalse. - Allow read access only to a small set of security roles, such as an auditor role.
- Do not grant
s3:DeleteObjectors3:PutBucketPolicyto everyday administrators. - Keep the log bucket in a separate, dedicated account where possible, so that a compromise of a workload account does not give access to its own audit evidence.
Encrypting logs with KMS
By default, CloudTrail encrypts delivered log files with Amazon S3 server-side encryption using S3-managed keys (SSE-S3). You can instead use SSE-KMS with a customer managed key. This adds a second gate: to read a log file, a principal needs both S3 read access and kms:Decrypt on the key.
Key policy requirements
The key policy must let CloudTrail generate data keys and describe the key. It must also let your auditors decrypt. A trimmed example:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowCloudTrailEncrypt",
"Effect": "Allow",
"Principal": { "Service": "cloudtrail.amazonaws.com" },
"Action": "kms:GenerateDataKey*",
"Resource": "*",
"Condition": {
"StringEquals": {
"aws:SourceArn": "arn:aws:cloudtrail:us-east-1:123456789012:trail/org-audit-trail"
},
"StringLike": {
"kms:EncryptionContext:aws:cloudtrail:arn": "arn:aws:cloudtrail:*:123456789012:trail/*"
}
}
},
{
"Sid": "AllowCloudTrailDescribeKey",
"Effect": "Allow",
"Principal": { "Service": "cloudtrail.amazonaws.com" },
"Action": "kms:DescribeKey",
"Resource": "*"
},
{
"Sid": "AllowAuditorsToDecrypt",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::123456789012:role/SecurityAuditor" },
"Action": "kms:Decrypt",
"Resource": "*"
},
{
"Sid": "AllowKeyAdministration",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::123456789012:root" },
"Action": "kms:*",
"Resource": "*"
}
]
}Then attach the key to the trail:
aws cloudtrail update-trail \
--name org-audit-trail \
--kms-key-id arn:aws:kms:us-east-1:123456789012:key/11111111-2222-3333-4444-555555555555Gotchas with KMS
- Key and trail Regions. The key must be in the same Region as the bucket. A multi-Region trail delivers from several Regions to one bucket, so the key policy must allow the trail ARN, not only the Region where you created the key.
- Deleting or disabling the key makes logs unreadable. Treat key deletion as a destructive event. Use a long KMS deletion waiting period and alert on
ScheduleKeyDeletionandDisableKey. - Athena and CloudTrail Lake. Principals who query logs through Athena need
kms:Decryptas well as S3 access. - Digest files. Log file validation digests are signed separately by CloudTrail. Encryption does not replace validation.
IAM permissions on CloudTrail itself
The next layer is who can change the trail. These API calls are the ones to restrict:
| API call | Effect if misused |
|---|---|
| `StopLogging` | Recording stops until someone restarts it |
| `DeleteTrail` | The trail and its configuration are removed |
| `UpdateTrail` | Bucket, KMS key, or Region scope can be changed |
| `PutEventSelectors` | Events can be excluded from recording |
| `PutInsightSelectors` | Insights coverage can be reduced |
Give routine administrators none of these. Reserve them for a security role, and require MFA for it. You can express that as an IAM policy statement:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyTrailTamperingWithoutMFA",
"Effect": "Deny",
"Action": [
"cloudtrail:StopLogging",
"cloudtrail:DeleteTrail",
"cloudtrail:UpdateTrail",
"cloudtrail:PutEventSelectors"
],
"Resource": "*",
"Condition": {
"BoolIfExists": { "aws:MultiFactorAuthPresent": "false" }
}
}
]
}Service control policies
In an AWS Organizations setup, identity policies are not enough. An account administrator can edit identity policies in their own account. A service control policy (SCP) set at the organization level cannot be changed from inside the member account. The SCP below denies the tampering calls for everyone except one named role:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ProtectOrgTrail",
"Effect": "Deny",
"Action": [
"cloudtrail:StopLogging",
"cloudtrail:DeleteTrail",
"cloudtrail:UpdateTrail",
"cloudtrail:PutEventSelectors"
],
"Resource": "arn:aws:cloudtrail:*:*:trail/org-audit-trail",
"Condition": {
"ArnNotLike": {
"aws:PrincipalArn": "arn:aws:iam::*:role/SecurityTrailAdmin"
}
}
}
]
}Detecting tampering
Prevention can fail, so detect as well. Three mechanisms cover most cases.
- Alert on the control-plane calls. An EventBridge rule can match CloudTrail's own management events and notify a security topic.
- Validate log files.
aws cloudtrail validate-logsrecomputes hashes and compares them with signed digest files, which shows files that were modified, deleted, or missing. Enable validation when you create the trail with--enable-log-file-validation. - Review trail status regularly.
get-trail-statusshows whether logging is on and when the last delivery happened or failed.
An EventBridge event pattern for the tampering calls:
{
"source": ["aws.cloudtrail"],
"detail-type": ["AWS API Call via CloudTrail"],
"detail": {
"eventSource": ["cloudtrail.amazonaws.com"],
"eventName": [
"StopLogging",
"DeleteTrail",
"UpdateTrail",
"PutEventSelectors"
]
}
}Check the current state of a trail:
aws cloudtrail get-trail-status --name org-audit-trailLook at IsLogging, LatestDeliveryTime, LatestDeliveryError, and LatestDigestDeliveryError. A stale LatestDeliveryTime or any error value deserves attention even when IsLogging is true.
Limits of detection
Alerts that depend on the trail being intact can be defeated by the same attacker who stops the trail. The StopLogging call itself is usually recorded before logging stops, and EventBridge also receives management events independently of your trail, which is why this alert still works. However, an attacker with deep permissions can disable the rule too. That is why the rule and its target topic should live in a protected account, and why you add the SCP above.
Making the log bucket resistant to deletion
Three S3 features raise the cost of tampering with stored logs.
- Versioning. A deleted or overwritten object leaves earlier versions behind.
- MFA Delete. Permanent deletes of versions require an MFA code from the root user. It can only be enabled with the CLI by the bucket owner.
- S3 Object Lock. In compliance mode, no user, including the root user, can delete or overwrite an object version before its retention period ends. This gives write-once-read-many (WORM) storage.
Lifecycle rules can then move older logs to a cheaper storage class and expire them at the end of your retention period. Make sure retention matches your compliance requirement, and that an Object Lock period is not longer than you can afford, because you cannot shorten it in compliance mode.
aws s3api put-bucket-versioning \
--bucket my-cloudtrail-logs-bucket \
--versioning-configuration Status=EnabledA short hardening checklist
| Control | Command or setting |
|---|---|
| Least-privilege bucket policy with `aws:SourceArn` | `aws s3api put-bucket-policy` |
| Block Public Access | Bucket and account settings |
| SSE-KMS with a scoped key policy | `aws cloudtrail update-trail --kms-key-id` |
| Log file validation | `--enable-log-file-validation` |
| Restricted `StopLogging`, `DeleteTrail`, `UpdateTrail` | IAM policy and SCP |
| Alerts on tampering calls | EventBridge rule |
| Versioning and Object Lock | S3 bucket settings |
| Separate log archive account | AWS Organizations design |
Exercise
You enabled SSE-KMS on a trail. The next day, an analyst with full S3 read access on the log bucket gets an AccessDenied error when downloading a log file. Name the cause and the fix.
Show answer
The log files are encrypted with a customer managed KMS key, and reading them needs kms:Decrypt in addition to S3 read access. The analyst's role is missing from the key policy (or lacks an IAM permission for the key). Add kms:Decrypt for that role in the key policy, or grant it in an identity policy if the key policy allows IAM to control access.
Exercise
Why is an SCP a stronger protection for StopLogging than an IAM policy attached to the account's administrators?
Show answer
An administrator in a member account can edit or detach IAM policies in that account. An SCP is set in the organization's management account and cannot be changed from the member account, so it limits even account administrators. SCPs do not apply to the management account itself.
Exercise
A trail shows IsLogging: true, but LatestDeliveryError contains an AccessDenied message from S3 and the last delivery was two days ago. What probably happened, and where do you look first?
Show answer
The bucket policy or bucket-level settings were changed so CloudTrail can no longer write, for example the s3:PutObject statement was removed or its aws:SourceArn no longer matches the trail. Review the current bucket policy, compare it with the required statements, and find who changed it by looking up PutBucketPolicy events with lookup-events or in the trail itself.