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. 05使用 VPC Flow Logs 监控

适用场景

  1. 01检查过于宽松的防火墙配置
  2. 02对 GCP 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/互连流量过滤。

前提条件

  • 已启用 Compute Engine API 的 GCP 项目
  • IAM 角色:用于防火墙管理的 roles/compute.securityAdmin、用于审计的 roles/compute.networkViewer
  • 用于分层防火墙策略的 Organization Admin 角色
  • 已通过适当权限完成身份验证的 gcloud 命令行
  • 在目标子网上启用 VPC Flow Logs 以进行监控

工作流

步骤 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 Flow Logs 进行监控

配置 VPC Flow Logs 以监控流量模式并验证防火墙规则的有效性。

# 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 Flow Logs在子网级别捕获的网络遥测数据,记录流量元数据,用于监控、取证和防火墙规则验证
隐含规则GCP 默认防火墙规则,允许到所有目标的出站流量并拒绝来自所有来源的入站流量,优先级最低(65535)

工具与系统

  • gcloud compute firewall-rules:用于在 GCP 中创建、列出和管理 VPC 防火墙规则的命令行命令
  • 分层防火墙策略:组织级和文件夹级策略,跨所有项目强制执行安全控制
  • VPC Flow Logs:子网级流量日志记录,用于监控、故障排查和验证防火墙有效性
  • Cloud Logging:用于分析 VPC Flow Logs 和防火墙规则命中次数的查询引擎
  • Security Command Center:GCP 原生安全平台,可提供关于过于宽松防火墙配置的发现

常见场景

场景:在发现规则过于宽松后锁定生产 VPC

背景:安全审计发现生产 VPC 具有默认允许规则,允许来自 0.0.0.0/0 的 SSH 以及不受限制的出站流量。SCC 报告了 14 项防火墙发现项。

方法:

  1. 使用 gcloud compute firewall-rules list 枚举所有现有规则,并按风险分类
  2. 在所有子网上启用 VPC Flow Logs,以捕获 7 天的基准流量模式
  3. 分析流日志,以识别需要显式允许规则的合法流量
  4. 为每个应用层创建有针对性的入站规则(web:443,app:8080,db:5432)
  5. 将任意来源 SSH 规则替换为仅来自堡垒机子网的 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

评论

支持 Markdown