Skip to content

How can I implement score-based IP banning in AWS WAF?

0

I'm using AWS WAF to protect my application, but rate-based rules automatically unblock IPs when their request count drops below the threshold. This allows attackers to:

  1. Send bursts of malicious requests until blocked
  2. Wait for the rate to drop
  3. Resume the attack
2 Answers
0
Accepted Answer

Had a strong suspicion security automations would help here, so I started tinkering. Landed on using DynamoDB for state, which isn't in the AWS public docs architectures (most use Athena or S3).

In the Solution CloudWatch Logs subscription filter catches WAF BLOCK events and sends them to Lambda. That bumps the IP's score in DynamoDB. Once it crosses a threshold, the IP gets flagged. A second Lambda runs every minute via EventBridge, reads banned IPs, and syncs them to a WAF IP Set. DynamoDB TTL handles cleanup when bans expire.

CloudFormation + full setup: https://github.com/FireShip-it/waf-autoban

answered a month ago

EXPERT

reviewed a month ago

0

You're right that a rate-based rule blocks an IP address only when its request count is over the limit for the evaluation window. The longest window is 10 minutes, and you can't configure a longer or permanent block duration on the rule itself. To ban an IP address for longer, keep the ban list in an IP set that you manage.

For a fixed ban period, AWS documents two patterns:

To make the ban score-based, extend the first pattern:

  1. Use Count rules to generate signals. These rules add labels to the request but don't block it. Signals can be requests for /.env or /wp-login.php, managed rule group matches, or a second rate-based rule with a lower limit in Count mode. Each match appears in the log record's labels, nonTerminatingMatchingRules and rateBasedRuleList fields. For details, see Log fields for protection pack (web ACL) traffic.
  2. Score each IP address. The Lambda function gives each signal a weight and adds it to the IP address's item in DynamoDB with an atomic ADD. A TTL on the item lets old scores expire, for example after 24 hours.
  3. Escalate for repeat offenders. When a score passes your threshold, increase the IP address's strike count and calculate its bannedUntil time from that count. For example, ban for 1 hour, then 24 hours, then 7 days.
  4. Apply and release the bans. Write the banned addresses to the IP set. Then use a one-time schedule in EventBridge Scheduler to remove each address when its bannedUntil time passes.

Design for the following:

  • UpdateIPSet replaces the whole address list and requires the current LockToken. If two functions update the set at the same time, one of them fails with WAFOptimisticLockException. Use one function that applies the changes in batches every minute, rather than one update per log record.
  • IP set changes take from a few seconds to several minutes to propagate. Keep the rate-based rule as the immediate first block.
  • An IP set holds up to 10,000 addresses, according to AWS WAF quotas. Each set is for either IPv4 or IPv6, so you need one of each. Ban IPv6 clients by /64 prefix so that address rotation doesn't avoid the ban.
  • For a CloudFront distribution, create the IP set in us-east-1 with the CLOUDFRONT scope. If a proxy is in front of AWS WAF, use the forwarded IP address in both the rate-based rule and the IP set rule.
  • Many users can share one IP address behind NAT or a mobile carrier. For mid-range scores, use a CAPTCHA or Challenge action, and block only the highest scores.

Security Automations for AWS WAF also blocks addresses based on log analysis, but the solution retires in December 2026. It isn't a good base for a new deployment.

Related information:

AWS
SUPPORT ENGINEER

answered a month ago

EXPERT

reviewed a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.