Skip to main content

๐Ÿ›ก๏ธ WAF Setup for Bike4Mind API Protection

This directory contains scripts for setting up AWS WAF (Web Application Firewall) to protect your Bike4Mind application from API hammering and enable emergency IP blocking.

๐Ÿšจ Emergency Protection Featuresโ€‹

  • Rate Limiting: 1000 requests per 5 minutes per IP
  • Emergency IP Blocking: Instantly block malicious IPs
  • CloudWatch Monitoring: Track blocked requests and metrics
  • Emergency Commands: Quick scripts for threat response

๐Ÿ“‹ Prerequisitesโ€‹

  • AWS CLI configured with appropriate permissions
  • CloudFront distribution already deployed (via SST)
  • Admin access to AWS WAF and CloudFront

๐Ÿš€ Setup Instructionsโ€‹

Step 1: Find Your CloudFront Distribution IDโ€‹

# Run the helper script to find your distribution ID
./get-cloudfront-id.sh

This will either automatically find your distribution ID or list all distributions for you to choose from.

Step 2: Deploy WAF Protectionโ€‹

# Replace DISTRIBUTION_ID with your actual CloudFront distribution ID
./setup-waf.sh E1234567890ABC

What this creates:

  • โœ… WAF Web ACL with rate limiting (1000 req/5min per IP)
  • โœ… IP Set for emergency blocking
  • โœ… CloudWatch metrics and monitoring
  • โœ… Association with your CloudFront distribution

Step 3: Configure Emergency Commandsโ€‹

After WAF setup completes, it will output commands like:

โœ… IP Set created: 12345678-1234-1234-1234-123456789012

Update the emergency script:

  1. Edit emergency-waf-commands.sh
  2. Replace REPLACE_WITH_ACTUAL_IP_SET_ID with your actual IP Set ID
  3. Save the file

๐Ÿšจ Emergency Response Commandsโ€‹

Block an IP Addressโ€‹

./emergency-waf-commands.sh block 192.168.1.100

Block an Entire Subnetโ€‹

./emergency-waf-commands.sh block-subnet 10.0.0.0/8

Emergency Reset (Unblock All)โ€‹

./emergency-waf-commands.sh unblock-all

List Currently Blocked IPsโ€‹

./emergency-waf-commands.sh list

View WAF Metricsโ€‹

# Last 1 hour (default)
./emergency-waf-commands.sh metrics

# Last 24 hours
./emergency-waf-commands.sh metrics 24

๐Ÿ“Š Monitoringโ€‹

CloudWatch Metricsโ€‹

The WAF automatically sends metrics to CloudWatch:

  • Namespace: AWS/WAFV2
  • Metrics: BlockedRequests, AllowedRequests
  • Dimensions: WebACL, Rule

View in AWS Consoleโ€‹

  1. Go to AWS CloudWatch โ†’ Metrics
  2. Select "WAFV2" namespace
  3. View metrics for your Web ACL

๐Ÿ”ง Manual Commands (Reference)โ€‹

If you need to run WAF commands manually:

Get Current Blocked IPsโ€‹

aws wafv2 get-ip-set \
--scope CLOUDFRONT \
--id YOUR_IP_SET_ID \
--region us-east-1

Block an IPโ€‹

aws wafv2 update-ip-set \
--scope CLOUDFRONT \
--id YOUR_IP_SET_ID \
--addresses "192.168.1.100/32" "10.0.0.0/8" \
--region us-east-1

View WAF Rulesโ€‹

aws wafv2 get-web-acl \
--scope CLOUDFRONT \
--id YOUR_WEB_ACL_ID \
--region us-east-1

๐Ÿšจ Emergency Scenariosโ€‹

Scenario 1: API Attack in Progressโ€‹

# Immediately block the attacking IP
./emergency-waf-commands.sh block 203.0.113.15

# Check if blocking is working
./emergency-waf-commands.sh metrics

# View currently blocked IPs
./emergency-waf-commands.sh list

Scenario 2: Distributed Attackโ€‹

# Block entire subnet ranges
./emergency-waf-commands.sh block-subnet 203.0.113.0/24
./emergency-waf-commands.sh block-subnet 198.51.100.0/24

# Monitor blocking effectiveness
./emergency-waf-commands.sh metrics

Scenario 3: False Positive (Unblock)โ€‹

# Emergency reset to unblock all IPs
./emergency-waf-commands.sh unblock-all

# Or manually remove specific IPs by updating the IP set
# (requires getting current IPs and removing the specific one)

๐Ÿ“ Rate Limiting Detailsโ€‹

Current Configuration:

  • Limit: 1000 requests per 5 minutes
  • Scope: Per IP address
  • Action: Block excess requests
  • Metric: Tracked in CloudWatch

To Modify Rate Limits:

  1. Go to AWS WAF Console
  2. Find your Web ACL: bike4mind-api-protection
  3. Edit the api-rate-limit rule
  4. Adjust the limit as needed

๐Ÿ”’ Security Best Practicesโ€‹

  1. Regular Monitoring: Check WAF metrics weekly
  2. IP Set Maintenance: Review blocked IPs monthly
  3. Alert Setup: Configure CloudWatch alarms for high block rates
  4. Access Control: Limit who can modify WAF rules
  5. Documentation: Keep emergency procedures updated

๐Ÿงน Cleanup (If Needed)โ€‹

To remove WAF protection:

# Disassociate from CloudFront
aws wafv2 disassociate-web-acl \
--resource-arn "arn:aws:cloudfront::ACCOUNT:distribution/DISTRIBUTION_ID" \
--region us-east-1

# Delete Web ACL
aws wafv2 delete-web-acl \
--scope CLOUDFRONT \
--id YOUR_WEB_ACL_ID \
--lock-token LOCK_TOKEN \
--region us-east-1

# Delete IP Set
aws wafv2 delete-ip-set \
--scope CLOUDFRONT \
--id YOUR_IP_SET_ID \
--lock-token LOCK_TOKEN \
--region us-east-1

๐Ÿ†˜ Troubleshootingโ€‹

WAF Not Blockingโ€‹

  1. Check if WAF is associated with CloudFront distribution
  2. Verify rules are enabled in Web ACL
  3. Check CloudWatch metrics for rule evaluation

Commands Not Workingโ€‹

  1. Ensure AWS CLI is configured for us-east-1 region
  2. Verify you have WAF permissions
  3. Check IP Set ID is correct in emergency script

High False Positivesโ€‹

  1. Review rate limit threshold (may be too low)
  2. Check if legitimate traffic patterns changed
  3. Consider allowlisting known good IPs

๐Ÿ“ž Emergency Contactsโ€‹

  • Primary: DevOps Engineer
  • Secondary: Security Lead
  • Escalation: CTO