

# AWS IoT 策略过于宽容
<a name="audit-chk-iot-policy-permissive"></a>

AWS IoT 策略提供的权限过于宽泛或不受限制。它为一系列设备授予发送或接收 MQTT 消息的权限，或为一系列设备授予访问或修改影子和任务执行数据的权限。

一般而言，针对某个设备的策略应该授予仅与该设备关联的资源的访问权限，而不应该牵涉其它设备，或只牵涉少量其他设备。除了某些例外情况，在此类策略中使用通配符（例如“\*”）指定资源被视为过于宽泛或不受限制。

此检查在 CLI 和 API 中显示为 `IOT_POLICY_OVERLY_PERMISSIVE_CHECK`。

**严重性：**严重

## Details
<a name="audit-chk-iot-policy-permissive-details"></a>

此检查发现不合规的 AWS IoT 策略时，会返回以下原因代码：
+ ALLOWS\_BROAD\_ACCESS\_TO\_IOT\_DATA\_PLANE\_ACTIONS

## 为什么这非常重要
<a name="audit-chk-iot-policy-permissive-why-it-matters"></a>

策略过于宽容的证书、Amazon Cognito 身份或事物组一旦遭到破坏，就会影响您的整个账户。攻击者可以使用此类宽泛的访问权限，读取或修改所有设备的影子、任务或任务执行数据。或者，攻击者可以使用已遭破坏的证书连接您网络中的恶意设备或发动 DDOS 攻击。

## 如何修复
<a name="audit-chk-iot-policy-permissive-how-to-fix"></a>

按照以下步骤来修复附加到事物、事物组或其它实体的任何不合规策略：

1. 使用 [CreatePolicyVersion](https://docs.aws.amazon.com/iot/latest/apireference/API_CreatePolicyVersion.html) 创建新的兼容版本的策略。将 `setAsDefault` 标记设置为 True。（这可使此新版本适用于使用策略的所有实体。）

1. 使用 [ListTargetsForPolicy](https://docs.aws.amazon.com/iot/latest/apireference/API_ListTargetsForPolicy.html) 获取策略附加到的目标列表（证书、事物组），并确定组中包含的设备或使用证书进行连接的设备。

1. 验证所有关联的设备能否连接到 AWS IoT。如果设备无法连接，使用 [SetPolicyVersion](https://docs.aws.amazon.com/iot/latest/apireference/API_SetPolicyVersion.html) 将默认策略回滚到之前的版本，修改策略，然后重试。

您可以使用缓解操作实现以下目的：
+ 对您的审计查找结果应用 `REPLACE_DEFAULT_POLICY_VERSION` 缓解操作以进行此更改。
+ 如果要实现自定义响应以响应 Amazon SNS 消息，请应用 `PUBLISH_FINDINGS_TO_SNS` 缓解操作。

有关更多信息，请参阅 [缓解操作](dd-mitigation-actions.md)。

使用 [AWS IoT Core 策略变量](https://docs.aws.amazon.com/iot/latest/developerguide/iot-policy-variables.html)在策略中动态引用 AWS IoT 资源。

## MQTT 权限
<a name="audit-chk-iot-policy-permissive-mqtt-permissions"></a>

MQTT 消息通过 AWS IoT 消息代理发送，并由设备用来执行多个操作，包括访问和修改影子状态和任务执行状态。为设备授权以连接、发布或订阅 MQTT 消息的策略，应该限定对特定资源执行如下操作：

**Connect**  
+ 不合规：

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

  通配符 \* 允许任何设备连接到 AWS IoT。

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

  除非 `iot:Connection.Thing.IsAttached` 在条件键中设置为 True，这相当于上例中的通配符 \*。
+ 合规：

------
#### [ 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
  ```

  此示例允许设备更新任何设备的影子（\* = 所有设备）。

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

  此示例允许设备读取、更新或删除任何设备的影子。
+ 合规：

------
#### [ 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/*
  ```

  与上一个示例相同，不过使用的是 \# 通配符。

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

  此示例允许设备查看任何设备上的影子更新（\+ = 所有设备）。
+ 合规：

------
#### [ 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}}:topic/$aws/things/*
  ```

  这是合规的，因为设备只能从其有权订阅的主题接收消息。

## 影子和任务权限
<a name="shadow-job-permissions"></a>

为设备授予执行 API 操作权限以访问或修改设备影子或任务执行数据的策略，应该限定对特定资源执行以下操作。下面是一些 API 操作：
+ `DeleteThingShadow`
+ `GetThingShadow`
+ `UpdateThingShadow`
+ `DescribeJobExecution`
+ `GetPendingJobExecutions`
+ `StartNextPendingJobExecution`
+ `UpdateJobExecution`

**示例**
+ 不合规：

  ```
  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"
        ]
      }
    ]
  }
  ```

------

  此示例允许设备仅对两个事物执行指定操作。