AWS Fundamentals Series

🕵️ The Top 1% Hands-On for Detecting a Leaked Access Key's Misuse With CloudTrail and GuardDuty: From Logging to Detection to Alert

Introduction

This article is part of the Top 1% Series Complete Article Guide, the 10th article in the AWS Fundamentals Series.

The Big Picture

This hands-on consists of four steps.

graph LR
    Step1["Step1<br/>Enable CloudTrail<br/>and GuardDuty"]
    Step2["Step2<br/>Reproduce a call from an<br/>unusual region"]
    Step3["Step3<br/>Check GuardDuty's<br/>finding"]
    Step4["Step4<br/>Investigate details<br/>via CloudTrail logs"]
    Step1 --> Step2 --> Step3 --> Step4

Hands-On Steps

Step 1: Enable CloudTrail and GuardDuty

Enable both, if you haven’t already.

aws cloudtrail create-trail --name my-trail --s3-bucket-name my-cloudtrail-logs-bucket
aws cloudtrail start-logging --name my-trail

aws guardduty create-detector --enable

The moment CloudTrail is enabled, it starts recording nearly every API call across the account. GuardDuty is enabled to analyze this CloudTrail log, along with VPC Flow Logs, DNS logs, and other data sources.

Step 2: Reproduce a call from an unusual region

Using a test access key, call an API from a region that user has never used before (simulating one of GuardDuty’s detection targets: an unusual usage pattern).

AWS_ACCESS_KEY_ID=<test key> AWS_SECRET_ACCESS_KEY=<test secret> \
  aws ec2 describe-instances --region ap-southeast-2

This command itself succeeds normally, but the fact that it’s an operation from a region that user has never accessed before is exactly the kind of behavior GuardDuty’s anomaly-detection algorithm pays attention to.

Step 3: Check GuardDuty’s finding

After waiting a while, check GuardDuty’s findings.

aws guardduty list-findings --detector-id <detector-id>
aws guardduty get-findings --detector-id <detector-id> --finding-ids <finding-id>

The finding includes a severity-tagged alert of a type like UnauthorizedAccess:IAMUser/InstanceCredentialExfiltration, or one indicating an API call from an unusual location. Information on “what,” “when,” and “by which IAM entity” is bundled together, readable at a glance.

Step 4: Investigate details via CloudTrail logs

Using the event ID included in GuardDuty’s finding, check that API call’s details in CloudTrail’s log.

aws cloudtrail lookup-events --lookup-attributes AttributeKey=EventName,AttributeValue=DescribeInstances --max-results 5

Where GuardDuty tells you “something’s wrong,” CloudTrail’s log provides the raw evidence needed for after-the-fact investigation — down to that API call’s source IP address, User-Agent, and request parameters. Only combining the two makes both “detecting what happened” and “investigating why and how it happened” possible.

What a Pro Sees Here (Top 1% Understanding)

CloudTrail “records” while GuardDuty “analyzes and alerts” — a clear division of labor

It’s not uncommon for practitioners to conflate CloudTrail and GuardDuty, but their roles are entirely different. CloudTrail is purely a logging service, endlessly recording every API call without applying any judgment. GuardDuty, on the other hand, is a service that cross-references that log (and other data sources) against machine learning and known threat intelligence, judges “this is anomalous,” and then alerts you. With CloudTrail enabled alone, nobody can read that log every day and spot anomalies by hand. Only once the “analyze and alert” layer of GuardDuty is combined does it become a detection mechanism that actually works in real-world operations.

What CloudTrail log retention period means for incident response

By the time GuardDuty detects something, it’s not unusual for hours or even days to have already passed since the incident actually occurred. If the CloudTrail logs needed for investigation have already been deleted or overwritten by then, an after-the-fact investigation becomes impossible. Retaining CloudTrail logs long-term in an S3 bucket, and protecting that bucket itself with MFA delete and versioning, is the real-world prerequisite for preventing the situation of “we detected it, but there’s no evidence left to investigate.”

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. GuardDuty is enabled but the expected finding doesn’t appear: GuardDuty’s learning period (learning a baseline of normal usage patterns) takes some time. Detection accuracy can be low right after enabling it.
  2. Can’t find the CloudTrail log: lookup-events only covers the last 90 days of event history by default. For anything older, you need to reference the files saved in the S3 bucket directly.
  3. Can’t map a GuardDuty finding to a CloudTrail log: Use the access key ID, IAM entity name, and timestamp included in the finding as filter conditions for lookup-events.

Summary

Takeaways to Apply Today

  1. Always set versioning and access restrictions on the S3 bucket storing CloudTrail logs.
  2. Share an investigation procedure using CloudTrail logs with your team ahead of time, in preparation for when a GuardDuty finding appears.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).