複数リージョンの GuardDuty に対して検出結果のエクスポート設定を一括で行うスクリプトを作った

一括でできると気持ちいい
2021.03.09

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、大前です。

複数リージョンで GuardDuty が有効化されている環境に対して検出結果の S3 エクスポート設定を実施する機会がありました。 一方で、複数リージョンに対して手でポチポチ設定していくのはなかなか大変なので、一発で複数リージョンに対して検出結果のエクスポート設定を行うスクリプトを作ってみました。

ご参考の上、よければお使いください。

前提知識

そもそも「GuardDuty の検出結果エクスポートって何?」な方は以下ブログを参照ください。

作成したスクリプト

実際に作成したスクリプトは以下です。作成に当たっては、こちら のブログを参考にしました。

エクスポート先の S3 バケットや、エクスポート設定に必要な KMS キーについては環境に合わせて修正してください。

# 変数
FINDING_PUBLISHING_FREQ=FIFTEEN_MINUTES # 15分ごとにエクスポート(推奨)
DESTINATION_TYPE=S3 # 固定
DESTINATION_ARN=arn:aws:s3:::sample-target-bucket # エクスポート先 S3 バケット
KMS_KEY_ARN=arn:aws:kms:ap-northeast-1:0123456789:key/xxxxxxxxx # エクスポート時に使用する KMS キー

# Region 一覧
regions=(\
"ap-northeast-1" \
"ap-northeast-2" \
"ap-east-1" \
"ap-south-1" \
"ap-southeast-1" \
"ap-southeast-2" \
"af-south-1" \
"ca-central-1" \
"eu-central-1" \
"eu-north-1" \
"eu-south-1" \
"eu-west-1" \
"eu-west-2" \
"eu-west-3" \
"me-south-1" \
"sa-east-1" \
"us-east-1" \
"us-east-2" \
"us-west-1" \
"us-west-2"\
)

for r in ${regions[*]}
do
dtid=`aws guardduty list-detectors --query 'DetectorIds' --region $r --output text`
# S3 へのエクスポート頻度変更
echo "running... aws guardduty update-detector --detector-id $dtid --finding-publishing-frequency $FINDING_PUBLISHING_FREQ --region $r"
aws guardduty update-detector --detector-id "${dtid}" \
    --finding-publishing-frequency "${FINDING_PUBLISHING_FREQ}" \
    --region "${r}"
# S3 へのエクスポート設定
echo "running... aws guardduty create-publishing-destination --detector-id $dtid --destination-type $DESTINATION_TYPE --destination-properties DestinationArn=$DESTINATION_ARN,KmsKeyArn=$KMS_KEY_ARN --region $r"
aws guardduty create-publishing-destination --detector-id "${dtid}" \
    --destination-type "${DESTINATION_TYPE}" \
    --destination-properties DestinationArn="${DESTINATION_ARN}",KmsKeyArn="${KMS_KEY_ARN}" \
    --region "${r}"
done

内部で実行している CLI のドキュメントものせておきますので、改修する際の参考にしてください。

実行してみる

実際にスクリプトを実行してみます。今回は、東京リージョンとバージニア北部で GuardDuty を有効化したアカウントでスクリプトを実行します。

$ sh {作成ファイル}.sh

実行時、以下のログが出ていれば対象のリージョンでエクスポート設定が成功しています。

running... aws guardduty update-detector --detector-id xxxxxxx --finding-publishing-frequency FIFTEEN_MINUTES --region ap-northeast-1
running... aws guardduty create-publishing-destination --detector-id xxxxxxx --destination-type S3 --destination-properties DestinationArn=arn:aws:s3:::sample-target-bucket,KmsKeyArn=arn:aws:kms:ap-northeast-1:0123456789:key/xxxxxxxxx --region ap-northeast-1
{
    "DestinationId": "xxxxxxxxxxx"
}

GuardDuty が有効化されていないリージョンや、オプトインされていないリージョン等に対してはエラーメッセージが出ますが、更新が可能なリージョンに対しては一通り設定更新が行われます。

スクリプトの実行後、GuardDuty の画面を確認するとエクスポート設定が入っている事が確認できます。

CLI で確認したい場合は、以下スクリプトで確認可能です。

# Region 一覧
regions=(\
"ap-northeast-1" \
"ap-northeast-2" \
"ap-east-1" \
"ap-south-1" \
"ap-southeast-1" \
"ap-southeast-2" \
"af-south-1" \
"ca-central-1" \
"eu-central-1" \
"eu-north-1" \
"eu-south-1" \
"eu-west-1" \
"eu-west-2" \
"eu-west-3" \
"me-south-1" \
"sa-east-1" \
"us-east-1" \
"us-east-2" \
"us-west-1" \
"us-west-2"\
)

for r in ${regions[*]}
do
# DetectorId 取得
dtid=`aws guardduty list-detectors --query 'DetectorIds' --region $r --output text`
if [ -z $dtid ]; then
continue
fi
# DestinationId 取得
destid=`aws guardduty list-publishing-destinations --detector-id $dtid --query 'Destinations[*].DestinationId' --region $r --output text`
# エクスポート設定取得
aws guardduty describe-publishing-destination --detector-id ${dtid} --destination-id ${destid} --region ${r}
done

おわりに

複数リージョンの GuardDuty に対して一括で検出結果のエクスポート設定を行うスクリプトを紹介しました。 よければご活用ください。

以上、AWS 事業本部の大前でした。

参考