mcpField
I
implementing-gcp-vpc-firewall-rules

implementing-gcp-vpc-firewall-rules

github.com/mukul975セキュリティ35.2k46 回インストール8 週間前に更新
リポジトリ

implementing-gcp-vpc-firewall-rules について

GCP 管理者やセキュリティチーム向けに、VPC ファイアウォール制御の実装と監査手順を示すスキルです。受信・送信ルールの制限、階層型ポリシー、フローログ監視を扱います。

主な機能

  1. 01既存のファイアウォールルール監査
  2. 02最小権限の受信ルール作成
  3. 03送信トラフィックの制限
  4. 04階層型ファイアウォールポリシーの適用
  5. 05VPC Flow Logs による監視

活用シーン

  1. 01過度に許可的な設定の点検
  2. 02GCP VPC 内のワークロード分割
  3. 03組織全体へのファイアウォール制御の適用

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/mukul975/anthropic-cybersecurity-skills --skill implementing-gcp-vpc-firewall-rules

インストール後、Claude Code で/implementing-gcp-vpc-firewall-rulesを入力して読み込まれたか確認します。

同梱ファイル

SKILL.md と一緒にインストールされる参考資料やスクリプト。

LICENSE
references/
api-reference.md
scripts/
agent.py

詳細

GCP VPC ファイアウォールルールの実装

使用するタイミング

  • ネットワークレベルのアクセス制御が必要な新しい GCP ワークロードをデプロイするとき
  • 既存のファイアウォール構成に過度に許容的なルールがないか監査するとき
  • GCP VPC ネットワーク内でゼロトラストのネットワークセグメンテーションを実装するとき
  • オープンなファイアウォールルールに関する Security Command Center の検出結果に対応するとき
  • GCP 組織全体で階層型ファイアウォールポリシーを構築するとき

次の用途には使用しないでください: アプリケーション層のフィルタリング(Cloud Armor WAF を使用)、DNS ベースのフィルタリング(Cloud DNS のレスポンスポリシーを使用)、または VPC ファイアウォールルールが VPC 内のトラフィックに適用されることを理解せずに行う VPN/Interconnect トラフィックのフィルタリング。

前提条件

  • Compute Engine API が有効な GCP プロジェクト
  • IAM ロール: ファイアウォール管理用の roles/compute.securityAdmin、監査用の roles/compute.networkViewer
  • 階層型ファイアウォールポリシー用の Organization Admin ロール
  • 適切な権限で認証された gcloud CLI
  • 監視対象のサブネットで有効化された VPC フローログ

ワークフロー

ステップ 1: 既存のファイアウォールルールをセキュリティ上の欠陥がないか監査する

すべてのファイアウォールルールを列挙し、過度に許容的な構成を特定します。

# List all firewall rules in the project
gcloud compute firewall-rules list \
  --format="table(name, network, direction, priority, allowed[].map().firewall_rule().list():label=ALLOWED, sourceRanges, targetTags)"

# Find rules allowing all traffic from 0.0.0.0/0
gcloud compute firewall-rules list \
  --filter="direction=INGRESS AND sourceRanges=0.0.0.0/0" \
  --format="table(name, network, allowed, priority, targetTags)" \
  --sort-by=priority

# Find rules allowing all protocols and ports
gcloud compute firewall-rules list \
  --filter="direction=INGRESS AND allowed[].IPProtocol=all" \
  --format="table(name, network, sourceRanges, targetTags)"

# Find rules with SSH (22) or RDP (3389) open to the internet
gcloud compute firewall-rules list \
  --filter="direction=INGRESS AND sourceRanges=0.0.0.0/0 AND (allowed[].ports=22 OR allowed[].ports=3389)" \
  --format="table(name, network, allowed, sourceRanges)"

# Check for disabled rules
gcloud compute firewall-rules list \
  --filter="disabled=true" \
  --format="table(name, network, direction)"
ステップ 2: 制限的な上り(イングレス)ファイアウォールルールを作成する

ターゲティングにネットワークタグとサービスアカウントを使用して、最小権限の上りルールを実装します。

# Create rule allowing HTTPS from the internet to web servers only
gcloud compute firewall-rules create allow-https-web \
  --network=production-vpc \
  --direction=INGRESS \
  --action=ALLOW \
  --rules=tcp:443 \
  --source-ranges=0.0.0.0/0 \
  --target-tags=web-server \
  --priority=1000 \
  --description="Allow HTTPS to web servers from internet"

# Create rule allowing SSH only from bastion host subnet
gcloud compute firewall-rules create allow-ssh-bastion \
  --network=production-vpc \
  --direction=INGRESS \
  --action=ALLOW \
  --rules=tcp:22 \
  --source-ranges=10.0.1.0/24 \
  --target-tags=ssh-allowed \
  --priority=1000 \
  --description="Allow SSH only from bastion subnet"

# Create rule allowing internal communication between app tiers
gcloud compute firewall-rules create allow-app-to-db \
  --network=production-vpc \
  --direction=INGRESS \
  --action=ALLOW \
  --rules=tcp:5432 \
  --source-tags=app-server \
  --target-tags=db-server \
  --priority=1000 \
  --description="Allow PostgreSQL from app tier to database tier"

# Create service-account-based rule (more secure than tags)
gcloud compute firewall-rules create allow-api-internal \
  --network=production-vpc \
  --direction=INGRESS \
  --action=ALLOW \
  --rules=tcp:8080 \
  --source-service-accounts=api-client@project.iam.gserviceaccount.com \
  --target-service-accounts=api-server@project.iam.gserviceaccount.com \
  --priority=1000
ステップ 3: 下り(エグレス)の制限を実装する

下りファイアウォールルールを構成して送信トラフィックを制御し、データ流出を防止します。

# Deny all egress by default (low priority)
gcloud compute firewall-rules create deny-all-egress \
  --network=production-vpc \
  --direction=EGRESS \
  --action=DENY \
  --rules=all \
  --destination-ranges=0.0.0.0/0 \
  --priority=65534 \
  --description="Default deny all egress traffic"

# Allow egress to Google APIs via restricted VIP
gcloud compute firewall-rules create allow-google-apis \
  --network=production-vpc \
  --direction=EGRESS \
  --action=ALLOW \
  --rules=tcp:443 \
  --destination-ranges=199.36.153.4/30 \
  --priority=1000 \
  --description="Allow HTTPS to Google APIs restricted VIP"

# Allow DNS resolution
gcloud compute firewall-rules create allow-dns-egress \
  --network=production-vpc \
  --direction=EGRESS \
  --action=ALLOW \
  --rules=udp:53,tcp:53 \
  --destination-ranges=169.254.169.254/32,8.8.8.8/32,8.8.4.4/32 \
  --priority=1000 \
  --description="Allow DNS resolution to metadata and Google DNS"

# Allow egress to specific external services
gcloud compute firewall-rules create allow-external-apis \
  --network=production-vpc \
  --direction=EGRESS \
  --action=ALLOW \
  --rules=tcp:443 \
  --destination-ranges=PARTNER_CIDR/32 \
  --target-tags=api-client \
  --priority=1000
ステップ 4: 階層型ファイアウォールポリシーをデプロイする

すべてのプロジェクトに適用される組織レベルおよびフォルダレベルのファイアウォールポリシーを作成します。

# Create an organization-level firewall policy
gcloud compute firewall-policies create \
  --organization=ORG_ID \
  --short-name=org-security-policy \
  --description="Organization-wide security firewall policy"

# Add rule to block known malicious IP ranges at org level
gcloud compute firewall-policies rules create 100 \
  --firewall-policy=org-security-policy \
  --organization=ORG_ID \
  --direction=INGRESS \
  --action=deny \
  --src-ip-ranges=THREAT_INTEL_CIDR_1,THREAT_INTEL_CIDR_2 \
  --layer4-configs=all \
  --description="Block known malicious IPs organization-wide"

# Add rule to enforce HTTPS-only ingress at org level
gcloud compute firewall-policies rules create 200 \
  --firewall-policy=org-security-policy \
  --organization=ORG_ID \
  --direction=INGRESS \
  --action=allow \
  --src-ip-ranges=0.0.0.0/0 \
  --layer4-configs=tcp:443 \
  --description="Allow only HTTPS from external sources"

# Associate policy with the organization
gcloud compute firewall-policies associations create \
  --firewall-policy=org-security-policy \
  --organization=ORG_ID
ステップ 5: 監視のために VPC フローログを有効化する

VPC フローログを構成して、トラフィックパターンを監視し、ファイアウォールルールの有効性を検証します。

# Enable flow logs on a subnet
gcloud compute networks subnets update production-subnet \
  --region=us-central1 \
  --enable-flow-logs \
  --logging-aggregation-interval=interval-5-sec \
  --logging-flow-sampling=1.0 \
  --logging-metadata=include-all

# Query flow logs in Cloud Logging for denied traffic
gcloud logging read '
  resource.type="gce_subnetwork"
  AND jsonPayload.disposition="DENIED"
  AND timestamp>="2026-02-22T00:00:00Z"
' --limit=50 --format=json

# Find traffic hitting overly permissive rules
gcloud logging read '
  resource.type="gce_subnetwork"
  AND jsonPayload.rule_details.reference:"/firewall-rules/default-allow-"
' --limit=100 --format="table(jsonPayload.connection.src_ip,jsonPayload.connection.dest_ip,jsonPayload.connection.dest_port)"

# Export flow logs to BigQuery for analysis
gcloud logging sinks create vpc-flow-bq \
  bigquery.googleapis.com/projects/PROJECT/datasets/vpc_flow_logs \
  --log-filter='resource.type="gce_subnetwork"'

主要な概念

用語定義
VPC ファイアウォールルールIP 範囲、プロトコル、ポート、タグに基づいて、VM インスタンスとの間のトラフィックを許可または拒否する、ステートフルなネットワークレベルのアクセス制御
階層型ファイアウォールポリシーVPC レベルのルールより前に評価され、配下のすべてのプロジェクトに適用される組織レベルまたはフォルダレベルのファイアウォールポリシー
ネットワークタグファイアウォールルールの適用対象を決定するために VM インスタンスに付与されるラベル。上り・下りルールのターゲティングに使用される
サービスアカウントのファイアウォールルール接続されたサービスアカウントに基づいてインスタンスを対象とするファイアウォールルール。変更可能なネットワークタグよりも安全なターゲティングを提供する
VPC フローログサブネットレベルで取得されるネットワークテレメトリで、監視、フォレンジック、ファイアウォールルールの検証のためにトラフィックのメタデータを記録する
暗黙のルールすべての宛先への下りを許可し、すべての送信元からの上りを拒否する、最も低い優先度(65535)を持つデフォルトの GCP ファイアウォールルール

ツールとシステム

  • gcloud compute firewall-rules: GCP で VPC ファイアウォールルールを作成、一覧表示、管理するためのコマンドライン CLI
  • 階層型ファイアウォールポリシー: すべてのプロジェクトにわたってセキュリティ制御を適用する組織レベルおよびフォルダレベルのポリシー
  • VPC フローログ: 監視、トラブルシューティング、ファイアウォールの有効性検証のためのサブネットレベルのトラフィックログ
  • Cloud Logging: VPC フローログとファイアウォールルールのヒット数を分析するためのクエリエンジン
  • Security Command Center: 過度に許容的なファイアウォール構成に関する検出結果を提供する GCP ネイティブのセキュリティプラットフォーム

一般的なシナリオ

シナリオ:過度に許可的なルールの発見後に本番 VPC をロックダウンする

背景:セキュリティ監査により、本番 VPC に 0.0.0.0/0 からの SSH と無制限の送信を許可するデフォルト許可ルールが存在することが判明しました。SCC は 14 件のファイアウォールに関する検出結果を報告しています。

手順:

  1. gcloud compute firewall-rules list で既存のルールをすべて列挙し、リスクごとに分類する
  2. すべてのサブネットで VPC フローログを有効にし、7 日間の基準トラフィックパターンを収集する
  3. フローログを分析して、明示的な許可ルールが必要な正当なトラフィックを特定する
  4. アプリケーションの階層ごとに対象を絞った内向きルールを作成する(web: 443、app: 8080、db: 5432)
  5. どこからでも SSH を許可するルールを、bastion サブネットからのみ SSH を許可するルールに置き換える
  6. デフォルト拒否の送信ルールを実装し、必要な送信先に対する明示的な許可ルールを追加する
  7. アプリケーションが正しく動作することを確認してから、過度に許可的なデフォルト許可ルールを削除する

注意点:トラフィックパターンを理解しないままファイアウォールルールを削除すると、障害の原因になります。ルールを削除する前に、必ずフローログを有効にしてトラフィックを分析してください。ネットワークタグは compute.instances.setTags 権限を持つ任意のユーザーが追加できるため、重要なルールの対象指定にはサービスアカウントベースの方式よりも安全性が低くなります。

出力形式

GCP VPC Firewall Audit Report
================================
Project: production-project
VPC Network: production-vpc
Audit Date: 2026-02-23

RULE INVENTORY:
  Total firewall rules: 34
  Ingress rules: 22
  Egress rules: 12
  Disabled rules: 3

CRITICAL FINDINGS:
[FW-001] SSH Open to Internet
  Rule: default-allow-ssh
  Source: 0.0.0.0/0 -> tcp:22
  Target: All instances (no tags)
  Priority: 65534
  Remediation: Restrict to bastion subnet CIDR

[FW-002] No Egress Restrictions
  Issue: Only implied allow-all-egress rule exists
  Risk: No controls on outbound data exfiltration
  Remediation: Add default-deny egress and explicit allow rules

REMEDIATION ACTIONS COMPLETED:
  Rules deleted: 3 (overly permissive defaults)
  Rules created: 8 (targeted allow rules)
  Egress deny rule: Created at priority 65534
  Flow logs enabled: 6 subnets

コメント