デフォルトセキュリティグループ
CIS AWS Foundations Benchmark というセキュリティガイドラインが公開されており、このガイドラインは、AWSアカウントをセキュアに保つために必要なAWSのセキュリティ設定を集めたベストプラクティス集として活用できる。
この CIS AWS Foundations Benchmark
では、デフォルトセキュリティグループについて、以下の設定とすることが奨励されている。
- 4.3 IAM すべての VPC のデフォルトセキュリティグループがすべてのトラフィックを制限するようにします
この CIS AWS Foundations Benchmark
に 準拠していないデフォルトセキュリティグループ を SSM Automation
を用いて 自動修復 するために以下の設定を行う。
- 上記のポリシーに準拠しているか
AWS Config
を用いて定期的にチェックを行う - 非準拠であった場合には、
AWS Config
がSSM Automation
を自動起動する SSM Automation
が非準拠のデフォルトセキュリティグループの設定を自動修復する
1. AWS Configの有効化
AWS Config
を有効化する手順については、こちら。
2. AWS Configを用いた定期チェック
デフォルトセキュリティグループ設定のチェックには、あらかじめAWS Config
に用意されている vpc-default-security-group-closed
マネージドルールを使用する。デフォルトセキュリティグループがこの条件を満たしていない場合、このリソースはルールに 非準拠(NON_COMPLIANT) であると判定される。
なお、この Config Rule
を設定する前に ConfigurationRecorder
を生成しておく必要がある。そこで、DependsOn
属性に ConfigurationRecorder
リソースを設定している。
Resources: ConfigVpcDefaultSecurityGroupClosed: DependsOn: - ConfigConfigurationRecorder Type: 'AWS::Config::ConfigRule' Properties: ConfigRuleName: vpc-default-security-group-closed Description: いずれの Amazon Virtual Private Cloud (VPC) のデフォルトのセキュリティグループでもインバウンドとアウトバウンドのいずれのトラフィックも許可しないことを確認します。 Source: Owner: AWS SourceIdentifier: VPC_DEFAULT_SECURITY_GROUP_CLOSED
3. SSM Automation を用いた自動修復
Systems Manager Automation
は、AWS Config
で 直接指定できる、現時点で唯一の自動修復手段 となっている。そこで、デフォルトのセキュリティグループを修復する Systems Manager Automation
ドキュメント を作成し、AWS Config
との紐付けを行う。
下のSystems Manager Automation
ドキュメントは、EC2
の RevokeSecurityGroupIngress
, RevokeSecurityGroupEgress
, DescribeSecurityGroups
を実行して、デフォルトセキュリティグループの設定を自動修復する。
Resources: SSMAutomationRevokeDefaultSecurityGroup: Type: 'AWS::SSM::Document' Properties: Content: schemaVersion: "0.3" assumeRole: "{{ AutomationAssumeRole }}" description: Revoke Default Security Group. mainSteps: - name: DescribeSecurityGroups action: aws:executeAwsApi onFailure: Abort inputs: Service: ec2 Api: DescribeSecurityGroups GroupIds: ["{{ GroupId }}"] outputs: - Name: IpPermissionsIngress Selector: $.SecurityGroups[0].IpPermissions Type: MapList - Name: IpPermissionsEgress Selector: $.SecurityGroups[0].IpPermissionsEgress Type: MapList - name: RevokeSecurityGroupIngress action: aws:executeAwsApi onFailure: Continue inputs: Service: ec2 Api: RevokeSecurityGroupIngress GroupId: "{{ GroupId }}" IpPermissions: "{{ DescribeSecurityGroups.IpPermissionsIngress }}" - name: RevokeSecurityGroupEgress action: aws:executeAwsApi onFailure: Continue inputs: Service: ec2 Api: RevokeSecurityGroupEgress GroupId: "{{ GroupId }}" IpPermissions: "{{ DescribeSecurityGroups.IpPermissionsEgress }}" parameters: AutomationAssumeRole: type: String description: Automation Assume Role Arn GroupId: type: String description: Group Id DocumentType: Automation
4. AWS Config と SSM Automation の紐付け
Systems Manager Automation
ドキュメントは、上述の通り IAM
の RevokeSecurityGroupIngress
などを実行する必要があるため、このAWS API アクションを Systems Manager Automation
から呼び出すことを可能とする IAM Role を作成する。
Resources: IAMRoleForSSM: Type: 'AWS::IAM::Role' Properties: AssumeRolePolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Principal: Service: ssm.amazonaws.com Action: 'sts:AssumeRole' Description: A role required for SSM to access IAM. Policies: - PolicyName: !Sub '${PrefixOfLogicalName}-AWSSystemManagerIAMRole-${AWS::Region}' PolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Action: - 'ec2:RevokeSecurityGroupIngress' - 'ec2:RevokeSecurityGroupEgress' - 'ec2:DescribeSecurityGroups' Resource: - '*' RoleName: !Sub '${AWS::StackName}-SSM-${AWS::Region}'
このIAM RoleのARNは、AWS Config
から Systems Manager Automation
へ渡されるパラメータの1つとして規定される。AWS::Config::RemediationConfiguration
は、非準拠(NON_COMPLIANT)と判定された場合の自動修復方法を規定し、Config Rule
と Systems Manager Automation
との紐付けや、受け渡されるパラメータの規定を行う。自動修復を行う場合は、AutomationAssumeRole, MaximumAutomaticAttempts, RetryAttemptSeconds の各パラメータの入力が必須である。
Resources: ConfigVpcDefaultSecurityGroupClosedRemediationConfiguration: Condition: CreateRemediationResources Type: 'AWS::Config::RemediationConfiguration' Properties: Automatic: true ConfigRuleName: !Ref ConfigVpcDefaultSecurityGroupClosed MaximumAutomaticAttempts: 1 Parameters: AutomationAssumeRole: StaticValue: Values: - !GetAtt IAMRoleForSSM.Arn GroupId: ResourceValue: Value: RESOURCE_ID RetryAttemptSeconds: 30 TargetId: !Ref SSMAutomationRevokeDefaultSecurityGroup TargetType: SSM_DOCUMENT
以上で、デフォルトセキュリティグループからインバウンドおよびアウトバンドの許可ルールを削除することができた。