

# 如何使用 AWS IoT Device Defender detect
<a name="detect-HowToHowTo"></a>

1. 可以只将 AWS IoT Device Defender Detect 与云端指标相结合使用，但如果计划使用设备报告的指标，必须首先在连接 AWS IoT 的设备或设备网关上部署 AWS IoT SDK。有关更多信息，请参阅[从设备发送指标](detect-device-side-metrics.md#DetectMetricsMessages)。

1. 在定义行为并创建警报之前，请先考虑查看设备生成的指标。AWS IoT 可以从设备收集指标，因此您可以首先识别账户中的一组设备或所有设备的常见或异常行为。使用 [CreateSecurityProfile](https://docs.aws.amazon.com/iot/latest/apireference/API_CreateSecurityProfile.html)，但只指定您感兴趣的那些 `additionalMetricsToRetain`。此时不要指定 `behaviors`。

   使用 AWS IoT 控制台查看设备指标，以了解设备的典型行为。

1. 为安全配置文件创建一组行为。行为包含为账户中的一组设备或所有设备指定正常行为的指标。有关详细信息和示例，请参阅[云端指标](detect-cloud-side-metrics.md)和 [设备端指标](detect-device-side-metrics.md)。创建一组行为后，可以使用 [ValidateSecurityProfileBehaviors](https://docs.aws.amazon.com/iot/latest/apireference/API_ValidateSecurityProfileBehaviors.html) 进行验证。

1. 使用 [CreateSecurityProfile](https://docs.aws.amazon.com/iot/latest/apireference/API_CreateSecurityProfile.html) 操作创建包含行为的安全配置文件。当设备违反行为时，可以使用 `alertTargets` 参数向目标（SNS 主题）发送告警。（如果使用 SNS 发送告警，请注意系统将根据您AWS 账户的 SNS 主题配额统计数量。） 大规模的违规项可能会超过您的 SNS 主题配额。也可以使用 CloudWatch 指标检查违规。有关更多信息，请参阅《AWS IoT Core 开发人员指南》**中的[使用 Amazon CloudWatch 监控 AWS IoT 警报和指标](https://docs.aws.amazon.com/iot/latest/developerguide/monitoring-cloudwatch.html)。

1. 使用 [AttachSecurityProfile](https://docs.aws.amazon.com/iot/latest/apireference/API_AttachSecurityProfile.html) 操作将安全配置文件附加到一组设备（事物组）、账户中所有已注册的事物、所有未注册的事物或所有设备。AWS IoT Device DefenderDetect 开始检测异常行为，如果检测到任何行为违规，则发送告警。您可能想要将安全配置文件附加到所有未注册的事物，例如，您希望与不在账户事物注册表中的移动设备进行交互。您可以根据需求，为不同设备组定义不同的行为集。

   要将安全配置文件附加到一组设备，必须指定包含这些设备的事物组的 ARN。事物组 ARN 采用以下格式。

   ```
   arn:aws:iot:region:account-id:thinggroup/thing-group-name
   ```

   要将安全配置文件附加到AWS 账户中所有已注册的事物（忽略未注册的事物），必须指定以下格式的 ARN：

   ```
   arn:aws:iot:region:account-id:all/registered-things
   ```

   要将安全配置文件附加到所有未注册的事物，必须指定以下格式的 ARN。

   ```
   arn:aws:iot:region:account-id:all/unregistered-things
   ```

   要将安全配置文件附加到所有设备，必须指定以下格式的 ARN。

   ```
   arn:aws:iot:region:account-id:all/things
   ```

1. 也可以使用 [ListActiveViolations](https://docs.aws.amazon.com/iot/latest/apireference/API_ListActiveViolations.html) 操作跟踪违规项，从而了解对于给定的安全配置文件或目标设备，检测到了哪些违规项。

   使用 L[istViolationEvents](https://docs.aws.amazon.com/iot/latest/apireference/API_ListViolationEvents.html) 操作查看在指定时间段内检测到哪些违规项。您可以按安全配置文件、设备或告警验证状态筛选这些结果。

1. 通过使用 [PutVerificationStateOnViolation](https://docs.aws.amazon.com/iot/latest/apireference/API_PutVerificationStateOnViolation.html) 操作标记告警的验证状态并提供该验证状态的说明，可以验证、组织和管理报警。

1. 如果设备违反定义的行为过于频繁或太不频繁，那么应该调整行为定义。

1. 要查看您设置的安全配置文件和正在受到监控的设备，请使用 [ListSecurityProfiles](https://docs.aws.amazon.com/iot/latest/apireference/API_ListSecurityProfiles.html)、[ListSecurityProfilesForTarget](https://docs.aws.amazon.com/iot/latest/apireference/API_ListSecurityProfilesForTarget.html) 和 [ListTargetsForSecurityProfile](https://docs.aws.amazon.com/iot/latest/apireference/API_ListTargetsForSecurityProfile.html) 操作。

   使用 [DescribeSecurityProfile](https://docs.aws.amazon.com/iot/latest/apireference/API_DescribeSecurityProfile.html) 操作获取有关安全配置文件的更多详细信息。

1. 要更新安全配置文件，请使用 [UpdateSecurityProfile](https://docs.aws.amazon.com/iot/latest/apireference/API_UpdateSecurityProfile.html) 操作。使用 [DetachSecurityProfile](https://docs.aws.amazon.com/iot/latest/apireference/API_DetachSecurityProfile.html) 操作从账户或目标事物组分离安全配置文件。使用 [DeleteSecurityProfile](https://docs.aws.amazon.com/iot/latest/apireference/API_DeleteSecurityProfile.html) 操作完全删除安全配置文件。