您可以設定 Google Cloud Armor 安全性政策,保護服務免受阻斷服務 (DoS) 和網路攻擊。這些政策會使用 Google 的全球基礎架構保護應用程式,並控管流經負載平衡器的流量。
本文提供各種負載平衡器的安全性政策設定範例。
設定外部應用程式負載平衡器的安全性政策
如要設定 Google Cloud Armor 安全性政策,允許或拒絕傳送至全域外部應用程式負載平衡器或傳統版應用程式負載平衡器的流量,請按照下列步驟操作:
- 建立 Cloud Armor 安全性政策。
- 根據 IP 位址、自訂運算式或預先設定的運算式集新增規則。
- 將安全性政策附加至後端服務。
- 視需要更新安全性政策。
在本範例中,您會建立兩項 Cloud Armor 安全性政策,並套用至不同的後端服務。
在本範例中,這些是 Cloud Armor 安全性政策:
mobile-clients-policy適用於games服務的外部使用者。internal-users-policy適用於貴機構的「test-network」團隊。
您將 mobile-clients-policy 套用至 games 服務,後者後端服務名為 games;您將 internal-users-policy 套用至測試團隊的內部 test 服務,後者對應的後端服務名為 test-network。
如果後端服務的後端執行個體位於多個區域,這項政策會套用至所有區域的執行個體。在本例中,mobile-clients-policy 適用於 us-central 中的執行個體 1、2、3 和 4,以及 us-east 中的執行個體 5 和 6。
建立範例
請按照本節的步驟,建立上一節討論的範例設定。
控制台
為外部使用者設定安全性政策:
前往 Google Cloud 控制台的「Cloud Armor policies」(Cloud Armor 政策) 頁面。
按一下「Create policies」(建立政策)。
在「Name」(名稱) 欄位中,輸入
mobile-clients-policy。在「Description」(說明) 欄位輸入
Policy for external users。在「預設規則動作」部分,選取「拒絕」。
在「拒絕狀態」,選取「404 (找不到)」。
點選「下一步」。
新增更多規則:
- 按一下「Add rule」(新增規則)。
- 在「Description」(說明) 欄位輸入
allow traffic from 192.0.2.0/24。 - 在「模式」中,選取「基本模式 (僅限 IP 位址/範圍)」。
- 在「Match」(相符) 欄位中輸入
192.0.2.0/24。 - 在「動作」部分選取「允許」。
- 在「Priority」(優先順序) 欄位中輸入
1000。 - 按一下「完成」。
- 點選「下一步」。
對目標套用政策:
- 點選「新增目標」。
- 在「目標」清單中選取目標。
- 按一下「完成」。
- 點選「建立政策」。
視需要啟用 Google Cloud Armor Adaptive Protection:
- 如要啟用 Adaptive Protection,請選取「啟用」。
為內部使用者設定安全性政策:
- 按一下「Create policies」(建立政策)。
- 在「Name」(名稱) 欄位中,輸入
internal-users-policy。 - 在「Description」(說明) 欄位輸入
Policy for internal test users。 - 在「預設規則動作」部分,選取「拒絕」。
- 在「拒絕狀態」中,選取「502 (閘道錯誤)」。
- 點選「下一步」。
新增更多規則:
- 按一下「Add rule」(新增規則)。
- 在「Description」(說明) 欄位輸入
allow traffic from 198.51.100.0/24。 - 在「模式」中,選取「基本模式 (僅限 IP 位址/範圍)」。
- 在「Match」(相符) 欄位中輸入
198.51.100.0/24。 - 在「動作」部分選取「允許」。
- 如要僅預覽,請勾選「啟用」核取方塊。
- 在「Priority」(優先順序) 欄位中輸入
1000。 - 按一下「完成」。
- 點選「下一步」。
對目標套用政策:
- 點選「新增目標」。
- 在「目標」清單中選取目標。
- 按一下「完成」。
- 點選「建立政策」。
gcloud
建立 Cloud Armor 安全性政策:
gcloud compute security-policies create mobile-clients-policy \ --description "policy for external users"gcloud compute security-policies create internal-users-policy \ --description "policy for internal test users"更新安全性政策的預設規則,拒絕流量:
gcloud compute security-policies rules update 2147483647 \ --security-policy mobile-clients-policy \ --action "deny-404"gcloud compute security-policies rules update 2147483647 \ --security-policy internal-users-policy \ --action "deny-502"在安全性政策中新增規則:
gcloud compute security-policies rules create 1000 \ --security-policy mobile-clients-policy \ --description "allow traffic from 192.0.2.0/24" \ --src-ip-ranges "192.0.2.0/24" \ --action "allow"gcloud compute security-policies rules create 1000 \ --security-policy internal-users-policy \ --description "allow traffic from 198.51.100.0/24" \ --src-ip-ranges "198.51.100.0/24" \ --action "allow"將安全性政策附加至後端服務:
gcloud compute backend-services update games \ --security-policy mobile-clients-policygcloud compute backend-services update test-network \ --security-policy internal-users-policy視需要啟用 Adaptive Protection:
gcloud compute security-policies update mobile-clients-policy \ --enable-layer7-ddos-defensegcloud compute security-policies update internal-users-policy \ --enable-layer7-ddos-defense
建立安全性政策
使用 Google Cloud 控制台或 gcloud CLI 建立安全性政策。這些操作說明假設您要為現有的負載平衡器和後端服務設定政策。如需範例,請參閱「建立範例」。
控制台
如要建立規則並將安全性政策附加至後端服務,請按照下列步驟操作:
前往 Google Cloud 控制台的「Cloud Armor policies」(Cloud Armor 政策) 頁面。
按一下「Create policies」(建立政策)。
在「Name」(名稱) 欄位中,輸入政策名稱。
選用:輸入政策說明。
在「政策類型」中,選擇「後端安全性政策」或「邊緣安全性政策」。
在「預設規則動作」部分,選取「允許」可預設允許存取,選取「拒絕」則預設拒絕存取。
預設規則的優先順序最低,只有在沒有其他適用規則時才會生效。
如果設定「拒絕」規則,請選取拒絕狀態「回應代碼」。 如果要求遭到拒絕,Cloud Armor 會傳回這個狀態碼。
點選「下一步」。
新增更多規則:
- 按一下「Add rule」(新增規則)。
- 選用:輸入規則說明。
選取模式:
- 基本模式:根據 IP 位址或 IP 範圍允許或拒絕流量。
- 進階模式:根據規則運算式允許或拒絕流量。
在「比對」欄位中,指定規則適用的條件:
- 基本模式:輸入要比對的 IP 位址或範圍。
- 進階模式:輸入運算式,評估傳入的要求。請參閱「自訂規則語言屬性」。
在「動作」部分,選取「允許」或「拒絕」。
如要啟用預覽模式,請選取「啟用」。在預覽模式中,Cloud Armor 會記錄規則的效果,但不會強制執行。
輸入規則的「優先順序」 (0 至 2,147,483,646)。請參閱規則評估順序。
按一下「完成」。
如要新增更多規則,請按一下「新增規則」。否則,請點選「下一步」。
對目標套用政策:
- 點選「新增目標」。
- 在「目標」清單中選取目標。
- 如要新增更多目標,請按一下「新增目標」。
- 按一下「完成」。
- 點選「建立政策」。
gcloud
如要建立 Cloud Armor 安全性政策,請使用
gcloud compute security-policies create指令。使用
--type旗標指定CLOUD_ARMOR(後端) 或CLOUD_ARMOR_EDGE(邊緣)。如果省略這個旗標,Cloud Armor 會建立後端安全性政策:gcloud compute security-policies create NAME \ [--type=CLOUD_ARMOR|CLOUD_ARMOR_EDGE] \ [--file-format=FILE_FORMAT | --description=DESCRIPTION] \ [--file-name=FILE_NAME]更改下列內容:
NAME:安全性政策的名稱FILE_FORMAT:--file-name中指定的檔案格式;請指定yaml或jsonDESCRIPTION:安全政策說明FILE_NAME:包含安全政策 YAML 或 JSON 匯出內容的檔案名稱
下列指令會更新政策、開啟 JSON 剖析功能,並將記錄層級變更為
VERBOSE:gcloud compute security-policies update my-policy \ --json-parsing=STANDARD \ --log-level=VERBOSE如要將規則新增至安全性政策,請使用
gcloud compute security-policies rules create PRIORITY指令。gcloud compute security-policies rules create PRIORITY \ [--security-policy POLICY_NAME] \ [--description DESCRIPTION] \ --src-ip-ranges IP_RANGE,... | --expression EXPRESSION \ --action=[ allow | deny-403 | deny-404 | deny-502 ] \ [--preview]更改下列內容:
PRIORITY:規則優先順序。請參閱規則評估順序POLICY_NAME:安全性政策名稱DESCRIPTION:規則說明IP_RANGE,...:以半形逗號分隔的 IP 位址範圍清單EXPRESSION:Cloud Armor 規則語言運算式
舉例來說,下列指令會新增規則,封鎖來自
192.0.2.0/24和198.51.100.0/24的流量。規則的優先順序為 1000:gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --description "block traffic from 192.0.2.0/24 and 198.51.100.0/24" \ --src-ip-ranges "192.0.2.0/24","198.51.100.0/24" \ --action "deny-403"新增
--preview旗標後,系統會將規則新增至政策,但不會強制執行,且只會記錄觸發規則的流量。gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --description "block traffic from 192.0.2.0/24 and 198.51.100.0/24" \ --src-ip-ranges "192.0.2.0/24","198.51.100.0/24" \ --action "deny-403" \ --preview使用
--expression旗標指定自訂條件。請參閱自訂規則語言屬性。下列指令會新增規則,允許來自 IP 位址1.2.3.4的流量,該位址在User-Agent標頭中包含字串example:gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --expression "inIpRange(origin.ip, '1.2.3.4/32') && has(request.headers['user-agent']) && request.headers['user-agent'].contains('example')" \ --action allow \ --description "Block User-Agent 'example'"下列指令會新增規則,如果要求的 Cookie 含有特定值,就會封鎖要求:
gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --expression "has(request.headers['cookie']) && request.headers['cookie'].contains('cookie_name=cookie_value')" \ --action "deny-403" \ --description "Cookie Block"下列指令會新增規則,封鎖來自
AU地區的要求:gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --expression "origin.region_code == 'AU'" \ --action "deny-403" \ --description "AU block"下列指令會新增規則,封鎖來自
AU區域且不在指定 IP 範圍內的要求:gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --expression "origin.region_code == 'AU' && !inIpRange(origin.ip, '1.2.3.0/24')" \ --action "deny-403" \ --description "country and IP block"下列指令會新增規則,封鎖 URI 符合規則運算式的要求:
gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --expression "request.path.matches('/example_path/')" \ --action "deny-403" \ --description "regex block"如果
user-id標頭的 Base64 解碼值包含特定值,下列指令會新增規則來封鎖要求:gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --expression "has(request.headers['user-id']) && request.headers['user-id'].base64Decode().contains('myValue')" \ --action "deny-403" \ --description "country and IP block"下列指令會新增規則,使用預先設定的運算式集來防範 SQLi 攻擊:
gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --expression "evaluatePreconfiguredWaf('sqli-v422-stable')" \ --action "deny-403"下列指令會新增規則,使用預先設定的運算式,允許具名 IP 位址清單中的所有 IP 位址存取:
gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --expression "evaluatePreconfiguredWaf('sourceiplist-fastly')" \ --action "allow"
設定區域外部應用程式負載平衡器的安全性政策
請按照本節的步驟,為區域性外部應用程式負載平衡器設定區域範圍的 Cloud Armor 安全性政策。
保護區域性負載平衡工作負載
請按照本節的步驟,為區域範圍的後端服務設定安全性政策:
建立區域範圍安全性政策。
gcloud compute security-policies create POLICY_NAME \ --type=CLOUD_ARMOR \ --region=REGION更改下列內容:
POLICY_NAME:安全性政策的名稱REGION:要建立安全性政策的區域
將區域範圍的安全性政策附加至區域範圍的後端服務。將
BACKEND_NAME替換為現有區域範圍後端服務的名稱。gcloud compute backend-services update BACKEND_NAME \ --security-policy=POLICY_NAME \ --region=REGION
範例:套用區域範圍的安全性政策
在這個範例中,身為安全管理員,您希望滿足所有後端工作負載和 WAF 規則都部署在特定區域的駐留需求。假設您已符合下列條件:
- 您在該區域中建立了區域範圍的負載平衡後端服務。
- 您已在部署作業中停用任何現有的全域範圍安全性政策。
- 您已在相同區域中建立並附加區域範圍的安全性政策 (如上一節所述)。
您可以使用下列指令,將 WAF 規則和其他進階規則加入政策:
為政策新增 WAF 規則:
gcloud compute security-policies rules create 1000 --action=deny-404 \ --expression="evaluatePreconfiguredWaf('xss-v422-stable', ['owasp-crs-v042200-id941100-xss', 'owasp-crs-v042200-id941160-xss'])" \ --security-policy=POLICY_NAME \ --region=REGION更改下列內容:
POLICY_NAME:安全性政策的名稱REGION:安全政策的區域
為政策新增進階規則:
gcloud compute security-policies rules create 1000 --action=allow \ --expression="has(request.headers['cookie']) && request.headers['cookie'].contains('80=EXAMPLE')" \ --security-policy=POLICY_NAME \ --region=REGION為政策新增頻率限制規則:
gcloud compute security-policies rules create 1000 --action=throttle \ --src-ip-ranges="1.1.1.1/32" \ --rate-limit-threshold-count=1000 \ --rate-limit-threshold-interval-sec=120 \ --conform-action="allow" \ --exceed-action="deny-429" \ --enforce-on-key=IP \ --ban-duration-sec=999 \ --ban-threshold-count=5000 \ --ban-threshold-interval-sec=60 \ --security-policy=POLICY_NAME \ --region=REGION更改下列內容:
POLICY_NAME:安全性政策的名稱REGION:安全政策的區域
後續步驟
- 建立及查看 Cloud Armor 安全性政策
- 進一步瞭解速率限制。