

# 未驗證的 Cognito 角色過度寬鬆
<a name="audit-chk-unauth-cognito-role-permissive"></a>

附加到未經驗證的 Amazon Cognito 身分集區角色是過於寬鬆的政策，因為它會授與執行以下任何 AWS IoT 動作的許可：
+ 管理或修改物件。
+ 讀取物件管理資料。
+ 管理與物件不相關的資料或資源。

或者，因為它授與可在廣泛的裝置上執行以下 AWS IoT 動作的許可：
+ 使用 MQTT 連接、發佈或訂閱保留的主題 (包括影子或任務執行資料)。
+ 使用 API 命令讀取或修改影子或任務執行資料。

一般而言，使用未經驗證 Amazon Cognito 身分集區角色連線的裝置應該只有受限的許可來發佈和訂閱特定物件的 MQTT 主題，或使用 API 命令來讀取和修改影子或任務執行資料的特定物件相關資料。

此檢查會以 `UNAUTHENTICATED_COGNITO_ROLE_OVERLY_PERMISSIVE_CHECK` 出現在 CLI 和 API 中。

嚴重性：**關鍵**

## 詳細資訊
<a name="audit-chk-unauth-cognito-role-permissive-details"></a>

對此檢查，AWS IoT Device Defender 會稽核所有 Amazon Cognito 身分集區在稽核執行前的 31 天內用於連接到 AWS IoT 訊息代理程式。稽核包含所有 Amazon Cognito 身分集區已驗證或未經驗證的 Amazon Cognito 身分集區連線。

當此檢查發現不合規未經驗證的 Amazon Cognito 身分集區角色時，將會傳回下列原因代碼：
+ ALLOWS\$1ACCESS\$1TO\$1IOT\$1ADMIN\$1ACTIONS
+ ALLOWS\$1BROAD\$1ACCESS\$1TO\$1IOT\$1DATA\$1PLANE\$1ACTIONS

## 為什麼它很重要
<a name="audit-chk-unauth-cognito-role-permissive-why-it-matters"></a>

由於未經驗證的使用者從未經過身分驗證，與經過身分驗證的 Amazon Cognito 身分相比其風險要大得多。如果未經驗證身分遭到入侵，則可以使用管理動作修改帳戶設定、刪除資源或獲得存取敏感資料。或者，通過廣泛的存取裝置設定，它可以存取或修改您帳戶中所有裝置的影子和任務。使用許可為的訪客使用者可能入侵您的整個叢集或啟動 DDOS 攻擊的訊息。

## 如何修正它
<a name="audit-chk-unauth-cognito-role-permissive-how-to-fix"></a>

連接到未經驗證的 Amazon Cognito 身分集區角色的政策僅應授與裝置完成其任務所需的那些許可。建議下列步驟：

1. 建立新的合規角色。

1. 建立 Amazon Cognito 身分集區，並連接至合規角色。

1. 驗證您的身分可以存取 AWS IoT 以使用新的集區。

1. 驗證完成後，將合規角色連接至標記為不合規的 Amazon Cognito 身分集區。

您也可以使用緩解行動：
+ 套用 `PUBLISH_FINDINGS_TO_SNS` 緩解動作，實作自訂回應以回應 Amazon SNS 訊息。

如需更多詳細資訊，請參閱 [緩解動作](dd-mitigation-actions.md)。

## 管理或修改物件
<a name="manage-modify-things-check"></a>

以下 AWS IoT API 動作用來管理或修改物件。執行這些動作的許可不應授與透過未經驗證的 Amazon Cognito 身分集區連接的裝置：
+ `AddThingToThingGroup` 
+ `AttachThingPrincipal` 
+ `CreateThing` 
+ `DeleteThing` 
+ `DetachThingPrincipal` 
+ `ListThings` 
+ `ListThingsInThingGroup` 
+ `RegisterThing` 
+ `RemoveThingFromThingGroup` 
+ `UpdateThing` 
+ `UpdateThingGroupsForThing` 

授與許可的任何角色執行這些動作，即使是單一資源都是視為不合規。

## 讀取物件管理資料
<a name="read-thing-admin-data-check"></a>

以下 AWS IoT API 動作用來讀取或修改物件資料。透過未經驗證的 Amazon Cognito 身分集區連接的裝置，不應授與其執行這些動作的許可。
+ `DescribeThing`
+ `ListJobExecutionsForThing`
+ `ListThingGroupsForThing`
+ `ListThingPrincipals`

**Example**  
+ 不合規：

------
#### [ JSON ]

****  

  ```
  {
    "Version":"2012-10-17",		 	 	 
    "Statement": [
      {
        "Sid": "AllowIoTThingOperations",
        "Effect": "Allow",
        "Action": [ 
            "iot:DescribeThing",
            "iot:ListJobExecutionsForThing",
            "iot:ListThingGroupsForThing",
            "iot:ListThingPrincipals"
        ],
        "Resource": [
          "arn:aws:iot:us-east-1:123456789012:thing/name-of-thing"
        ]
      }
    ]
  }
  ```

------

  這可讓裝置執行指定的動作，即使僅授與一個物件。

## 管理非物件
<a name="manage-non-things-check"></a>

透過未經驗證 Amazon Cognito 身分集區連接的裝置，不應被授與許可來執行非這些章節中所討論的任何其他 AWS IoT API 動作。您可以藉由建立裝置尚未使用的單獨身分集區，利用透過未經驗證 Amazon Cognito 身分集區連接的應用程式管理您的帳戶。

## 訂閱/發佈至 MQTT 主題
<a name="audit-chk-unauth-cognito-role-permissive-mqtt-topics"></a>

會透過 AWS IoT 訊息代理程式傳送 MQTT 訊息，並裝置會使用該訊息來執行許多動作，包括存取和修改影子狀態和任務執行狀態。與裝置連接、發佈或訂閱 MQTT 訊息許可的政策，應該對特定資源加以限制這些動作如下所示：

**連線**  
+ 不合規：

  ```
  arn:aws:iot:region:account-id:client/*
  ```

  萬用字元 \$1 可讓任何裝置連接到 AWS IoT。

  ```
  arn:aws:iot:region:account-id:client/${iot:ClientId}
  ```

  除非 `iot:Connection.Thing.IsAttached` 在條件索引鍵中設定為 true，這相當於先前範例中的萬用字元 \$1。
+ 合規：

------
#### [ JSON ]

****  

  ```
  {
    "Version":"2012-10-17",		 	 	 
    "Statement": [
      {
        "Effect": "Allow",
        "Action": [
          "iot:Connect"
        ],
        "Resource": [
          "arn:aws:iot:us-east-1:123456789012:client/${iot:Connection.Thing.ThingName}"
        ]
      }
    ]
  }
  ```

------

  資源規範包含與連接所用的裝置名稱相符的變數。條件陳述式會檢查 MQTT 用戶端所用的憑證是否與連接到具有所用名稱的物件相符，進一步限制許可。

**發布**  
+ 不合規：

  ```
  arn:aws:iot:region:account-id:topic/$aws/things/*/shadow/update
  ```

  這可讓裝置更新任何裝置 (\$1 = 所有裝置) 的影子。

  ```
  arn:aws:iot:region:account-id:topic/$aws/things/*
  ```

  這可讓裝置讀取、更新或刪除任何裝置 (\$1 = 所有裝置) 的影子。
+ 合規：

------
#### [ JSON ]

****  

  ```
  {
      "Version":"2012-10-17",		 	 	 
      "Statement": [
          {
              "Effect": "Allow",
              "Action": [
                  "iot:Publish"
              ],
              "Resource": [
                  "arn:aws:iot:us-east-1:123456789012:topic/$aws/things/${iot:Connection.Thing.ThingName}/shadow/*"
              ]
          }
      ]
  }
  ```

------

  資源規格包含萬用字元，但只符合任何影子相關主題物件名稱連線的裝置。

**訂閱**  
+ 不合規：

  ```
  arn:aws:iot:region:account-id:topicfilter/$aws/things/*
  ```

  這可讓裝置為所有裝置訂閱保留影子或任務主題。

  ```
  arn:aws:iot:region:account-id:topicfilter/$aws/things/*
  ```

  前述範例相同，但使用 \$1 萬用字元。

  ```
  arn:aws:iot:region:account-id:topicfilter/$aws/things/+/shadow/update
  ```

  這可讓裝置看到任何裝置 (\$1 = 所有裝置) 的影子更新。
+ 合規：

------
#### [ JSON ]

****  

  ```
  {
      "Version":"2012-10-17",		 	 	 
      "Statement": [
          {
              "Effect": "Allow",
              "Action": [
                  "iot:Subscribe"
              ],
              "Resource": [
                  "arn:aws:iot:us-east-1:123456789012:topicfilter/$aws/things/${iot:Connection.Thing.ThingName}/shadow/*",
                  "arn:aws:iot:us-east-1:123456789012:topicfilter/$aws/things/${iot:Connection.Thing.ThingName}/jobs/*"
              ]
          }
      ]
  }
  ```

------

  資源規格包含萬用字元，但只會針對物件名稱用於連線的裝置，比對任何影子相關主體與任何任務相關主題。

**接收**  
+ 合規：

  ```
  arn:aws:iot:region:account-id:topicfilter/$aws/things/*
  ```

  這是允許的，因為裝置只能從已訂閱許可的主題接收訊息。

## 讀取/修改影子或任務資料
<a name="read-modify-shadow-job-data-check"></a>

授與裝置執行 API 動作的許可以存取或修改裝置影子或任務執行資料的政策，應該於特定的資源限制這些動作。以下是 API 動作：
+ `DeleteThingShadow`
+ `GetThingShadow`
+ `UpdateThingShadow`
+ `DescribeJobExecution`
+ `GetPendingJobExecutions`
+ `StartNextPendingJobExecutio`n
+ `UpdateJobExecution`

**Example**  
+ 不合規：

  ```
  arn:aws:iot:region:account-id:thing/*
  ```

  這可讓裝置在任何物件上執行指定的動作。
+ 合規：

------
#### [ JSON ]

****  

  ```
  {
    "Version":"2012-10-17",		 	 	 
    "Statement": [
      {
        "Effect": "Allow",
        "Action": [ 
            "iot:DeleteThingShadow",
            "iot:GetThingShadow",
            "iot:UpdateThingShadow",
            "iotjobsdata:DescribeJobExecution",
            "iotjobsdata:GetPendingJobExecutions",
            "iotjobsdata:StartNextPendingJobExecution",
            "iotjobsdata:UpdateJobExecution"
        ],
        "Resource": [
          "arn:aws:iot:us-east-1:123456789012:thing/MyThing1",
          "arn:aws:iot:us-east-1:123456789012:thing/MyThing2"
        ]
      }
    ]
  }
  ```

------

  這可讓裝置僅在兩個物件上執行指定的動作。