Skip to content

Instantly share code, notes, and snippets.

@SergioDevOps
Forked from josnidhin/modsec-ratelimit.conf
Created January 5, 2023 19:37
Show Gist options
  • Save SergioDevOps/830dccf11dfe29a356237edac755ec60 to your computer and use it in GitHub Desktop.
Save SergioDevOps/830dccf11dfe29a356237edac755ec60 to your computer and use it in GitHub Desktop.
A simple mod security config for IP Rate limiting Apache server behind a load balancer.
# assumes libapache2-modsecurity is installed
# Reference Mannual - https://github.com/SpiderLabs/ModSecurity/wiki
SecRuleEngine On
<LocationMatch "^/.*">
# initialise the state based on X-Forwarded-For ip address
SecRule REQUEST_HEADERS:X-Forwarded-For "@unconditionalMatch" "phase:2,initcol:ip=%{MATCHED_VAR},pass,nolog,id:100"
# if greater then burst_rate_limit then pause set RATELIMITED var and then return 503
SecRule IP:ACCESS_COUNT "@gt {{ burst_rate_limit }}" "phase:2,pause:300,deny,status:503,setenv:RATELIMITED,skip:1,nolog,id:102"
# if above rule doesnt match increment the count
SecAction "phase:2,setvar:ip.access_count=+1,pass,nolog,id:103"
# set the base rate to one per second
SecAction "phase:5,deprecatevar:ip.access_count=1/1,pass,nolog,id:104"
# set a header when ratelimited
Header always set Retry-After "10" env=RATELIMITED
</LocationMatch>
ErrorDocument 503 "Service Unavailable"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment