Question: How do I use AWS WAF to block HTTP requests that don't contain a User-Agent header?

I want to restrict HTTP requests that don't contain a specific User-Agent or contain a specific User-Agent header value in the request.

Short descriptionBy default, AWS WAF filters don't check whether the HTTP request parameters are present or not. To check whether the HTTP request parameters are present or not, do the following:Block requests that don't contain a User-Agent header using AWS Managed Rules.-or-Block requests that don't contain a User-Agent header or block traffic if the requests contain a specific User-Agent using custom rules.

ResolutionBlock requests that don’t contain a User-Agent header using AWS Managed RulesUse AWS Managed Rules for AWS WAF to block HTTP requests that don't contain a User-Agent header without having to write your own rules.Note: Managed Rules are subject to version changes and expiration. For more information, see Best practices for handling managed rule group versions.The following rules inspect requests missing the HTTP User-Agent header and User-Agent strings that don't seem to be from a web browser:NoUserAgent_HEADERThis rule is from the Core rule set (CRS) managed rule group. This rule inspects for requests that are missing the HTTP User-Agent header.SignalNonBrowserUserAgentThis rule is from the AWS WAF Bot Control rule group. This rule inspects for User-Agent strings that don't seem to be from a web browser including requests with no User-Agent.To add an AWS Managed Rule group to your web ACLOpen the AWS WAF console.In the navigation pane, under AWS WAF, choose Web ACLs.For Region, choose the AWS Region where you created your web ACL.Note: Select Global if your web ACL is set up for Amazon CloudFront.Select your web ACL.Choose Rules, and then choose Add Rules. Select Add managed rule groups from the dropdown.Expand the AWS managed rule groups section.Locate the rule group and turn on Add to web ACL.For example, you can turn on Add to web ACL for Core rule set that contains the NoUserAgent_HEADER rule.(Optional) Choose Edit to view and modify the settings for the rule group.Choose Add rules.Choose Save.To edit an existing AWS Managed Rule group in your web ACLOpen the AWS WAF console.In the navigation pane, under AWS WAF, choose Web ACLs.For Region, choose the AWS Region where you created your web ACL.Note: Select Global if your web ACL is set up for Amazon CloudFront.Select your web ACL.Choose Rules, and then choose Edit to view and modify the settings.Note: For additional information on editing settings, see Working with managed rule groups.When you finish editing, choose Save.If you get false-positive scenarios with AWS Managed Rules rule groups, see AWS Managed Rules for AWS WAF.Block requests that don’t contain a User-Agent header using custom rulesTo block requests that don't contain a User-Agent header using a custom rule, do the following:Open the AWS WAF console.In the navigation pane, under AWS WAF, choose Web ACLs.For Region, select the AWS Region where you created your web ACL.Note: Select Global if your web ACL is set up for Amazon CloudFront.Select your web ACL.Choose Rules, and then choose Add Rules, Add my own rules and rule groups.Choose Rule Builder for the Rule type.For Name, enter a name to identify this rule.For Type, choose Regular rule.For If a request, choose doesn't match the statement (NOT).On Statement, for Inspect, choose Single header.For Header field name, enter a name for the field. For example: User-Agent.For Match type, choose Size greater than.For Size in bytes, enter 0 (zero).(Optional) Choose a Text transformation or choose None.For Action, choose Block.Choose Add Rule.(Optional) For Set Rule Priority, select your rule and move its priority. The rules are processed in the order that they appear. For more information, see Processing order of rules and rule groups in a web ACL.Choose Save.Block traffic if the requests contain a specific User-Agent using custom rulesTo block traffic if the requests contain a specific User-Agent using a custom rule, do the following:Open the AWS WAF console.In the navigation pane, under AWS WAF, choose Web ACLs.For Region, select the AWS Region where you created your web ACL.Note: Select Global if your web ACL is set up for Amazon CloudFront.Select your web ACL.Choose Rules, and then choose Add Rules, Add my own rules and rule groups.Choose Rule Builder for the Rule type.For Name, enter a name to identify this rule.For Type, choose Regular rule.For If a request, choose matches a statement.On Statement, for Inspect, choose Single header.For Header field name, enter a name for the field. For example: User-Agent.For Match type, choose Contains string.For String to match, enter the value for your specific User-Agent to block.(Optional) Choose a Text transformation or choose None.For Action, choose Block.Choose Add Rule.(Optional) For Set Rule Priority, select your rule and move its priority. The rules are processed in the order that they appear. For more information, see Processing order of rules and rule groups in a web ACL.Choose Save.