jeffersongoncalves / laravel-scanner-guard-aws-waf
AWS WAF denylist sink for laravel-scanner-guard: keeps IP sets in sync so a web ACL rule blocks banned IPs at CloudFront, ALB or API Gateway
Package info
github.com/jeffersongoncalves/laravel-scanner-guard-aws-waf
pkg:composer/jeffersongoncalves/laravel-scanner-guard-aws-waf
Requires
- php: ^8.2
- aws/aws-sdk-php: ^3.300
- illuminate/contracts: ^12.0|^13.0
- jeffersongoncalves/laravel-scanner-guard: ^2.2.1
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.24
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^3.0|^4.0
- pestphp/pest-plugin-laravel: ^3.0|^4.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Laravel Scanner Guard AWS WAF
AWS WAF denylist sink for laravel-scanner-guard.
Keeps AWS WAF IP sets in sync with your bans and ip_blocklist, so a web ACL rule blocks them at
CloudFront, an Application Load Balancer or API Gateway: banned scanners never reach your servers.
Installation
composer require jeffersongoncalves/laravel-scanner-guard-aws-waf
Add the sink to config/scanner-guard.php:
'sinks' => [ \JeffersonGoncalves\ScannerGuardAwsWaf\AwsWafSink::class, ],
AWS setup
-
Create the IP sets. AWS has no mixed set, so one for IPv4 and one for IPv6. The sink replaces the whole set on every change, so give it sets of its own:
aws wafv2 create-ip-set --name scanner-guard-v4 --scope REGIONAL --region sa-east-1 \ --ip-address-version IPV4 --addresses aws wafv2 create-ip-set --name scanner-guard-v6 --scope REGIONAL --region sa-east-1 \ --ip-address-version IPV6 --addresses
For CloudFront use
--scope CLOUDFRONT --region us-east-1. -
Block them. In the web ACL of your load balancer, API or distribution, add one rule per set: Originates from an IP address in → the set → action Block. Put them first.
-
Allow the app to write them:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": ["wafv2:GetIPSet", "wafv2:UpdateIPSet"], "Resource": [ "arn:aws:wafv2:sa-east-1:123456789012:regional/ipset/scanner-guard-v4/*", "arn:aws:wafv2:sa-east-1:123456789012:regional/ipset/scanner-guard-v6/*" ] } ] } -
Configure the app with the ARNs. Region and scope come from them:
SCANNER_GUARD_AWS_WAF_IPV4_ARN=arn:aws:wafv2:sa-east-1:123456789012:regional/ipset/scanner-guard-v4/... SCANNER_GUARD_AWS_WAF_IPV6_ARN=arn:aws:wafv2:sa-east-1:123456789012:regional/ipset/scanner-guard-v6/...
Run php artisan scanner-guard:sync-denylist once to check: it exits non-zero and logs the AWS error
if something is wrong.
How it syncs
scanner-guard:sync-denylist runs every minute and right after every ban, unban and blocklist change.
The sink reads the set's lock token (GetIPSet) and replaces its addresses (UpdateIPSet). A SHA-256
of the last list written to each set is kept in the scanner-guard cache store, so an unchanged list
costs no API call: in practice AWS is only called when a ban starts or ends. AWS takes a little while
to apply a change everywhere.
- An API error (including a lock token that went stale because something else changed the set) throws:
sync-denylistlogs it, exits non-zero and keeps running your other sinks. Nothing is cached, so the next sync retries. - A set holds 10,000 addresses. Beyond that the first 10,000 are kept (
ip_blocklistentries, then the bans with the latest expiry) and a warning is logged. The app still blocks the rest. - AWS rejects
/0: such anip_blocklistentry is skipped here (logged as a warning) but still blocked by the app. Host bits are cleared (10.1.2.3/8becomes10.0.0.0/8). - Leave an ARN empty to keep that IP version out of AWS. At least one is required.
- IP sets have no comments, so reasons and expiries stay in the app.
Configuration
php artisan vendor:publish --tag="laravel-scanner-guard-aws-waf-config"
| Key | Env | Default |
|---|---|---|
ipv4_arn |
SCANNER_GUARD_AWS_WAF_IPV4_ARN |
null |
ipv6_arn |
SCANNER_GUARD_AWS_WAF_IPV6_ARN |
null |
credentials |
AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN |
the SDK's default chain (EC2, ECS or EKS role, profile) |
client |
HTTP timeouts (5s connect, 10s total) |
client is passed to Aws\WAFV2\WAFV2Client. The AWS SDK ships every service: to keep only this one
in your app, see aws/aws-sdk-php's removeUnusedServices
(list WAFV2 and whatever else you use).
Behind CloudFront or a load balancer: the app needs the visitor's IP
scanner-guard bans whatever $request->ip() returns. Behind a load balancer or CloudFront that is
their IP unless the app trusts them in TrustProxies. Otherwise the app bans its own proxy (rejecting
real visitors itself), so check $request->ip() before turning the sink on.
The web ACL matches the address that connects to the resource it protects. On a load balancer behind CloudFront or another CDN, attach the ACL to the distribution instead, or set the rule to read the IP from a header, and only if nothing can reach the load balancer directly (the header can be forged).
Testing
composer test
SERVER_TESTS=1 composer test also runs the sink against Moto, a
mock of AWS (needs Docker).
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.
