非AWS Organizations環境下でAWS Security Hubをマルチアカウント・全リージョン展開してみた

スクリプトを使ってAWS Security Hubをマルチアカウント・マルチリージョン展開してみた
2021.10.25

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

AWS Security HubはAWS Organizationsの利用に関係なく、 セキュリティマスターアカウントと監視されるメンバーアカウントを関連付けることができます。

AWS Organizationsを利用している場合は、次の記事のように、シームレスにSecurity Hubをセットアップできます。

今回は、AWS Organizationsを利用していない(できない)環境向けに、Security Hubをマルチアカウント・全リージョン展開する方法を紹介します。

ポイント

  • AWS Security HubはAWS Organizationsの利用によらずマスター・メンバーアカウント管理可能
  • マスター・メンバーアカウント間で招待・受諾のフローが必要
  • メンバーアカウントでは事前にAWS Security Hub・(Security Hubが依存する)AWS Configを有効化

※ 図は 20201013 AWS Black Belt Online Seminar AWS Security Hub から

これらをコンソールから操作するのは大変なため、AWSがGitHubで提供している次のスクリプトを利用して、マルチアカウント・全リージョン展開します。

GitHub - awslabs/aws-securityhub-multiaccount-scripts

やってみた

1. 作業用ロールの準備

本スクリプトは、マスター・メンバーアカウントそれぞれで作業(AWS Security Hub/AWS Configの有効化と招待・受諾が発生します。

呼び出し元は、各アカウントで IAM Role を assume して操作します。

次のポリシーを含む 同じ名前のロール を各AWSアカウント上に展開してください。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Condition": {
                "StringLike": {
                    "iam:AWSServiceName": [
                        "securityhub.amazonaws.com",
                        "config.amazonaws.com"
                    ]
                }
            },
            "Action": "iam:CreateServiceLinkedRole",
            "Resource": "*",
            "Effect": "Allow"
        },
        {
            "Action": "securityhub:*",
            "Resource": "*",
            "Effect": "Allow"
        },
        {
            "Action": [
                "config:DescribeConfigurationRecorders",
                "config:DescribeDeliveryChannels",
                "config:DescribeConfigurationRecorderStatus",
                "config:DeleteConfigurationRecorder",
                "config:DeleteDeliveryChannel",
                "config:PutConfigurationRecorder",
                "config:PutDeliveryChannel",
                "config:StartConfigurationRecorder"
            ],
            "Resource": "*",
            "Effect": "Allow"
        },
        {
            "Action": "iam:PassRole",
            "Resource": "arn:aws:iam::*:role/aws-service-role/config.amazonaws.com/AWSServiceRoleForConfig",
            "Effect": "Allow"
        },
        {
            "Action": [
                "s3:CreateBucket",
                "s3:PutBucketPolicy",
                "s3:ListBucket"
            ],
            "Resource": "arn:aws:s3:::config-bucket-*",
            "Effect": "Allow"
        }
    ]
}

2. ソースコードの取得

$ git clone https://github.com/awslabs/aws-securityhub-multiaccount-scripts.git
$ cd aws-securityhub-multiaccount-scripts

3. メンバーアカウント一覧をCSVファイル化

CSVファイルにメンバーアカウントのIDとセキュリティ緊急時の通知用メールアドレスを記載します。

accounts.csv

111,foo.111@example.com
222,foo.222@example.com

4. 実行

$ python3 enablesecurityhub.py \
  --master_account マスターアカウントID \
  --assume_role your-role-name \
  accounts.csv

全アカウント・リージョンで次のセキュリティ基準が有効化されます。

  • CIS AWS Foundations Benchmark v1.2.0
  • PCI DSS v3.2.1

Type : By InvitationEnabled 状態で 管理されていることがわかります。

スクリプトを要件に合わせる

リージョンを制限

セットアップするリージョンを制限したい場合、 --enabled_regions にリージョンのリストを渡します。

$ python3 enablesecurityhub.py \
  --master_account マスターアカウントID \
  --assume_role your-role-name \
  --enabled_regions us-east-1,ap-northeast-1 \
  accounts.csv

デフォルトでは全リージョン展開されます。

Security Standards をカスタマイズ

AWS Security Hub では、以下のセキュリティスタンダードが提供されています。

  • AWS 基礎セキュリティのベストプラクティス v1.0.0
  • CIS AWS Foundations Benchmark v1.2.0
  • PCI DSS v3.2.1

デフォルトでは、AWSとCISの2つが有効化されます。

提供されているスクリプトでは、デフォルトの2つが必ず有効化されます。

3つとも有効化したい

3つとも有効したい場合は --enable_standards に PCI の ARN を渡します。

$ python3 enablesecurityhub.py \
  --master_account マスターアカウントID \
  --assume_role your-role-name \
  --standards-control-arn standards/pci-dss/v/3.2.1 \
  accounts.csv

個別に有効化したい

AWSあるいはCISのスタンダードだけを有効化したい場合のように、デフォルトで有効化されるスタンダードの一部が不要なケースがあります。

disablesecurityhub.py を実行して、不要なスタンダードを消し込むことで対応します。

また、次の PR を適用することで、明示的に指定したスタンダードだけを有効にすることもできます。

Enable standards only when explicitly specified by ssteo · Pull Request #47 · awslabs/aws-securityhub-multiaccount-scripts · GitHub

この PR をマージすると、次の様にすれば、AWS ベストプラクティスだけを有効化できます。

$ python3 enablesecurityhub.py \
  --master_account マスターアカウントID \
  --assume_role your-role-name \
  --enable_standards standards/aws-foundational-security-best-practices/v/1.0.0 \
  accounts.csv

AWS CLI だと、次のコマンドに相当する動作です。

$ aws securityhub enable-security-hub --no-enable-default-standards
$ aws securityhub batch-enable-standards \
  --standards-subscription-requests '{"StandardsArn":"standards/aws-foundational-security-best-practices/v/1.0.0"}'

実行ログのサンプル

最後に、実行ログのサンプルを紹介します。

$ python3 enablesecurityhub.py \
  --master_account マスターアカウントID \
  --assume_role your-role-name \
  --enabled_regions us-west-2 \
  --enable_standards standards/aws-foundational-security-best-practices/v/1.0.0 \
  accounts.csv

        *****************************************************************************************************************************************************************************************
        *      By turning on this Standards you will enable security evaluations to run. For current pricing and example scenarios please refer to the current AWS Security Hub pricing.        *
        *      Important: You must enable AWS Config for all resources in each AWS Region where you will be running a Standard. If Config is not already enabled it will be enabled and         *
        *      configured in each region.                                                                                                                                                       *
        *                                                                                                                                                                                       *
        *      In addition to AWS Security Hub charges, you will also incur charges for the Configuration Items recorded by AWS Config, as per the AWS Config pricing. These charges are        *
        *      separate from (and not included in) AWS Security Hub pricing.                                                                                                                    *
        *****************************************************************************************************************************************************************************************

        Continue?(yes/no):

yes
Enabling members in these regions: ['us-west-2']
Enabling the following Security Hub Standards for enabled account(s) and region(s): ['standards/aws-foundational-security-best-practices/v/1.0.0']
Assumed session for マスターアカウントID.
Finished enabling stanard arn:aws:securityhub:us-west-2::standards/aws-foundational-security-best-practices/v/1.0.0 on account マスターアカウントID for region us-west-2
Assumed session for メンバーアカウントID.
Beginning メンバーアカウントID in us-west-2
Finished enabling stanard arn:aws:securityhub:us-west-2::standards/aws-foundational-security-best-practices/v/1.0.0 on account メンバーアカウントID for region us-west-2
Added Account メンバーアカウントID to member list in SecurityHub master account マスターアカウントID for region us-west-2
Invited Account メンバーアカウントID to SecurityHub master account マスターアカウントID in region us-west-2
Accepting Account メンバーアカウントID to SecurityHub master account マスターアカウントID in region us-west-2
Finished メンバーアカウントID in us-west-2

最後に

AWS Security Hubをマルチアカウント運用する場合、各リージョンでのSecurity Hubの有効化と関連付けをいかに楽にセットアップするかがポイントです。

運用中のアカウントでAWS Security Hubが有効化されていない場合、紹介したスクリプトを走らせるだけでかんたんにセットアップできます。

AWS Organizationsを利用している場合は、以下のブログを参照ください。

参考