skills/auditing-terraform-infrastructure-for-security/SKILL.md
使用 Checkov、tfsec、Terrascan 和 OPA/Rego 策略,审计 Terraform 基础设施即代码中的安全错误配置, 在云部署前检测过度宽松的 IAM 策略、公开资源暴露、缺失加密和不安全的默认设置。
npx skillsauth add killvxk/cybersecurity-skills-zh auditing-terraform-infrastructure-for-securityInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
不适用于:运行时安全监控(使用 CSPM 工具)、应用安全测试(使用 SAST/DAST 工具),或部署后的云配置漂移检测(使用 AWS Config 或 Azure Policy)。
pip install checkov)brew install tfsec 或从 GitHub 下载二进制文件)brew install terrascan)运行 Checkov 进行全面的 IaC 安全扫描,使用内置和自定义策略。
# 扫描 Terraform 目录
checkov -d ./terraform/ --framework terraform
# 按特定检查类别扫描
checkov -d ./terraform/ --check CKV_AWS_18,CKV_AWS_19,CKV_AWS_20,CKV_AWS_21
# 扫描并以 JSON 格式输出结果
checkov -d ./terraform/ --output json > checkov-results.json
# 扫描 Terraform 计划文件以获得更准确的分析
terraform init && terraform plan -out=tfplan
terraform show -json tfplan > tfplan.json
checkov -f tfplan.json --framework terraform_plan
# 跳过特定检查(附带理由)
checkov -d ./terraform/ --skip-check CKV_AWS_145 \
--bc-api-key $BRIDGECREW_API_KEY
# 扫描 Terraform 模块
checkov -d ./modules/ --framework terraform --compact
# 列出所有可用检查
checkov --list --framework terraform | grep CKV_AWS
使用 tfsec 进行 Terraform 原生安全分析,提供详细的修复指南。
# 扫描 Terraform 目录
tfsec ./terraform/
# 以最低严重性阈值扫描
tfsec ./terraform/ --minimum-severity HIGH
# 以 JSON 格式输出供 CI/CD 处理
tfsec ./terraform/ --format json > tfsec-results.json
# 使用自定义检查扫描
tfsec ./terraform/ --custom-check-dir ./custom-checks/
# 排除特定规则
tfsec ./terraform/ --exclude-downloaded-modules \
--exclude aws-s3-enable-bucket-logging
# 扫描并对特定严重性失败
tfsec ./terraform/ --minimum-severity CRITICAL --soft-fail
# 生成 SARIF 输出供 GitHub Security 选项卡使用
tfsec ./terraform/ --format sarif > tfsec.sarif
执行 Terrascan 对 CIS、NIST 和 SOC 2 框架进行合规检查。
# 对 CIS AWS 基准扫描 Terraform
terrascan scan -t aws -i terraform -d ./terraform/ \
--policy-type aws --verbose
# 对特定合规框架扫描
terrascan scan -t aws -i terraform -d ./terraform/ \
--policy-type aws \
--categories "Compliance Validation"
# 以 JSON 格式输出
terrascan scan -t aws -i terraform -d ./terraform/ \
--output json > terrascan-results.json
# 扫描 Terraform 计划
terrascan scan -t aws -i terraform \
--iac-file tfplan.json \
--iac-type tfplan
# 列出可用策略
terrascan scan --list-policies -t aws
编写 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 存储桶 '%s' 必须启用服务器端加密", [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 策略 '%s' 不得使用通配符 (*) 操作", [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 策略 '%s' 对通配符资源使用了过度宽松的操作", [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("安全组规则 '%s' 允许来自 0.0.0.0/0 的 SSH 访问", [name])
}
# 对 OPA 策略评估 Terraform 计划
terraform show -json tfplan | opa eval \
--data ./policy/ \
--input /dev/stdin \
"data.terraform.aws" \
--format pretty
# 使用 Conftest 进行更简便的 OPA 策略测试
conftest test tfplan.json --policy ./policy/ --output json
将 IaC 安全扫描作为强制性 CI/CD 门控。
# GitHub Actions:Terraform 安全流水线
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
审计当前 Terraform 状态以识别已部署的安全问题。
# 将当前状态导出为 JSON
terraform show -json > terraform-state.json
# 使用 Checkov 扫描状态文件
checkov -f terraform-state.json --framework terraform_plan
# 查询状态中的特定安全问题
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
# 查找缺少必要标签的资源
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 "缺少标签: $resource 缺少 'Environment' 标签"
fi
done
| 术语 | 定义 | |------|------------| | 基础设施即代码(Infrastructure as Code) | 通过声明性配置文件(Terraform、CloudFormation)而非手动控制台操作来管理云基础设施的实践 | | 策略即代码(Policy as Code) | 将安全和合规策略表示为可执行代码(Rego、Python),可对基础设施定义进行自动化评估 | | 左移安全(Shift Left Security) | 通过在部署前扫描 IaC 而非配置后审计,将安全检查提前到开发生命周期的早期阶段 | | Terraform 计划(Terraform Plan) | Terraform 将要进行的变更预览,可导出为 JSON 在应用前进行安全扫描 | | Checkov | 开源 IaC 静态分析工具,支持 Terraform、CloudFormation、Kubernetes 和 Docker,内置 1000+ 策略 | | OPA/Rego | Open Policy Agent 及其策略语言 Rego,用于定义对结构化数据输入进行评估的自定义安全规则 |
场景背景:DevOps 团队通过 GitHub Actions 中的 Terraform 部署基础设施,但没有安全扫描。近期审计发现多个 S3 存储桶未加密,安全组允许来自互联网的 SSH 访问。
方法:
checkov -d ./terraform/ 建立当前发现的基线常见陷阱:在现有项目中添加安全扫描最初会产生数百条发现。逐步实施:先从仅阻止严重问题开始,再扩展到高风险问题。对有意为之的例外使用内联抑制注释(#checkov:skip=CKV_AWS_18:静态网站的公开存储桶),并附上文档化的理由。
Terraform 安全审计报告
==================================
仓库: acme-corp/infrastructure
分支: main
扫描日期: 2026-02-23
工具: Checkov 3.x, tfsec 1.x, OPA 自定义策略
扫描结果:
Checkov 检查通过: 187
Checkov 检查失败: 34
tfsec 检查通过: 156
tfsec 检查失败: 28
OPA 自定义策略: 12 通过,3 失败
严重发现:
[TF-001] 未加密的 S3 存储桶
文件: modules/storage/main.tf:24
资源: aws_s3_bucket.data_lake
检查: CKV_AWS_19
修复: 添加 server_side_encryption_configuration 块
[TF-002] 安全组允许来自 0.0.0.0/0 的 SSH 访问
文件: modules/network/security.tf:45
资源: aws_security_group_rule.ssh_access
检查: CKV_AWS_24
修复: 将 cidr_blocks 限制为堡垒机子网
[TF-003] IAM 策略使用通配符操作
文件: modules/iam/policies.tf:12
资源: aws_iam_policy.developer_policy
检查: CKV_AWS_1
修复: 将操作范围缩减到所需的特定服务
各严重程度摘要:
严重: 6 条发现
高: 14 条发现
中: 28 条发现
低: 18 条发现
信息: 12 条发现
testing
设计并执行社会工程学渗透测试,包括钓鱼、语音钓鱼、短信钓鱼和物理借口活动,以衡量人员安全韧性并识别培训差距。
testing
主持结构化的事件后审查,以识别根本原因、记录有效和无效的措施,并提出可操作的改进建议以提升未来的事件响应能力。
testing
通过分析举报的邮件、提取指标、评估凭据受攻陷情况、在全组织范围隔离恶意邮件并修复受影响账号来响应网络钓鱼事件。涵盖邮件头分析、URL/附件沙箱检测和邮箱范围清除操作。适用于网络钓鱼响应、邮件事件、凭据钓鱼、鱼叉式网络钓鱼调查或钓鱼修复相关请求。
tools
票据传递(Pass-the-Ticket,PtT)是一种横向移动技术,使用窃取的 Kerberos 票据(TGT 或 TGS)在不知道用户密码的情况下向服务进行认证。通过从已控制的主机内存中提取 Kerberos 票据,攻击者可以将这些票据注入自己的会话以模拟票据所有者。