Lesson 1 of 1 AWS WAF

Filtering Malicious Web Requests with AWS WAF

This lesson explains how AWS WAF filters HTTP requests before they reach your application, blocking common attacks and abusive traffic.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify the resources a web ACL can protect and what its rules inspect.
  • Distinguish terminating and non-terminating rule actions, including Count.
  • Explain how managed rule groups and logging are used, and why new rules are tested in Count mode.

Web ACLs and rules

You attach a web ACL to a CloudFront distribution, an Application Load Balancer, an API Gateway REST API, an AppSync API or a Cognito user pool. Its rules inspect:

  • IP addresses and countries.
  • Headers and body content.
  • SQL injection and cross-site scripting patterns.
  • Request rates, with rate-based rules that block clients sending too many requests.

The following diagram shows a web ACL inspecting requests before they reach the application.

AWS WAF in front of an application. A web ACL attached to CloudFront, an Application Load Balancer, an API Gateway REST API, an AppSync API or a Cognito user pool inspects each request with rules for IP addresses and countries, headers and body, SQL injection and cross-site scripting, and request rates. Allowed requests reach the application; blocked requests do not.
Because the web ACL sits in front of the resource, blocked requests consume none of the application's capacity.

Rule actions

A web ACL evaluates its rules in priority order, and each rule names the action to take when a request matches. Allow and Block are terminating actions: evaluation stops, and the request is either forwarded to the application or rejected, by default with HTTP 403, or with a custom response. A request that matches no rule receives the web ACL's default action.

Count records that the request matched, in metrics and logs, and then continues with the next rule, so it never affects traffic. Deploying a new rule in Count mode first shows which real requests it would block before it is switched to Block. CAPTCHA presents a puzzle that a person must solve before the request proceeds, and Challenge runs a silent check in the browser that confirms it is a real browser rather than a script; both suit endpoints that bots target, such as login pages. The following table summarises the actions.

ActionEffect
Allow, BlockLet the request through or stop it
CountRecord matches without affecting traffic: the safe way to test a new rule
CAPTCHA, ChallengeCheck that a request comes from a human or a real browser

Labels added by one rule can be matched by later rules.

Example. Block any IP sending more than 100 requests to /login in five minutes, without affecting the rest of the site.

{
  "Name": "limit-login",
  "Priority": 1,
  "Action": {"Block": {}},
  "Statement": {
    "RateBasedStatement": {
      "Limit": 100,
      "EvaluationWindowSec": 300,
      "AggregateKeyType": "IP",
      "ScopeDownStatement": {
        "ByteMatchStatement": {
          "FieldToMatch": {"UriPath": {}},
          "PositionalConstraint": "STARTS_WITH",
          "SearchString": "/login",
          "TextTransformations": [{"Priority": 0, "Type": "NONE"}]
        }
      }
    }
  },
  "VisibilityConfig": {"SampledRequestsEnabled": true, "CloudWatchMetricsEnabled": true, "MetricName": "limit-login"}
}
True or false

How rules act

Exam skills 2.1.2 · 1.1.4

Check four facts about how AWS WAF evaluates rules.

Log in to try this activity Interactive activities are free with an account.

Managed rules and logging

Writing rules for every known attack is impractical, so AWS WAF offers managed rule groups that AWS keeps up to date. A rule group is added to a web ACL like a single rule, and individual rules inside it can be overridden, for example set to Count when they block legitimate traffic.

The core rule set blocks common web exploits, such as those in the OWASP Top Ten, and the known bad inputs group blocks request patterns associated with known vulnerabilities; further free groups cover SQL injection, IP reputation and anonymous proxies. Bot Control identifies and manages bot traffic, allowing known good bots such as search engines while blocking scrapers. Account Takeover Prevention inspects sign-in requests and blocks credential stuffing, in which attackers try stolen usernames and passwords, and Account Creation Fraud Prevention detects fake sign-ups. These last three are charged in addition to standard WAF fees. The following table summarises the principal groups.

Managed rule groupProtects against
Core rule set, known bad inputsCommon exploits, with no signatures to write
Bot ControlUnwanted bots
Account Takeover PreventionCredential stuffing on login pages
Account Creation Fraud PreventionFake sign-ups

WAF logs record every inspected request with the rule that matched, which shows why a request was blocked. They can be sent to CloudWatch Logs, S3 or Firehose for analysis.

Sort into groups

Which protection?

Exam skills 2.1.2

Choose the AWS WAF protection for each threat.

Log in to try this activity Interactive activities are free with an account.
Worked example

A new rule that blocks real customers

A developer adds a managed SQL injection rule group in Block mode on a Friday. On Monday, support reports that customers cannot save addresses containing apostrophes, such as "O'Connor Street".

LessonPractice
Test before blockingDeploy new rules in Count mode first and review sampled requests
Narrow the scopeUse a scope-down statement or override the specific rule that matched
See what matchedWAF logs show the terminating rule and the request

The logs identify the rule that matched the apostrophe. The developer sets that one rule to Count for the address field's path, keeps the rest of the group blocking, and the application continues to use parameterised queries, which are the real defence against SQL injection.

Check yourself

Which AWS WAF rule type limits the number of requests from each IP address?

Show answer

A rate-based rule. It counts requests for each IP address, or another key, over a time window and acts on those above the limit.

Exam Tips

Remember for the exam

  • To block SQL injection or XSS at the edge or in front of an API, use AWS WAF.
  • To limit the requests each client IP can make, use a rate-based rule.
  • To test a new rule without blocking users, use the Count action.
  • For credential stuffing on a login endpoint, use Account Takeover Prevention.