DDoS attacks can cripple services. This guide covers attack types, defense strategies, and building resilient infrastructure.
Understanding DDoS Attacks
Attack Types
volumetric_attacks:
description: " flood network with massive traffic"
examples:
- "UDP Flood"
- "ICMP Flood"
- "NTP Amplification"
measured: "Gbps or Tbps"
protocol_attacks:
description: "Exploit protocol weaknesses"
examples:
- "SYN Flood"
- "Ping of Death"
- "Smurf Attack"
measured: "Mpps (packets per second)"
application_layer_attacks:
description: "Target specific applications"
examples:
- "HTTP Flood"
- "Slowloris"
- "SQL Injection DDoS"
measured: "Requests per second"
Attack Vector Example
# Simple SYN flood (for understanding only)
# Never execute actual attacks
def syn_flood(target_ip, target_port, duration):
"""Concept: Send many SYN packets without completing handshake"""
# In reality, use hping3 or similar tools
# This is pseudocode for understanding
pass
Defense Strategies
Multi-Layer Defense
defense_layers:
- name: "Edge/CDN"
purpose: "Absorb volumetric attacks"
tools: ["Cloudflare", "Akamai", "AWS CloudFront"]
- name: "Network Layer"
purpose: "Filter bad traffic"
tools: ["DDoS protection services", "Border firewalls"]
- name: "Application Layer"
purpose: "Block application attacks"
tools: ["WAF", "Rate limiting", "CAPTCHA"]
mitigation_phases:
1. "Detection - Identify attack early"
2. "Diversion - Route traffic through scrubbing"
3. "Filtering - Block malicious requests"
4. "Analysis - Understand attack pattern"
5. "Return - Gradually return to normal"
Cloudflare DDoS Protection
# Cloudflare configuration
# Under Attack Mode
# Enable in Cloudflare dashboard or via API
# Rate limiting
rules = [
{
"id": "rate-limit-1",
"action": "block",
"expression": "ip.src eq 10.0.0.0/8",
"config": {
"target": "ip",
"rate": 100,
"period": 60
}
}
]
# JavaScript challenge for suspicious traffic
challenge_rules = [
{
"action": "js_challenge",
"expression": "cf.threat_score gt 10"
}
]
AWS Shield
# AWS DDoS protection layers
aws_protection:
- name: "AWS Shield Standard"
included: "All AWS customers"
protects:
- "Layer 3/4 attacks"
- "DDoS attacks on CloudFront, Route 53"
- name: "AWS Shield Advanced"
cost: "$3,000/month"
protects:
- "All Standard protections"
- "Application-layer DDoS"
- "24/7 DDoS Response Team"
- "DDoS cost protection"
Implementation
Rate Limiting at Edge
# Cloudflare Workers rate limiting
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const ip = request.headers.get('CF-Connecting-IP')
// Check rate limit
const limit = 100 // requests per minute
const key = `rate_limit:${ip}`
const current = await RATE_LIMIT.get(key)
if (current && parseInt(current) >= limit) {
return new Response('Rate limit exceeded', {
status: 429,
headers: {
'Retry-After': '60'
}
})
}
// Increment counter
await RATE_LIMIT.put(key, (current ? parseInt(current) + 1 : 1), { expirationTtl: 60 })
return fetch(request)
}
IP Reputation System
# IP reputation checking
class IPReputation:
"""Check IP reputation"""
def __init__(self):
self.blocklist = set()
self.allowlist = set()
def is_malicious(self, ip):
if ip in self.allowlist:
return False
if ip in self.blocklist:
return True
# Check threat intelligence feeds
return self.check_threat_feeds(ip)
def check_threat_feeds(self, ip):
# Check various threat intelligence sources
# - AbuseIPDB
# - Project Honey Pot
# - Spamhaus
pass
def get_score(self, ip):
"""Get threat score (0-100)"""
score = 0
# Check if in any blocklist
if ip in self.blocklist:
score += 50
# Check geographic risk
# Check attack history
# Check if proxy/VPN
return score
Best Practices
# DDoS protection best practices
infrastructure:
- "Use CDN with DDoS protection"
- "Enable rate limiting globally"
- "Design for partial failure"
- "Maintain spare capacity"
monitoring:
- "Monitor traffic patterns"
- "Set up alerts for anomalies"
- "Test defenses regularly"
- "Have runbooks ready"
response:
- "Have incident response plan"
- "Contact provider early"
- "Document everything"
- "Post-mortem after attack"
Conclusion
DDoS protection requires multiple layers:
- Edge: CDN absorbs volumetric attacks
- Network: Filter at network perimeter
- Application: Rate limiting, WAF
- Planning: Response plans and runbooks
Invest in DDoS protection services for production applications.
Comments