GuardDutyのOrganizations全体有効化のために委任と自動有効化の設定をするPythonスクリプト書いた

GuardDutyをOrganizations全体で有効化するときに使ってください。
2021.12.23

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

こんにちは、臼田です。

みなさん、GuardDuty有効化してますか?(挨拶

脅威検知のサービスであるAmazon GuardDutyは全てのAWSアカウントと全てのリージョンで有効化するサービスです。AWS Organizationsと連携して管理できるので、その有効化をPythonでサクッと出来るようにスクリプトを書きました。

概要

だいたい以下にあります。

上記手順は半自動化されていますが、私はもっとめんどくさがりなので全部自動化しました。

スクリプト

今回は2本立てです。

1つはOrganizationsの管理アカウントで実行する、委任と管理アカウント自体のGuardDuty有効化です。

もう1つは委任先のセキュリティ管理アカウントで実行する、Organizations全体の有効化です。

管理アカウント用スクリプト

import boto3


admin_account = "999999999999"

ec2_client = boto3.client('ec2')
regions = list(map(lambda x: x['RegionName'],
                   ec2_client.describe_regions()['Regions']))

for region in regions:
    try:
        print("## " + region)
        gd = boto3.client('guardduty', region_name=region)
        gd.enable_organization_admin_account(
            AdminAccountId=admin_account
        )
        gd.create_detector(
            Enable=True,
            FindingPublishingFrequency='SIX_HOURS',
            DataSources={
                'S3Logs': {
                    'Enable': True
                }
            }
        )
    except Exception as e:
        print("## Error Occurred")
        print(e)


# disable organization delegation
# for region in regions:
#     try:
#         print("## " + region)
#         gd = boto3.client('guardduty', region_name=region)
#         admin_account = gd.list_organization_admin_accounts()['AdminAccounts'][0]['AdminAccountId']
#         gd.disable_organization_admin_account(
#             AdminAccountId = admin_account
#         )
#     except Exception as e:
#         print("## Error Occurred")
#         print(e)

admin_account委任先のAWSアカウントIDを入れたら動きます。CloudShellから使うと手っ取り早いかと。

全てのリージョンで委任を行い、自身のGuardDutyも有効化しています。個別に有効化しているのは、管理アカウントについては委任先からメンバーに追加する際に、GuardDutyが有効になっている必要があるからです。

おまけに検証で使った委任を取り消すスクリプトもつけておきました。

セキュリティ管理アカウント用スクリプト

import boto3


email = "example@example.com"

account_id = boto3.client('sts').get_caller_identity()['Account']

ec2_client = boto3.client('ec2')
regions = list(map(lambda x: x['RegionName'],
                   ec2_client.describe_regions()['Regions']))

# create organizations accounts list
organizations = boto3.client('organizations')
accounts = list(
    map(lambda x: x['Id'], organizations.list_accounts()['Accounts']))
account_details = []
for account in accounts:
    account_details.append({
        'AccountId': account,
        'Email': email
    })
# delete owner account
accounts.remove(account_id)

for region in regions:
    try:
        print("## " + region)
        gd = boto3.client('guardduty', region_name=region)
        did = gd.list_detectors()['DetectorIds'][0]
        # enable auto enable
        gd.update_organization_configuration(
            DetectorId=did,
            AutoEnable=True,
            DataSources={
                'S3Logs': {
                    'AutoEnable': True
                }
            }
        )
        # add all accounts
        gd.create_members(
            DetectorId=did,
            AccountDetails=account_details
        )
    except Exception as e:
        print("## Error Occurred")
        print(e)

メールアドレスはexampleを入れていますが、これはもともと何でもいいので、そのまま実行して問題ありません。

AWSアカウントが増えたときに自動的にGuardDutyを有効化する設定を有効にし、既存の全てのAWSアカウントをメンバーに追加しています。

まとめ

GuardDutyをOrganizations全体で有効化するときに役立つスクリプトを作りました。

はかどりますね。