Programming Memo

RailsとかAWSとか色々

テンプレートを利用してWAFのACLを作成する

テンプレートを用いたAccess Controll Listの作成

AWS WAFの英語版ドキュメントに公開されているURLを指定してCloudFormationのstackを作成すると、WAFにACLのサンプルを作成できる。 Tutorial: Quickly Setting Up AWS WAF Protection Against Common Attacks

On the Select Template page, choose Specify an Amazon S3 template URL. For the template URL, enter https://s3.amazonaws.com/cloudformation-examples/community/common-attacks.json

作成されるRuleとConditionは以下の通り。

ACL: CommonAttackProtection

  • CommonAttackProtectionSqliRule

    • URI contains SQL injection threat after decoding as URL.
    • Query string contains SQL injection threat after decoding as URL.
    • Query string contains SQL injection threat after decoding as HTML tags.
    • Body contains SQL injection threat after decoding as URL.
    • Body contains SQL injection threat after decoding as HTML tags.
  • CommonAttackProtectionManualIPBlockRule

    • There are no IP addresses in this IP match condition.
  • CommonAttackProtectionXssRule

    • Body contains a cross-site scripting threat after decoding as URL.
    • Query string contains a cross-site scripting threat after decoding as URL.
    • Body contains a cross-site scripting threat after decoding as HTML tags.
    • Query string contains a cross-site scripting threat after decoding as HTML tags.
    • URI contains a cross-site scripting threat after decoding as URL.
  • CommonAttackProtectionLargeBodyMatchRule

    • The length of the Body is greater than 8192.

削除する場合は、stackをdeleteすればACL以下のすべてのオブジェクトがまとめて削除される。

費用

このACLで発生する費用は 9USD / Month (ACL 5USD × 1) + (Rule 1USD × 4) = 9USD 料金 - AWS WAF | AWS - Amazon Web Services

AWS CLI

ここから先はAWS CLIで登録する手順。

まず、公式ドキュメントにあるStackを作成するためのサンプルポリシーをjsonで保存するか、自作でPolicy Documentを作成する。 AWS Identity and Access Management によるアクセスの制御 今回は以下の自作のPolicy Documentを利用する。

{
    "Version":"2016-09-20",
    "Statement":[{
        "Effect":"Allow",
        "Action":[
            "cloudformation:*"
        ],
        "Resource":"*"
    }]
}

IAMのポリシーがアタッチされたユーザーでstack作成用のポリシーを作成する。 create-policyメソッドで--policy-documentに先ほどのjsonを指定する。 同時に作成したポリシーのARNを取得しておく。

$ CF_POLICY_ARN=$( \
$   aws iam create-policy \
$     --policy-name AmazonCloudFormationFullAccess \
$     --policy-document file://cloudformation_fullaccess_policy.json \
$     --query 'Policy.Arn' \
$     --output text \
$ )
$ echo $CF_POLICY_ARN

上記のポリシーをユーザーにアタッチする。

$ aws iam attach-user-policy \
$   --user-name <USER_NAME> \
$   --policy-arn ${CF_POLICY_ARN}

続いて、ユーザーにWAFFullAccessポリシーをアタッチする。 あらかじめアタッチしておかないと、stackを作成する際にAccessDeniedExceptionでエラーになるので注意。

$ aws iam attach-user-policy \
$   --user-name <USER_NAME> \
$   --policy-arn 'arn:aws:iam::aws:policy/AWSWAFFullAccess'

stackを作成する。 10分ほど時間がかかるので放置。

$ aws cloudformation create-stack \
$   --stack-name 'commonattackprotection' \
$   --template-url 'https://s3.amazonaws.com/cloudformation-examples/community/common-attacks.json'

以下、作成されたWAFのオブジェクトを確認するコマンドの一覧。

# 全オブジェクト
$ aws cloudformation list-stack-resources
$ aws cloudformation describe-stack-resources
# ACL
$ aws waf list-web-acls
# Rule
$ aws waf list-rules
# Condition
$ aws waf list-byte-match-sets
$ aws waf list-size-constraint-sets
$ aws waf list-xss-match-sets
$ aws waf list-ip-sets
$ aws waf list-sql-injection-match-sets

不要になったらstackをdeleteする。

$ aws cloudformation delete-stack --stack-name 'commonattackprotection'