

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

# 查詢 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：非預期的輸入結束：預期為 OBJECT 的關閉標記)。如需詳細資訊，請參閱 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)。