# forensic-workstation / aws / t1 "Standard" # # The isolated network: no internet gateway, no NAT, no peering, private # endpoints for Session Manager, an S3 endpoint limited to the evidence and # package buckets, DNS limited to AWS's own names, and every refused # connection logged and alerted. data "aws_caller_identity" "current" {} data "aws_region" "current" {} data "aws_partition" "current" {} # A closed list, so a zone AWS adds later cannot change which one is picked # and force the subnet, and the workstation with it, to be rebuilt. data "aws_availability_zones" "available" { state = "available" filter { name = "zone-name" values = ["${data.aws_region.current.region}a", "${data.aws_region.current.region}b", "${data.aws_region.current.region}c"] } } locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "restricted" managed_by = "terraform" }, var.extra_tags) account = data.aws_caller_identity.current.account_id region = data.aws_region.current.region partition = data.aws_partition.current.partition az = data.aws_availability_zones.available.names[0] } resource "aws_vpc" "forensics" { cidr_block = var.cidr enable_dns_support = true enable_dns_hostnames = true tags = { Name = "${var.name}-isolated" } } # The VPC's default security group allows everything between its members. # Emptying it means nothing lands in an open group by accident. resource "aws_default_security_group" "empty" { vpc_id = aws_vpc.forensics.id } resource "aws_subnet" "forensics" { vpc_id = aws_vpc.forensics.id cidr_block = var.cidr availability_zone = local.az tags = { Name = "${var.name}-isolated" } } resource "aws_route_table" "forensics" { vpc_id = aws_vpc.forensics.id tags = { Name = "${var.name}-isolated" } } resource "aws_route_table_association" "forensics" { subnet_id = aws_subnet.forensics.id route_table_id = aws_route_table.forensics.id } resource "aws_security_group" "endpoints" { name_prefix = "${var.name}-endpoints-" description = "HTTPS from the forensic workstation to the private endpoints" vpc_id = aws_vpc.forensics.id ingress { description = "HTTPS from inside this VPC" from_port = 443 to_port = 443 protocol = "tcp" cidr_blocks = [var.cidr] } } resource "aws_vpc_endpoint" "ssm" { for_each = toset(["ssm", "ssmmessages", "ec2messages"]) vpc_id = aws_vpc.forensics.id service_name = "com.amazonaws.${local.region}.${each.value}" vpc_endpoint_type = "Interface" subnet_ids = [aws_subnet.forensics.id] security_group_ids = [aws_security_group.endpoints.id] private_dns_enabled = true } data "aws_iam_policy_document" "s3_endpoint" { statement { sid = "ReadEvidence" actions = ["s3:GetObject", "s3:ListBucket", "s3:GetBucketLocation"] resources = [ "arn:${local.partition}:s3:::${var.evidence_bucket}", "arn:${local.partition}:s3:::${var.evidence_bucket}/*", ] principals { type = "*" identifiers = ["*"] } } statement { sid = "AmazonLinuxPackages" actions = ["s3:GetObject"] resources = ["arn:${local.partition}:s3:::al2023-repos-${local.region}-de612dc2/*"] principals { type = "*" identifiers = ["*"] } } } resource "aws_vpc_endpoint" "s3" { vpc_id = aws_vpc.forensics.id service_name = "com.amazonaws.${local.region}.s3" vpc_endpoint_type = "Gateway" route_table_ids = [aws_route_table.forensics.id] policy = data.aws_iam_policy_document.s3_endpoint.json } # DNS: security groups do not filter queries to the VPC resolver, and flow # logs do not record them, so a sample under examination could leak data # through lookups. Only AWS's own names and the VPC's internal names resolve. resource "aws_route53_resolver_firewall_domain_list" "allowed" { name = "${var.name}-allowed" domains = ["amazonaws.com.", "*.amazonaws.com.", "*.internal."] } resource "aws_route53_resolver_firewall_domain_list" "everything" { name = "${var.name}-everything" domains = ["*"] # every domain; the allow rule above is checked first } resource "aws_route53_resolver_firewall_rule_group" "forensics" { name = "${var.name}-dns" } resource "aws_route53_resolver_firewall_rule" "allow_aws" { name = "allow-aws" action = "ALLOW" firewall_domain_list_id = aws_route53_resolver_firewall_domain_list.allowed.id firewall_rule_group_id = aws_route53_resolver_firewall_rule_group.forensics.id priority = 100 } resource "aws_route53_resolver_firewall_rule" "block_rest" { name = "block-everything-else" action = "BLOCK" block_response = "NXDOMAIN" firewall_domain_list_id = aws_route53_resolver_firewall_domain_list.everything.id firewall_rule_group_id = aws_route53_resolver_firewall_rule_group.forensics.id priority = 200 } resource "aws_route53_resolver_firewall_rule_group_association" "forensics" { name = "${var.name}-dns" firewall_rule_group_id = aws_route53_resolver_firewall_rule_group.forensics.id vpc_id = aws_vpc.forensics.id priority = 101 } # Query logs record every lookup, so a blocked one can be alerted on. resource "aws_cloudwatch_log_group" "dns" { name = "/${var.name}/dns-queries" retention_in_days = 400 } resource "aws_route53_resolver_query_log_config" "forensics" { name = "${var.name}-dns" destination_arn = aws_cloudwatch_log_group.dns.arn } resource "aws_route53_resolver_query_log_config_association" "forensics" { resolver_query_log_config_id = aws_route53_resolver_query_log_config.forensics.id resource_id = aws_vpc.forensics.id } # Flow logs record every connection the security groups refuse. resource "aws_cloudwatch_log_group" "flows" { name = "/${var.name}/flow-logs" retention_in_days = 400 } data "aws_iam_policy_document" "flow_trust" { statement { actions = ["sts:AssumeRole"] principals { type = "Service" identifiers = ["vpc-flow-logs.amazonaws.com"] } condition { test = "StringEquals" variable = "aws:SourceAccount" values = [local.account] } } } resource "aws_iam_role" "flows" { name = "${var.name}-flow-logs" assume_role_policy = data.aws_iam_policy_document.flow_trust.json } resource "aws_iam_role_policy" "flows" { name = "write-flow-logs" role = aws_iam_role.flows.id policy = jsonencode({ Version = "2012-10-17" Statement = [{ Effect = "Allow" Action = ["logs:CreateLogStream", "logs:PutLogEvents", "logs:DescribeLogStreams"] Resource = "${aws_cloudwatch_log_group.flows.arn}:*" }] }) } resource "aws_flow_log" "forensics" { vpc_id = aws_vpc.forensics.id traffic_type = "ALL" log_destination_type = "cloud-watch-logs" log_destination = aws_cloudwatch_log_group.flows.arn iam_role_arn = aws_iam_role.flows.arn max_aggregation_interval = 60 } locals { signals = { refused-connection = { group = aws_cloudwatch_log_group.flows.name pattern = "[version, account, eni, source, destination, srcport, destport, protocol, packets, bytes, windowstart, windowend, action=\"REJECT\", flowlogstatus]" description = "Something on the forensic workstation tried to connect somewhere it may not. Check the flow log for the destination; a sample may be calling home." } blocked-lookup = { group = aws_cloudwatch_log_group.dns.name pattern = "{ $.firewall_rule_action = \"BLOCK\" }" description = "Something on the forensic workstation looked up a name outside AWS. Check the query log for the name." } } } resource "aws_cloudwatch_log_metric_filter" "signal" { for_each = local.signals name = "${var.name}-${each.key}" log_group_name = each.value.group pattern = each.value.pattern metric_transformation { name = "${var.name}-${each.key}" namespace = "Security/Forensics" value = "1" default_value = "0" } } resource "aws_cloudwatch_metric_alarm" "signal" { for_each = local.signals alarm_name = "${var.name}-${each.key}" alarm_description = each.value.description namespace = "Security/Forensics" metric_name = "${var.name}-${each.key}" statistic = "Sum" period = 300 evaluation_periods = 1 threshold = 1 comparison_operator = "GreaterThanOrEqualToThreshold" treat_missing_data = "notBreaching" alarm_actions = [var.alert_topic_arn] depends_on = [aws_cloudwatch_log_metric_filter.signal] }