Communitygithub.com

terraform-インフラストラクチャのセキュリティ監査

Terraformインフラストラクチャ・アズ・コードのセキュリティ設定ミスを監査します。

terraform-インフラストラクチャのセキュリティ監査 とは?

このスキルは、TerraformのIaC構成ファイルやプランファイルに対して、複数の静的解析ツール(Checkov、tfsec、Terrascan)とOPAポリシーエンジンを組み合わせ、セキュリティ設定ミスやコンプライアンス違反を体系的に検出するためのワークフローを提供します。CI/CDパイプラインへの統合を前提とし、Shift Leftセキュリティの実践を支援します。Checkovは1000以上の組み込みポリシーでIaC全般をスキャンし、tfsecはTerraform特有の脆弱性に対する詳細な修正ガイダンスを提供、TerrascanはCISやNISTなどのフレームワーク準拠をチェックします。さらに、Rego言語で組織固有のポリシーをコード化し、OPAやConftestで評価することで、きめ細かなガバナンスを実現します。既存のTerraform状態ファイルに対する監査や、GitHub ActionsなどのCI/CDパイプラインへのゲート追加手順も含まれており、実践的なセキュリティ監査の自動化を実現します。

対応~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/mukul975/Anthropic-Cybersecurity-Skills/tree/main/skills/auditing-terraform-infrastructure-for-security

お気に入りのAIに質問する

このエージェントスキルを事前に読み込んだ状態で新しいチャットを開きます。

ドキュメント

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コードをスキャン

組み込みポリシーとカスタムポリシーを用いて、包括的なIaCセキュリティスキャンを実行するためにCheckovを実行します。

# 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

ステップ2:tfsecでTerraform固有の問題をスキャン

詳細な修正ガイダンスを含むTerraformネイティブのセキュリティ分析にはtfsecを使用します。

# Terraformディレクトリをスキャン
tfsec ./terraform/

# 最小重大度しきい値を指定してスキャン
tfsec ./terraform/ --minimum-severity HIGH

# CI/CD処理用にJSONで出力
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

# GitHub Securityタブ用にSARIF出力を生成
tfsec ./terraform/ --format sarif > tfsec.sarif

ステップ3:Terrascanでマルチフレームワーク準拠チェック

CIS、NIST、SOC 2などのフレームワークに対するコンプライアンスチェックのためにTerrascanを実行します。

# 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

ステップ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バケット'%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

ステップ5:CI/CDパイプラインにセキュリティスキャンを統合

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: 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スキャン
        uses: bridgecrewio/checkov-action@master
        with:
          directory: terraform/
          framework: terraform
          output_format: sarif
          output_file_path: checkov.sarif
          soft_fail: false

      - name: tfsecスキャン
        uses: aquasecurity/[email protected]
        with:
          working_directory: terraform/
          soft_fail: false

      - name: SARIFアップロード
        uses: github/codeql-action/upload-sarif@v2
        with:
          sarif_file: checkov.sarif

      - name: OPAポリシーチェック
        run: |
          conftest test terraform/tfplan.json \
            --policy ./policy/ \
            --output json

ステップ6:デプロイ済みの設定ミスを特定するためにTerraform状態をスキャン

現在の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 PlanTerraformが行う変更のプレビューであり、変更を適用する前にセキュリティスキャン用にJSONとしてエクスポートできる
CheckovIaC用のオープンソース静的解析ツール。Terraform、CloudFormation、Kubernetes、Dockerをサポートし、1000以上の組み込みポリシーを備える
OPA/RegoOpen Policy Agentとそのポリシー言語Rego。構造化データ入力に対して評価されるカスタムセキュリティルールを定義するためのもの

ツールとシステム

  • Checkov:Terraform、CloudFormation、Kubernetes、ARM、Dockerfile向けの1000以上のポリシーを備えた包括的なIaCスキャナー
  • tfsec:Terraform固有の静的解析ツール。詳細な修正ガイダンスとSARIF出力を提供
  • Terrascan:ポリシー・アズ・コードを使用してコンプライアンスフレームワーク(CIS、NIST、SOC 2)をサポートするマルチIaCスキャナー
  • OPA/Conftest:Rego言語を使用して組織固有のセキュリティルールを定義するためのカスタムポリシーエンジン
  • Bridgecrew:Checkov上に構築された商用プラットフォーム。ドリフト検出とサプライチェーンセキュリティを提供

一般的なシナリオ

シナリオ:既存のTerraform CI/CDパイプラインにセキュリティゲートを追加

コンテキスト:あるDevOpsチームはGitHub ActionsでTerraformを介してインフラをデプロイしているが、セキュリティスキャンは行っていない。最近の監査結果により、暗号化されていない複数のS3バケットと、インターネットからのSSHを許可するセキュリティグループが明らかになった。

アプローチ:

  1. GitHub Actionsワークフローに最初のセキュリティゲートとしてCheckovを追加する
  2. checkov -d ./terraform/を実行して、現在の所見のベースラインを確立する
  3. 既存の所見をトリアージする:CRITICAL問題を修正し、HIGHのチケットを作成し、受け入れられたリスクを抑制する
  4. Terraform固有のチェックのためにセカンダリスキャナーとしてtfsecを追加する
  5. 組織標準(必須タグ、命名規則)のカスタムOPAポリシーを作成する
  6. CRITICALまたはHIGHの所見があるPRをブロックするようにパイプラインを設定する
  7. GitHub Securityタブ統合用にSARIFレポートを生成する

落とし穴:既存のプロジェクトにセキュリティスキャンを追加すると、最初は数百の所見が発生する。CRITICALのみでブロックすることから始め、徐々にHIGHに拡大する。意図的な例外には、正当な理由を文書化したインライン抑制コメント(#checkov:skip=CKV_AWS_18:静的ウェブサイト用のパブリックバケット)を使用する。

出力形式

Terraform Security Audit Report
==================================
リポジトリ: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 不合格

CRITICAL 発見事項:
[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
  修正:アクションを必要な特定のサービスに限定する

重大度別の要約:
  Critical:   6 件
  High:       14 件
  Medium:     28 件
  Low:        18 件
  Info:       12 件

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

abusing-dpapi-for-credential-access

Extract and decrypt Windows DPAPI-protected secrets (Credential Manager, browser logins/cookies, Wi-Fi credentials, KeePass keys) online or offline using SharpDPAPI, SharpChrome, Mimikatz, or Impacket

abusing-shadow-credentials-for-privesc

Take over Active Directory accounts by writing attacker-controlled public keys to msDS-KeyCredentialLink (Shadow Credentials) with pyWhisker, Whisker, or Certipy, then authenticate via PKINIT to recover the target

achieving-cmmc-level-2-compliance

>-

acquiring-disk-image-with-dd-and-dcfldd

Create forensically sound bit-for-bit disk images with dd or dcfldd on a Linux forensic workstation, preserving evidence integrity through hash verification (MD5/SHA) during acquisition. Use when imaging a suspect drive, USB device, or memory card for investigation, preserving volatile disk evidence during incident response, or producing a verified copy for legal or law-enforcement proceedings before any destructive analysis.

analyzing-active-directory-acl-abuse

Detect dangerous ACL misconfigurations in Active Directory using ldap3

analyzing-android-malware-with-apktool

Perform static analysis of Android APK malware using apktool for resource decompilation, jadx for Java source recovery, and androguard for manifest inspection, dangerous permission-combination detection, and identification of obfuscated code, dynamic code loading, and reflection-based API calls. Use to statically triage a suspicious APK without executing it or to build mobile malware detection rules.

analyzing-api-gateway-access-logs

Parses API Gateway access logs (AWS API Gateway, Kong, Nginx) to detect

analyzing-apt-group-with-mitre-navigator

Query ATT&CK data with attackcti, mitreattack-python, and stix2, then build MITRE ATT&CK Navigator layers and multi-layer heatmap overlays mapping one or more APT groups

analyzing-azure-activity-logs-for-threats

Queries Azure Monitor activity logs and sign-in logs via azure-monitor-query

analyzing-bootkit-and-rootkit-samples

Analyzes bootkit and advanced rootkit malware infecting the Master

analyzing-browser-forensics-with-hindsight

Parse Chromium-based browser databases with Hindsight to extract and correlate browsing history, downloads, cookies, cached content, autofill data, saved passwords, and extensions from Chrome, Edge, Brave, Opera, and Vivaldi into a unified timeline (XLSX, JSON, or SQLite output). Use during incident response, insider-threat investigations, or criminal cases when you need to reconstruct a user

analyzing-campaign-attribution-evidence

Systematically evaluate cyber-campaign evidence to attribute an operation to a threat actor, using the Diamond Model and Analysis of Competing Hypotheses (ACH) to weigh infrastructure overlaps, TTP consistency, malware code similarity, and timing/language artifacts into confidence-weighted attribution assessments. Use when an incident investigation needs a defensible attribution confidence level.

analyzing-certificate-transparency-for-phishing

Monitor Certificate Transparency logs using crt.sh and Certstream to

analyzing-cloud-storage-access-patterns

Detect abnormal access in AWS S3, GCS, and Azure Blob Storage by analyzing CloudTrail Data Events, GCS audit logs, and Azure Storage Analytics for after-hours bulk downloads, new-IP access, and API-call spikes (e.g. GetObject) via statistical baselines and time-series anomaly detection. Use when investigating suspected cloud data exfiltration or building related detection rules.

analyzing-cobalt-strike-beacon-configuration

Extract and analyze Cobalt Strike beacon configuration from PE files

analyzing-cobaltstrike-malleable-c2-profiles

Parse and analyze Cobalt Strike Malleable C2 profiles with dissect.cobaltstrike (profiles and beacon-payload configs) and pyMalleableC2 (AST parsing) to extract HTTP/DNS transforms, URIs, headers, sleep/jitter, and injection behavior, then generate network detection signatures. Use when reverse-engineering a captured malleable profile or building detections against Cobalt Strike Beacon traffic.

analyzing-command-and-control-communication

Analyzes malware C2 communication over HTTP, HTTPS, DNS, and custom

analyzing-cyber-kill-chain

Analyzes intrusion activity against the Lockheed Martin Cyber Kill Chain

analyzing-disk-image-with-autopsy

Perform comprehensive forensic analysis of raw (dd), E01, or AFF disk images with Autopsy and The Sleuth Kit, recovering deleted files, examining metadata and embedded artifacts, keyword searching, and building investigation timelines with visual reports. Use for structured analysis of a forensic disk image or when stakeholders need visual reports from evidence.

analyzing-dns-logs-for-exfiltration

Analyzes DNS query logs to detect data exfiltration via DNS tunneling,

関連スキル