Give modsecurity rule to defend http flood and slowloris attacks
- User
Give modsecurity rule to defend http flood and slowloris attacks
- Assistant
Give modsecurity rule to defend http flood and slowloris attacks
Certainly! Below is an example of ModSecurity rules that can help mitigate HTTP flood and Slowloris type attacks. These rules focus on limiting the number of requests per IP in a given timeframe (to counter HTTP flood) and detecting slow header transmission (to mitigate Slowloris).
A common approach to mitigate HTTP floods is to limit the number of requests per IP address within a short timeframe.
# Initialize collection for IP tracking
SecAction "id:9000000,phase:1,pass,nolog,initcol:ip=%{REMOTE_ADDR},expirevar:ip.counter=60"
# Increment request counter
SecAction "id:9000001,phase:1,pass,nolog,initcol:ip=%{REMOTE_ADDR},nolog,setsid:ip,expirevar:ip.counter=60,exec:/bin/true"
SecRule IP:counter "@eq 0" "id:9000002,phase:1,nolog,pass,setvar:ip.counter=1"
SecRule IP:counter "@gt 0" "id:9000003,phase:1,nolog,pass,setvar:ip.counter=+1"
# Deny requests if more than 100 requests are made within 60 seconds
SecRule IP:counter "@gt 100" "id:9000004,phase:1,deny,status:429,msg:'Rate limit exceeded - potential HTTP flood attack',log"
You can adjust the limit 100 and timeframe 60 seconds based on your traffic profile.
Slowloris attacks keep many HTTP connections open by sending headers very slowly, exhausting the web server's resources. ModSecurity itself cannot directly control connection timeouts, but it provides some capabilities to detect suspiciously slow headers and content length.
ModSecurity has a variable %{REQUEST_HEADERS:...} and timing variables with the experimental DETECT_SEQUENTIAL_ERRORS or timeout detection via the REQBODY_PROCESSOR (depending on your version). But better is to combine ModSecurity with web server-level timeout directives.
However, here is a ModSecurity rule example detecting slow requests by monitoring slow header completion with a short synthetic timeout by tracking the time taken to receive headers (experimental and depends on your ModSecurity version):
# Defend against slow header attacks (Slowloris)
SecAction "id:9000010,phase:1,pass,nolog,initcol:ip=%{REMOTE_ADDR}"
# Track the time to complete header reading
SecRule REQUEST_HEADERS:Content-Length "@gt 0" "id:9000011,phase:1,pass,nolog,setvar:ip.slow_headers_time=%{TIME}"
# If time taken between first header and now is more than X seconds (e.g., 10 seconds), block it
SecRule IP:slow_headers_time "@gt 10" "id:9000012,phase:1,deny,status:408,msg:'Slowloris attack detected - headers too slow',log"
Web Server Configuration: The best defense against Slowloris is setting web server timeouts for slow clients:
RequestReadTimeout directive (mod_reqtimeout module)RequestReadTimeout header=10-20,minrate=500
Use both: Combine ModSecurity with proper web server timeouts and rate limiting at firewall/load balancer level for best results.
# HTTP Flood: Rate limiting requests per IP (max 100 requests per 60 seconds)
SecAction "id:9000000,phase:1,pass,nolog,initcol:ip=%{REMOTE_ADDR},expirevar:ip.counter=60"
SecRule IP:counter "@eq 0" "id:9000002,phase:1,nolog,pass,setvar:ip.counter=1"
SecRule IP:counter "@gt 0" "id:9000003,phase:1,nolog,pass,setvar:ip.counter=+1"
SecRule IP:counter "@gt 100" "id:9000004,phase:1,deny,status:429,msg:'Rate limit exceeded - potential HTTP flood attack',log"
# Slowloris detection example (experimental, depends on ModSecurity version)
SecAction "id:9000010,phase:1,pass,nolog,initcol:ip=%{REMOTE_ADDR}"
SecRule REQUEST_HEADERS:Content-Length "@gt 0" "id:9000011,phase:1,pass,nolog,setvar:ip.slow_headers_time=%{TIME}"
SecRule IP:slow_headers_time "@gt 10" "id:9000012,phase:1,deny,status:408,msg:'Slowloris attack detected - headers too slow',log"
If you want, I can also help configure Apache or Nginx timeout settings for better Slowloris protection. Would you like that?
Not yet included in a published release.