# The evidence bucket: encrypted with its own key, locked so nothing in it can be # changed or deleted for the retention period, and writable by EC2 instances # only under memory/, which is where the capture script puts its output. data "aws_caller_identity" "current" {} data "aws_partition" "current" {} data "aws_region" "current" {} locals { account = data.aws_caller_identity.current.account_id root = "arn:${data.aws_partition.current.partition}:iam::${local.account}:root" tags = merge({ owner = var.owner environment = var.environment data_classification = "restricted" managed_by = "terraform" }, var.extra_tags) } resource "aws_kms_key" "evidence" { description = "${var.name} evidence" enable_key_rotation = true deletion_window_in_days = 30 policy = data.aws_iam_policy_document.key.json } data "aws_iam_policy_document" "key" { statement { sid = "AccountManagesKeyThroughIAM" effect = "Allow" principals { type = "AWS" identifiers = [local.root] } actions = ["kms:*"] resources = ["*"] } # Any principal in this account may use the key through S3, and only for # this bucket. The bucket policy decides who may write, so an instance's # upload is checked there, and the key follows. statement { sid = "AccountUsesKeyThroughThisBucket" effect = "Allow" principals { type = "AWS" identifiers = ["*"] } actions = ["kms:GenerateDataKey", "kms:Encrypt", "kms:Decrypt"] resources = ["*"] condition { test = "StringEquals" variable = "kms:CallerAccount" values = [local.account] } condition { test = "StringEquals" variable = "kms:ViaService" values = ["s3.${data.aws_region.current.region}.amazonaws.com"] } condition { test = "ArnLike" variable = "kms:EncryptionContext:aws:s3:arn" values = ["arn:${data.aws_partition.current.partition}:s3:::${var.name}-${local.account}", "arn:${data.aws_partition.current.partition}:s3:::${var.name}-${local.account}/*"] } } # CloudWatch Logs encrypts the responder's log group with this key, and can # only do that if the key policy names it. statement { sid = "CloudWatchLogsEncrypt" effect = "Allow" principals { type = "Service" identifiers = ["logs.${data.aws_region.current.region}.amazonaws.com"] } actions = ["kms:Encrypt*", "kms:Decrypt*", "kms:ReEncrypt*", "kms:GenerateDataKey*", "kms:Describe*"] resources = ["*"] condition { test = "ArnLike" variable = "kms:EncryptionContext:aws:logs:arn" values = ["arn:${data.aws_partition.current.partition}:logs:${data.aws_region.current.region}:${local.account}:log-group:/aws/lambda/${var.name}-responder"] } } } resource "aws_s3_bucket" "evidence" { bucket = "${var.name}-${local.account}" object_lock_enabled = true lifecycle { prevent_destroy = true } } resource "aws_s3_bucket_versioning" "evidence" { bucket = aws_s3_bucket.evidence.id versioning_configuration { status = "Enabled" } } resource "aws_s3_bucket_object_lock_configuration" "evidence" { bucket = aws_s3_bucket.evidence.id depends_on = [aws_s3_bucket_versioning.evidence] rule { default_retention { mode = var.lock_mode days = var.retention_days } } } resource "aws_s3_bucket_server_side_encryption_configuration" "evidence" { bucket = aws_s3_bucket.evidence.id rule { apply_server_side_encryption_by_default { sse_algorithm = "aws:kms" kms_master_key_id = aws_kms_key.evidence.arn } bucket_key_enabled = true } } resource "aws_s3_bucket_public_access_block" "evidence" { bucket = aws_s3_bucket.evidence.id block_public_acls = true block_public_policy = true ignore_public_acls = true restrict_public_buckets = true } resource "aws_s3_bucket_ownership_controls" "evidence" { bucket = aws_s3_bucket.evidence.id rule { object_ownership = "BucketOwnerEnforced" } } data "aws_iam_policy_document" "bucket" { statement { sid = "DenyUnencryptedTransport" effect = "Deny" actions = ["s3:*"] principals { type = "AWS" identifiers = ["*"] } resources = [aws_s3_bucket.evidence.arn, "${aws_s3_bucket.evidence.arn}/*"] condition { test = "Bool" variable = "aws:SecureTransport" values = ["false"] } } # An instance under investigation has whatever role it was launched with, and # that role will not mention this bucket. These two statements let any # instance in this account write memory images and read the capture tool, # and nothing else. ec2:SourceInstanceARN is only present on requests signed # with an instance's own credentials, so a person or another service cannot # use them. statement { sid = "InstancesWriteMemoryImages" effect = "Allow" actions = ["s3:PutObject"] principals { type = "AWS" identifiers = ["*"] } resources = ["${aws_s3_bucket.evidence.arn}/memory/*"] condition { test = "StringEquals" variable = "aws:PrincipalAccount" values = [local.account] } condition { test = "Null" variable = "ec2:SourceInstanceARN" values = ["false"] } } statement { sid = "InstancesReadCaptureTool" effect = "Allow" actions = ["s3:GetObject"] principals { type = "AWS" identifiers = ["*"] } resources = ["${aws_s3_bucket.evidence.arn}/tools/*"] condition { test = "StringEquals" variable = "aws:PrincipalAccount" values = [local.account] } condition { test = "Null" variable = "ec2:SourceInstanceARN" values = ["false"] } } } resource "aws_s3_bucket_policy" "evidence" { bucket = aws_s3_bucket.evidence.id policy = data.aws_iam_policy_document.bucket.json depends_on = [aws_s3_bucket_public_access_block.evidence] } # Memory capture, run on the instance by Systems Manager. Every parameter has an # allowed pattern because each one lands in a shell script. Without the # patterns, whoever can start this document could run anything as root. resource "aws_ssm_document" "memory" { name = "${var.name}-capture-memory" document_type = "Command" document_format = "YAML" content = yamlencode({ schemaVersion = "2.2" description = "Capture memory with AVML, hash it, and upload both to the evidence bucket." parameters = { EvidenceBucket = { type = "String", allowedPattern = "^[a-z0-9.-]{3,63}$" } ToolKey = { type = "String", allowedPattern = "^[A-Za-z0-9._/-]{1,200}$" } ImageKey = { type = "String", allowedPattern = "^[A-Za-z0-9._/-]{1,200}$" } } mainSteps = [{ action = "aws:runShellScript" name = "captureMemory" precondition = { StringEquals = ["platformType", "Linux"] } inputs = { timeoutSeconds = tostring(var.memory_timeout_seconds) runCommand = [ "set -euo pipefail", "TOOL=/var/tmp/avml.$$; IMG=/var/tmp/memory.$$.lime", "aws s3 cp \"s3://{{ EvidenceBucket }}/{{ ToolKey }}\" \"$TOOL\" --only-show-errors", "chmod 700 \"$TOOL\"", "\"$TOOL\" --compress \"$IMG\"", "sha256sum \"$IMG\" | awk '{print $1}' > \"$IMG.sha256\"", "aws s3 cp \"$IMG\" \"s3://{{ EvidenceBucket }}/memory/{{ ImageKey }}.lime\" --only-show-errors", "aws s3 cp \"$IMG.sha256\" \"s3://{{ EvidenceBucket }}/memory/{{ ImageKey }}.lime.sha256\" --only-show-errors", "shred -u \"$IMG\" \"$TOOL\" 2>/dev/null || rm -f \"$IMG\" \"$TOOL\"", "echo captured $(cat \"$IMG.sha256\" 2>/dev/null || true)", ] } }] }) } # Status messages for whoever is running the incident. Encrypted with the # evidence key. resource "aws_sns_topic" "status" { name = "${var.name}-status" kms_master_key_id = aws_kms_key.evidence.id } resource "aws_sns_topic_subscription" "email" { count = var.alert_email == "" ? 0 : 1 topic_arn = aws_sns_topic.status.arn protocol = "email" endpoint = var.alert_email }