iac.htora.dev · security templates

Home/Templates/Notice data walking out the door

Notice data walking out the door

Alerts on the moves someone makes on the way out with data: sharing it outside, reading far more than usual, changing who can decrypt it, and quieting the logs.

Terraform

Why bother

Someone who already has access does not need to break in. They share a bucket with an outside account, copy a snapshot, grant themselves a way to decrypt, or read a whole dataset in an afternoon, often in the weeks before they leave. Each step uses permissions they have, so nothing is refused and nothing looks like an attack. These alerts watch for the steps themselves, and the saved queries give the investigator the history to go with them.

How you know it worked

In a test account or project, share a test bucket outside and then undo it. The sharing alert reaches the security inbox within minutes.

How it works

1Someone with access actsshares, reads, grants, or turns logging down2The audit log records itadmin actions always, object reads where turned on3A filter or rule matchesper action, or per person for bulk reads4An alert names what happenedand a saved query shows the rest
Step by step, from the thing that happens to the result you want.

Set it up

Pick your platform, then open the level you want. Each level is complete on its own. Read the comments in the files as you go. Anything with a real consequence is explained on the line where it happens.

Terraform. CloudWatch metric filters on the audit trail, and Contributor Insights on object reads

What you need first

  • A multi-region CloudTrail trail delivering to CloudWatch Logs, such as the Standard audit log template builds. Apply in that log group's region.
  • An email address, or for Standard an SNS topic, that reaches the security team.
  • For bulk reads (Standard), the ARNs of the buckets that contain the data you most need to protect.

What it creates

  • Three metric filters and alarms on the trail's log group: data shared outside the account, changes to who can use a KMS key, and trails or flow logs turned down
  • Named pipeline roles left out of the alarms (Standard)
  • A trail that records object reads from the named buckets only, into its own encrypted bucket and log group (Standard)
  • A Contributor Insights rule ranking principals by objects read, and an alarm when the top one passes the threshold in five minutes (Standard)
  • Saved Logs Insights queries for permission changes, one principal's activity, changes by person, and decrypt grants, plus bulk readers when buckets are named (Standard)
  • An alert topic encrypted with a key CloudWatch alarms are allowed to use (Standard)

See the four alerts this creates

Saved queries this installs

Standard and Hardened install these five, ready for the first hour of a case. Quick start installs none.

Saved queryWhat it answersWhere to open it
insider-data-theft/permission-changesWho gave whom new IAM access, newest first. Run it first when an alarm names a person.CloudWatch console, Logs Insights, Queries, folder insider-data-theft
insider-data-theft/one-principalEverything one principal did. Replace the name in the filter first.CloudWatch console, Logs Insights, Queries, folder insider-data-theft
insider-data-theft/changes-by-personThe changes that alarm, with who made each one, for reviewing a week at once.CloudWatch console, Logs Insights, Queries, folder insider-data-theft
insider-data-theft/decrypt-grantsKMS grants that let someone decrypt, made with no AWS service acting for the caller.CloudWatch console, Logs Insights, Queries, folder insider-data-theft
insider-data-theft/bulk-readersWho read the most S3 objects, hour by hour.
Only when sensitive_bucket_arns is set.
CloudWatch console, Logs Insights, Queries, folder insider-data-theft

The code

Quick startReadyone file, 128 lines
One file. Three alarms on the audit trail, for sharing outside, decrypt rights, and logging turned down.
Change these first
  • trail_log_group = "your-cloudtrail-log-group" main.tf, line 18
  • alert_email = "security-alerts@example.com" main.tf, line 19
Run it
  1. aws sts get-caller-identity
    Shows the account and role you are signed in as. Check it before every apply.
  2. terraform init
    Downloads the provider. Run it once per folder.
  3. terraform plan
    Shows what will be created. Changes nothing. Read it.
  4. terraform apply
    Shows the plan again and waits for you to type yes.
  5. terraform output verify
    Prints the commands that prove it worked.
  6. terraform destroy
    When you have finished. If it refuses, How to undo it below explains why.
Download main.tf
Checks

Formatting passedValid passedLint passed

Security scan: two checks passed, and one rule flagged, each with a written reason:

Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.0.

outcomes/insider-data-theft/aws/t0
main.tf
Open main.tf as plain text
StandardReadysix files, 626 lines
Adds named pipeline exceptions, object-read recording for chosen buckets, a per-person bulk read alarm, saved queries, and an encrypted topic the alarms can publish to.
Change these first
  • trail_log_group. CloudWatch log group a multi-region CloudTrail trail delivers management events to, such as the Standard audit log template builds.
Run it
  1. aws sts get-caller-identity
    Shows the account and role you are signed in as. Check it before every apply.
  2. # terraform.tfvars
    trail_log_group = ""
    Create this file beside the others and fill in your values. Terraform reads it on its own.
  3. terraform init
    Downloads the provider. Run it once per folder.
  4. terraform plan
    Shows what will be created. Changes nothing. Read it.
  5. terraform apply
    Shows the plan again and waits for you to type yes.
  6. terraform output verify
    Prints the commands that prove it worked.
Download all six files as a zip
Checks

Formatting passedValid passedLint passed

Security scan: 73 checks passed, and eight rules flagged, each with a written reason:

Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.0.

outcomes/insider-data-theft/aws/t1
main.tf
Open main.tf as plain text
outputs.tf
Open outputs.tf as plain text
queries.tf
Open queries.tf as plain text
reads.tf
Open reads.tf as plain text
variables.tf
Open variables.tf as plain text
versions.tf
Open versions.tf as plain text
HardenedReadysix files, 262 lines
Standard as a module. Plan rules refuse the key, filters, or alarms deleted, key rotation off, force_destroy, public access allowed, encryption other than KMS, the trail switched off or narrowed, silent alarms. A daily plan reports drift, and the scan fails on any unexplained finding.
Change these first
  • trail_log_group. CloudWatch log group a multi-region CloudTrail trail delivers management events to, such as the Standard audit log template builds.
Run it
  1. aws sts get-caller-identity
    Shows the account and role you are signed in as. Check it before every apply.
  2. # terraform.tfvars
    trail_log_group = ""
    Create this file beside the others and fill in your values. Terraform reads it on its own.
  3. terraform init
    Downloads the provider. Run it once per folder.
  4. terraform plan
    Shows what will be created. Changes nothing. Read it.
  5. terraform apply
    Shows the plan again and waits for you to type yes.
  6. terraform output verify
    Prints the commands that prove it worked.
Download all six files as a zip
Checks

Formatting passedValid passedLint passed

Security scan: 67 checks passed, and none flagged.

Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.0.

outcomes/insider-data-theft/aws/t2
.checkov.yml
Open .checkov.yml as plain text
guard.yml
Open guard.yml as plain text
main.tf
Open main.tf as plain text
outputs.tf
Open outputs.tf as plain text
variables.tf
Open variables.tf as plain text
versions.tf
Open versions.tf as plain text

How to check it worked

Add and remove a statement on a test bucket's policy. The shared-outside alarm goes to ALARM and emails within about ten minutes.

What catches people

The management trail does not record object reads, so bulk downloads are invisible without data events. Recording them for every bucket is expensive, which is why Standard records reads only from the buckets you name, on a trail of its own that records nothing else.

AWS services create KMS grants on a caller's behalf all day. Those calls carry the service's name as the source address, and the filter leaves them out, so the alarm fires on grants a person made directly.

Deployment pipelines change bucket and key policies as their job, and will trip these alarms on every release. Standard takes up to four role ARNs to leave out, and matches them as CloudTrail records them, by the assumed-role session. Each one is a blind spot, so name the pipeline and nothing else.

CloudWatch alarms cannot publish to an SNS topic encrypted with the AWS managed key, and nothing reports the failure. Standard creates its topic with a key whose policy allows them, and an existing topic needs the same.

How to undo it

terraform destroy removes the alarms, the queries, the read trail, and its log group. The read bucket is kept until you empty it, because it contains evidence.

What it costs

Cents a month for the filters and alarms, and a dollar a month for the KMS key in Standard. The read trail is the real cost: CloudTrail charges for each object read it records, and CloudWatch Logs charges to ingest and keep them. Contributor Insights bills per rule and per matched event. Name few buckets and check the bill after a week.

Good to know

  • Alerts start an inquiry. Most will have an innocent explanation, and the investigation that follows should involve HR and Legal before anyone is confronted. The insider cases lesson covers how.
  • Tune before you trust. Run the saved queries over a normal week first, name the pipeline identities that change policies as their job, and set the bulk read threshold from what you saw.
  • The data perimeter template refuses copies to outside accounts. These alerts cover what a perimeter allows: sharing that goes through, reads by people entitled to read, and changes made by administrators.

Registry 0.23.0. Built 2026-11-17.

Made by Habibullah Tora. Source on GitHub. Code under the MIT licence, writing under CC BY 4.0.