

# 查询 Amazon GuardDuty 调查发现
<a name="querying-guardduty"></a>

[Amazon GuardDuty](https://aws.amazon.com/guardduty/) 是一项安全监控服务，用于帮助识别您 AWS 环境中的意外活动和潜在的未经授权或恶意活动。当检测到意外和潜在恶意活动时，GuardDuty 会生成安全[调查结果](https://docs.aws.amazon.com/guardduty/latest/ug/guardduty_findings.html)，您可以将其导出到 Amazon S3 进行存储和分析。将调查结果导出到 Amazon S3 后，您可以使用 Athena 进行查询。本文介绍如何在 Athena 中为 GuardDuty 调查结果创建表并查询它们。

有关 Amazon GuardDuty 更多信息，请参阅《[Amazon GuardDuty 用户指南](https://docs.aws.amazon.com/guardduty/latest/ug/)》。

## 先决条件
<a name="querying-guardduty-prerequisites"></a>
+ 启用 GuardDuty 功能以将调查结果导出到 Amazon S3。有关步骤，请参阅《Amazon GuardDuty 用户指南》中[导出调查结果](https://docs.aws.amazon.com/guardduty/latest/ug/guardduty_exportfindings.html)。

## 在 Athena 中为 GuardDuty 调查发现创建表
<a name="querying-guardduty-creating-a-table-in-athena-for-guardduty-findings"></a>

要从 Athena 查询您的 GuardDuty 调查结果，您必须为它们创建一个表。

**要在 Athena 中为 GuardDuty 调查结果创建表**

1. 从 [https://console.aws.amazon.com/athena/](https://console.aws.amazon.com/athena/home) 打开 Athena 控制台。

1. 将以下 DDL 语句粘贴到 Athena 控制台中。修改 `LOCATION 's3://amzn-s3-demo-bucket/AWSLogs/account-id/GuardDuty/'` 中的值以指向您在 Amazon S3 中的 GuardDuty 调查结果。

   ```
   CREATE EXTERNAL TABLE `gd_logs` (
     `schemaversion` string,
     `accountid` string,
     `region` string,
     `partition` string,
     `id` string,
     `arn` string,
     `type` string,
     `resource` string,
     `service` string,
     `severity` string,
     `createdat` string,
     `updatedat` string,
     `title` string,
     `description` string)
   ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
    LOCATION 's3://amzn-s3-demo-bucket/AWSLogs/account-id/GuardDuty/'
    TBLPROPERTIES ('has_encrypted_data'='true')
   ```
**注意**  
SerDe 期望每个 JSON 文档都位于单行文本中，并且不使用行终止字符分隔记录中的字段。如果 JSON 文本采用美观的打印格式，当您在创建表后尝试对其进行查询时，可能会收到类似以下内容的错误消息：HIVE\$1CURSOR\$1ERROR: Row is not a valid JSON Object（HIVE\$1CURSOR\$1ERROR：行不是有效的 JSON 对象）或 HIVE\$1CURSOR\$1ERROR: JsonParseException: Unexpected end-of-input: expected close marker for OBJECT（HIVE\$1CURSOR\$1ERROR：JsonParseException：意外的输入结束：对象的预期关闭标记）。有关更多信息，请参阅 GitHub 上 OpenX SerDe 文档中的 [JSON 数据文件](https://github.com/rcongiu/Hive-JSON-Serde#json-data-files)。

1. 在 Athena 控制台中运行查询以注册 `gd_logs` 表。查询完成后，调查结果准备就绪，可供您从 Athena 查询。

## 示例查询
<a name="querying-guardduty-examples"></a>

以下示例演示了如何从 Athena 查询 GuardDuty 调查结果。

**Example – DNS 数据泄露**  
以下查询返回可能会通过 DNS 查询泄露数据的 Amazon EC2 实例的相关信息。  

```
SELECT
    title,
    severity,
    type,
    id AS FindingID,
    accountid,
    region,
    createdat,
    updatedat,
    json_extract_scalar(service, '$.count') AS Count,
    json_extract_scalar(resource, '$.instancedetails.instanceid') AS InstanceID,
    json_extract_scalar(service, '$.action.actiontype') AS DNS_ActionType,
    json_extract_scalar(service, '$.action.dnsrequestaction.domain') AS DomainName,
    json_extract_scalar(service, '$.action.dnsrequestaction.protocol') AS protocol,
    json_extract_scalar(service, '$.action.dnsrequestaction.blocked') AS blocked
FROM gd_logs
WHERE type = 'Trojan:EC2/DNSDataExfiltration'
ORDER BY severity DESC
```

**Example – 未授权的 IAM 用户访问**  
以下查询返回所有区域中 IAM 委托人的所有 `UnauthorizedAccess:IAMUser` 调查结果类型。  

```
SELECT title,
         severity,
         type,
         id,
         accountid,
         region,
         createdat,
         updatedat,
         json_extract_scalar(service, '$.count') AS Count, 
         json_extract_scalar(resource, '$.accesskeydetails.username') AS IAMPrincipal, 
         json_extract_scalar(service,'$.action.awsapicallaction.api') AS APIActionCalled
FROM gd_logs
WHERE type LIKE '%UnauthorizedAccess:IAMUser%' 
ORDER BY severity desc;
```

## 有关查询 GuardDuty 调查结果的提示
<a name="querying-guardduty-tips"></a>

创建查询时，请记住以下几点。
+ 要从嵌套 JSON 字段中提取数据，请使用 Presto `json_extract` 或 `json_extract_scalar` 函数。有关更多信息，请参阅 [从字符串中提取 JSON 数据](extracting-data-from-JSON.md)。
+ 请确保 JSON 字段中的所有字符均为小写字符。
+  有关下载查询结果的信息，请参阅[使用 Athena 控制台下载查询结果文件](saving-query-results.md)。