关于 auditing-terraform-infrastructure-for-security
用于审计 Terraform 代码、计划和状态文件的安全技能,覆盖部署前后检查。它结合 IaC 扫描器、自定义 OPA 策略和 CI/CD 检查,面向云基础设施团队。
主要功能
- 01使用 Checkov 和 tfsec 扫描 Terraform
- 02通过 Terrascan 检查合规性
- 03编写自定义 OPA 策略
- 04在 CI/CD 中加入安全门禁
- 05审计 Terraform 状态中的配置问题
适用场景
- 01应用前审查 Terraform 变更
- 02在拉取请求中执行安全检查
- 03从 Terraform 状态发现已部署问题
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mukul975/ anthropic-cybersecurity-skills --skill auditing-terraform-infrastructure-for-security安装后在 Claude Code 中输入/auditing-terraform-infrastructure-for-security确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
LICENSEreferences/api-reference.mdscripts/agent.py详细介绍
审计 Terraform 基础设施的安全性
何时使用
- 在 Terraform 部署的 CI/CD 流水线中集成安全扫描时
- 在应用之前审查 Terraform 计划和模块以符合安全最佳实践时
- 为云基础设施配置构建策略即代码的护栏时
- 审计现有的 Terraform 状态文件以识别已部署的错误配置时
- 在多个 Terraform 项目中强制执行组织安全标准时
不要使用于运行时安全监控(使用 CSPM 工具)、应用程序安全测试(使用 SAST/DAST 工具),或云配置漂移检测(在部署后使用 AWS Config 或 Azure Policy)。
先决条件
- 已安装 Checkov(
pip install checkov) - 已安装 tfsec(
brew install tfsec或从 GitHub 获取二进制文件) - 已安装 Terrascan(
brew install terrascan) - Terraform v1.0+ 用于生成计划
- 用于自定义策略执行的 OPA(Open Policy Agent)
- 包含要审计的 Terraform 代码的 Git 仓库
工作流
步骤 1:使用 Checkov 扫描 Terraform 代码
运行 Checkov 进行全面的 IaC 安全扫描,使用内置和自定义策略。
# Scan a Terraform directory
checkov -d ./terraform/ --framework terraform
# Scan with specific check categories
checkov -d ./terraform/ --check CKV_AWS_18,CKV_AWS_19,CKV_AWS_20,CKV_AWS_21
# Scan and output results in JSON
checkov -d ./terraform/ --output json > checkov-results.json
# Scan a Terraform plan file for more accurate analysis
terraform init && terraform plan -out=tfplan
terraform show -json tfplan > tfplan.json
checkov -f tfplan.json --framework terraform_plan
# Skip specific checks with justification
checkov -d ./terraform/ --skip-check CKV_AWS_145 \
--bc-api-key $BRIDGECREW_API_KEY
# Scan Terraform modules
checkov -d ./modules/ --framework terraform --compact
# List all available checks
checkov --list --framework terraform | grep CKV_AWS
步骤 2:使用 tfsec 扫描 Terraform 特定问题
使用 tfsec 进行 Terraform 原生安全分析,并提供详细的修复指导。
# Scan a Terraform directory
tfsec ./terraform/
# Scan with minimum severity threshold
tfsec ./terraform/ --minimum-severity HIGH
# Output in JSON for CI/CD processing
tfsec ./terraform/ --format json > tfsec-results.json
# Scan with custom checks
tfsec ./terraform/ --custom-check-dir ./custom-checks/
# Exclude specific rules
tfsec ./terraform/ --exclude-downloaded-modules \
--exclude aws-s3-enable-bucket-logging
# Scan and fail on specific severity
tfsec ./terraform/ --minimum-severity CRITICAL --soft-fail
# Generate SARIF output for GitHub Security tab
tfsec ./terraform/ --format sarif > tfsec.sarif
步骤 3:运行 Terrascan 进行多框架合规检查
执行 Terrascan 以针对 CIS、NIST 和 SOC 2 框架进行合规检查。
# Scan Terraform against CIS AWS benchmark
terrascan scan -t aws -i terraform -d ./terraform/ \
--policy-type aws --verbose
# Scan against specific compliance frameworks
terrascan scan -t aws -i terraform -d ./terraform/ \
--policy-type aws \
--categories "Compliance Validation"
# Output in JSON
terrascan scan -t aws -i terraform -d ./terraform/ \
--output json > terrascan-results.json
# Scan a Terraform plan
terrascan scan -t aws -i terraform \
--iac-file tfplan.json \
--iac-type tfplan
# List available policies
terrascan scan --list-policies -t aws
步骤 4:为组织标准创建自定义 OPA 策略
为组织特定的安全要求编写 Rego 策略。
# policy/aws_s3_encryption.rego
package terraform.aws.s3
deny[msg] {
resource := input.resource.aws_s3_bucket[name]
not resource.server_side_encryption_configuration
msg := sprintf("S3 bucket '%s' must have server-side encryption enabled", [name])
}
# policy/aws_iam_no_wildcards.rego
package terraform.aws.iam
deny[msg] {
resource := input.resource.aws_iam_policy[name]
statement := resource.policy.Statement[_]
statement.Action == "*"
statement.Effect == "Allow"
msg := sprintf("IAM policy '%s' must not use wildcard (*) actions", [name])
}
deny[msg] {
resource := input.resource.aws_iam_policy[name]
statement := resource.policy.Statement[_]
statement.Resource == "*"
statement.Effect == "Allow"
contains(statement.Action[_], "*")
msg := sprintf("IAM policy '%s' has overly permissive actions on wildcard resources", [name])
}
# policy/aws_no_public_ingress.rego
package terraform.aws.security_group
deny[msg] {
resource := input.resource.aws_security_group_rule[name]
resource.type == "ingress"
resource.cidr_blocks[_] == "0.0.0.0/0"
resource.from_port <= 22
resource.to_port >= 22
msg := sprintf("Security group rule '%s' allows SSH from 0.0.0.0/0", [name])
}
# Evaluate Terraform plan against OPA policies
terraform show -json tfplan | opa eval \
--data ./policy/ \
--input /dev/stdin \
"data.terraform.aws" \
--format pretty
# Run Conftest for easier OPA policy testing
conftest test tfplan.json --policy ./policy/ --output json
步骤 5:将安全扫描集成到 CI/CD 流水线
将 IaC 安全扫描添加为强制性的 CI/CD 关卡。
# GitHub Actions: Terraform security pipeline
name: Terraform Security Scan
on:
pull_request:
paths: ['terraform/**']
jobs:
security-scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
- name: Terraform Init & Plan
run: |
cd terraform/
terraform init
terraform plan -out=tfplan
terraform show -json tfplan > tfplan.json
- name: Checkov Scan
uses: bridgecrewio/checkov-action@master
with:
directory: terraform/
framework: terraform
output_format: sarif
output_file_path: checkov.sarif
soft_fail: false
- name: tfsec Scan
uses: aquasecurity/[email protected]
with:
working_directory: terraform/
soft_fail: false
- name: Upload SARIF
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: checkov.sarif
- name: OPA Policy Check
run: |
conftest test terraform/tfplan.json \
--policy ./policy/ \
--output json
步骤 6:扫描 Terraform 状态以查找已部署的错误配置
审计当前的 Terraform 状态,以识别已部署的安全问题。
# Export current state as JSON
terraform show -json > terraform-state.json
# Scan the state with Checkov
checkov -f terraform-state.json --framework terraform_plan
# Query state for specific security issues
terraform state list | while read resource; do
terraform state show "$resource" 2>/dev/null | grep -i "public\|0.0.0.0\|encrypt.*false\|password"
done
# Find resources without required tags
terraform state list | grep aws_instance | while read resource; do
tags=$(terraform state show "$resource" | grep -A20 "tags")
if ! echo "$tags" | grep -q "Environment"; then
echo "MISSING TAG: $resource lacks 'Environment' tag"
fi
done
关键概念
| 术语 | 定义 |
|---|---|
| 基础设施即代码 | 通过声明式配置文件(Terraform、CloudFormation)而非手动控制台操作来管理云基础设施的实践 |
| 策略即代码 | 将安全和合规策略表达为可执行代码(Rego、Python),可针对基础设施定义自动评估 |
| 左移安全 | 通过在部署前扫描 IaC 而非在配置后审计,将安全检查移至开发生命周期的更早阶段 |
| Terraform 计划 | Terraform 将进行的更改的预览,可在应用更改前导出为 JSON 用于安全扫描 |
| Checkov | 用于 IaC 的开源静态分析工具,支持 Terraform、CloudFormation、Kubernetes 和 Docker,具有 1000+ 内置策略 |
| OPA/Rego | Open Policy Agent 及其策略语言 Rego,用于定义针对结构化数据输入进行评估的自定义安全规则 |
工具与系统
- Checkov:全面的 IaC 扫描器,具有 1000+ 策略,适用于 Terraform、CloudFormation、Kubernetes、ARM 和 Dockerfile
- tfsec:Terraform 特定的静态分析工具,提供详细的修复指导和 SARIF 输出
- Terrascan:多 IaC 扫描器,支持合规框架(CIS、NIST、SOC 2)及策略即代码
- OPA/Conftest:自定义策略引擎,用于使用 Rego 语言定义组织特定的安全规则
- Bridgecrew:基于 Checkov 构建的商业平台,提供漂移检测和供应链安全
常见场景
场景:为现有 Terraform CI/CD 流水线添加安全门禁
背景:一个 DevOps 团队通过 GitHub Actions 中的 Terraform 部署基础设施,但没有任何安全扫描。近期的审计发现显示,多个 S3 存储桶未启用加密,且安全组允许来自互联网的 SSH 访问。
做法:
- 在 GitHub Actions 工作流中将 Checkov 添加为第一道安全门禁
- 运行
checkov -d ./terraform/以建立当前发现问题的基线 - 对现有发现进行分级处理:修复 CRITICAL 问题,为 HIGH 问题创建工单,抑制已接受的风险
- 添加 tfsec 作为第二扫描器,用于 Terraform 专项检查
- 为组织标准(必需标签、命名约定)编写自定义 OPA 策略
- 将流水线配置为阻止带有 CRITICAL 或 HIGH 问题的 PR
- 生成 SARIF 报告以集成到 GitHub Security 标签页
注意事项:向现有项目添加安全扫描最初会产生数百条发现。应逐步实施,先从仅阻止 CRITICAL 开始,再扩展到 HIGH。对有意设置的例外使用内联抑制注释(#checkov:skip=CKV_AWS_18:Public bucket for static website),并附上书面理由。
输出格式
Terraform Security Audit Report
==================================
Repository: acme-corp/infrastructure
Branch: main
Scan Date: 2026-02-23
Tools: Checkov 3.x, tfsec 1.x, OPA custom policies
SCAN RESULTS:
Checkov checks passed: 187
Checkov checks failed: 34
tfsec checks passed: 156
tfsec checks failed: 28
OPA custom policies: 12 passed, 3 failed
CRITICAL FINDINGS:
[TF-001] S3 Bucket Without Encryption
File: modules/storage/main.tf:24
Resource: aws_s3_bucket.data_lake
Check: CKV_AWS_19
Fix: Add server_side_encryption_configuration block
[TF-002] Security Group Allows SSH from 0.0.0.0/0
File: modules/network/security.tf:45
Resource: aws_security_group_rule.ssh_access
Check: CKV_AWS_24
Fix: Restrict cidr_blocks to bastion subnet
[TF-003] IAM Policy with Wildcard Actions
File: modules/iam/policies.tf:12
Resource: aws_iam_policy.developer_policy
Check: CKV_AWS_1
Fix: Scope actions to specific services required
SUMMARY BY SEVERITY:
Critical: 6 findings
High: 14 findings
Medium: 28 findings
Low: 18 findings
Info: 12 findings
评论
支持 Markdown