How do I use AWS WAF to block HTTP requests that don't contain a User-Agent header?
Last updated: 2019-12-06
I want to block HTTP requests that don't have a User-Agent header or have an empty User-Agent header value in the request. How do I use AWS WAF to block these requests?
Short Description
By default, AWS WAF filters don't check the HTTP request parameter. However, you can create a rule with one of the following conditions to check HTTP requests:
Resolution
Method #1: Create a rule with a Regex matching condition
First, create the Regex matching condition:
- Open the AWS WAF console.
- In the navigation pane, choose String and Regex matching.
- Choose Create condition.
- For Name, enter UA-condition.
- For Region, choose the Region where you created your web access control list (web ACL).
Note: Select Global if your web ACL is set up for Amazon CloudFront. - For Type, choose Regex match.
- For Part of the request to filter on, choose Header.
- For Header, choose User-Agent.
- For Transformation, choose None.
- For Regex patterns to match to request, keep the default selection of Create regex pattern set.
- For New pattern set name, enter testpattern.
- Enter a regular expression .+ and choose the plus (+) symbol.
Note: The regular expression (regex) matches any character except a line terminator. - Choose Create pattern set and add filter.
- Choose Create.
Then, create a rule and add the condition to it:
- In the navigation pane, choose Rules.
- Choose Create rule.
- For Name, enter UA-Rule.
Note: The Amazon CloudWatch metric name automatically populates based on your entry in the Name field. - For Rule type, choose Regular rule.
- For Region, choose the Region where you created your web ACL.
Note: Select Global if your web ACL is set up for CloudFront. - For Add conditions, choose does not and match at least one of the filters in the string match condition.
- Choose UA-condition from the condition drop-down menu.
- Choose Create.
Finally, add this rule to your web ACL:
- In the navigation pane, choose Web ACLs.
- Choose the name of your web ACL.
- Select the Rules tab and choose Edit web ACL.
- For Rules, choose UA-Rule.
- Choose Add rule to web ACL.
- Confirm that Block is selected for Action.
- For Default action, choose Allow all requests that don't match any rules.
- Choose Update.
Method #2: Create a rule with a Size constraint condition
Note: By default, the number of pattern sets per account is 5. If you've exceeded this AWS WAF limit, you can use the following size constraint solution.
First, create the Size constraint condition:
- Open the AWS WAF console.
- In the navigation pane, choose Size constraints.
- Choose Create condition.
- For Name, enter UA-condition2.
- For Region, choose the Region where you created your web ACL.
Note: Select Global if your web ACL is set up for CloudFront. - For Part of the request to filter on, choose Header.
- For Header, enter User-Agent.
- For Comparison operator, choose Greater than or equal.
- For Size (Bytes), keep the default value of 0.
- For Transformation, choose None.
- Choose Add filter.
- Choose Create.
Then, create a rule and add the condition to it:
- In the navigation pane, choose Rules.
- Choose Create rule.
- For Name, enter UA-Rule2.
Note: The CloudWatch metric name automatically populates based on your entry in the Name field. - For Rule type, choose Regular rule.
- For Region, choose the Region where you created your web ACL.
Note: Select Global if your web ACL is set up for CloudFront. - For Add conditions, choose does not and match at least one of the filters in the size constraint condition.
- Choose UA-condition2 from the condition drop-down menu.
- Choose Create.
Finally, add this rule to your web ACL:
- In the navigation pane, choose Web ACLs.
- Choose the name of your web ACL.
- Select the Rules tab and choose Edit web ACL.
- For Rules, choose UA-Rule.
- Choose Add rule to web ACL.
- Confirm that Block is selected for Action.
- For Default action, choose Allow all requests that don't match any rules.
- Choose Update.
Did this article help you?
Anything we could improve?
Need more help?