

# 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>
+ 결과를 Amazon S3에 내보내는 GuardDuty 기능을 활성화합니다. 실행 단계는 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 콘솔에 붙여 넣습니다. Amazon S3의 GuardDuty 결과를 가리키도록 `LOCATION 's3://amzn-s3-demo-bucket/AWSLogs/account-id/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: 행이 유효한 JSON 객체가 아님(HIVE\$1CURSOR\$1ERROR: Row is not a valid JSON Object) 또는 HIVE\$1CURSOR\$1ERROR: JsonParseException: 예기치 않은 입력 종료: OBJECT의 닫기 마커 필요(HIVE\$1CURSOR\$1ERROR: JsonParseException: Unexpected end-of-input: expected close marker for 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) 단원을 참조하세요.