Sub topic 5
Enabling GuardDuty and generating sample findings
This chapter covers turning on GuardDuty in one account and Region, confirming it works, and producing sample findings to explore. It assumes you know AWS accounts, Regions, IAM roles and policies, and the AWS CLI. You should also know that GuardDuty reads CloudTrail events, VPC Flow Logs, and Route 53 DNS logs, which earlier chapters covered.
What a detector is
A detector is the GuardDuty resource that represents the service in one account and one Region. Everything else hangs off it: findings, suppression rules, IP lists, and protection plan settings. Each finding belongs to exactly one detector.
Three facts shape how you work with detectors:
- A detector is Regional. Enabling GuardDuty in
us-east-1does nothing foreu-west-1. You must create a detector in every Region you use, and ideally in Regions you don't use, because an attacker may start resources there. - An account has at most one detector per Region.
- GuardDuty does not need you to turn on CloudTrail, Flow Logs, or DNS logging. It reads independent, internal copies of those streams. Your own logging settings do not affect what it sees, and it adds no load to your workloads.
The detector ID is a 32-character string. Most CLI calls need it, so you will look it up often.
Permissions you need
To enable the service you need guardduty:CreateDetector. GuardDuty also creates a service-linked role named AWSServiceRoleForAmazonGuardDuty the first time you enable it. Creating that role requires iam:CreateServiceLinkedRole. If a restrictive policy blocks the role creation, the enable call fails.
For daily work, the AWS managed policy AmazonGuardDutyFullAccess is the broad option. AmazonGuardDutyReadOnlyAccess is enough for people who only review findings. In production, prefer a custom policy that grants only the actions each role needs.
Enabling GuardDuty with the CLI
Create and enable a detector in the current Region:
aws guardduty create-detector --enable --finding-publishing-frequency FIFTEEN_MINUTESThe command returns the new detector ID. Two options matter:
--enableturns the detector on. Without it, the detector exists but is suspended.--finding-publishing-frequencysets how often GuardDuty publishes updates to existing findings to EventBridge. The values areFIFTEEN_MINUTES,ONE_HOUR, andSIX_HOURS. The default isSIX_HOURS.
The frequency is easy to misread, so note exactly what it controls. A new finding is sent to EventBridge within about five minutes of being generated, whatever you set. The setting only affects how soon later occurrences of an already-reported finding are sent. If an attacker keeps probing the same port, the finding's count rises, and that update reaches EventBridge on the schedule you chose. Automation that reacts to repeated activity benefits from FIFTEEN_MINUTES.
If you run the create command twice in the same Region, the second call fails because a detector already exists. Use list-detectors to find it.
DETECTOR_ID=$(aws guardduty list-detectors --query 'DetectorIds[0]' --output text)
echo "$DETECTOR_ID"The --query expression takes the first ID in the list. This is safe because there is at most one detector per Region. If GuardDuty is not enabled, the query returns None, and later commands fail with a confusing error. Check the variable before using it.
Verifying the detector
Inspect the detector's state with get-detector:
aws guardduty get-detector --detector-id "$DETECTOR_ID" \
--query '{Status:Status,Frequency:FindingPublishingFrequency,Role:ServiceRole}'Status should be ENABLED. A status of DISABLED means the detector was suspended, either by update-detector --no-enable or by an administrator. Suspending keeps your existing findings and settings but stops analysis. Deleting the detector removes the findings too, so avoid delete-detector unless you mean to start over.
Enabling in every Region
Because detectors are Regional, a script is the practical way to cover a whole account. This Python example uses boto3 to enable GuardDuty in every Region that your account has enabled. It skips Regions where a detector already exists.
import boto3
def enable_everywhere():
session = boto3.session.Session()
regions = session.get_available_regions("guardduty")
for region in regions:
client = session.client("guardduty", region_name=region)
try:
existing = client.list_detectors()["DetectorIds"]
if existing:
print(f"{region}: already enabled ({existing[0]})")
continue
created = client.create_detector(
Enable=True,
FindingPublishingFrequency="FIFTEEN_MINUTES",
)
print(f"{region}: created {created['DetectorId']}")
except Exception as error:
print(f"{region}: skipped ({error})")
if __name__ == "__main__":
enable_everywhere()The try block matters. Opt-in Regions that your account has not enabled reject calls, and you want the loop to continue past them. For many accounts, this per-account loop is the wrong tool. A later chapter covers delegated administrators, which enable GuardDuty across an AWS Organization from one place.
Choosing protection plans
A new detector analyzes the foundational data sources. Optional protection plans, such as S3 Protection, EKS Protection, Malware Protection, RDS Protection, Lambda Protection, and Runtime Monitoring, are separate features with separate pricing. Depending on when the account was created, some plans may be on by default. Check the settings after enabling, and decide each plan on purpose. You can inspect the current configuration:
aws guardduty get-detector --detector-id "$DETECTOR_ID" --query 'Features'Each entry shows a feature name and whether its status is ENABLED or DISABLED. Change a plan with update-detector and the --features option. Turn plans on gradually, so you can see how each one changes your findings and your bill.
Generating sample findings
An empty console is a poor place to learn. Sample findings give you realistic finding records without a real attack. They are good for learning the finding structure and for testing EventBridge rules, Lambda functions, and Security Hub integration before real threats arrive.
DETECTOR_ID=$(aws guardduty list-detectors --query 'DetectorIds[0]' --output text)
aws guardduty create-sample-findings --detector-id "$DETECTOR_ID" --finding-types Recon:EC2/PortProbeUnprotectedPort UnauthorizedAccess:EC2/SSHBruteForceThe --finding-types option takes one or more finding type names separated by spaces. If you omit it, GuardDuty generates one sample for every finding type it supports, which produces a large batch. Naming the types you want keeps the list readable.
Sample findings have these properties:
- Their titles and descriptions start with a sample marker, and the finding's
service.additionalInfo.samplefield istrue. Use that field to separate samples from real findings in code. - They use made-up resource values, such as placeholder instance IDs and documentation IP addresses. They do not point at real resources, so you cannot act on them in your account.
- They flow through the normal pipeline. They appear in the console, go to EventBridge, and reach Security Hub when the integration is on. This is what makes them useful for testing automation, and also why you should warn your security team before generating them. A sample with high severity can page someone.
Listing and reading the findings
Findings take a short time to appear. List the IDs of high-severity findings with a criteria filter:
aws guardduty list-findings --detector-id "$DETECTOR_ID" --finding-criteria '{"Criterion":{"severity":{"Gte":7}}}'list-findings returns only IDs. To get the content, pass those IDs to get-findings:
FINDING_IDS=$(aws guardduty list-findings --detector-id "$DETECTOR_ID" \
--finding-criteria '{"Criterion":{"severity":{"Gte":7}}}' \
--query 'FindingIds' --output json)
aws guardduty get-findings --detector-id "$DETECTOR_ID" --finding-ids "$FINDING_IDS" \
--query 'Findings[].{Type:Type,Severity:Severity,Count:Service.Count}'Two details cause errors here. First, get-findings accepts at most 50 IDs per call, so page through larger lists. Second, severity is a number, so the criterion uses Gte with a number and not a string. Severity 7.0 and above is high, 4.0 to 6.9 is medium, and 1.0 to 3.9 is low. The SSH brute force sample usually falls in the medium range, and a port probe sample is low, so your high-severity query may return different results from the types you picked. Lower the threshold to see them.
Cleaning up samples
Samples stay until you archive or delete them, and they mix with real findings in dashboards. Archive them when you finish:
aws guardduty archive-findings --detector-id "$DETECTOR_ID" --finding-ids "$FINDING_ID"Archived findings are hidden from the default view but remain available for 90 days, after which GuardDuty removes them. Archiving a sample also tells you how archiving works: it is the same mechanism that suppression rules use for noise, which the tuning chapter covers.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `BadRequestException` on `create-detector` | A detector already exists in the Region | Use `list-detectors` and reuse the ID |
| `AccessDeniedException` on enabling | Missing `iam:CreateServiceLinkedRole` or an SCP block | Grant the permission or adjust the SCP |
| `--detector-id` is `None` or empty | GuardDuty is not enabled in the current Region | Check `aws configure get region` and enable it |
| No findings after sample creation | Delay, or the wrong Region | Wait a few minutes and confirm the Region |
| Findings missing from Security Hub | The integration or Security Hub is not enabled in that Region | Enable both in the same Region |
The enablement flow
Exercise
You enabled GuardDuty with --finding-publishing-frequency SIX_HOURS, and a Lambda function reacts to findings through EventBridge. A new SSH brute force finding appears, and the attack continues for an hour. When does EventBridge receive the first notification, and when do you learn that the attack is still going? What setting would you change to get faster updates?
Show answer
The first notification for the new finding arrives within about five minutes, because the publishing frequency does not delay new findings. Updates to the same finding, such as a rising count while the attack continues, are published only on the configured schedule, so with SIX_HOURS you could wait up to six hours. Change the frequency to FIFTEEN_MINUTES with aws guardduty update-detector --detector-id "$DETECTOR_ID" --finding-publishing-frequency FIFTEEN_MINUTES.
Exercise
A teammate runs create-sample-findings in a production account with no --finding-types option. List two problems this could cause.
Show answer
It generates a sample for every supported finding type, which floods the console and Security Hub with many records, so real findings are harder to see. The samples also flow through EventBridge, so any rule that notifies people or starts remediation will fire, possibly paging the on-call engineer or running automation against placeholder resources. Naming specific types, checking the service.additionalInfo.sample field in rules, and archiving the samples afterward reduces the impact.