Tag: CloudTrail

  • AWS CloudTrail and GuardDuty: the detections worth turning on first

    AWS CloudTrail and GuardDuty: the detections worth turning on first

    Plenty of AWS accounts have CloudTrail technically on, and GuardDuty either off or enabled in one region and forgotten. Neither state gives you much. The first week is less about clever detection logic and more about making sure the data exists, is complete, and can’t be quietly switched off by the person you’re trying to catch.

    What each service actually sees

    Flow diagram: workload accounts feed an organization CloudTrail trail and GuardDuty, logs land in a locked-down S3 bucket, and EventBridge routes high severity findings to paging and everything else to a ticket queue

    The diagram shows how account activity flows through CloudTrail and GuardDuty to alerts, and which controls protect the pipeline itself.

    CloudTrail records API calls. Every call to the AWS control plane, from the console, the CLI or an SDK, produces an event with the caller identity, source IP, user agent, the action and the parameters. Management events are recorded by default and you can browse the last 90 days in Event history without configuring anything. That default is a convenience, not a security control. It’s per region, it can’t be queried at scale, it expires, and it doesn’t cover data events such as S3 object reads or Lambda invocations.

    GuardDuty is a different animal. It consumes CloudTrail management events, VPC flow logs and DNS query logs through its own independent feed, so you don’t have to configure those logs yourself, and it applies threat intelligence and anomaly models on top. It tells you things like “these EC2 instance credentials were just used from an IP outside AWS”. You could write that detection yourself against CloudTrail, but I wouldn’t want to maintain it.

    GuardDuty finds the known-bad and the statistically odd. CloudTrail is the evidence you read afterwards, and the source for detections specific to your own environment. You want both.

    Fix the trail first

    One organization trail, multi-region, with log file validation, delivering to a bucket in a separate logging account that the workload accounts can’t write to or delete from. That’s the whole design.

    aws cloudtrail create-trail \
      --name org-trail \
      --s3-bucket-name example-org-cloudtrail-logs \
      --is-multi-region-trail \
      --is-organization-trail \
      --enable-log-file-validation
    
    aws cloudtrail start-logging --name org-trail

    Log file validation makes CloudTrail write hourly digest files containing SHA-256 hashes of the log files, signed with RSA. It’s how you prove later that nobody edited or removed a log file, and you check it with aws cloudtrail validate-logs. It sounds optional, so teams skip it, and then they spend a day arguing about whether a gap in the logs was an attack or a misconfiguration. Turn it on.

    Multi-region matters because attackers don’t limit themselves to the regions you use. A stolen key that launches GPU instances in a region you’ve never touched is the classic cryptomining pattern, and a single-region trail won’t show it.

    Data events (S3 object-level, Lambda, DynamoDB) cost money per event, so don’t enable them everywhere. Pick the buckets that hold things you’d have to disclose a breach of, and log those.

    Turn on GuardDuty everywhere, with delegation

    Enable it in every region of every account, including regions you’ve blocked with SCPs. Use a delegated administrator so new accounts enroll automatically.

    aws guardduty create-detector --enable \
      --finding-publishing-frequency FIFTEEN_MINUTES

    The default publishing frequency for updates to existing findings is six hours. Fifteen minutes is a better setting. New findings go out to EventBridge as they’re created either way.

    Beyond the basic data sources, GuardDuty has optional protection plans: S3 data events, EKS audit logs, malware scanning for EBS volumes, RDS login activity, Lambda network activity and runtime monitoring. Check the current pricing page before enabling them all. My order: S3 protection first, because the buckets are where the money is, then EKS if you run it, then the rest as you need them.

    A few finding types I’d make sure reach a human quickly:

    • UnauthorizedAccess:IAMUser/InstanceCredentialExfiltration.OutsideAWS, which means instance role credentials are being used from outside AWS. This is the SSRF-to-metadata pattern, the same family of problem as the Capital One breach in 2019.
    • Stealth:IAMUser/CloudTrailLoggingDisabled, for someone turning off the evidence.
    • Policy:S3/BucketBlockPublicAccessDisabled, for someone removing a guardrail.
    • CryptoCurrency:EC2/BitcoinTool.B!DNS, which is almost always a compromised instance or a stolen key.

    Route high severity findings (GuardDuty’s high band starts at 7.0) to paging. Everything else goes to a queue someone actually reads. If every finding pages, people will mute the channel within a month.

    The detections GuardDuty won’t write for you

    Some of the most valuable signals are about your own policy. For those, you query CloudTrail directly, through an EventBridge rule, CloudTrail Lake, or Athena over the bucket. These are the ones I’d put in first:

    • Any use of the root user. There should be none. Root events have userIdentity.type of Root.
    • StopLogging, DeleteTrail, UpdateTrail and PutEventSelectors on your trail.
    • DisableGuardDuty, DeleteDetector, or changes to the delegated admin.
    • ConsoleLogin where additionalEventData.MFAUsed is No. Federated SSO logins can show No too, so filter on the identity type or you’ll drown.
    • CreateAccessKey for an IAM user, especially one that hasn’t had a key before. Long-lived keys are what leaks.
    • PutBucketPolicy, PutBucketAcl and DeletePublicAccessBlock on buckets.
    • AuthorizeSecurityGroupIngress with 0.0.0.0/0 on anything other than 80 or 443.

    An EventBridge event pattern for the trail tampering case looks like this:

    {
      "source": ["aws.cloudtrail"],
      "detail-type": ["AWS API Call via CloudTrail"],
      "detail": {
        "eventSource": ["cloudtrail.amazonaws.com"],
        "eventName": ["StopLogging", "DeleteTrail", "UpdateTrail", "PutEventSelectors"]
      }
    }

    EventBridge rules are regional. The event for a call made in a given region shows up on that region’s bus, so a rule deployed in one region misses the region an attacker chose. Deploy it in each region, or forward events to a central bus.

    For quick investigation without any setup, lookup-events still works on the 90-day history:

    aws cloudtrail lookup-events \
      --lookup-attributes AttributeKey=EventName,AttributeValue=CreateAccessKey \
      --max-results 20

    It’s rate limited and slow, so it’s fine for triage and wrong for a dashboard.

    Protect the pipeline itself

    An attacker with admin in a workload account will try to blind you before doing anything loud. The logging account needs a bucket policy that only lets the CloudTrail service principal write, and ideally S3 Object Lock so a compromised role can’t delete history. Add an SCP in the organization that denies cloudtrail:StopLogging, cloudtrail:DeleteTrail and guardduty:DeleteDetector to everyone except a break-glass role. The detections above then become a second line instead of the only one.

    I’d also test it. Pick a sandbox account, stop logging on a test trail, and time how long until a human sees the alert. If the notification target is a topic with no subscribers, you’ll find out in the first five minutes, which is a much better time than during an incident.

    What I’d skip at first

    Skip the giant custom detection library. Skip the SIEM purchase until you’ve seen whether the GuardDuty findings and ten CloudTrail rules are being acted on. Three months of acting on a small, trusted set of alerts teaches you more about your environment than a catalog of two hundred rules nobody has time to tune.