tfsec AWS VPC 安全检查详解:7 条检查项的触发条件、修复示例与扫描实践
tfsec AWS VPC 安全检查详解:7 条检查项的触发条件、修复示例与扫描实践
本篇围绕 tfsec 仓库中 AWS VPC 检查组索引文档 展开,逐条讲解 7 个 aws-vpc-* 检查项的判定标准、不合规/合规 Terraform 示例及修复建议,并结合 入口命令、规则 ID 映射表 等源码说明如何按规则 ID 过滤、按严重级别筛选结果,以及如何在 Terraform 文件中精确豁免某条 VPC 规则。
一、aws-vpc 检查组总览
aws-vpc-* 规则族聚焦于 AWS 虚拟私有云层面的网络访问控制面:安全组(Security Group)、网络 ACL(Network ACL)、默认 VPC 的使用规范。根据 检查组索引,该组共包含 7 个检查项:
| 规则 ID | 默认严重级别 | 检查目标 | 旧版 ID(AWSxxx) |
|---|---|---|---|
aws-vpc-no-public-ingress-sgr |
critical | 入站安全组规则允许来自 0.0.0.0/0 的流量 |
AWS006、AWS008 |
aws-vpc-no-public-egress-sgr |
critical | 出站安全组规则允许流量发往 0.0.0.0/0 |
AWS007、AWS009 |
aws-vpc-no-public-ingress-acl |
critical | 入站网络 ACL 规则允许特定端口来自 0.0.0.0/0 |
AWS049 |
aws-vpc-no-excessive-port-access |
critical | 入站网络 ACL 规则放行了全部端口 | AWS050 |
aws-vpc-no-default-vpc |
high | Terraform 中定义了 aws_default_vpc 资源 |
AWS082 |
aws-vpc-add-description-to-security-group |
low | 安全组缺少 description 字段 |
AWS018 |
aws-vpc-add-description-to-security-group-rule |
low | 安全组规则缺少 description 字段 |
—(新增检查) |
旧版 ID 的对应关系可在 internal/pkg/legacy/map.go 中直接核对,例如该文件中明确映射了 "AWS018": "aws-vpc-add-description-to-security-group"、"AWS082": "aws-vpc-no-default-vpc"、"AWS006": "aws-vpc-no-public-ingress-sgr"。tfsec 在扫描时通过 flags.go 中的 scanner.ScannerWithAlternativeIDProvider(legacy.FindIDs) 把新规则 ID 反向解析回旧 ID,保证用户历史配置(如忽略声明)在 ID 结构变更后仍然有效。
需要说明的背景是:从 go.mod 可见,本仓库模块为 github.com/aquasecurity/tfsec,核心扫描逻辑委托给 github.com/aquasecurity/defsec v0.84.1 的 terraform scanner 执行(见 root.go 中 scanner.New(options...) 与 scnr.ScanFSWithMetrics(...))。项目描述已注明 “Tfsec is now part of Trivy”,运行二进制时也会打印迁移提示(见 cmd/tfsec/main.go),因此本文所述检查行为适用于当前仓库对应的 defsec 规则集。
二、四条 critical 级检查:防止对公网过度开放
1. aws-vpc-no-public-ingress-sgr:入站安全组规则禁止面向 0.0.0.0/0
该检查针对独立的 aws_security_group_rule 资源。官方说明指出:向公网开放端口通常应当避免,应尽量把访问限制到确实需要的 IP 地址或网段(见 检查文档)。
不合规示例(命中 aws-vpc-no-public-ingress-sgr):
resource "aws_security_group_rule" "bad_example" {
type = "ingress"
cidr_blocks = ["0.0.0.0/0"]
}
合规示例:
resource "aws_security_group_rule" "good_example" {
type = "ingress"
cidr_blocks = ["10.0.0.0/16"]
}
修复建议即文档中给出的 “Set a more restrictive cidr range”——把 CIDR 收敛到具体网段。仓库自带的 _examples/1693/main.tf 演示了该规则在 for_each 展开多条端口的场景下,如何按属性精确豁免(忽略 443 端口的那条规则):
# tfsec:ignore:aws-vpc-no-public-ingress-sgr[from_port=443]
resource "aws_security_group_rule" "this" {
for_each = local.rules
type = "ingress"
description = "test"
from_port = each.value
to_port = each.value
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.this.id
}
2. aws-vpc-no-public-egress-sgr:出站安全组规则禁止面向 0.0.0.0/0
与入站方向对应,这条 critical 级检查约束的是出流量:端口向公网发起连接“通常应当避免”,应限制到明确需要的目标地址或网段(见 检查文档)。
不合规示例(内联 egress 块写法):
resource "aws_security_group" "bad_example" {
egress {
cidr_blocks = ["0.0.0.0/0"]
}
}
合规示例:
resource "aws_security_group" "good_example" {
egress {
cidr_blocks = ["1.2.3.4/32"]
}
}
3. aws-vpc-no-public-ingress-acl:入站网络 ACL 规则禁止面向 0.0.0.0/0
网络 ACL 是 VPC 级的无状态防火墙,作用于子网维度,与安全组规则形成双层控制。该 critical 级检查关注 aws_network_acl_rule 中 egress = false(即入站)且 cidr_block = "0.0.0.0/0" 的放行规则(见 检查文档):
不合规示例(对公网开放 22 端口):
resource "aws_network_acl_rule" "bad_example" {
egress = false
protocol = "tcp"
from_port = 22
to_port = 22
rule_action = "allow"
cidr_block = "0.0.0.0/0"
}
合规示例(收敛到内网网段):
resource "aws_network_acl_rule" "good_example" {
egress = false
protocol = "tcp"
from_port = 22
to_port = 22
rule_action = "allow"
cidr_block = "10.0.0.0/16"
}
4. aws-vpc-no-excessive-port-access:入站网络 ACL 规则禁止放行全部端口
同为 critical 级。文档给出的判定标准是“只放行确实需要的端口,其他一律不允许”。当 aws_network_acl_rule 的 protocol = "all" 且为入站放行规则时即命中(见 检查文档):
不合规示例:
resource "aws_network_acl_rule" "bad_example" {
egress = false
protocol = "all"
rule_action = "allow"
cidr_block = "0.0.0.0/0"
}
合规示例(明确指定协议与端口范围):
resource "aws_network_acl_rule" "good_example" {
egress = false
protocol = "tcp"
from_port = 22
to_port = 22
rule_action = "allow"
cidr_block = "0.0.0.0/0"
}
三、aws-vpc-no-default-vpc:避免使用默认 VPC 承载工作负载
该检查级别为 high。文档给出的解释是:默认 VPC 缺少标准 VPC 所具备的大量关键安全特性,新资源不应创建在默认 VPC 中,Terraform 里也不应出现对默认 VPC 的引用(见 检查文档)。
不合规示例(在 Terraform 中管理默认 VPC):
resource "aws_default_vpc" "default" {
tags = {
Name = "Default VPC"
}
}
合规做法(文档中的安全示例):
# no aws default vpc present
即:为工作负载显式创建非默认 VPC(aws_vpc 资源),而不是依赖 aws_default_vpc。可能影响是默认 VPC 未施加关键安全特性,修复建议为 “Create a non-default vpc for resources to be created in”。
四、两条 low 级检查:为安全组与规则补充描述
5. aws-vpc-add-description-to-security-group:安全组缺少 description
级别 low。文档解释:安全组应包含 description 字段以便审计,它能简化审计、排障与安全组管理,为防火墙规则提供上下文(见 检查文档)。
不合规示例(description 为空字符串):
resource "aws_security_group" "bad_example" {
name = "http"
description = ""
ingress {
description = "HTTP from VPC"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = [aws_vpc.main.cidr_block]
}
}
合规示例:
resource "aws_security_group" "good_example" {
name = "http"
description = "Allow inbound HTTP traffic"
ingress {
description = "HTTP from VPC"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = [aws_vpc.main.cidr_block]
}
}
6. aws-vpc-add-description-to-security-group-rule:安全组规则缺少 description
级别 low,检查对象是内联 ingress/egress 块上的 description(见 检查文档)。不合规示例是规则块没有 description:
resource "aws_security_group" "bad_example" {
name = "http"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = [aws_vpc.main.cidr_block]
}
}
合规示例(同时补齐安全组与规则的 description):
resource "aws_security_group" "good_example" {
name = "http"
description = "Allow inbound HTTP traffic"
ingress {
description = "HTTP from VPC"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = [aws_vpc.main.cidr_block]
}
}
这两条规则在 rules.md 的规则清单中被归为同一条描述 “Missing description for security group/security group rule.”,即它们共同服务于“为防火墙规则留痕”这一目标。
五、扫描实践:只跑 VPC 规则、按严重级别筛选、理解退出码
运行与过滤
tfsec 的命令行入口接受一个可选目录参数:tfsec <a href="https://link.gitcode.com/i/64d2ff2b920c758e8d7fa923d822e176" target="_blank">directory](见 [root.go),不带参数时扫描当前工作目录。结合 flags.go 中的参数定义,常用的针对本规则族的操作方式有:
# 只运行 aws-vpc 检查组(逗号分隔多个规则 ID)
tfsec --filter-results "aws-vpc-no-public-ingress-sgr,aws-vpc-no-public-egress-sgr"
# 反向排除某条规则(逗号分隔)
tfsec --exclude aws-vpc-add-description-to-security-group-rule
# 只看 HIGH 及以上严重级别(low 级的两条描述检查会被过滤掉)
tfsec --minimum-severity HIGH
从源码看,--filter-results 经 scanner.ScannerWithIncludedRules 生效,--exclude 经 scanner.ScannerWithExcludedRules 生效(flags.go),二者均接受逗号分隔的规则 ID 列表;--minimum-severity 取值限定为 CRITICAL, HIGH, MEDIUM, LOW(flags.go)。
退出码语义
在 CI 中判断是否阻断时需要注意退出码的细分逻辑,root.go 中 getDetailedExitCode 定义:
- 没有失败规则 → 退出码
0; - 存在失败规则但全部为 LOW 级别 → 退出码
2(专门用于“低严重度失败”); - 存在 CRITICAL/HIGH/MEDIUM 级别的失败规则 → 退出码
1。
这意味着本组中 4 条 critical 规则与 high 级的 aws-vpc-no-default-vpc 一旦命中就会触发退出码 1,而仅命中两条 low 级描述检查时退出码为 2——CI 流水线可据此区分“阻断级问题”和“建议级问题”。另外 --soft-fail(-s)可抑制错误码,便于先观测后收紧。
在 Terraform 中豁免特定规则
当某条规则对业务确实不适用时,README 给出的是行内忽略注释方式(见 README.md):在问题行或资源块上方添加 tfsec:ignore:<rule> 注释。以本组最典型的入站开放规则为例:
resource "aws_security_group_rule" "my-rule" {
type = "ingress"
cidr_blocks = ["0.0.0.0/0"] #tfsec:ignore:aws-vpc-no-public-ingress-sgr
}
也支持在块上方单独一行放置注释、或在同一行串联多个规则。如前文 _examples/1693/main.tf 所示,还支持带属性条件的精确豁免(tfsec:ignore:aws-vpc-no-public-ingress-sgr<a href="https://link.gitcode.com/i/10785c59f8840257ced8d9b6a677e5de" target="_blank">from_port=443]),只忽略 for_each 展开后特定端口的那条规则实例。若需临时禁用所有忽略规则做全量核查,可使用 --no-ignores 参数([flags.go)。
六、小结
aws-vpc 检查组的 7 条规则构成 AWS 网络边界的完整检查面:4 条 critical 规则拦截安全组与网络 ACL 对 0.0.0.0/0 的入站/出站开放以及全端口放行,1 条 high 规则约束不在 Terraform 中引用默认 VPC,2 条 low 规则要求安全组及其规则具备描述字段。配合 --filter-results 定向运行、--minimum-severity 分级过滤、细粒度退出码(0/1/2)以及 tfsec:ignore:<rule><a href="https://link.gitcode.com/i/0162f3cf05917c30aeba95b0efccef8a" target="_blank">...] 精确豁免机制,这套检查可以完整地嵌入 Terraform 的 CI 流水线,实现网络层安全基线的自动化把关。各检查项的完整说明可继续在 [docs/checks/aws/vpc/index.md 及其子目录文档中查阅。