

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

# 自動稽核允許從公有 IP 地址存取 AWS 的安全群組
<a name="audit-security-groups-access-public-ip"></a>

*Eugene Shifer 和 Stephen DiCato，Amazon Web Services*

## 總結
<a name="audit-security-groups-access-public-ip-summary"></a>

作為安全最佳實務，將 AWS 資源的暴露降至最低至絕對必要的資源至關重要。例如，滿足一般公有需求的 Web 伺服器需要允許從網際網路傳入存取，但對其他工作負載的存取應僅限於特定網路，以減少不必要的暴露。Amazon Virtual Private Cloud (Amazon VPC) 中的[安全群組](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-security-groups.html)是一項有效的控制，可協助您限制資源存取。不過，評估安全群組可能是一項繁瑣的任務，尤其是在多帳戶架構中。 [AWS Config 規則](https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config.html)和[AWS Security Hub CSPM 控制項](https://docs.aws.amazon.com/securityhub/latest/userguide/controls-view-manage.html)可協助您識別允許從公有網際網路 (0.0.0.0/0) 存取特定網路通訊協定的安全群組，例如 Secure Shell (SSH)、HTTP、HTTPS 和 Windows 遠端桌面通訊協定 (RDP)。不過，如果服務在非標準連接埠上執行，或存取僅限於特定公有 IP 地址，則這些規則和控制項不適用。例如，當 Web 服務與 TCP 連接埠 8443 而非標準 TCP 連接埠 443 相關聯時，可能會發生這種情況。當開發人員能夠從其家用網路存取伺服器時，也可能發生這種情況，例如用於測試目的。

若要解決此問題，您可以使用此模式中提供的基礎設施做為程式碼 (IaC) 解決方案，來識別允許從任何非私有 ([RFC 1918](https://datatracker.ietf.org/doc/html/rfc1918) 不合規） IP 地址存取您 AWS 帳戶 或 AWS 組織中任何工作負載的安全群組。[AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) 範本會佈建自訂 AWS Config 規則、 [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html)函數和必要的許可。您可以將它部署為單一帳戶中的[堆疊](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/stacks.html)，或透過 管理，將其部署為整個組織的[堆疊集](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/what-is-cfnstacksets.html) AWS Organizations。

## 先決條件和限制
<a name="audit-security-groups-access-public-ip-prereqs"></a>

**先決條件**
+ 作用中 AWS 帳戶
+ 使用 [GitHub](https://github.com/skills/introduction-to-github?tab=readme-ov-file) 的經驗
+ 如果您要部署到單一 AWS 帳戶：
  + 建立 CloudFormation 堆疊的[許可](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-iam-template.html) 
  + 在目標帳戶中AWS Config [設定](https://docs.aws.amazon.com/config/latest/developerguide/getting-started.html) 
  + （選用） 目標帳戶中[設定的](https://docs.aws.amazon.com/securityhub/latest/userguide/securityhub-settingup.html#securityhub-manual-setup-overview) Security Hub CSPM
+ 如果您要部署到 AWS 組織：
  + 建立 CloudFormation 堆疊集[的許可](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-iam-template.html) 
  + 使用 AWS Organizations 整合[設定](https://docs.aws.amazon.com/securityhub/latest/userguide/securityhub-settingup.html#securityhub-orgs-setup-overview) Security Hub CSPM
  + 在您部署此解決方案的帳戶中AWS Config [設定](https://docs.aws.amazon.com/config/latest/developerguide/getting-started.html) 
  + 將 指定 AWS 帳戶 為 AWS Config 和 Security Hub CSPM 的委派管理員

**限制**
+ 如果您要部署到未啟用 Security Hub CSPM 的個別帳戶，您可以使用 AWS Config 來評估問題清單。
+ 如果您要部署到沒有 AWS Config 和 Security Hub CSPM 委派管理員的組織，您必須登入個別成員帳戶才能檢視問題清單。
+ 如果您使用 AWS Control Tower 來管理組織中的帳戶，請使用 Customizations for(CfCT) 在此模式中部署 IaC。 [AWS Control Tower CfCT](https://docs.aws.amazon.com/controltower/latest/userguide/cfct-overview.html) 使用 CloudFormation AWS Control Tower 主控台會從護欄建立組態偏離，並要求您重新註冊組織單位 (OUs) 或受管帳戶。
+ 有些 AWS 服務 不適用於所有 AWS 區域。如需區域可用性，請參閱[AWS 服務 依區域](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/)。如需特定端點，請參閱[服務端點和配額](https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html)頁面，然後選擇服務的連結。

## Architecture
<a name="audit-security-groups-access-public-ip-architecture"></a>

**部署至個別 AWS 帳戶**

下列架構圖顯示資源在單一 AWS 中的部署 AWS 帳戶。您可以透過 CloudFormation 主控台直接使用 CloudFormation 範本來佈建資源。如果 Security Hub CSPM 已啟用，您可以在 AWS Config 或 Security Hub CSPM 中檢視結果。如果 Security Hub CSPM 未啟用，您只能在 中檢視結果 AWS Config。

![\[將 IaC 範本部署為單一 AWS 帳戶中的 CloudFormation 堆疊。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/caa8013b-3578-434b-b2c0-5ca7faf45d2d/images/01318e4c-49b5-415f-ac7a-e45451c374cf.png)


該圖顯示以下工作流程：

1. 您可以建立 CloudFormation 堆疊。這會部署 Lambda 函數和 AWS Config 規則。規則和函數都會設定在 AWS Config 和 日誌中發佈資源評估所需的 AWS Identity and Access Management (IAM) 許可。

1.  AWS Config 規則會以[偵測評估模式](https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config-rules.html#aws-config-rules-evaluation-modes)運作，並每 24 小時叫用 Lambda 函數。

1. Lambda 函數會評估安全群組並傳送更新至 AWS Config。

1. Security Hub CSPM 會收到所有 AWS Config 調查結果。

1. 視您在帳戶中設定的服務而定 AWS Config，您可以在 Security Hub CSPM 或 中檢視問題清單。

**部署至 AWS 組織**

下圖顯示透過 AWS Organizations 和 管理的多個帳戶之間的模式部署 AWS Control Tower。您可以透過 CfCT 部署 CloudFormation 範本。評估結果集中在委派管理員帳戶中的 Security Hub CSPM 中。圖表的 AWS CodePipeline 工作流程區段顯示 CfCT 部署期間發生的背景步驟。

![\[在 AWS 組織中將 IaC 範本部署為 CloudFormation 堆疊集。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/caa8013b-3578-434b-b2c0-5ca7faf45d2d/images/f4500347-a481-4cd3-ba14-25a034af7531.png)


該圖顯示以下工作流程：

1. 在管理帳戶中，您將 IaC 範本的壓縮 (ZIP) 檔案上傳至由 CfCT 部署的 Amazon Simple Storage Service (Amazon S3) 儲存貯體。

1. CfCT 管道會解壓縮 檔案、執行 [cfn-nag](https://github.com/stelligent/cfn_nag) (GitHub) 檢查，並將範本部署為 CloudFormation 堆疊集。

1. 根據您在 CfCT 資訊清單檔案中指定的組態，CloudFormation StackSets 會將堆疊部署到個別帳戶或指定的 OUs。這會在目標帳戶中部署 Lambda 函數和 AWS Config 規則。規則和函數都會設定在 AWS Config 和 日誌中發佈資源評估所需的 IAM 許可。

1.  AWS Config 規則會以[偵測評估模式](https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config-rules.html#aws-config-rules-evaluation-modes)運作，並每 24 小時叫用 Lambda 函數。

1. Lambda 函數會評估安全群組並傳送更新至 AWS Config。

1. AWS Config 會將所有調查結果轉送至 Security Hub CSPM。

1. Security Hub CSPM 調查結果會在委派的管理員帳戶中彙總。

1. 您可以在委派管理員帳戶中檢視 Security Hub CSPM 中的彙總調查結果。

## 工具
<a name="audit-security-groups-access-public-ip-tools"></a>

**AWS 服務**
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) 可協助您設定 AWS 資源、快速且一致地佈建資源，以及在整個 AWS 帳戶 和 生命週期中管理資源 AWS 區域。
+ [AWS Config](https://docs.aws.amazon.com/config/latest/developerguide/WhatIsConfig.html) 提供 中資源的詳細檢視 AWS 帳戶 及其設定方式。它可協助您識別資源彼此之間的關係，以及其組態隨著時間的變化。An AWS Config [rule](https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config.html) 會定義您的理想資源組態設定，並可 AWS Config 評估您的 AWS 資源是否符合規則中的條件。
+ [AWS Control Tower](https://docs.aws.amazon.com/controltower/latest/userguide/what-is-control-tower.html) 可協助您設定和管理 AWS 多帳戶環境，並遵循規範最佳實務。[自訂 AWS Control Tower (CfCT)](https://docs.aws.amazon.com/controltower/latest/userguide/cfct-overview.html) 可協助您自訂 AWS Control Tower 登陸區域，並保持符合 AWS 最佳實務。此解決方案的自訂是透過 CloudFormation 範本 AWS Organizations [和服務控制政策 (SCPs](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_policies_scps.html)實作。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一項運算服務，可協助您執行程式碼，無需佈建或管理伺服器。它只會在需要時執行程式碼並自動擴展，因此您只需按使用的運算時間付費。
+ [AWS Organizations](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_introduction.html) 是一種帳戶管理服務，可協助您將多個 合併 AWS 帳戶 到您建立並集中管理的組織。
+ [AWS Security Hub CSPM](https://docs.aws.amazon.com/securityhub/latest/userguide/what-is-securityhub.html) 提供 中安全狀態的完整檢視 AWS。它還可協助您根據安全產業標準和最佳實務來檢查 AWS 環境。

**其他工具**
+ [Python](https://www.python.org/) 是一種一般用途的電腦程式設計語言。

**程式碼儲存庫**

此模式的程式碼可在 GitHub [Detect 易受攻擊的安全群組](https://github.com/aws-samples/detect-public-security-groups/tree/main)儲存庫中使用。

## 最佳實務
<a name="audit-security-groups-access-public-ip-best-practices"></a>

我們建議您遵守下列資源中的最佳實務：
+ [使用 的組織單位最佳實務 AWS Organizations](https://aws.amazon.com/blogs/mt/best-practices-for-organizational-units-with-aws-organizations/) (AWS 雲端操作和遷移部落格）
+ [在 AWS Control Tower 上使用 建立初始基礎的指導 AWS](https://aws.amazon.com/solutions/guidance/establishing-an-initial-foundation-using-control-tower-on-aws/) (AWS 解決方案程式庫）
+ [建立和修改 AWS Control Tower 資源的指引 ](https://docs.aws.amazon.com/controltower/latest/userguide/getting-started-guidance.html)(AWS Control Tower 文件）
+ [CfCT 部署考量 ](https://docs.aws.amazon.com/controltower/latest/userguide/cfct-considerations.html)(AWS Control Tower 文件）
+ [套用最低權限許可 ](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege)(IAM 文件）

## 史詩
<a name="audit-security-groups-access-public-ip-epics"></a>

### 檢閱 CloudFormation 範本
<a name="review-the-cfnshort-template"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 決定您的部署策略。 | 檢閱解決方案和程式碼，以判斷您 AWS 環境的部署策略。判斷您要部署到單一帳戶或 AWS 組織。 | 應用程式擁有者，一般 AWS | 
| 複製儲存庫。 | 輸入下列命令以複製[偵測易受攻擊的安全群組](https://github.com/aws-samples/detect-public-security-groups.git)儲存庫：<pre>git clone https://github.com/aws-samples/detect-public-security-groups.git</pre> | 應用程式開發人員、應用程式擁有者 | 
| 驗證 Python 版本。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/audit-security-groups-access-public-ip.html) | AWS 管理員、應用程式開發人員 | 

### 部署 CloudFormation 範本
<a name="deploy-the-cfnshort-template"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 部署 CloudFormation 範本。 | 將 CloudFormation 範本部署到您的 AWS 環境中。執行以下任意一項：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/audit-security-groups-access-public-ip.html) | 應用程式開發人員、AWS 管理員、一般 AWS | 
| 驗證部署。 | 在 [CloudFormation 主控台](https://console.aws.amazon.com/cloudformation/)中，確認堆疊或堆疊集已成功部署。 | AWS 管理員、應用程式擁有者 | 

### 檢閱問題清單
<a name="review-the-findings"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 檢視 AWS Config 規則調查結果。 | 在 Security Hub CSPM 中，執行下列動作以檢視個別問題清單：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/audit-security-groups-access-public-ip.html)在 Security Hub CSPM 中，執行下列動作以檢視依 分組的問題清單總數 AWS 帳戶：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/audit-security-groups-access-public-ip.html)在 中 AWS Config，若要檢視問題清單，請遵循 AWS Config 文件中[檢視合規資訊和評估結果](https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config_view-compliance.html)中的指示。 | AWS 管理員、AWS 系統管理員、雲端管理員 | 

## 疑難排解
<a name="audit-security-groups-access-public-ip-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| CloudFormation 堆疊集建立或刪除失敗。 | 部署 AWS Control Tower 時，它會強制執行必要的護欄，並取得 AWS Config 對彙總器和規則的控制。這包括防止透過 CloudFormation 進行任何直接變更。若要正確部署或移除此 CloudFormation 範本，包括所有相關資源，您必須使用 CfCT。 | 
| CfCT 無法刪除 CloudFormation 範本。 | 如果 CloudFormation 範本即使在資訊清單檔案中進行必要變更並移除範本檔案後仍存在，請確認資訊清單檔案包含 `enable_stack_set_deletion` 參數，且值設定為 `false`。如需詳細資訊，請參閱 CfCT 文件中的[刪除堆疊集](https://docs.aws.amazon.com/controltower/latest/userguide/cfct-delete-stack.html)。 | 

## 相關資源
<a name="audit-security-groups-access-public-ip-resources"></a>
+ [AWS Config 自訂規則](https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config_develop-rules.html) (AWS Config 文件）