按照这些说明创建 Google Cloud Armor 安全政策,以过滤流向外部 HTTP(S) 负载平衡器的传入流量。如需了解安全政策的概念信息,请参阅 Google Cloud Armor 安全政策概览。
如需了解如何在 Google Kubernetes Engine (GKE) 上配置 Google Cloud Armor,请参阅通过 BackendConfig 参数配置 Ingress 功能部分的 Google Cloud Armor 安全政策。
准备工作
在配置安全政策之前,请执行以下操作:
确保您熟悉 HTTP(S) 负载均衡概念。
检查现有后端服务以确定哪些服务尚未关联安全政策。这些后端服务及其关联的后端将不受 Google Cloud Armor 的保护。如需获取 Google Cloud Armor 提供的保护,请按照本文档中的说明将新创建的或现有的安全政策关联到这些后端服务。
为 Google Cloud Armor 安全政策设置 IAM 权限
以下操作需要 Identity and Access Management (IAM) 角色 Compute Security Admin (roles/compute.securityAdmin):
- 配置、修改、更新和删除 Google Cloud Armor 安全政策
- 允许的 API 方法:
SecurityPolicies insert
SecurityPolicies delete
SecurityPolicies patch
SecurityPolicies addRule
SecurityPolicies patchRule
SecurityPolicies removeRule
具有 Network Admin 角色 (roles/compute.networkAdmin) 的用户可以执行以下操作:
- 为后端服务设置 Google Cloud Armor 安全政策
- 允许的 API 方法:
BackendServices setSecurityPolicy
具有 Security Admin 和 Network Admin 角色的用户可以使用 API 方法 SecurityPolicies get
、list
和 getRule
查看 Google Cloud Armor 安全政策。
为自定义角色设置 IAM 权限
下表列出了 IAM 角色的基本权限及其关联的 API 方法。
IAM 权限 | API 方法 |
---|---|
compute.securityPolicies.create |
SecurityPolicies insert |
compute.securityPolicies.delete |
SecurityPolicies delete |
compute.securityPolicies.get |
SecurityPolicies get SecurityPolicies getRule |
compute.securityPolicies.list |
SecurityPolicies list |
compute.securityPolicies.use |
BackendServices setSecurityPolicy |
compute.securityPolicies.update |
SecurityPolicies patch SecurityPolicies addRule SecurityPolicies patchRule SecurityPolicies removeRule |
compute.backendServices.setSecurityPolicy |
BackendServices setSecurityPolicy |
为 HTTP(S) 负载平衡配置安全政策
以下是配置 Google Cloud Armor 安全政策的高级步骤,可启用允许或拒绝流向外部 HTTP(S) 负载平衡器流量的规则:
- 创建 Google Cloud Armor 安全政策。
- 根据 IP 地址列表、自定义表达式或预配置表达式集向安全规则添加规则。
- 将安全政策附加到您想要控制访问权限的外部 HTTP(S) 负载平衡器的后端服务中。
- 根据需要更新安全政策。
在以下示例中,您将创建两项 Google Cloud Armor 安全政策并将其应用于不同的后端服务。
在示例中,以下项是 Google Cloud Armor 安全政策:
mobile-clients-policy
适用于您的games
服务的外部用户。internal-users-policy
适用于您组织的test-network
团队。
您将 mobile-clients-policy
应用于 games
服务,其后端服务称为 games
,并将 internal-users-policy
应用于测试团队的内部 test
服务,其相应的后端服务称为 test-network
。
如果后端服务的后端实例位于多个区域,则与该服务关联的 Google Cloud Armor 安全政策适用于所有区域中的实例。在上述示例中,安全政策 mobile-clients-policy
适用于 us-central
中的实例 1、2、3 和 4,以及 us-east
中的实例 5 和实例 6。
创建示例
按照说明创建上一部分讨论的示例配置。
控制台
为外部用户配置安全政策:
在 Google Cloud Console 中,前往网络安全页面。
在政策页面上,点击创建政策。
在名称字段中,输入
mobile-clients-policy
。在说明字段中,输入
Policy for external users
。对于默认规则操作,选择拒绝。
对于拒绝状态,请选择 404(未找到)。
点击下一步。
添加更多规则:
- 点击添加规则。
- 在说明字段中,输入
allow traffic from 192.0.2.0/24
。 - 对于模式,请选择基本模式(仅限 IP 地址/范围)。
- 在匹配字段中,输入
192.0.2.0/24
。 - 对于操作,请选择允许。
- 在 Priority(优先级)字段中,输入
1000
。 - 点击完成。
- 点击下一步。
将政策应用于目标:
- 点击添加目标。
- 在目标列表中,选择一个目标。
- 点击完成。
- 点击创建政策。
(可选)启用自动调节式保护:
- 如需启用自动调节式保护,请勾选启用复选框。
为内部用户配置安全政策:
- 在政策页面上,点击创建政策。
- 在名称字段中,输入
internal-users-policy
。 - 在说明字段中,输入
Policy for internal test users
。 - 对于默认规则操作,选择拒绝。
- 对于拒绝状态,请选择 502 (网关错误)。
- 点击下一步。
添加更多规则:
- 点击添加规则。
- 在说明字段中,输入
allow traffic from 198.51.100.0/24
。 - 对于模式,请选择基本模式(仅限 IP 地址/范围)。
- 在匹配字段中,输入
198.51.100.0/24
。 - 对于操作,请选择允许。
- 对于仅可预览,请选择启用复选框。
- 在 Priority(优先级)字段中,输入
1000
。 - 点击完成。
- 点击下一步。
将政策应用于目标:
- 点击添加目标。
- 在目标列表中,选择一个目标。
- 点击完成。
- 点击创建政策。
gcloud
创建 Google 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-policy
gcloud compute backend-services update test-network \ --security-policy internal-users-policy
(可选)启用自动调节式保护:
gcloud compute security-policies update mobile-clients-policy \ --enable-layer7-ddos-defense
gcloud compute security-policies update internal-users-policy \ --enable-layer7-ddos-defense
配置安全政策、规则和表达式
您可以使用 Google Cloud Console、Google Cloud CLI 或 REST API 配置 Google Cloud Armor 安全政策、规则和表达式。使用 gcloud CLI 创建安全政策时,请使用 --type
标志指定安全政策是后端安全政策还是边缘安全政策。
测试安全政策
我们建议您在预览模式下部署所有新规则,然后检查请求日志,以验证政策和规则的行为符合预期。
示例表达式
下面是示例表达式。如需详细了解表达式,请参阅 Google Cloud Armor 自定义规则语言参考。
如果您要配置使用 ISO 3166-1 alpha 2 国家或地区代码的规则或表达式,请注意 Google Cloud Armor 会独立处理每个代码。Google Cloud Armor 规则和表达式显式使用这些地区代码来允许或拒绝请求。
以下表达式与来自 IP 地址
1.2.3.4
的请求匹配,并且在用户代理标头中包含字符串Godzilla
:inIpRange(origin.ip, '1.2.3.4/32') && has(request.headers['user-agent']) && request.headers['user-agent'].contains('Godzilla')
以下表达式与具有特定值的 Cookie 的请求匹配:
has(request.headers['cookie']) && request.headers['cookie'].contains('cookie_name=cookie_value')
以下表达式与来自地区
AU
的请求匹配:origin.region_code == 'AU'
以下表达式与来自区域
AU
且不在指定 IP 范围内的请求匹配:origin.region_code == "AU" && !inIpRange(origin.ip, '1.2.3.0/24')
如果 URI 与正则表达式匹配,则以下表达式与具有特定文件的编号变量路径的请求匹配:
request.path.matches('/path/[0-9]+/target_file.html')
如果
user-id
标头的 Base64 解码值包含特定值,则以下表达式与请求匹配:has(request.headers['user-id']) && request.headers['user-id'].base64Decode().contains('myValue')
以下表达式使用设置为与 SQLi 攻击匹配的预配置表达式:
evaluatePreconfiguredExpr('sqli-stable')
使用 JSON 解析
您可以为每个安全政策启用或停用 POST 请求的 JSON 正文内容解析。当 Content-Type
标头设置为 application/json
时,请使用 Google Cloud CLI 中的 --json-parsing
标志。
默认情况下,此选项处于停用状态。该标志的语法如下:
--json-parsing=[STANDARD | DISABLED]
此标志仅适用于 gcloud compute security-policies update
。除非在文件中创建安全政策,然后导入该文件,否则您无法使用此选项创建新的安全政策。如需了解详情,请参阅导入安全政策。
如需详细了解 JSON 解析,请参阅 JSON 解析和详细日志记录。
使用详细日志记录
您可以配置 Google Cloud Armor 日志记录级别,以使用 gcloud CLI 中的 --log-level
标志为每项安全政策启用更详细的日志记录。
默认情况下,此选项处于停用状态。该标志的语法如下:
--log-level=[NORMAL | VERBOSE]
此标志仅适用于 gcloud compute security-policies update
。除非在文件中创建安全政策,然后导入该文件,否则您无法使用此选项创建新的安全政策。如需了解详情,请参阅导入安全政策。
如需详细了解详细日志记录,请参阅 JSON 解析和详细日志记录。
配置速率限制规则
本部分介绍了如何配置 Google Cloud Armor 规则,以通过配置节流操作或基于速率的黑名单操作,按客户端强制执行速率限制。
基于速率的节流规则
基于速率的节流规则在 gcloud CLI 中采用以下格式:
gcloud compute security-policies rules create PRIORITY --security-policy=SECURITY_POLICY (--expression=EXPRESSION | --src-ip-ranges=SRC_IP_RANGE) --action "throttle" --rate-limit-threshold-count=RATE_LIMIT_THRESHOLD_COUNT --rate-limit-threshold-interval-sec=RATE_LIMIT_THRESHOLD_INTERVAL_SEC --conform-action=[allow] --exceed-action=[deny-403|deny-404|deny-429|deny-502|redirect] --exceed-redirect-type=[google-recaptcha|external-302] --exceed-redirect-target=REDIRECT_URL --enforce-on-key=[IP | ALL | HTTP-HEADER | XFF-IP | HTTP-COOKIE] --enforce-on-key-name=[HTTP_HEADER_NAME|HTTP_COOKIE_NAME]
例如,以下 gcloud
命令会创建一个优先级为 105
的 throttle
规则,对于 1.2.3.0/24
中的每个 IP 地址,速率限制为每 60 秒 100 次请求。超出此节流限制的请求会返回 429
错误代码。
gcloud compute security-policies rules create 105 \ --security-policy sec-policy \ --src-ip-ranges="1.2.3.0/24" \ --action=throttle \ --rate-limit-threshold-count=100 \ --rate-limit-threshold-interval-sec=60 \ --conform-action=allow \ --exceed-action=deny-429 \ --enforce-on-key=IP
例如,以下 gcloud
命令会创建一个优先级为 110
的 throttle
规则,对于来自 1.2.3.0/24
的 IP 地址的所有请求中的 HTTP 标头 User-Agent
的每个唯一值,速率限制为每 60 秒 10 次请求。超出此节流限制的请求会返回 429
错误代码。
gcloud compute security-policies rules create 110 \ --security-policy sec-policy \ --src-ip-ranges="1.2.3.0/24" \ --action=throttle \ --rate-limit-threshold-count=10 \ --rate-limit-threshold-interval-sec=60 \ --conform-action=allow \ --exceed-action=deny-429 \ --enforce-on-key=HTTP-HEADER \ --enforce-on-key-name='User-Agent'
您也可以为具有有效 reCAPTCHA 豁免 Cookie 的用户发出基于速率的禁止措施。例如,以下 gcloud
命令会在包含有效的 reCAPTCHA 豁免 Cookie 的所有请求中为每个唯一的 reCAPTCHA 豁免 Cookie 创建优先级为 115
的 throttle
规则,速率限制为每 5 分钟 20 个请求。超出节流限制的请求会被重定向以进行 reCAPTCHA Enterprise 评估。如需详细了解豁免 Cookie 和 reCAPTCHA Enterprise 评估,请参阅机器人管理概览。
gcloud compute security-policies rules create 115 \ --security-policy sec-policy \ --expression="token.recaptcha_exemption.valid" \ --action=throttle \ --rate-limit-threshold-count=20 \ --rate-limit-threshold-interval-sec=300 \ --conform-action=allow \ --exceed-action=redirect \ --exceed-redirect-type=google-recaptcha \ --enforce-on-key=HTTP-COOKIE \ --enforce-on-key-name="recaptcha-ca-e"
基于速率的黑名单规则
基于速率的黑名单规则在 gcloud CLI 中采用以下格式:
gcloud compute security-policies rules create PRIORITY \ --security-policy=SECURITY_POLICY \ (--expression=EXPRESSION | --src-ip-ranges=SRC_IP_RANGE) \ --action "rate-based-ban" \ --rate-limit-threshold-count=RATE_LIMIT_THRESHOLD_COUNT \ --rate-limit-threshold-interval-sec=RATE_LIMIT_THRESHOLD_INTERVAL_SEC \ --ban-duration-sec=BAN_DURATION_SEC \ --ban-threshold-count=BAN_THRESHOLD_COUNT \ --ban-threshold-interval-sec=BAN_THRESHOLD_INTERVAL_SEC \ --conform-action=[allow] \ --exceed-action=[deny-403|deny-404|deny-429|deny-502|redirect] --exceed-redirect-type=[google-recaptcha|external-302] --exceed-redirect-target=REDIRECT_URL --enforce-on-key=[IP | ALL | HTTP-HEADER | XFF-IP | HTTP-COOKIE] --enforce-on-key-name=[HTTP_HEADER_NAME|HTTP_COOKIE_NAME]
例如,以下 gcloud
命令会为其请求与值为 tuna
的标头 fish
匹配的每个 IP 地址创建一个优先级为 100 的基于速率的黑名单规则。如果这些请求的速率超出每 120 秒 50 个请求的限制,则此规则会将其屏蔽 300 秒。被屏蔽的请求会返回错误代码 404
。
gcloud compute security-policies rules create 100 \ --security-policy=sec-policy \ --expression="request.headers['fish'] == 'tuna'" \ --action=rate-based-ban \ --rate-limit-threshold-count=50 \ --rate-limit-threshold-interval-sec=120 \ --ban-duration-sec=300 \ --conform-action=allow \ --exceed-action=deny-404 \ --enforce-on-key=IP
例如,以下 gcloud
命令会创建一个优先级为 101 的基于速率的黑名单规则,以将区域代码与 US
匹配的所有请求限制为每 60 秒 10 次请求。如果来自 US
区域的请求的速率超出每 600 秒 1000 次请求的限制,则此规则还会屏蔽这些请求 300 秒。被屏蔽的请求会返回错误代码 403
。
gcloud compute security-policies rules create 101 \ --security-policy sec-policy \ --expression "origin.region_code == 'US'" \ --action rate-based-ban \ --rate-limit-threshold-count 10 \ --rate-limit-threshold-interval-sec 60 \ --ban-duration-sec 300 \ --ban-threshold-count 1000 \ --ban-threshold-interval-sec 600 \ --conform-action allow \ --exceed-action deny-403 \ --enforce-on-key ALL
例如,以下 gcloud
命令会创建一个优先级为 102 的基于速率的屏蔽规则,以将来自任何来源 IP 地址范围的所有请求限制为每 60 秒 20 次请求。如果来自任何来源 IP 地址范围的请求的速率超过每 400 秒 500 次请求的限制,则此规则还会屏蔽这些请求 600 秒。被屏蔽的请求会返回错误代码 429
。
gcloud compute security-policies rules create 102 \ --security-policy sec-policy \ --src-ip-ranges="*" \ --action rate-based-ban \ --rate-limit-threshold-count 20 \ --rate-limit-threshold-interval-sec 60 \ --ban-duration-sec 600 \ --ban-threshold-count 500 \ --ban-threshold-interval-sec 400 \ --conform-action allow \ --exceed-action deny-429 \ --enforce-on-key ALL
使用控制台和 gcloud CLI 创建安全政策
本部分中的说明假设您要配置应用于现有外部 HTTP(S) 负载均衡器和后端服务的安全政策。如需查看如何完成字段的示例,请参阅创建示例。
控制台
创建 Google Cloud Armor 安全政策和规则并将安全政策附加到后端服务:
在 Google Cloud Console 中,前往网络安全页面。
在政策页面上,点击创建政策。
在名称字段中,输入政策的名称。
可选:输入政策说明。
对于政策类型,选择后端安全政策或边缘安全政策。
对于默认规则操作,为允许访问的默认规则,选择允许;为禁止访问 IP 地址或 IP 地址范围的默认规则,选择拒绝。
默认规则是最低优先级规则,仅在没有其他规则适用时才生效。
如果您要配置拒绝规则,请选择拒绝状态消息。如果没有访问权限的用户尝试获得访问权限,这是 Google Cloud Armor 显示的错误消息。
无论您要配置何种规则类型,都请点击下一步。
添加更多规则:
- 点击添加规则。
- 可选:输入规则的说明。
选择模式:
- 基本模式:根据 IP 地址或 IP 范围允许或拒绝流量。
- 高级模式:根据规则表达式允许或拒绝流量。
在匹配字段中,指定适用规则的条件。
- 基本模式:在规则中输入要匹配的 IP 地址或 IP 范围。
- 高级模式:输入要根据传入请求进行评估的表达式或子表达式。如需了解如何编写表达式,请参阅自定义规则语言参考。
对于操作,请选择允许或拒绝,以便在规则匹配时允许或拒绝流量。
如要启用预览模式,请选中启用复选框。在预览模式下,您可以查看规则的行为,但是不启用规则。
输入该规则的优先级。这可以是 0 到 2,147,483,646(含)之间的任何正整数。如需详细了解评估顺序,请参阅规则评估顺序。
点击完成。
如要添加更多规则,请点击添加规则,然后重复上述步骤。否则,请点击下一步。
将政策应用于目标:
- 点击添加目标。
- 在目标列表中,选择一个目标。
- 如需添加更多目标,请点击添加目标。
- 点击完成。
- 点击创建政策。
gcloud
如要创建新的 Google Cloud Armor 安全政策,请使用
gcloud compute security-policies create
命令。将NAME
和DESCRIPTION
替换为安全政策的名称和说明:在type
字段中,使用CLOUD_ARMOR
创建后端安全政策,或使用CLOUD_ARMOR_EDGE
创建边缘安全政策。type
标志不是必需标志,如果未指定类型,则默认创建后端安全政策:gcloud compute security-policies create NAME \ [--type=CLOUD_ARMOR|CLOUD_ARMOR_EDGE] \ [--file-format=FILE_FORMAT | --description=DESCRIPTION] \ [--file-name=FILE_NAME]
以下命令会更新您之前创建的政策,启用 JSON 解析,并将日志级别更改为
VERBOSE
:gcloud compute security-policies update my-policy \ --json-parsing=STANDARD --log-level=VERBOSE
要向安全政策添加规则,请使用
gcloud compute security-policies rules create PRIORITY
命令。将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]
例如,以下命令会添加一个规则,以阻止来自 IP 地址范围 192.0.2.0/24 和 198.51.100.0/24 的流量。该规则的优先级为 1000,它是名为
my-policy
的政策中的规则。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
的流量,并且在用户代理标头中包含字符串Godzilla
:gcloud beta 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('Godzilla')" \ --action allow \ --description "Block User-Agent 'Godzilla'"
如果请求的 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"
以下命令会添加一条规则来阻止不在指定 IP 范围内的
AU
区域的请求: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('/bad_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 "evaluatePreconfiguredExpr('sqli-stable')" \ --action "deny-403"
以下命令将添加一条规则,该规则使用预配置的表达式以允许从已命名的 IP 地址列表中的所有 IP 地址访问:
gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --expression "evaluatePreconfiguredExpr('sourceiplist-fastly')" \ --action "allow"
配置机器人管理规则
本部分介绍如何为机器人管理配置 Google Cloud Armor 安全政策规则。如需了解详情,请参阅机器人管理概览。
使用 reCAPTCHA Enterprise 手动验证来区分真人和自动化客户端
如需将您自己的 reCAPTCHA WAF 网站密钥与安全政策关联或取消关联,请使用以下命令,并替换安全政策名称 (SECURITY_POLICY) 和 reCAPTCHA WAF 验证网站密钥 (SITE_KEY)。
gcloud compute security-policies update SECURITY_POLICY \ --recaptcha-redirect-site-key SITE_KEY
以下示例将 reCAPTCHA WAF 网站密钥与安全政策相关联。关联的网站密钥适用于在给定安全政策下使用手动验证功能的所有规则。
gcloud compute security-policies update my-policy \ --recaptcha-redirect-site-key "SITE_KEY"
您还可以取消 reCAPTCHA WAF 网站密钥与安全政策的关联,如下所示:
gcloud compute security-policies update my-policy \ --recaptcha-redirect-site-key ""
如需为 reCAPTCHA Enterprise 评估创建用于内部重定向流量的规则,请在 gcloud
中使用以下格式:
gcloud compute security-policies rules create PRIORITY \ --security-policy SECURITY_POLICY \ (--expression EXPRESSION | --src-ip-ranges SRC_IP_RANGE) \ --action redirect \ --redirect-type google-recaptcha
以下示例将创建一条规则,该规则会重定向尝试到达 /login.html
的流量,以进行 reCAPTCHA Enterprise 手动验证。
gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --expression "request.path.matches(\"/login.html\")" \ --action redirect \ --redirect-type google-recaptcha
强制执行 reCAPTCHA Enterprise 流畅评估
在继续操作之前,请参阅机器人管理概览,了解使用 reCAPTCHA Enterprise 操作令牌或会话令牌的前提条件。
您可以使用 token.recaptcha_action.ATTRIBUTE
(将 ATTRIBUTE 替换为 Google Cloud Armor 规则语言中的有效令牌特性)从 reCAPTCHA Enterprise 操作令牌中提取特性。同样,您可以将 token.recaptcha_session.ATTRIBUTE
用于 reCAPTCHA Enterprise 会话令牌。如需详细了解可用的 reCAPTCHA Enterprise 令牌特性的语法,请参阅规则语言参考文档。
以下示例将创建一条规则,该规则允许目标为 /login.html
并且 reCAPTCHA Enterprise 操作令牌的分数不低于 0.8
的流量。
gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --expression "request.path.matches(\"/login.html\") && token.recaptcha_action.score >= 0.8" \ --action allow
重定向(302 响应)
要创建规则以将流量重定向到用户配置的网址,请在 gcloud
中使用以下格式。
gcloud compute security-policies rules create PRIORITY \ --security-policy SECURITY_POLICY \ (--expression EXPRESSION | --src-ip-ranges SRC_IP_RANGE) \ --action redirect \ --redirect-type external-302 \ --redirect-target REDIRECT_URL
以下示例会创建一条规则,用于将来自 10.10.10.0/24 的流量重定向到 https://www.example.com。
gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --src-ip-ranges "10.10.10.0/24" \ --action redirect \ --redirect-type external-302 \ --redirect-target "https://www.example.com"
修饰请求
如需创建允许流量但在将流量通过代理发送到受保护后端之前添加自定义标头和用户定义的静态值的规则,请在 gcloud
中使用以下格式:
gcloud compute security-policies rules create PRIORITY \ --security-policy SECURITY_POLICY \ (--expression EXPRESSION | --src-ip-ranges SRC_IP_RANGE) \ --action allow \ --request-headers-to-add HEADER_1=VALUE_1,HEADER_2=VALUE_2,...
以下示例将创建一条规则,该规则允许目标为 /login.html
并且 reCAPTCHA Enterprise 操作令牌分数低于 0.2
的流量,但会为其添加自定义标头。
gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --expression "request.path.matches("/login.html") && token.recaptcha_action.score < 0.2" \ --action allow \ --request-headers-to-add "reCAPTCHA-Warning=high"
使用自动调节式保护
根据安全政策应用自动调节式保护。
控制台
如需为安全政策激活自动调节式保护,请执行以下操作:
在 Google Cloud Console 中,前往网络安全页面。
在政策页面上,点击安全政策的名称。
点击修改。
在自动调节式保护下,选择启用。
点击更新。
如需为安全政策停用自动调节式保护,请执行以下操作:
在 Google Cloud Console 中,前往网络安全页面。
在政策页面上,点击安全政策的名称。
点击修改。
在自动调节式保护下,取消选择启用。
点击更新。
gcloud
如需为安全政策激活自动调节式保护,请执行以下操作:
gcloud compute security-policies update MY-SECURITY-POLICY \ --enable-layer7-ddos-defense
如需为安全政策停用自动调节式保护,请执行以下操作:
gcloud compute security-policies update MY-SECURITY-POLICY \ --no-enable-layer7-ddos-defense
列出可用的预配置规则
列出预配置规则以查看预定义的应用保护规则和签名,例如 Google Cloud Armor 提供的 ModSecurity 核心规则集。这些预配置规则包含多个内置签名,Google Cloud Armor 使用这些签名对传入请求进行评估。您可以使用自定义规则语言参考将这些预配置的规则添加到新规则或现有规则中。
如需了解详情,请参阅预配置的规则。
gcloud
运行
gcloud compute security-policies list-preconfigured-expression-sets
命令:gcloud compute security-policies list-preconfigured-expression-sets
以下示例显示了命令输出的形式:
EXPRESSION_SET expression-set-1 RULE_ID expression-set-1-id-1 expression-set-1-id-2 expression-set-2 alias-1 RULE_ID expression-set-2-id-1 expression-set-2-id-2
以下示例包含命令的实际输出示例。请注意,实际输出将包含调整 Google Cloud Armor WAF 规则中列出的所有规则。
gcloud compute security-policies list-preconfigured-expression-sets
EXPRESSION_SET sqli-canary RULE_ID owasp-crs-v030001-id942110-sqli owasp-crs-v030001-id942120-sqli … xss-canary RULE_ID owasp-crs-v030001-id941110-xss owasp-crs-v030001-id941120-xss … sourceiplist-fastly sourceiplist-cloudflare sourceiplist-imperva
列出安全政策
按照这些说明列出当前项目或您指定的项目中的所有 Google Cloud Armor 安全政策。
控制台
在 Google Cloud Console 中,前往网络安全页面。
如需查看特定政策,请在安全政策页面的政策列表中,点击其名称。
gcloud
gcloud compute security-policies list
例如:
gcloud compute security-policies list
NAME my-policy
如需了解详情,请参阅 gcloud compute security-policies list
。
更新安全政策
使用以下说明更新 Google Cloud Armor 安全政策。例如,您可以修改政策的说明,修改默认规则的行为,更改目标后端服务,或添加新规则。
控制台
gcloud
如要更新安全政策,请使用以下 Google Cloud CLI 说明:
- 如要更新安全政策中的规则,请参阅更新安全政策中的单个规则。
- 如需向安全政策添加规则,请参阅向安全政策添加规则。
- 如要从安全政策中删除规则,请参阅从安全政策中删除规则。
- 如需更新单个更新中的多个规则(原子更新),请参阅以原子方式更新安全政策中的多个规则。
- 如需更新安全政策中的非规则字段(例如说明字段),请参阅导出安全政策和导入安全政策。
导出安全政策
您可以使用 Google Cloud CLI 将 Google Cloud Armor 安全政策导出为 YAML 或 JSON 文件。导出政策后,您能够检索其副本,并在源代码控制系统中执行修改或保存操作。
gcloud
在以下命令中,
NAME
是安全政策的名称。有效文件格式为 YAML 和 JSON。如果您不提供文件格式,则 Google Cloud Armor 会使用默认的 YAML。gcloud compute security-policies export NAME \ --file-name FILE_NAME \ --file-format FILE_FORMAT
以下示例将
my-policy
安全政策以 YAML 格式导出到my-file
文件:gcloud compute security-policies export my-policy \ --file-name my-file \ --file-format yaml
以下示例显示了导出的安全政策:
description: my description fingerprint: PWfLGDWQDLY= id: '123' name: my-policy rules: - action: allow description: default rule match: config: srcIpRanges: - '*' versionedExpr: SRC_IPS_V1 preview: false priority: 2147483647 selfLink: https://www.googleapis.com/compute/v1/projects/my-project/global/securityPolicies/my-policy
您可以使用任何文本编辑器修改导出的文件,然后使用
import
命令将其导入回 Google Cloud。
导入安全政策
您可以使用 Google Cloud CLI 从 YAML 或 JSON 文件导入 Google Cloud Armor 安全政策。您不能使用 import
命令更新现有政策的规则。相反,您必须使用更新安全政策中的单个规则过程逐个更新规则,或者使用以原子方式更新安全政策中的多个规则过程一次性更新所有规则。
gcloud
如需导入安全政策,请使用 gcloud compute security-policies import NAME
命令。将 NAME
替换为要导入的安全政策的名称。如果您未提供文件格式,将根据文件结构假定正确的格式。如果结构无效,您会看到错误。
gcloud compute security-policies import NAME \ --file-name FILE_NAME \ [--file-format FILE_FORMAT]
例如,以下命令通过导入文件 my-file
来更新政策 my-policy
。
gcloud compute security-policies import my-policy \ --file-name my-file \ --file-format json
如果导入时该政策的指纹已过期,则 Google Cloud Armor 会显示错误。这意味着该政策在上次导出后已被修改。如需解决此问题,请对该政策使用 describe
命令以获取最新的指纹。合并描述的政策和您的政策之间的任何差异,然后用最新的指纹替换过时的指纹。
删除安全政策
使用以下说明删除 Google Cloud Armor 安全政策。必须先从政策中移除所有后端服务,然后才能删除政策。
控制台
在 Google Cloud Console 中,前往网络安全页面。
在安全政策页面上,选中要删除的安全政策名称旁边的复选框。
点击页面右上角的
删除。
gcloud
使用 gcloud compute security-policies delete NAME
。
将 NAME
替换为安全政策的名称:
gcloud compute security-policies delete NAME
将安全政策附加到后端服务
使用以下说明将 Google Cloud Armor 安全政策附加到后端服务。一个安全政策可以附加到多个后端服务,但是一个后端服务只能附加每种类型中的一个安全政策。
控制台
在 Google Cloud Console 中,前往网络安全页面。
在安全政策页面上,点击安全政策的名称。此时将显示政策详情页面。
在页面中间,点击目标标签页。
点击将政策应用于新目标。
点击添加目标。
在目标列表中,选择一个目标,然后点击添加。
gcloud
将后端安全政策附加到后端服务时,请使用 gcloud compute backend-services
命令和 --security-policy
标志:
gcloud compute backend-services update my-backend \ --security-policy my-policy
将边缘安全政策附加到后端服务时,请使用 gcloud compute backend-services
命令和 --edge-security-policy
标志:
gcloud compute backend-services update my-backend \ --edge-security-policy my-policy
从后端服务中移除安全政策
使用以下说明从后端服务中移除 Google Cloud Armor 后端安全政策或边缘安全政策。
控制台
在 Google Cloud Console 中,前往网络安全页面。
在安全政策页面上,点击安全政策的名称。此时将显示政策详情页面。
在页面中间,点击目标标签页。
选择要从中移除政策的目标后端服务。
点击移除。
在移除目标消息中,点击移除。
gcloud
移除后端安全政策时,请使用 gcloud compute
backend-services
命令和 --security-policy
标志:
gcloud compute backend-services update my-backend \ --security-policy ""
移除边缘安全政策时,请使用 gcloud compute
backend-services
命令和 --edge-security-policy
标志:
gcloud compute backend-services update my-backend \ --edge-security-policy ""
将安全政策附加到后端存储桶
使用以下说明将 Google Cloud Armor 边缘安全政策附加到后端存储桶。边缘安全政策可以附加到多个后端存储桶。
控制台
在 Google Cloud Console 中,前往网络安全页面。
在安全政策页面上,点击安全政策的名称。此时将显示政策详情页面。
在页面中间,点击目标标签页。
点击将政策应用于新目标。
点击添加目标。
在目标列表中,选择一个目标,然后点击添加。
gcloud
将边缘安全政策附加到后端存储桶时,请使用 cloud compute backend-buckets
命令和 --edge-security-policy
标志:
gcloud compute backend-services update my-bucket \ --edge-security-policy my-edge-policy
从后端存储桶中移除安全政策
使用以下说明从后端存储桶移除 Google Cloud Armor 边缘安全政策。
控制台
在 Google Cloud Console 中,前往网络安全页面。
在安全政策页面上,点击安全政策的名称。此时将显示政策详情页面。
在页面中间,点击目标标签页。
选择要从中移除政策的目标后端服务。
点击移除。
在移除目标消息中,点击移除。
gcloud
从后端存储桶移除边缘安全政策时,请使用 cloud compute backend-buckets
命令和 --edge-security-policy
标志:
gcloud compute backend-services update my-bucket \ --edge-security-policy ""
向安全政策添加规则
使用以下说明将规则添加到 Google Cloud Armor 安全政策。
控制台
在 Google Cloud Console 中,前往网络安全页面。
在安全政策页面上,点击安全政策的名称。此时将显示政策详情页面。
在页面中间,点击规则标签页。
点击添加规则。
可选:输入规则的说明。
选择模式:
- 基本模式:根据 IP 地址或 IP 范围允许或拒绝流量。
- 高级模式:根据规则表达式允许或拒绝流量。
在匹配字段中,指定适用规则的条件。
- 基本模式:输入一 (1) 到五 (5) 个要在规则中匹配的 IP 地址范围。
高级模式:输入要根据传入请求进行评估的表达式或子表达式。如需了解如何编写表达式和如何读取以下示例,请参阅自定义规则语言参考。
以下表达式与来自 IP 地址
1.2.3.4
的请求匹配,并且在用户代理标头中包含字符串Godzilla
:inIpRange(origin.ip, '1.2.3.4/32') && has(request.headers['user-agent']) && request.headers['user-agent'].contains('Godzilla')
以下表达式与具有特定值的 Cookie 的请求匹配:
has(request.headers['cookie']) && request.headers['cookie'].contains('cookie_name=cookie_value')
以下表达式与来自地区
AU
的请求匹配:origin.region_code == 'AU'
以下表达式与来自区域
AU
且不在指定 IP 范围内的请求匹配:origin.region_code == "AU" && !inIpRange(origin.ip, '1.2.3.0/24')
如果 URI 与正则表达式匹配,则以下表达式与请求匹配:
request.path.matches('/bad_path/)')
如果
user-id
标头的 Base64 解码值包含特定值,则以下表达式与请求匹配:has(request.headers['user-id']) && request.headers['user-id'].base64Decode().contains('myValue')
以下表达式使用设置为与 SQLi 攻击匹配的预配置表达式:
evaluatePreconfiguredExpr('sqli-stable')
对于操作,请选择允许或拒绝。
如果您要配置拒绝规则,请选择拒绝状态消息。
若要为规则启用预览模式,请选中启用复选框。
在优先级字段中,输入一个正整数。
点击添加。
gcloud
使用 gcloud compute security-policies rules create PRIORITY
命令。将 PRIORITY
替换为政策中规则的优先级:
gcloud compute security-policies rules create PRIORITY \ --security-policy POLICY_NAME \ --description DESCRIPTION \ --src-ip-ranges IP_RANGES | --expression EXPRESSION \ --action=[ allow | deny-403 | deny-404 | deny-502 ] \ --preview
例如,以下命令会添加一个规则,以阻止来自 IP 地址范围 192.0.2.0/24 和 198.51.100.0/24 的流量。该规则的优先级为 1000,且是名为 my-policy
政策中的规则:
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"
使用 --expression
标志在自定义规则语言参考中指定条件。以下命令将添加一条规则,以允许来自 IP 地址 1.2.3.4
的流量,并且在用户代理标头中包含字符串 Godzilla
:
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('Godzilla')" \ --action allow \ --description "Block User-Agent 'Godzilla'"
如果请求的 Cookie 包含特定值,以下命令将添加一条规则以阻止请求:
gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --expression "has(request.headers['cookie']) && request.headers['cookie'].contains('80=BLAH')" \ --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"
以下命令会添加一条规则来阻止不在指定 IP 范围内的 AU
区域的请求:
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('/bad_path/)')" \ --action deny-502 \ --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 "evaluatePreconfiguredExpr('sqli-stable')" \ --action deny-403
列出安全政策中的规则
使用以下说明列出 Google Cloud Armor 安全政策中的规则。
控制台
在 Google Cloud Console 中,前往网络安全页面。
在安全政策页面上,点击安全政策的名称。随即会显示政策详情页面,并且政策规则在页面中间的规则标签页中列出。
gcloud
使用以下 gcloud
命令列出单个安全政策中的所有规则以及该政策的说明:
gcloud compute security-policies describe NAME \
使用以下 gcloud
命令描述指定安全政策中指定的优先级的规则:
gcloud compute security-policies rules describe PRIORITY \ --security-policy POLICY_NAME
例如,以下命令描述安全政策 my-policy
中优先级为 1000 的规则:
gcloud compute security-policies rules describe 1000 \ --security-policy my-policy
输出:
action: deny(403) description: block traffic from 192.0.2.0/24 and 198.51.100.0/24 kind: compute#securityPolicyRule match: srcIpRanges: - '192.0.2.0/24' - '198.51.100.0/24' preview: false priority: 1000
更新安全政策中的单个规则
使用以下说明更新 Google Cloud Armor 安全政策中的单条规则。如需原子更新多个规则,请参阅以原子方式更新安全政策中的多个规则。
控制台
在 Google Cloud Console 中,前往网络安全页面。
在安全政策页面上,点击安全政策的名称。此时将显示政策详情页面。
在页面中间,点击规则标签页。
在要更新的规则旁边,点击
修改。随即会显示修改规则页面。进行所需的更改,然后点击更新。
gcloud
使用此命令可在指定的安全策略中更新具有指定优先级的规则。使用此命令一次只能更新一个安全策略:
gcloud compute security-policies rules update PRIORITY [ \ --security-policy POLICY_NAME \ --description DESCRIPTION \ --src-ip-ranges IP_RANGES | --expression EXPRESSION \ --action=[ allow | deny-403 | deny-404 | deny-502 ] \ --preview ]
例如,以下命令将更新优先级为 1111 的规则,以允许来自 IP 地址范围 192.0.2.0/24 的流量:
gcloud compute security-policies rules update 1111 \ --security-policy my-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 update
。
如需更新规则的优先级,必须使用 REST API。如需了解详情,请参阅 securityPolicies.patchRule
。
以原子方式更新安全政策中的多条规则
原子更新会将更改应用于单个更新中的多条规则。如果您要逐条更新规则,您可能会看到一些意外行为,因为新旧规则可能会在短时间内协同工作。
要以原子方式更新多个规则,请将当前安全政策导出到 JSON 或 YAML 文件中,然后对其进行修改。使用修改后的文件创建新的安全政策,然后切换相关后端服务的安全政策。
gcloud
导出政策以进行更新,如以下示例所示:
gcloud compute security-policies export my-policy \ --file-name my-file \ --file-format yaml
导出后的政策将类似于以下示例:
description: my description fingerprint: PWfLGDWQDLY= id: '123' name: my-policy rules: - action: deny(404) description: my-rule-1 match: expr: expression: evaluatePreconfiguredExpr('xss-stable') versionedExpr: SRC_IPS_V1 preview: false priority: 1 - action: allow description: my-rule-2 match: config: srcIpRanges: - '1.2.3.4' versionedExpr: SRC_IPS_V1 preview: false priority: 2 - action: deny description: default rule kind: compute#securityPolicyRule match: config: srcIpRanges: - '*' versionedExpr: SRC_IPS_V1 preview: false priority: 2147483647 selfLink: https://www.googleapis.com/compute/v1/projects/my-project/global/securityPolicies/my-policy
使用任何文本编辑器修改政策。例如,您可以修改现有规则的优先级并添加新规则:
description: my description fingerprint: PWfLGDWQDLY= id: '123' name: my-policy rules: - action: deny(404) description: my-rule-1 match: expr: expression: evaluatePreconfiguredExpr('xss-stable') versionedExpr: SRC_IPS_V1 preview: false priority: 1 - action: allow description: my-new-rule match: config: srcIpRanges: - '1.2.3.1' versionedExpr: SRC_IPS_V1 preview: false priority: 10 - action: allow description: my-rule-2 match: config: srcIpRanges: - '1.2.3.4' versionedExpr: SRC_IPS_V1 preview: false priority: 11 - action: deny description: default rule kind: compute#securityPolicyRule match: config: srcIpRanges: - '*' versionedExpr: SRC_IPS_V1 preview: false priority: 2147483647 selfLink: https://www.googleapis.com/compute/v1/projects/my-project/global/securityPolicies/my-policy
创建一个新的 Google Cloud Armor 安全政策并指定修改后的文件名和格式,如以下示例所示:
gcloud compute security-policies create new-policy \ --file-name modified-policy \ --file-format yaml
从相关后端服务中移除旧安全政策,如以下示例所示:
gcloud compute backend-services update my-backend \ --security-policy ""
将新安全政策添加到后端服务,如以下示例所示:
gcloud compute backend-services update my-backend \ --security-policy new-policy
如果未使用旧政策,请将其删除:
gcloud compute security-policies delete my-policy
从安全政策中删除规则
使用这些说明删除 Google Cloud Armor 安全政策中的规则。
控制台
在 Google Cloud Console 中,前往网络安全页面。
在安全政策页面上,点击安全政策的名称。此时将显示政策详情页面。
在该页面中间的规则标签页上,选中要删除的规则旁边的复选框。
点击
删除。
gcloud
使用此命令从指定安全政策中移除具有指定优先级的规则。您一次只能修改一个安全政策,但可以一次删除多个规则:
gcloud compute security-policies rules delete PRIORITY [...] [ --security-policy POLICY_NAME \ ]
例如:
gcloud compute security-policies rules delete 1000 \ --security-policy my-policy