入侵偵測與防範服務 會監控 Google Cloud 工作負載流量中是否有惡意活動,並採取預防措施。如要在網路上啟用這項服務,您必須設定多個 Cloud Next Generation Firewall 元件。本教學課程說明如何設定網路中的入侵偵測和防護服務,
目標
本教學課程將說明如何完成下列工作:
- 建立包含兩個子網路的虛擬私有雲 (VPC) 網路。
- 在 VPC 網路的第一個子網路中建立伺服器虛擬機器 (VM) 執行個體,並在 VM 上安裝 Apache 伺服器。
- 在虛擬私有雲網路的第二個子網路中建立用戶端 VM 執行個體。
- 建立安全性設定檔和安全性設定檔群組。
- 建立防火牆端點,並與虛擬私有雲網路建立關聯。
- 新增全域網路防火牆政策,並加入下列防火牆規則:
- 防火牆規則,可讓 Identity-Aware Proxy (IAP) 存取虛擬私有雲網路中的 VM 執行個體。
- 防火牆規則,可將所有輸入流量導向第 7 層檢查。
- 確認是否已封鎖傳送至伺服器 VM 執行個體的惡意流量。
- 清除資源。
下圖顯示本教學課程中部署設定的大致架構。虛擬私有雲 fw-policy-ips
vpc-ips
中的防火牆政策會將輸入流量重新導向至可用區 asia-southeast1-a
中的防火牆端點。防火牆端點 endpoint-ips
會檢查流量是否有任何威脅。如果偵測到任何威脅,系統會根據安全設定檔 sec-profile-ips
中指定的指示,採取預防措施。
費用
建立防火牆端點會產生費用。如要瞭解定價詳細資料,請參閱 Cloud Next Generation Firewall 定價。
事前準備
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
- 為專案啟用 Compute Engine API。
- 為專案啟用 Network Security API。
- 為專案啟用 Identity-Aware Proxy API。
- 在機構中擁有 Compute 網路管理員 (
roles/compute.networkAdmin
) IAM 角色。 - 如果您希望透過指令列操作,請先安裝 Google Cloud CLI。如需該工具的概念與安裝資訊,請參閱 gcloud CLI 總覽。
注意:如果您先前沒有執行過 gcloud CLI,請先執行
gcloud init
,初始化 gcloud CLI 目錄。
建立含子網路的自訂虛擬私有雲網路
在本節中,您會建立自訂模式的虛擬私有雲網路,並設定兩個 IPv4 子網路。
主控台
在 Google Cloud 控制台中,前往「VPC networks」(虛擬私有雲網路) 頁面。
按一下「建立虛擬私有雲網路」。
在「Name」(名稱) 中輸入
vpc-ips
。在「Description」(說明) 中輸入
VPC network to set up intrusion detection and prevention service
。在「子網路建立模式」部分,選取「自訂」。
在「New subnet」(新子網路) 部分,指定子網路的以下設定參數:
- Name (名稱):
subnet-ips-server
- Region (區域):
asia-southeast1
- IPv4 範圍:
10.0.0.0/24
- Name (名稱):
按一下 [完成]。
按一下「Add subnet」(新增子網路),然後指定下列設定參數:
- Name (名稱):
subnet-ips-client
- Region (區域):
us-central1
- IPv4 範圍:
192.168.10.0/24
- Name (名稱):
按一下 [完成]。
點選「建立」。
gcloud
如要建立 VPC 網路,請執行下列指令:
gcloud compute networks create vpc-ips \ --subnet-mode custom \ --description "VPC network to set up intrusion detection and prevention service."
在「授權 Cloud Shell」對話方塊中,按一下「授權」。
如要建立子網路,請執行下列指令:
gcloud compute networks subnets create subnet-ips-server \ --network vpc-ips \ --region asia-southeast1 \ --range 10.0.0.0/24
如要建立另一個子網路,請執行下列指令:
gcloud compute networks subnets create subnet-ips-client \ --network vpc-ips \ --region us-central1 \ --range 192.168.10.0/24
建立 VM 執行個體
在本節中,您將建立伺服器和用戶端 VM 執行個體。
建立伺服器 VM 執行個體
在本節中,您會在子網路 subnet-ips-server
中建立 VM 執行個體,並在該執行個體上安裝 Apache 伺服器。
主控台
前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。
在「機器設定」窗格中,執行下列操作:
- 在「Name」(名稱) 中輸入
vm-server-ips
。 - 在「Region」(區域) 中選取
asia-southeast1 (Singapore)
。 - 在「Zone」(可用區) 中選取
asia-southeast1-a
。
- 在「Name」(名稱) 中輸入
在導覽選單中,按一下「OS and Storage」(OS 和儲存空間)。
在「Operating system and storage」(作業系統和儲存空間) 專區中,確認「映像檔」為「Debian GNU/Linux 12 (bookworm)」。如果不是,請按一下「變更」,然後將「作業系統」欄位設為「Debian」,並將「版本」欄位設為「Debian GNU/Linux 12 (bookworm)」。
在導覽選單中,按一下「網路」。
- 在「Network interfaces」(網路介面) 區段中,按一下
default
,然後指定下列設定參數:- 網路:
vpc-ips
- 「Subnetwork」(子網路):
subnet-ips-server IPv4 (10.0.0.0/24)
- 網路:
- 按一下 [完成]。
- 在「Network interfaces」(網路介面) 區段中,按一下
在導覽選單中,按一下「進階」,並在「開機指令碼」欄位中輸入下列指令碼:
#! /bin/bash apt update apt -y install apache2 cat <<EOF > /var/www/html/index.html <html><body><p>Hello world.</p></body></html> EOF
點選「建立」。
建立伺服器 VM 後,請記下其外部 IP 位址。
gcloud
如要建立伺服器 VM,請執行下列指令:
gcloud compute instances create vm-server-ips \ --network vpc-ips \ --zone asia-southeast1-a \ --subnet subnet-ips-server \ --stack-type IPV4_ONLY \ --image-project debian-cloud \ --image-family debian-11 \ --metadata=startup-script='#! /bin/bash apt update apt -y install apache2 cat <<EOF > /var/www/html/index.html <html><body><p>Hello World.</p></body></html> EOF'
記下傳回狀態中的 VM 外部 IP 位址。
建立用戶端 VM 執行個體
在本節中,您會在子網路 subnet-ips-client
中建立 VM 執行個體。
主控台
前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。
在「機器設定」窗格中,執行下列操作:
- 在「Name」(名稱) 中輸入
vm-client-ips
。 - 在「Region」(區域) 中選取
us-central1 (Iowa)
。 - 在「Zone」(可用區) 中選取
us-central1-a
。
- 在「Name」(名稱) 中輸入
在導覽選單中,按一下「網路」。
- 在「Network interfaces」(網路介面) 區段中,按一下
default
,然後指定下列設定參數:- 網路:
vpc-ips
- 「Subnetwork」(子網路):
subnet-ips-client IPv4 (192.168.10.0/24)
- 網路:
- 按一下 [完成]。
- 在「Network interfaces」(網路介面) 區段中,按一下
點選「建立」。
gcloud
如要建立用戶端 VM,請執行下列指令:
gcloud compute instances create vm-client-ips \ --network vpc-ips \ --zone us-central1-a \ --subnet subnet-ips-client \ --stack-type IPV4_ONLY
建立安全性設定檔
在本節中,您將在貴機構中建立 threat-prevention
類型的安全性設定檔。如要查看建立安全設定檔所需的權限,請參閱「這項工作需要的權限」。
主控台
前往 Google Cloud 控制台的「Security profiles」(安全性設定檔) 頁面。
在專案選取器選單中,選取您的機構。
選取「安全性設定檔」分頁標籤。
按一下 Create profile。
在「Name」(名稱) 中輸入
sec-profile-ips
。在「Description」(說明) 中輸入
Security profile to set up intrusion detection and prevention service
。按一下「繼續」。
點選「建立」。
gcloud
如要建立安全性設定檔,請執行下列指令:
gcloud network-security security-profiles \ threat-prevention \ create sec-profile-ips \ --organization ORGANIZATION_ID \ --location global \ --project PROJECT_ID \ --description "Security profile to set up intrusion detection and prevention service."
更改下列內容:
ORGANIZATION_ID
:建立安全性設定檔的機構。PROJECT_ID
:用於安全設定檔配額和存取限制的專案 ID。
建立安全性設定檔群組
在本節中,您將建立安全性設定檔群組,納入前一節建立的安全性設定檔。如要查看建立安全性設定檔群組所需的權限,請參閱「這項工作需要的權限」。
主控台
前往 Google Cloud 控制台的「Security profiles」(安全性設定檔) 頁面。
在專案選取器選單中,選取您的機構。
選取「安全性設定檔群組」分頁標籤。
按一下「建立設定檔群組」。
在「Name」(名稱) 中輸入
sec-profile-group-ips
。在「Description」(說明) 中輸入
Security profile group to set up intrusion detection and prevention service
。在「威脅防護設定檔」清單中,選取
sec-profile-ips
。點選「建立」。
gcloud
如要建立安全性設定檔群組,請執行下列指令:
gcloud network-security security-profile-groups \ create sec-profile-group-ips \ --organization ORGANIZATION_ID \ --location global \ --project PROJECT_ID \ --threat-prevention-profile \ organizations/ORGANIZATION_ID/locations/global/securityProfiles/sec-profile-ips \ --description "Security profile group to set up intrusion detection and prevention service."
更改下列內容:
ORGANIZATION_ID
:建立安全性設定檔群組的機構。PROJECT_ID
:用於安全設定檔群組的配額和存取限制的專案 ID。
建立防火牆端點
在本節中,您將在特定區域建立防火牆端點。如要查看建立防火牆端點所需的權限,請參閱「這項工作需要的權限」。
注意:建立防火牆端點時,防火牆端點的狀態會設為 Creating
。防火牆端點準備就緒後,狀態會變更為 Active
。
主控台
前往 Google Cloud 控制台的「Firewall endpoints」(防火牆端點) 頁面。
在專案選取器選單中,選取您的機構。
點選「建立」。
在「Region」(區域) 清單中選取「
asia-southeast1 (Singapore)
」。在「Zone」(可用區) 清單中選取「
asia-southeast1-a
」。在「Name」(名稱) 中輸入
endpoint-ips
。點選「建立」。
gcloud
如要建立防火牆端點,請執行下列指令:
gcloud network-security firewall-endpoints \ create endpoint-ips \ --organization ORGANIZATION_ID \ --zone asia-southeast1-a \ --billing-project PROJECT_ID
更改下列內容:
ORGANIZATION_ID
:建立防火牆端點的機構。PROJECT_ID
:用於防火牆端點帳單的專案 ID。
建立防火牆端點關聯
在本節中,您會將防火牆端點與在上一個步驟中建立的虛擬私有雲網路建立關聯。
主控台
在 Google Cloud 控制台中,前往「VPC networks」(虛擬私有雲網路) 頁面。
按一下
vpc-ips
網路,顯示其「虛擬私有雲網路詳細資料」頁面。選取「防火牆端點」分頁標籤。
按一下「新增端點關聯」。
在「Region」(區域) 清單中選取「
asia-southeast1
」。在「Zone」(可用區) 清單中選取「
asia-southeast1-a
」。在「Firewall endpoint」(防火牆端點) 清單中,選取「
endpoint-ips
」。點選「建立」。
gcloud
如要建立防火牆端點關聯,請執行下列指令:
gcloud network-security firewall-endpoint-associations \ create endpoint-association-ips \ --endpoint organizations/ORGANIZATION_ID/locations/asia-southeast1-a/firewallEndpoints/endpoint-ips \ --network vpc-ips \ --zone asia-southeast1-a \ --project PROJECT_ID
更改下列內容:
ORGANIZATION_ID
:建立防火牆端點的機構。PROJECT_ID
:建立關聯的專案 ID。
建立全域網路防火牆政策
在本節中,您將建立全域網路防火牆政策,其中包含下列兩項防火牆規則:
- 優先順序為
100
的輸入防火牆規則,允許 TCP 流量傳輸至通訊埠3389
和22
。這項規則可讓 IAP 存取虛擬私有雲網路中的 VM 執行個體。 - 優先順序為
200
的輸入防火牆規則,可對特定可用區中伺服器 VM 的輸入流量執行第 7 層檢查。
主控台
在 Google Cloud 控制台中,前往「Firewall policies」(防火牆政策) 頁面。
在專案選取器清單中,選取機構內的專案。
按一下「建立防火牆政策」。
在「Name」(名稱) 中輸入
fw-policy-ips
。在「Deployment scope」(部署範圍) 部分,選取「Global」(全域)。
按一下「繼續」,然後按一下「新增規則」。
在「Priority」(優先順序) 欄位中輸入
100
。針對「記錄」,選取「開啟」。
在「Direction of traffic」(流量方向) 中選取 [Ingress] (輸入)。
在「Action on match」(相符時執行的動作) 中選取 [Allow] (允許)。
在「來源」篩選器中選取「IPv4」,然後在「IP 範圍」欄位中輸入
35.235.240.0/20
。在「Protocols and ports」(通訊協定和通訊埠) 部分,選取「Specified protocols and ports」(指定的通訊協定和通訊埠)。
選取「TCP」,然後在「Ports」中輸入
22,3389
。點選「建立」。
按一下 [新增規則]。
在「Priority」(優先順序) 欄位中輸入
200
。針對「記錄」,選取「開啟」。
在「Direction of traffic」(流量方向) 中選取 [Ingress] (輸入)。
在「Action on match」(相符時執行的動作) 中選取「Proceed to L7 inspection」(進行 L7 檢查)。
在「Security profile group」(安全性設定檔群組) 清單中,選取「
sec-profile-group-ips
」。在「目的地」篩選器中選取「IPv4」,然後在「IP 範圍」欄位中,輸入您在「建立伺服器 VM 執行個體」一節中建立的伺服器 VM 外部 IP 位址。
點選「建立」。
按一下「繼續」。
按一下「將政策連結到虛擬私有雲網路」。
選取
vpc-ips
網路。按一下「關聯」。
點選「建立」。
gcloud
如要建立全域網路防火牆政策,請執行下列指令:
gcloud compute network-firewall-policies \ create fw-policy-ips \ --global \ --project PROJECT_ID
更改下列內容:
PROJECT_ID
:建立全域網路防火牆政策的專案 ID。
如要新增防火牆規則來啟用 IAP 存取權,請執行下列指令:
gcloud compute network-firewall-policies rules create 100 \ --firewall-policy fw-policy-ips \ --direction INGRESS \ --action ALLOW \ --src-ip-ranges 35.235.240.0/20 \ --layer4-configs tcp:22, tcp:3389 \ --global-firewall-policy \ --enable-logging
如要新增防火牆規則,啟用第 7 層檢查功能來防範及偵測威脅,請執行下列指令:
gcloud compute network-firewall-policies rules create 200 \ --direction INGRESS \ --firewall-policy fw-policy-ips \ --action apply_security_profile_group \ --dest-ip-ranges SERVER_VM_IP \ --layer4-configs tcp:0-65535 \ --global-firewall-policy \ --security-profile-group \ //networksecurity.googleapis.com/organizations/ORGANIZATION_ID \ /locations/global/securityProfileGroups/sec-profile-group-ips \ --enable-logging
更改下列內容:
SERVER_VM_IP
:您在「建立伺服器 VM 執行個體」一節中建立的伺服器 VM 外部 IP 位址。ORGANIZATION_ID
:建立安全設定檔群組的機構。
如要將防火牆政策與虛擬私有雲網路建立關聯,請執行下列指令:
gcloud compute network-firewall-policies associations create \ --firewall-policy fw-policy-ips \ --network vpc-ips \ --name fw-pol-association-ips \ --global-firewall-policy \ --project PROJECT_ID
更改下列內容:
PROJECT_ID
:建立 VPC 關聯的專案 ID。
測試設定
在本節中,您將產生流量,並由端點攔截,然後套用全域網路防火牆政策,執行第 7 層檢查,藉此測試設定。
主控台
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
從
vm-server-ips
VM 的「External IP」(外部 IP) 欄中,複製 VM 的外部 IP 位址。在
vm-client-ips
VM 的「連線」欄中,按一下「SSH」。在「SSH-in-browser」(直接透過瀏覽器進行 SSH 連線) 對話方塊中,按一下「Authorize」(授權),然後等待連線建立。
如要確認非威脅要求未遭到封鎖,請執行下列指令:
curl EXTERNAL_IP -m 2
將
EXTERNAL_IP
替換為vm-server-ips
VM 的外部 IP。預期的回應訊息如下:
<!doctype html><html><body><h1>Hello World!</h1></body></html>
如要確認惡意要求遭到封鎖,請執行下列指令:這項指令會傳送要求,存取禁止存取的密碼檔案。
curl -m 2 EXTERNAL_IP:80/cgi-bin/../../../../bin/cat%20/etc/passwd/
將
EXTERNAL_IP
替換為vm-server-ips
VM 的外部 IP。防火牆端點偵測到要求中的威脅並封鎖封包,因此預期會出現
Connection timed out
訊息。關閉「SSH-in-browser」(透過瀏覽器進行 SSH 連線) 對話方塊。
gcloud
如要連線至
vm-client-ips
VM,請執行下列指令:gcloud compute ssh vm-client-ips \ --zone=us-central1-a \ --tunnel-through-iap
出現提示時,請按下 Y 鍵確認,然後按下 Enter 鍵。
如要確認系統未封鎖非威脅要求,請執行下列指令:
curl EXTERNAL_IP -m 2
將
EXTERNAL_IP
替換為vm-server-ips
VM 的外部 IP。預期的回應訊息如下:
<!doctype html><html><body><h1>Hello World!</h1></body></html>
如要確認惡意要求遭到封鎖,請執行下列指令:
curl -m 2 EXTERNAL_IP:80/cgi-bin/../../../../bin/cat%20/etc/passwd/
將
EXTERNAL_IP
替換為vm-server-ips
VM 的外部 IP。防火牆端點偵測到要求中的威脅並封鎖封包,因此預期會出現
Connection timed out
訊息。如要關閉「透過瀏覽器建立 SSH 連線」,請輸入
exit
。
查看威脅記錄
前往 Google Cloud 控制台的「Threats」(威脅) 頁面。
必要時,請選取 Google Cloud 專案。
在「威脅」部分,您可以查看
vpc-ips
網路偵測到的威脅記錄項目。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本教學課程所用資源的費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
在本節中,您將刪除在本教學課程中建立的資源。
刪除防火牆端點關聯
主控台
在 Google Cloud 控制台中,前往「VPC networks」(虛擬私有雲網路) 頁面。
按一下
vpc-ips
網路,顯示其「虛擬私有雲網路詳細資料」頁面。選取「防火牆端點」分頁標籤。這個分頁會顯示已設定的防火牆端點關聯清單。
勾選「
endpoint-ips
」旁的核取方塊,然後按一下「刪除」。再按一下 [刪除] 加以確認。
gcloud
如要刪除防火牆端點關聯,請執行下列指令: yes,
gcloud network-security firewall-endpoint-association
delete endpoint-ips
--zone asia-southeast1-a
刪除防火牆端點
主控台
前往 Google Cloud 控制台的「Firewall endpoints」(防火牆端點) 頁面。
選取「
endpoint-ips
」,然後按一下「刪除」。再按一下 [刪除] 加以確認。
gcloud
如要刪除防火牆端點,請執行下列指令:
gcloud network-security firewall-endpoints delete endpoint-ips \ --organization ORGANIZATION_ID \ --zone asia-southeast1-a
更改下列內容:
ORGANIZATION_ID
:建立端點的機構。
刪除全域網路防火牆端點政策
主控台
在 Google Cloud 控制台中,前往「Firewall policies」(防火牆政策) 頁面。
在專案選取器選單中,選取含有政策的專案。
按一下 [
fw-policy-ips
]。按一下「關聯項目」分頁標籤。
選取所有關聯。
按一下「移除關聯項目」。
移除所有關聯後,按一下「刪除」。
gcloud
如要移除防火牆政策與 VPC 網路之間的關聯,請執行下列指令:
gcloud compute network-firewall-policies associations delete \ --name fw-pol-association-ips \ --firewall-policy fw-policy-ips \ --global-firewall-policy
The network firewall policy does not have an association with pol-association-fw-rules
Google Cloud刪除防火牆政策。
gcloud compute network-firewall-policies delete fw-policy-ips --global
出現提示時,請按下 Y 鍵確認,然後按下 Enter 鍵。
刪除安全性設定檔群組
主控台
前往 Google Cloud 控制台的「Security profiles」(安全性設定檔) 頁面。
選取「安全性設定檔群組」分頁標籤。
選取
sec-profile-group-ips
,然後按一下「Delete」(刪除)。再按一下 [刪除] 加以確認。
gcloud
如要刪除安全性設定檔群組,請執行下列指令:
gcloud network-security security-profile-groups \ delete sec-profile-group-ips \ --organization ORGANIZATION_ID \ --location global
更改下列內容:
ORGANIZATION_ID
:建立安全性設定檔群組的機構。
刪除安全性設定檔
主控台
前往 Google Cloud 控制台的「Security profiles」(安全性設定檔) 頁面。
選取「安全性設定檔」分頁標籤。這個分頁會顯示已設定的安全設定檔清單。
選取「
sec-profile-ips
」,然後按一下「刪除」。再按一下 [刪除] 加以確認。
gcloud
如要刪除安全性設定檔,請執行下列指令:
gcloud network-security security-profiles threat-prevention \ delete sec-profile-ips \ --organization ORGANIZATION_ID \ --location global
更改下列內容:
ORGANIZATION_ID
:建立安全設定檔的機構。
刪除 VM
主控台
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
勾選
vm-client-ips
和vm-server-ips
VM 的核取方塊。點選「刪除」。
在「Delete 2 instances?」(要刪除 2 個執行個體嗎?) 對話方塊中,按一下「Delete」(刪除)。
gcloud
如要刪除
vm-client-ips
VM,請執行下列指令:gcloud compute instances delete vm-client-ips \ --zone us-central1-a
出現提示時,請按下 Y 鍵確認,然後按下 Enter 鍵。
如要刪除
vm-server-ips
VM,請執行下列指令:gcloud compute instances delete vm-server-ips \ --zone asia-southeast1-a
出現提示時,請按下 Y 鍵確認,然後按下 Enter 鍵。
刪除虛擬私有雲網路及其子網路
主控台
在 Google Cloud 控制台中,前往「VPC networks」(虛擬私有雲網路) 頁面。
在「Name」(名稱) 欄中,按一下
vpc-ips
。按一下「刪除虛擬私有雲網路」。
在「刪除網路」對話方塊中,按一下「刪除」。
刪除 VPC 時,系統也會一併刪除子網路。
gcloud
如要刪除
vpc-ips
虛擬私有雲網路的子網路subnet-ips-client
,請執行下列指令:gcloud compute networks subnets delete subnet-ips-client \ --region us-central1
出現提示時,請按下 Y 鍵確認,然後按下 Enter 鍵。
如要刪除
vpc-ips
虛擬私有雲網路的子網路subnet-ips-server
,請執行下列指令:gcloud compute networks subnets delete subnet-ips-server \ --region=asia-southeast1
出現提示時,請按下 Y 鍵確認,然後按下 Enter 鍵。
如要刪除
vpc-ips
VPC 網路,請執行下列指令:gcloud compute networks delete vpc-ips
後續步驟
- 如需入侵偵測和預防服務的概念資訊,請參閱入侵偵測和預防服務總覽。
- 如要瞭解防火牆政策的概念資訊,請參閱防火牆政策。
- 如要瞭解防火牆政策規則的概念資訊,請參閱防火牆政策規則。
- 如要瞭解費用,請參閱 Cloud NGFW 定價。