AWSリソースのエビデンス取得用スクリプトを作成してみた: GuardDuty編

1 min read

はじめに

AWS GuardDutyでは使用可能な全リージョンで有効化するケースが多いかと思います。 その際に、各リージョンでのエビデンスを取得するのは大変なので、以下のようなスクリプトを作成しました。

※本スクリプトはCloudShell上でのみ動作確認をしています。

スクリプト内容

#!/bin/bash

# 全リージョンのリストを取得
regions=$(aws ec2 describe-regions --query 'Regions[].RegionName' --output text)

for region in $regions; do
    echo -e "\nリージョン: $region"
    
    # 各リージョンのフォルダを作成
    mkdir -p "$region"
    
    # 各リージョンのGuardDutyのDetectorが存在するか確認
    detector_ids=$(aws guardduty list-detectors --region $region --query 'DetectorIds[]' --output text)
    
    if [ -z "$detector_ids" ]; then
        echo "  GuardDutyが有効化されていません"
    else
        # DetectorのIDを取得
        detector_id=$(echo $detector_ids | cut -f1 -d' ')
        
        # Detectorの詳細情報を取得
        detector=$(aws guardduty get-detector --detector-id $detector_id --region $region)
        
        echo "  DetectorのID: $detector_id"
        echo "  GuardDutyのステータス: $(echo $detector | jq -r '.Status')"
        echo "  検出結果の更新頻度: $(echo $detector | jq -r '.FindingPublishingFrequency')"
        echo "  サービスロール: $(echo $detector | jq -r '.ServiceRole')"
        
        # 信頼リストと脅威リストの設定を取得
        ip_set_ids=$(aws guardduty list-ip-sets --detector-id $detector_id --region $region --query 'IpSetIds[]' --output text)
        threat_intel_set_ids=$(aws guardduty list-threat-intel-sets --detector-id $detector_id --region $region --query 'ThreatIntelSetIds[]' --output text)
        
        echo "  信頼リストの数: $(echo $ip_set_ids | wc -w)"
        echo "  脅威リストの数: $(echo $threat_intel_set_ids | wc -w)"

        # 発行先の設定を取得
        destination_id=$(aws guardduty list-publishing-destinations --detector-id $detector_id --region $region --query 'Destinations[].DestinationId' --output text)
        publishing_destination=$(aws guardduty describe-publishing-destination --detector-id $detector_id --destination-id $destination_id --region $region)
        
        echo "  発行先の有効化状況: $(echo $publishing_destination | jq -r '.Status')"
        echo "    バケット名: $(echo $publishing_destination | jq -r '.DestinationProperties.DestinationArn')"
        echo "    バケットの暗号化: $(echo $publishing_destination | jq -r '.DestinationProperties.KmsKeyArn')"

        # Featuresの取得
        features=$(echo $detector | jq -r '.Features')
        
        # S3保護の有効化状況を取得
        s3_protection=$(echo $features | jq -r '.[] | select(.Name == "S3_DATA_EVENTS") | .Status')
        echo "  S3保護の有効化状況: $s3_protection"
        
        # EKS保護の有効化状況を取得
        eks_protection=$(echo $features | jq -r '.[] | select(.Name == "EKS_AUDIT_LOGS") | .Status')
        echo "  EKS保護の有効化状況: $eks_protection"
        
        # ランタイムモニタリングの有効化状況を取得
        runtime_monitoring=$(echo $features | jq -r '.[] | select(.Name == "RUNTIME_MONITORING") | .Status')
        echo "  ランタイムモニタリングの有効化状況: $runtime_monitoring"

        # Amazon EKSのランタイム自動エージェントの状態を取得
        eks_auto_agent=$(echo $features | jq -r '.[] | select(.Name == "RUNTIME_MONITORING") | .AdditionalConfiguration[] | select(.Name == "EKS_ADDON_MANAGEMENT") | .Status')
        echo "    Amazon EKSのランタイム自動エージェントの状態: $eks_auto_agent"

        # AWS Fargate (ECS のみ)のランタイム自動エージェントの状態を取得
        fargate_auto_agent=$(echo $features | jq -r '.[] | select(.Name == "RUNTIME_MONITORING") | .AdditionalConfiguration[] | select(.Name == "ECS_FARGATE_AGENT_MANAGEMENT") | .Status')
        echo "    AWS Fargate (ECS のみ)のランタイム自動エージェントの状態: $fargate_auto_agent"

        # GuardDuty エージェント管理 (EC2)のランタイム自動エージェントの状態を取得
        ec2_auto_agent=$(echo $features | jq -r '.[] | select(.Name == "RUNTIME_MONITORING") | .AdditionalConfiguration[] | select(.Name == "EC2_AGENT_MANAGEMENT") | .Status')
        echo "    GuardDuty エージェント管理 (EC2)のランタイム自動エージェントの状態: $ec2_auto_agent"
        
        # マルウェア保護の有効化状況を取得
        malware_protection=$(echo $features | jq -r '.[] | select(.Name == "EBS_MALWARE_PROTECTION") | .Status')
        echo "  マルウェア保護の有効化状況: $malware_protection"

        # マルウェア保護のスナップショットの有効化状況を取得
        malware_detail=$(aws guardduty get-malware-scan-settings --detector-id $detector_id --region $region)
        malware_snapshot=$(echo $malware_detail | jq -r '.EbsSnapshotPreservation')
        echo "    マルウェア保護のスナップショットの有効化状況: $malware_snapshot"

        # RDS保護の有効化状況を取得
        rds_protection=$(echo $features | jq -r '.[] | select(.Name == "RDS_LOGIN_EVENTS") | .Status')
        echo "  RDS保護の有効化状況: $rds_protection"

        # Lambda保護の有効化状況を取得
        lambda_protection=$(echo $features | jq -r '.[] | select(.Name == "LAMBDA_NETWORK_LOGS") | .Status')
        echo "  Lambda保護の有効化状況: $lambda_protection"
      	
      	# ファイル出力
        echo "$detector" > "$region/get-detector_$region.json"
        echo "$publishing_destination" > "$region/describe-publishing-destination_$region.json"
        echo "$malware_detail" > "$region/get-malware-scan-settings_$region.json"
    fi
done

使用方法

以下のコマンドを実行し、スクリプトを実行してください。

sh get-guardduty.sh

スクリプト実行ログをファイルとして保存する場合はリダイレクトを使用してください。

sh get-guardduty.sh > get-guardduty.log

出力結果

標準出力は以下のように表示されます。一目で設定値がわかるようになっています。

リージョン: ap-south-1
  DetectorのID: XXXXXXXXXXXXXXXXXXXXXXXXXXXX
  GuardDutyのステータス: ENABLED
  検出結果の更新頻度: FIFTEEN_MINUTES
  サービスロール: arn:aws:iam::XXXXXXXXXXX:role/aws-service-role/guardduty.amazonaws.com/AWSServiceRoleForAmazonGuardDuty
  信頼リストの数: 0
  脅威リストの数: 0
  発行先の有効化状況: PUBLISHING
    バケット名: arn:aws:s3:::XXXXXXXXXXX/Logs/GuardDuty
    バケットの暗号化: arn:aws:kms:ap-northeast-1:XXXXXXXXXXX:key/XXXXXXXXXXX
  S3保護の有効化状況: ENABLED
  EKS保護の有効化状況: DISABLED
  ランタイムモニタリングの有効化状況: ENABLED
    Amazon EKSのランタイム自動エージェントの状態: ENABLED
    AWS Fargate (ECS のみ)のランタイム自動エージェントの状態: DISABLED
    GuardDuty エージェント管理 (EC2)のランタイム自動エージェントの状態: ENABLED
  マルウェア保護の有効化状況: DISABLED
    マルウェア保護のスナップショットの有効化状況: NO_RETENTION
  RDS保護の有効化状況: DISABLED
  Lambda保護の有効化状況: DISABLED

 ~~~省略~~~

以下のようにリージョン毎にディレクトリが作成されます。

[cloudshell-user@ip-XX-XXX-XXX-XXX ~]$ ls -l
total 128
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 May 31 07:42 ap-northeast-1
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 May 31 07:42 ap-northeast-2
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 May 31 07:42 ap-northeast-3
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 May 31 07:41 ap-south-1
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 May 31 07:43 ap-southeast-1
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 May 31 07:43 ap-southeast-2
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 May 31 07:42 ca-central-1
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 May 31 07:43 eu-central-1
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 May 31 07:41 eu-north-1
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 May 31 07:42 eu-west-1
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 May 31 07:41 eu-west-2
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 May 31 07:41 eu-west-3
-rw-r--r--. 1 cloudshell-user cloudshell-user 15129 May 31 07:44 get-guardduty.log
-rw-r--r--. 1 cloudshell-user cloudshell-user  4221 May 31 07:40 get-guardduty.sh
-rw-r--r--. 1 cloudshell-user cloudshell-user 34023 May 31 07:53 get-guardduty.zip
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 May 31 07:42 sa-east-1
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 May 31 07:43 us-east-1
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 May 31 07:43 us-east-2
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 May 31 07:43 us-west-1
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 May 31 07:44 us-west-2
[cloudshell-user@ip-XX-XXX-XXX-XXX ~]$ 

以下のようにリージョンディレクトリ内にJSONが保存されます。

[cloudshell-user@ip-XX-XXX-XXX-XXX ~]$  ls -l ap-northeast-1/
total 12
-rw-r--r--. 1 cloudshell-user cloudshell-user  357 May 31 07:42 describe-publishing-destination_ap-northeast-1.json
-rw-r--r--. 1 cloudshell-user cloudshell-user 3455 May 31 07:42 get-detector_ap-northeast-1.json
-rw-r--r--. 1 cloudshell-user cloudshell-user  132 May 31 07:42 get-malware-scan-settings_ap-northeast-1.json
[cloudshell-user@ip-XX-XXX-XXX-XXX ~]$