> ## Documentation Index
> Fetch the complete documentation index at: https://cloudanix.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Waf global webacl not empty remediation

### Triage and Remediation

<Tabs>
  <Tab title="Remediation">
    ### Remediation

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        To remediate the misconfiguration of WAF Global Rules being empty in AWS CloudWatch, you can follow these step-by-step instructions using the AWS Management Console:

        1. **Sign in to the AWS Management Console**: Go to [https://aws.amazon.com/](https://aws.amazon.com/) and sign in to your AWS account using your credentials.

        2. **Navigate to AWS WAF**: In the AWS Management Console, search for "WAF" in the services search bar and click on "AWS WAF" to open the AWS WAF console.

        3. **Select the Web ACL**: In the AWS WAF console, click on "Web ACLs" in the left-hand navigation pane. Select the Web ACL that you want to configure the global rules for.

        4. **Add Global Rules**: Within the selected Web ACL, click on the "Rules" tab. Here you will see the list of rules configured for the Web ACL.

        5. **Add a New Rule**: Click on the "Add rules" button to add a new rule to the Web ACL.

        6. **Configure Global Rule**: In the rule configuration window, select the rule type as "Global Rule" from the drop-down menu.

        7. **Define Rule Criteria**: Define the criteria for the global rule based on your security requirements. This could include conditions like IP addresses, URI paths, query strings, etc.

        8. **Set Rule Action**: Choose the appropriate action to be taken when the global rule conditions are met. This could be to allow, block, or count the request.

        9. **Review and Save**: Review the configured global rule settings to ensure they align with your security policies. Once confirmed, click on the "Save" button to add the global rule to the Web ACL.

        10. **Verify Configuration**: After adding the global rule, ensure that it is listed under the rules section of the Web ACL and that it is properly configured.

        By following these steps, you can remediate the misconfiguration of empty WAF Global Rules in AWS CloudWatch and enhance the security of your web applications.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To remediate the misconfiguration of empty WAF Global Rules in AWS Cloud Watch using AWS CLI, you can follow these steps:

        1. List the current WAF Global Rules in AWS Cloud Watch using the following AWS CLI command:

        ```
        aws wafv2 list-web-acls --scope REGIONAL
        ```

        2. Identify the Web ACL that has empty global rules.

        3. Update the Web ACL by adding appropriate global rules. You can create a new global rule or update existing ones using the AWS CLI command. For example, to create a new global rule, you can use the following command:

        ```
        aws wafv2 create-rule-group --name "MyGlobalRule" --scope REGIONAL --capacity 100 --rules file://global_rules.json
        ```

        Make sure to replace "MyGlobalRule" with the desired name for the rule group and provide the rule definitions in the JSON file "global\_rules.json".

        4. Associate the newly created global rule with the Web ACL using the following AWS CLI command:

        ```
        aws wafv2 associate-web-acl --web-acl-arn <web_acl_arn> --web-acl-name <web_acl_name> --scope REGIONAL
        ```

        Replace `<web_acl_arn>` and `<web_acl_name>` with the ARN and name of the Web ACL that needs to be updated.

        5. Verify that the global rules are no longer empty by listing the Web ACLs again using the command in step 1.

        By following these steps, you can successfully remediate the misconfiguration of empty WAF Global Rules in AWS Cloud Watch using AWS CLI.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the misconfiguration of empty WAF Global Rules in AWS CloudWatch using Python, you can follow these steps:

        1. **Install Boto3**: Make sure you have Boto3 installed. You can install it using pip:
           ```
           pip install boto3
           ```

        2. **Set Up Boto3 Credentials**: Ensure that you have set up your AWS credentials for Boto3 to access your AWS account. You can set up your credentials using AWS CLI or environment variables.

        3. **Write Python Code**: Use the following Python code to associate a Global WebACL with a WAF:

        ```python theme={null}
        import boto3

        def associate_global_webacl_to_waf(web_acl_arn, waf_name):
            # Initialize the WAF client
            wafv2_client = boto3.client('wafv2')

            # Get the ARN of the resource associated with the WAF
            waf_resource_response = wafv2_client.get_web_acl(
                Name=waf_name
            )
            waf_resource_arn = waf_resource_response['WebACL']['ARN']

            # Associate the Global WebACL with the WAF
            response = wafv2_client.associate_web_acl(
                WebACLArn=web_acl_arn,
                ResourceArn=waf_resource_arn
            )

            print("Global WebACL associated with WAF successfully.")

        def main():
            # Specify the ARN of the Global WebACL
            web_acl_arn = 'arn:aws:wafv2:us-west-2:123456789012:global/webacl/ExampleGlobalWebACL'

            # Specify the name of the WAF to associate with
            waf_name = 'ExampleWAF'

            # Associate Global WebACL with WAF
            associate_global_webacl_to_waf(web_acl_arn, waf_name)

        if __name__ == "__main__":
            main()
        ```

        Replace `'arn:aws:wafv2:us-west-2:123456789012:global/webacl/ExampleGlobalWebACL'` with the ARN of your Global WebACL, and `'ExampleWAF'` with the name of the WAF you want to associate with.

        4. **Run the Script**: Execute the Python script. Upon successful execution, the Global WebACL will be associated with the specified WAF.

        This script will use Boto3 to call the `associate_web_acl` method of the WAFv2 client, passing in the ARNs of the Global WebACL and the WAF resource to perform the association.
      </Accordion>

      <Accordion title="Using Terraform">
        ```hcl theme={null}
        resource "aws_wafv2_web_acl" "GLOBAL_WEB_ACL" {
          name        = "GLOBAL_WEB_ACL_NAME"        # replace with your WAF Web ACL name
          description = "Global WAF Web ACL for CloudFront"
          scope       = "CLOUDFRONT"                 # global WAF

          default_action {
            allow {}
          }

          visibility_config {
            cloudwatch_metrics_enabled = true
            metric_name                = "GLOBAL_WEB_ACL_METRIC"  # replace with your metric name
            sampled_requests_enabled   = true
          }

          # Add at least one rule so the Web ACL is not empty
          rule {
            name     = "BLOCK_IPSET"   # replace with your rule name
            priority = 1

            action {
              block {}
            }

            statement {
              ip_set_reference_statement {
                arn = aws_wafv2_ip_set.BLOCKED_IPSET.arn
              }
            }

            visibility_config {
              cloudwatch_metrics_enabled = true
              metric_name                = "BLOCK_IPSET_METRIC"   # replace with your rule metric name
              sampled_requests_enabled   = true
            }
          }
        }

        resource "aws_wafv2_ip_set" "BLOCKED_IPSET" {
          name               = "BLOCKED_IPSET_NAME"  # replace with your IP set name
          description        = "Blocked IPs"
          scope              = "CLOUDFRONT"
          ip_address_version = "IPV4"

          addresses = [
            "203.0.113.0/24",  # replace with IP ranges you want to block
          ]
        }

        resource "aws_cloudfront_distribution" "CLOUDFRONT_DISTRIBUTION" {
          # existing distribution config ...
          enabled             = true
          default_root_object = "index.html"

          origin {
            domain_name = "ORIGIN_DOMAIN_NAME"   # replace with your origin
            origin_id   = "ORIGIN_ID"            # replace with your origin ID
          }

          default_cache_behavior {
            # existing behavior config ...
            target_origin_id       = "ORIGIN_ID"
            viewer_protocol_policy = "redirect-to-https"

            allowed_methods = ["GET", "HEAD"]
            cached_methods  = ["GET", "HEAD"]
          }

          restrictions {
            geo_restriction {
              restriction_type = "none"
            }
          }

          viewer_certificate {
            cloudfront_default_certificate = true
          }

          # Associate the (now non-empty) global Web ACL with CloudFront
          web_acl_id = aws_wafv2_web_acl.GLOBAL_WEB_ACL.arn
        }
        ```

        The finding cannot be remediated on the `aws-networking-ec2-network-acl` surface or via CloudWatch; it must be fixed by defining a non-empty `aws_wafv2_web_acl` (scope `CLOUDFRONT`) and associating it with your CloudFront distribution as shown. This change updates the CloudFront distribution in place but does not force its replacement.

        For verification, `terraform plan` should show:

        * creation (or update) of `aws_wafv2_web_acl.GLOBAL_WEB_ACL` with at least one `rule` block,
        * creation (or update) of `aws_wafv2_ip_set.BLOCKED_IPSET`,
        * an update to `aws_cloudfront_distribution.CLOUDFRONT_DISTRIBUTION` setting `web_acl_id` to the Web ACL ARN.
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>
