

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 根據 IP 地址或地理位置限制存取 AWS WAF
<a name="aws-waf-restrict-access-geolocation"></a>

*Louis Hourcade，Amazon Web Services*

## 總結
<a name="aws-waf-restrict-access-geolocation-summary"></a>

[AWS WAF](https://docs.aws.amazon.com/waf/latest/developerguide/waf-chapter.html) 是一種 Web 應用程式防火牆，可協助保護 Web 應用程式和 APIs，防範可能影響可用性、危及安全性或消耗過多資源的常見 Web 入侵和機器人。中的 [Web 存取控制清單 (Web ACLs)](https://docs.aws.amazon.com/waf/latest/developerguide/web-acl.html) AWS WAF 可讓您控制流量到達應用程式的方式。在 Web ACL 中，您可以新增規則或規則群組，這些規則或規則群組旨在允許合法流量、控制機器人流量，以及封鎖常見的攻擊模式。如需詳細資訊，請參閱 [如何 AWS WAF 運作](https://docs.aws.amazon.com/waf/latest/developerguide/how-aws-waf-works.html)。

您可以將下列類型的規則與 AWS WAF Web ACLs建立關聯：
+ [受管規則群組](https://docs.aws.amazon.com/waf/latest/developerguide/waf-managed-rule-groups.html) – AWS 受管規則團隊和 AWS Marketplace 賣方提供預先設定的規則集。有些受管規則群組旨在協助保護特定類型的 Web 應用程式。其他則針對已知威脅或常見漏洞提供廣泛的保護。
+ [自訂規則](https://docs.aws.amazon.com/waf/latest/developerguide/waf-rules.html)和[自訂規則群組](https://docs.aws.amazon.com/waf/latest/developerguide/waf-user-created-rule-groups.html) – 您也可以建立規則和規則群組，以自訂對 Web 應用程式和 APIs存取。例如，您可以根據特定 IP 地址清單或國家/地區清單來限制流量。

透過使用此模式和相關聯的程式碼儲存庫，您可以使用 [AWS Cloud Development Kit (AWS CDK)](https://docs.aws.amazon.com/cdk/v2/guide/home.html) 部署具有自訂規則的 AWS WAF Web ACL。 ACLs 這些規則會根據最終使用者的 IP 地址或地理位置，限制對 Web 應用程式資源的存取。您也可以選擇性地連接數個受管規則群組。

## 先決條件和限制
<a name="aws-waf-restrict-access-geolocation-prereqs"></a>

**先決條件**
+ 作用中 AWS 帳戶
+ 部署 AWS WAF 資源[的許可](https://docs.aws.amazon.com/waf/latest/developerguide/security-iam.html) 
+ AWS CDK，在您的帳戶中[安裝和設定](https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html) 
+ Git，[已安裝](https://github.com/git-guides/install-git)

**限制**
+ 您只能在 AWS WAF 可用的 AWS 區域 中使用此模式。如需區域可用性，請參閱[AWS 服務 依區域](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/)。

## 工具
<a name="aws-waf-restrict-access-geolocation-tools"></a>

**AWS 服務**
+ [AWS Cloud Development Kit (AWS CDK)](https://docs.aws.amazon.com/cdk/v2/guide/home.html) 是一種軟體開發架構，可協助您在程式碼中定義和佈建 AWS 雲端 基礎設施。
+ [AWS WAF](https://docs.aws.amazon.com/waf/latest/developerguide/what-is-aws-waf.html) 是一種 Web 應用程式防火牆，可協助您監控轉送至受保護 Web 應用程式資源的 HTTP 和 HTTPS 請求。

**程式碼儲存庫**

此模式的程式碼可在 GitHub [IP 和含儲存庫的地理位置限制 AWS WAF](https://github.com/aws-samples/ip-and-geolocation-restriction-with-waf-cdk)中使用。此程式碼會部署兩個 AWS WAF Web ACLs。第一個是區域性 Web ACL，適用於 [Amazon API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html) 資源。第二個是 [Amazon CloudFront](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Introduction.html) 資源的全域 Web ACL。兩個 Web ACLs都包含下列自訂規則：
+ `IPMatch` 會封鎖來自不允許 IP 地址的請求。
+ `GeoMatch` 會封鎖來自不允許國家/地區的請求。

在部署期間，您可以選擇將所有下列受管規則群組連接至 Web ACLs：
+ [核心規則集 (CRS)](https://docs.aws.amazon.com/waf/latest/developerguide/aws-managed-rule-groups-baseline.html#aws-managed-rule-groups-baseline-crs) – 此規則群組包含通常適用於 Web 應用程式的規則。它有助於防止各種漏洞遭到利用，包括 OWASP 出版物中所述的一些高風險和常見漏洞，例如 [OWASP 前 10 名](https://owasp.org/www-project-top-ten/)。
+ [管理員保護](https://docs.aws.amazon.com/waf/latest/developerguide/aws-managed-rule-groups-baseline.html#aws-managed-rule-groups-baseline-admin) – 此規則群組包含的規則可協助您封鎖對公開管理頁面的外部存取。
+ [已知錯誤輸入](https://docs.aws.amazon.com/waf/latest/developerguide/aws-managed-rule-groups-baseline.html#aws-managed-rule-groups-baseline-known-bad-inputs) – 此規則群組有助於封鎖已知無效且與漏洞利用或探索相關聯的請求模式。
+ [Amazon IP 評價清單](https://docs.aws.amazon.com/waf/latest/developerguide/aws-managed-rule-groups-ip-rep.html#aws-managed-rule-groups-ip-rep-amazon) – 此規則群組包含以 Amazon 內部威脅情報為基礎的規則。它可協助您封鎖通常與機器人或其他威脅相關聯的 IP 地址。
+ [Linux 作業系統受管規則群組](https://docs.aws.amazon.com/waf/latest/developerguide/aws-managed-rule-groups-use-case.html#aws-managed-rule-groups-use-case-linux-os) – 此規則群組可協助封鎖與利用 Linux 漏洞相關的請求模式，包括 Linux 特定的本機檔案包含 (LFI) 攻擊。
+ [SQL 資料庫受管規則群組](https://docs.aws.amazon.com/waf/latest/developerguide/aws-managed-rule-groups-use-case.html#aws-managed-rule-groups-use-case-sql-db) – 此規則群組有助於封鎖與利用 SQL 資料庫相關聯的請求模式，例如 SQL Injection 攻擊。

## 史詩
<a name="aws-waf-restrict-access-geolocation-epics"></a>

### 設定 AWS WAF Web ACLs
<a name="configure-the-waf-web-acls"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 複製儲存庫。 | 輸入下列命令，將 [IP 和地理位置限制與 AWS WAF](https://github.com/aws-samples/ip-and-geolocation-restriction-with-waf-cdk) 儲存庫複製到您的本機工作站：<pre>git clone https://github.com/aws-samples/ip-and-geolocation-restriction-with-waf-cdk.git</pre> | Git | 
| 設定規則。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/aws-waf-restrict-access-geolocation.html) | 一般 AWS、Python | 

### 引導和部署程式碼
<a name="bootstrap-and-deploy-the-code"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 引導您的 AWS 環境。 | 如果尚未完成，您需要[先引導](https://docs.aws.amazon.com/cdk/v2/guide/bootstrapping-env.html) AWS 環境，才能部署 AWS CDK 應用程式。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/aws-waf-restrict-access-geolocation.html) | 一般 AWS | 
| 部署 AWS CDK 應用程式。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/aws-waf-restrict-access-geolocation.html) | 一般 AWS | 

### 驗證部署
<a name="validate-the-deployment"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 確認 Web ACLs已成功部署。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/aws-waf-restrict-access-geolocation.html) | 一般 AWS | 
| （選用） 將 Web ACLs 與您的資源建立關聯。 | 將 AWS WAF Web ACLs 與您的 AWS 資源建立關聯，例如 Application Load Balancer、API Gateway 或 CloudFront 分佈。如需說明，請參閱[將 Web ACL 與 資源建立關聯或取消關聯](https://docs.aws.amazon.com/waf/latest/developerguide/web-acl-associating-aws-resource.html)AWS。[https://docs.aws.amazon.com/waf/latest/developerguide/web-acl-associating-aws-resource.html](https://docs.aws.amazon.com/waf/latest/developerguide/web-acl-associating-aws-resource.html)如需範例，請參閱 AWS CDK 文件中的[類別 CfnWebACLAssociation （建構）](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_wafv2.CfnWebACLAssociation.html)。 | 一般 AWS | 

### 清除資源
<a name="clean-up-resources"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 刪除堆疊。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/aws-waf-restrict-access-geolocation.html) | 一般 AWS | 

## 相關資源
<a name="aws-waf-restrict-access-geolocation-resources"></a>
+ [API 參考](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-construct-library.html) (AWS CDK 文件）
+ [aws-cdk-lib.aws\$1wafv2 模組](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_wafv2-readme.html) (AWS CDK 文件）
+ [使用 Web ACLs](https://docs.aws.amazon.com/waf/latest/developerguide/web-acl-working-with.html)(AWS WAF 文件）
+ [管理您自己的規則群組](https://docs.aws.amazon.com/waf/latest/developerguide/waf-user-created-rule-groups.html) (AWS WAF 文件）
+ [規則](https://docs.aws.amazon.com/waf/latest/developerguide/waf-rules.html) (AWS WAF 文件）